Add endpoint to update priority

This commit is contained in:
Mo Tarbin 2024-11-03 22:47:03 -05:00
parent 3cad5f8d71
commit 493d9264d3
3 changed files with 64 additions and 0 deletions

View file

@ -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)
@ -1156,6 +1215,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)

View file

@ -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"`
}

View file

@ -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,