-
Notifications
You must be signed in to change notification settings - Fork 6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
自分の投稿の通知に含めるかの設定するapiの実装 #67
Open
Luftalian
wants to merge
1
commit into
main
Choose a base branch
from
me_noti_api
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,45 @@ | ||
package handler | ||
|
||
import "github.com/labstack/echo/v4" | ||
import ( | ||
"h23s_15/api" | ||
"h23s_15/model" | ||
"net/http" | ||
|
||
"github.com/labstack/echo/v4" | ||
) | ||
|
||
// 自分の投稿に対する通知の設定 | ||
// (PUT /words/me/) | ||
func (s Server) PutWordsMe(ctx echo.Context) error { | ||
return nil | ||
data := &api.PutWordsMeJSONRequestBody{} | ||
if err := ctx.Bind(data); err != nil { | ||
return echo.NewHTTPError(http.StatusBadRequest, err) | ||
} | ||
userId, err := getUserIdFromSession(ctx) | ||
if err != nil { | ||
return echo.NewHTTPError(http.StatusBadRequest, err) | ||
} | ||
err = model.PutWordsMe(data.IncludeMe, data.Word, userId) | ||
if err != nil { | ||
return echo.NewHTTPError(http.StatusBadRequest, err) | ||
} | ||
return ctx.JSON(http.StatusOK, nil) | ||
} | ||
|
||
// 自分の投稿に対する通知の一括設定 | ||
// (POST /words/me/all) | ||
func (s Server) PostWordsMeAll(ctx echo.Context) error { | ||
return nil | ||
data := &api.PostWordsMeAllJSONRequestBody{} | ||
if err := ctx.Bind(data); err != nil { | ||
return echo.NewHTTPError(http.StatusBadRequest, err) | ||
} | ||
userId, err := getUserIdFromSession(ctx) | ||
if err != nil { | ||
return echo.NewHTTPError(http.StatusBadRequest, err) | ||
} | ||
err = model.PostWordsMeAll(data.IncludeMe, userId) | ||
if err != nil { | ||
return echo.NewHTTPError(http.StatusBadRequest, err) | ||
} | ||
return ctx.JSON(http.StatusOK, nil) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package model | ||
|
||
import ( | ||
"database/sql" | ||
"errors" | ||
"fmt" | ||
|
||
"golang.org/x/exp/slog" | ||
) | ||
|
||
type WordsMe struct { | ||
IncludeMe bool `db:"me_notification"` | ||
Word string `db:"word"` | ||
} | ||
|
||
func PutWordsMe(includeMe bool, word string, userId string) error { | ||
wordMe := []WordsMe{} | ||
err := db.Select(&wordMe, "SELECT me_notification, word FROM words WHERE word = ? AND trap_id = ?", word, userId) | ||
if errors.Is(err, sql.ErrNoRows) { | ||
slog.Info("No Data Found") | ||
return err | ||
} else if err != nil { | ||
slog.Info("Error: %s", err) | ||
return err | ||
} | ||
if len(wordMe) != 1 { | ||
slog.Info("Already exist too many data same trap_id and word: %d", len(wordMe)) | ||
return fmt.Errorf("Already exist too many data same trap_id and word: %d", len(wordMe)) | ||
} | ||
Comment on lines
+26
to
+29
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
_, err = db.Exec("UPDATE words SET me_notification = ? WHERE word = ? AND trap_id = ?", includeMe, word, userId) | ||
if err != nil { | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
func PostWordsMeAll(includeMe bool, userId string) error { | ||
wordMes := []WordsMe{} | ||
err := db.Select(&wordMes, "SELECT me_notification, word FROM words WHERE trap_id = ?", userId) | ||
if errors.Is(err, sql.ErrNoRows) { | ||
slog.Info("No Data Found") | ||
return err | ||
} else if err != nil { | ||
slog.Info("Error: %s", err) | ||
return err | ||
} | ||
Comment on lines
+39
to
+46
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 一括全件更新のときはこのチェックなしでupdateかけていい |
||
_, err = db.Exec("UPDATE words SET me_notification = ? WHERE trap_id = ?", includeMe, userId) | ||
if err != nil { | ||
return err | ||
} | ||
return nil | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ここの存在確認、model.ExistWordを事前にhandler側の関数で呼び出してやるのがいいんじゃない?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
そもそも
db.Select
を使うとsql.ErrNoRows
は返ってこないです