Skip to content
This repository has been archived by the owner on Nov 3, 2024. It is now read-only.

Fix/err handling/article get #70

Merged
merged 2 commits into from
Oct 22, 2023
Merged
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
2 changes: 1 addition & 1 deletion backend/infrastructure/persistence/article.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func (ap *articlePersistence) FindByID(article *model.Article) error {
Joins("User").
Joins("Category").
Preload("Tags").
Find(article).
First(article).
Error
}

Expand Down
5 changes: 4 additions & 1 deletion backend/interfaces/api/handler/article.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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())
}

Expand Down
19 changes: 0 additions & 19 deletions backend/interfaces/api/handler/command.go

This file was deleted.

48 changes: 0 additions & 48 deletions backend/interfaces/api/handler/index.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package handler

import (
"fmt"
"net/http"

"github.com/gorilla/mux"
"github.com/yoshihiro-shu/draft-backend/backend/interfaces/api/request"
)

Expand All @@ -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}
}
12 changes: 0 additions & 12 deletions backend/interfaces/api/user_api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Loading