From d1d07b84a08720d1cf08cc660814210e12d075f5 Mon Sep 17 00:00:00 2001 From: Mo Tarbin Date: Sat, 21 Dec 2024 01:49:19 -0500 Subject: [PATCH] chore: Add chore archiving and unarchiving functionality --- internal/chore/handler.go | 67 +++++++++++++++++++++++++++++++ internal/chore/repo/repository.go | 8 ++++ 2 files changed, 75 insertions(+) diff --git a/internal/chore/handler.go b/internal/chore/handler.go index 16a675f..f6133c7 100644 --- a/internal/chore/handler.go +++ b/internal/chore/handler.go @@ -841,6 +841,70 @@ func (h *Handler) updateDueDate(c *gin.Context) { "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) { type CompleteChoreReq struct { Note string `json:"note"` @@ -1272,6 +1336,7 @@ func Routes(router *gin.Engine, h *Handler, auth *jwt.GinJWTMiddleware) { { choresRoutes.GET("/", h.getChores) choresRoutes.GET("/archived", h.getArchivedChores) + choresRoutes.PUT("/", h.editChore) choresRoutes.PUT("/:id/priority", h.updatePriority) 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.PUT("/:id/assignee", h.updateAssignee) choresRoutes.PUT("/:id/dueDate", h.updateDueDate) + choresRoutes.PUT("/:id/archive", h.archiveChore) + choresRoutes.PUT("/:id/unarchive", h.UnarchiveChore) choresRoutes.DELETE("/:id", h.deleteChore) } diff --git a/internal/chore/repo/repository.go b/internal/chore/repo/repository.go index 1a89f1b..0a43938 100644 --- a/internal/chore/repo/repository.go +++ b/internal/chore/repo/repository.go @@ -293,3 +293,11 @@ func (r *ChoreRepository) GetChoreDetailByID(c context.Context, choreID int, cir } 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 +}