Skip to content
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
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 0 additions & 8 deletions server/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -130,14 +130,10 @@ github.com/valyala/fasttemplate v1.2.2/go.mod h1:KHLXt3tVN2HBp8eijSv/kGJopbvo7S+
golang.org/x/arch v0.0.0-20210923205945-b76863e36670/go.mod h1:5om86z9Hs0C8fWVUuoMHwpExlXzs5Tkyp9hOrfG7pp8=
golang.org/x/arch v0.3.0 h1:02VY4/ZcO/gBOH6PUaoiptASxtXU10jazRCP865E97k=
golang.org/x/arch v0.3.0/go.mod h1:5om86z9Hs0C8fWVUuoMHwpExlXzs5Tkyp9hOrfG7pp8=
golang.org/x/crypto v0.9.0 h1:LF6fAI+IutBocDJ2OT0Q1g8plpYljMZ4+lty+dsqw3g=
golang.org/x/crypto v0.9.0/go.mod h1:yrmDGqONDYtNj3tH8X9dzUun2m2lzPa9ngI6/RUPGR0=
golang.org/x/crypto v0.10.0 h1:LKqV2xt9+kDzSTfOhx4FrkEBcMrAgHSYgzywV9zcGmM=
golang.org/x/crypto v0.10.0/go.mod h1:o4eNf7Ede1fv+hwOwZsTHl9EsPFO6q6ZvYR8vYfY45I=
golang.org/x/exp v0.0.0-20230522175609-2e198f4a06a1 h1:k/i9J1pBpvlfR+9QsetwPyERsqu1GIbi967PQMq3Ivc=
golang.org/x/exp v0.0.0-20230522175609-2e198f4a06a1/go.mod h1:V1LtkGg67GoY2N1AnLN78QLrzxkLyJw7RJb1gzOOz9w=
golang.org/x/net v0.10.0 h1:X2//UzNDwYmtCLn7To6G58Wr6f5ahEAQgKNzv9Y951M=
golang.org/x/net v0.10.0/go.mod h1:0qNGK6F8kojg2nk9dLZ2mShWaEBan6FAoqfSigmmuDg=
golang.org/x/net v0.11.0 h1:Gi2tvZIJyBtO9SDr1q9h5hEQCp/4L2RQ+ar0qjx2oNU=
golang.org/x/net v0.11.0/go.mod h1:2L/ixqYpgIVXmeoSA/4Lu7BzTG4KIyPIryS4IsOd1oQ=
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
Expand All @@ -146,12 +142,8 @@ golang.org/x/sys v0.0.0-20211103235746-7861aae1554b/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.0.0-20220704084225-05e143d24a9e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.8.0 h1:EBmGv8NaZBZTWvrbjNoL6HVt+IVy3QDQpJs7VRIw3tU=
golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.9.0 h1:KS/R3tvhPqvJvwcKfnBHJwwthS11LRhmM5D59eEXa0s=
golang.org/x/sys v0.9.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/text v0.9.0 h1:2sjJmO8cDvYveuX97RDLsxlyUxLl+GHoLxBiRdHllBE=
golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8=
golang.org/x/text v0.10.0 h1:UpjohKhiEgNc0CSauXmwYftY1+LlaC75SJwh0SgCX58=
golang.org/x/text v0.10.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=
golang.org/x/time v0.3.0 h1:rg5rLMjNzMS1RkNLzCG38eapWhnYLFYXDXj2gOlr8j4=
Expand Down
36 changes: 33 additions & 3 deletions server/handler/me.go
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)
}
52 changes: 52 additions & 0 deletions server/model/me.go
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
}
Comment on lines +18 to +25
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ここの存在確認、model.ExistWordを事前にhandler側の関数で呼び出してやるのがいいんじゃない?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

そもそもdb.Selectを使うとsql.ErrNoRowsは返ってこないです

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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

model.ExistWordを使えばこの考慮はいらないはず

_, 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
Copy link
Contributor

Choose a reason for hiding this comment

The 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
}