Skip to content

Commit

Permalink
feat(todo): update
Browse files Browse the repository at this point in the history
  • Loading branch information
dnabil committed Dec 9, 2023
1 parent 2bce496 commit 9bab3a1
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 0 deletions.
29 changes: 29 additions & 0 deletions todo-list-be/app/http/handler/todo_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,33 @@ func (h *TodoHandler) Create(c *gin.Context){
}

Response(c, http.StatusCreated, "todo created", todo)
}

func (h *TodoHandler) Update(c *gin.Context){
req := new(dto.UpdateTodoRequest)
if err := c.ShouldBindJSON(&req); err != nil {
Response(c, http.StatusBadRequest, "bad request", nil)
return
}

if err := req.Validate(); err != nil {
Response(c, http.StatusBadRequest, "validation fail", err)
return
}

auth, _, err := getAuth(c, h.Log)
if err != nil {
Response(c, err.Code(), err.Error(), nil)
return
}

req.UserID = auth.ID

todo, err := h.Service.Update(c.Request.Context(), req)
if err != nil {
Response(c, err.Code(), err.Error(), nil)
return
}

Response(c, http.StatusOK, "update success", todo)
}
1 change: 1 addition & 0 deletions todo-list-be/app/http/route/route.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ func (r *RouteConfig) Load() {
todo := api.Group("/todos")
todo.Use(r.AuthMiddleware)
todo.POST("/", r.TodoHandler.Create)
todo.PUT("/", r.TodoHandler.Update)
// api.GET("/todos", r.TodoHandler.Index) // ada yang completed ada yang ngga (param)
// api.DELETE("/todos", r.TodoHandler.DELETE)
}
15 changes: 15 additions & 0 deletions todo-list-be/dto/todo_dto.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,19 @@ func (r *CreateTodoRequest) Validate() error{
return validation.ValidateStruct(r,
validation.Field(&r.Name, validation.Required, validation.Length(2, 255)),
)
}

type UpdateTodoRequest struct {
ID uint `json:"id"`
UserID uint `json:"-"`

Name string `json:"name"`
IsDone *bool `json:"is_done"`
}
func (r *UpdateTodoRequest) Validate() error{
return validation.ValidateStruct(r,
validation.Field(&r.ID, validation.Required),
validation.Field(&r.Name, validation.Required, validation.Length(2, 255)),
validation.Field(&r.IsDone, validation.Required), // https://github.com/go-ozzo/ozzo-validation/issues/79 jadi pake *
)
}
5 changes: 5 additions & 0 deletions todo-list-be/repo/todo_repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"todo-list-be/model"

"github.com/sirupsen/logrus"
"gorm.io/gorm"
)

type TodoRepo struct {
Expand All @@ -15,4 +16,8 @@ func NewTodoRepo(log *logrus.Logger) *TodoRepo {
return &TodoRepo{
Log: log,
}
}

func (r *TodoRepo) FindByIdAndUserId(db *gorm.DB, todo *model.Todo, id uint, userId uint) error {
return db.Where("id = ? AND user_id = ?", id, userId).Take(todo).Error
}
34 changes: 34 additions & 0 deletions todo-list-be/service/todo_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,39 @@ func (s *TodoService) Create(ctx context.Context, req *dto.CreateTodoRequest) (*

resource := dto.NewTodoResource(todo)

return resource, nil
}

func (s *TodoService) Update(ctx context.Context, req *dto.UpdateTodoRequest) (*dto.TodoResource, errcode.ErrCodeI) {
tx := s.DB.WithContext(ctx).Begin()
defer tx.Rollback()

//
// find by id and userid
todo := new(model.Todo)
if err := s.Repo.FindByIdAndUserId(tx, todo, req.ID, req.UserID); err != nil {
if err == gorm.ErrRecordNotFound {
return nil, errcode.ErrNotFound
}

s.Log.WithError(err).Errorln("failed to find todo by id and user id")
return nil, errcode.ErrInternalServer
}

// update
todo.Name = req.Name
todo.IsDone = *req.IsDone
if err := s.Repo.Update(tx, todo); err != nil {
s.Log.WithError(err).Errorln("failed to update(s) todo")
return nil, errcode.ErrInternalServer
}
//

if err := tx.Commit().Error; err != nil {
s.Log.WithError(err).Warnln("Failed commit transaction")
return nil, errcode.ErrInternalServer
}

resource := dto.NewTodoResource(todo)
return resource, nil
}

0 comments on commit 9bab3a1

Please sign in to comment.