diff --git a/backend/infrastructure/persistence/article.go b/backend/infrastructure/persistence/article.go index 2a2e4a26..f6f41f15 100644 --- a/backend/infrastructure/persistence/article.go +++ b/backend/infrastructure/persistence/article.go @@ -27,7 +27,7 @@ func (ap *articlePersistence) FindByID(article *model.Article) error { Joins("User"). Joins("Category"). Preload("Tags"). - Find(article). + First(article). Error } diff --git a/backend/interfaces/api/handler/article.go b/backend/interfaces/api/handler/article.go index 41539d2a..cb328b78 100644 --- a/backend/interfaces/api/handler/article.go +++ b/backend/interfaces/api/handler/article.go @@ -7,6 +7,7 @@ import ( "github.com/gorilla/mux" "github.com/yoshihiro-shu/draft-backend/backend/application/usecase" "github.com/yoshihiro-shu/draft-backend/backend/interfaces/api/request" + "gorm.io/gorm" ) type ArticleHandler interface { @@ -41,8 +42,10 @@ func (ah *articleHandler) Get(w http.ResponseWriter, r *http.Request) error { } article, err := ah.articleUseCase.FindByID(id) - // TODO not no rows error if err != nil { + if err == gorm.ErrRecordNotFound { + return ah.C.JSON(w, http.StatusNotFound, err.Error()) + } return ah.C.JSON(w, http.StatusInternalServerError, err.Error()) } diff --git a/backend/interfaces/api/handler/command.go b/backend/interfaces/api/handler/command.go deleted file mode 100644 index 4708a596..00000000 --- a/backend/interfaces/api/handler/command.go +++ /dev/null @@ -1,19 +0,0 @@ -package handler - -import ( - "bytes" - "net/http" - "os/exec" -) - -func (h indexHandler) Command(w http.ResponseWriter, r *http.Request) error { - cmd := exec.Command("pwd") - var out bytes.Buffer - cmd.Stdout = &out - err := cmd.Run() - if err != nil { - return h.JSON(w, http.StatusOK, err.Error()) - } - - return h.JSON(w, http.StatusOK, out.String()) -} diff --git a/backend/interfaces/api/handler/index.go b/backend/interfaces/api/handler/index.go index 9bc10faf..a15aa700 100644 --- a/backend/interfaces/api/handler/index.go +++ b/backend/interfaces/api/handler/index.go @@ -1,10 +1,8 @@ package handler import ( - "fmt" "net/http" - "github.com/gorilla/mux" "github.com/yoshihiro-shu/draft-backend/backend/interfaces/api/request" ) @@ -20,57 +18,11 @@ type TestRedis struct { func (h indexHandler) Index(w http.ResponseWriter, r *http.Request) error { return h.JSON(w, http.StatusOK, "HELLO WORLD") } - -func (h indexHandler) TestHandler(w http.ResponseWriter, r *http.Request) error { - fmt.Println("UNKOOOOO") - fmt.Printf("RequestContext: %#v\n", h.Context) - fmt.Fprintf(w, "RequestContext: %#v\n", h.Context) - fmt.Println("UNKOOOOO") - return h.Context.JSON(w, http.StatusOK, h.Context) -} - func (h indexHandler) AuthIndex(w http.ResponseWriter, r *http.Request) error { id := h.GetAuthUserID(r.Context()) return h.JSON(w, http.StatusOK, id) } -func (h indexHandler) TestSetRedis(w http.ResponseWriter, r *http.Request) error { - k := r.FormValue("key") - v := r.FormValue("value") - - t := TestRedis{ - Key: k, - Value: v, - } - - err := h.Cache().SET(k, t) - if err != nil { - return h.JSON(w, http.StatusInternalServerError, err.Error()) - } - - cookie := &http.Cookie{ - Name: k, - Value: v, - } - - http.SetCookie(w, cookie) - - return h.JSON(w, http.StatusOK, t) -} - -func (h indexHandler) TestGetRedis(w http.ResponseWriter, r *http.Request) error { - vars := mux.Vars(r) - k := vars["key"] - - t := TestRedis{} - err := h.Cache().GET(k, &t) - if err != nil { - return h.JSON(w, http.StatusInternalServerError, err.Error()) - } - - return h.JSON(w, http.StatusOK, t) -} - func NewIndexHandler(c *request.Context) *indexHandler { return &indexHandler{c} } diff --git a/backend/interfaces/api/user_api/api.go b/backend/interfaces/api/user_api/api.go index 5cb91ea6..50d41d7c 100644 --- a/backend/interfaces/api/user_api/api.go +++ b/backend/interfaces/api/user_api/api.go @@ -50,18 +50,6 @@ func Apply(r router.Router, conf config.Configs, logger logger.Logger, db model. twitter := r.Group("/twitter") twitter.GET("/timeline", twitterHandler.GetTimeLine) } - { - // Grouping - t := r.Group("/test") - t.GET("", h.TestHandler) - t.POST("/redis", h.TestSetRedis) - t.GET("/redis/{key}", h.TestGetRedis) - t.GET("/v2", h.Index) - } - { - c := r.Group("/cmd") - c.GET("", h.Command) - } { auth := r.Group("/auth") userHandler := registory.NewUserRegistory(ctx)