From 9f50772dbea9ba7f0b205843671b89bd9e4ed682 Mon Sep 17 00:00:00 2001 From: Mo Tarbin Date: Sun, 3 Nov 2024 22:47:03 -0500 Subject: [PATCH] Add endpoint to update priority --- internal/chore/handler.go | 60 +++++++++++++++++++++++++++++++ internal/chore/model/model.go | 3 ++ internal/chore/repo/repository.go | 1 + 3 files changed, 64 insertions(+) diff --git a/internal/chore/handler.go b/internal/chore/handler.go index e66005b..0300ca7 100644 --- a/internal/chore/handler.go +++ b/internal/chore/handler.go @@ -1017,6 +1017,65 @@ func (h *Handler) ModifyHistory(c *gin.Context) { }) } +func (h *Handler) updatePriority(c *gin.Context) { + type PriorityReq struct { + Priority *int `json:"priority" binding:"required,gt=-1"` + } + + currrentUser, ok := auth.CurrentUser(c) + if !ok { + c.JSON(500, gin.H{ + "error": "Error getting current user", + }) + return + } + + var priorityReq PriorityReq + if err := c.ShouldBindJSON(&priorityReq); err != nil { + log.Print(err) + c.JSON(400, gin.H{ + "error": "Invalid request", + }) + return + } + + rawID := c.Param("id") + id, err := strconv.Atoi(rawID) + if err != nil { + c.JSON(400, gin.H{ + "error": "Invalid ID", + }) + return + } + + chore, err := h.choreRepo.GetChore(c, id) + if err != nil { + c.JSON(500, gin.H{ + "error": "Error getting chore", + }) + return + } + + if currrentUser.ID != chore.CreatedBy { + c.JSON(403, gin.H{ + "error": "You are not allowed to update this chore", + }) + return + } + + chore.Priority = *priorityReq.Priority + if err := h.choreRepo.UpsertChore(c, chore); err != nil { + c.JSON(500, gin.H{ + "error": "Error updating priority", + }) + return + } + + c.JSON(200, gin.H{ + "res": chore, + }) +} + func (h *Handler) DeleteHistory(c *gin.Context) { currentUser, ok := auth.CurrentUser(c) @@ -1181,6 +1240,7 @@ func Routes(router *gin.Engine, h *Handler, auth *jwt.GinJWTMiddleware) { { choresRoutes.GET("/", h.getChores) choresRoutes.PUT("/", h.editChore) + choresRoutes.PUT("/:id/priority", h.updatePriority) choresRoutes.POST("/", h.createChore) choresRoutes.GET("/:id", h.getChore) choresRoutes.GET("/:id/details", h.GetChoreDetail) diff --git a/internal/chore/model/model.go b/internal/chore/model/model.go index b2633b3..6e88f80 100644 --- a/internal/chore/model/model.go +++ b/internal/chore/model/model.go @@ -43,6 +43,8 @@ type Chore struct { CreatedBy int `json:"createdBy" gorm:"column:created_by"` // Who created the chore UpdatedBy int `json:"updatedBy" gorm:"column:updated_by"` // Who last updated the chore ThingChore *tModel.ThingChore `json:"thingChore" gorm:"foreignkey:chore_id;references:id;<-:false"` // ThingChore relationship + Status int `json:"status" gorm:"column:status"` + Priority int `json:"priority" gorm:"column:priority"` } type ChoreAssignees struct { ID int `json:"-" gorm:"primary_key"` @@ -100,6 +102,7 @@ type ChoreDetail struct { LastCompletedDate *time.Time `json:"lastCompletedDate" gorm:"column:last_completed_date"` LastCompletedBy *int `json:"lastCompletedBy" gorm:"column:last_completed_by"` TotalCompletedCount int `json:"totalCompletedCount" gorm:"column:total_completed"` + Priority int `json:"priority" gorm:"column:priority"` Notes *string `json:"notes" gorm:"column:notes"` CreatedBy int `json:"createdBy" gorm:"column:created_by"` } diff --git a/internal/chore/repo/repository.go b/internal/chore/repo/repository.go index e7035d5..cf5462c 100644 --- a/internal/chore/repo/repository.go +++ b/internal/chore/repo/repository.go @@ -249,6 +249,7 @@ func (r *ChoreRepository) GetChoreDetailByID(c context.Context, choreID int, cir chores.next_due_date, chores.assigned_to, chores.created_by, + chores.priority, recent_history.last_completed_date, recent_history.notes, recent_history.last_assigned_to as last_completed_by,