chore: Add chore archiving and unarchiving functionality

This commit is contained in:
Mo Tarbin 2024-12-21 01:49:19 -05:00
parent b9160bf681
commit d1d07b84a0
2 changed files with 75 additions and 0 deletions

View file

@ -841,6 +841,70 @@ func (h *Handler) updateDueDate(c *gin.Context) {
"res": chore, "res": chore,
}) })
} }
func (h *Handler) archiveChore(c *gin.Context) {
rawID := c.Param("id")
id, err := strconv.Atoi(rawID)
if err != nil {
c.JSON(400, gin.H{
"error": "Invalid ID",
})
return
}
currentUser, ok := auth.CurrentUser(c)
if !ok {
c.JSON(500, gin.H{
"error": "Error getting current user",
})
return
}
err = h.choreRepo.ArchiveChore(c, id, currentUser.ID)
if err != nil {
c.JSON(500, gin.H{
"error": "Error archiving chore",
})
return
}
c.JSON(200, gin.H{
"message": "Chore archived successfully",
})
}
func (h *Handler) UnarchiveChore(c *gin.Context) {
rawID := c.Param("id")
id, err := strconv.Atoi(rawID)
if err != nil {
c.JSON(400, gin.H{
"error": "Invalid ID",
})
return
}
currentUser, ok := auth.CurrentUser(c)
if !ok {
c.JSON(500, gin.H{
"error": "Error getting current user",
})
return
}
err = h.choreRepo.UnarchiveChore(c, id, currentUser.ID)
if err != nil {
c.JSON(500, gin.H{
"error": "Error unarchiving chore",
})
return
}
c.JSON(200, gin.H{
"message": "Chore archived successfully",
})
}
func (h *Handler) completeChore(c *gin.Context) { func (h *Handler) completeChore(c *gin.Context) {
type CompleteChoreReq struct { type CompleteChoreReq struct {
Note string `json:"note"` Note string `json:"note"`
@ -1272,6 +1336,7 @@ func Routes(router *gin.Engine, h *Handler, auth *jwt.GinJWTMiddleware) {
{ {
choresRoutes.GET("/", h.getChores) choresRoutes.GET("/", h.getChores)
choresRoutes.GET("/archived", h.getArchivedChores) choresRoutes.GET("/archived", h.getArchivedChores)
choresRoutes.PUT("/", h.editChore) choresRoutes.PUT("/", h.editChore)
choresRoutes.PUT("/:id/priority", h.updatePriority) choresRoutes.PUT("/:id/priority", h.updatePriority)
choresRoutes.POST("/", h.createChore) choresRoutes.POST("/", h.createChore)
@ -1284,6 +1349,8 @@ func Routes(router *gin.Engine, h *Handler, auth *jwt.GinJWTMiddleware) {
choresRoutes.POST("/:id/skip", h.skipChore) choresRoutes.POST("/:id/skip", h.skipChore)
choresRoutes.PUT("/:id/assignee", h.updateAssignee) choresRoutes.PUT("/:id/assignee", h.updateAssignee)
choresRoutes.PUT("/:id/dueDate", h.updateDueDate) choresRoutes.PUT("/:id/dueDate", h.updateDueDate)
choresRoutes.PUT("/:id/archive", h.archiveChore)
choresRoutes.PUT("/:id/unarchive", h.UnarchiveChore)
choresRoutes.DELETE("/:id", h.deleteChore) choresRoutes.DELETE("/:id", h.deleteChore)
} }

View file

@ -293,3 +293,11 @@ func (r *ChoreRepository) GetChoreDetailByID(c context.Context, choreID int, cir
} }
return &choreDetail, nil return &choreDetail, nil
} }
func (r *ChoreRepository) ArchiveChore(c context.Context, choreID int, userID int) error {
return r.db.WithContext(c).Model(&chModel.Chore{}).Where("id = ? and created_by = ?", choreID, userID).Update("is_active", false).Error
}
func (r *ChoreRepository) UnarchiveChore(c context.Context, choreID int, userID int) error {
return r.db.WithContext(c).Model(&chModel.Chore{}).Where("id = ? and created_by = ?", choreID, userID).Update("is_active", true).Error
}