diff --git a/internal/chore/handler.go b/internal/chore/handler.go index 2a62a95..16a675f 100644 --- a/internal/chore/handler.go +++ b/internal/chore/handler.go @@ -98,6 +98,26 @@ func (h *Handler) getChores(c *gin.Context) { }) } +func (h *Handler) getArchivedChores(c *gin.Context) { + u, ok := auth.CurrentUser(c) + if !ok { + c.JSON(500, gin.H{ + "error": "Error getting current circle", + }) + return + } + chores, err := h.choreRepo.GetArchivedChores(c, u.CircleID, u.ID) + if err != nil { + c.JSON(500, gin.H{ + "error": "Error getting chores", + }) + return + } + + c.JSON(200, gin.H{ + "res": chores, + }) +} func (h *Handler) getChore(c *gin.Context) { currentUser, ok := auth.CurrentUser(c) @@ -1251,6 +1271,7 @@ func Routes(router *gin.Engine, h *Handler, auth *jwt.GinJWTMiddleware) { choresRoutes.Use(auth.MiddlewareFunc()) { choresRoutes.GET("/", h.getChores) + choresRoutes.GET("/archived", h.getArchivedChores) choresRoutes.PUT("/", h.editChore) choresRoutes.PUT("/:id/priority", h.updatePriority) choresRoutes.POST("/", h.createChore) diff --git a/internal/chore/repo/repository.go b/internal/chore/repo/repository.go index bdd75ad..1a89f1b 100644 --- a/internal/chore/repo/repository.go +++ b/internal/chore/repo/repository.go @@ -52,7 +52,15 @@ func (r *ChoreRepository) GetChore(c context.Context, choreID int) (*chModel.Cho func (r *ChoreRepository) GetChores(c context.Context, circleID int, userID int) ([]*chModel.Chore, error) { var chores []*chModel.Chore // if err := r.db.WithContext(c).Preload("Assignees").Where("is_active = ?", true).Order("next_due_date asc").Find(&chores, "circle_id = ?", circleID).Error; err != nil { - if err := r.db.WithContext(c).Preload("Assignees").Preload("LabelsV2").Joins("left join chore_assignees on chores.id = chore_assignees.chore_id").Where("chores.circle_id = ? AND (chores.created_by = ? OR chore_assignees.user_id = ?)", circleID, userID, userID).Group("chores.id").Order("next_due_date asc").Find(&chores, "circle_id = ?", circleID).Error; err != nil { + if err := r.db.WithContext(c).Preload("Assignees").Preload("LabelsV2").Joins("left join chore_assignees on chores.id = chore_assignees.chore_id").Where("chores.circle_id = ? AND (chores.created_by = ? OR chore_assignees.user_id = ?)", circleID, userID, userID).Group("chores.id").Order("next_due_date asc").Find(&chores, "circle_id = ? AND is_active = ?", circleID, true).Error; err != nil { + return nil, err + } + return chores, nil +} + +func (r *ChoreRepository) GetArchivedChores(c context.Context, circleID int, userID int) ([]*chModel.Chore, error) { + var chores []*chModel.Chore + if err := r.db.WithContext(c).Preload("Assignees").Preload("LabelsV2").Joins("left join chore_assignees on chores.id = chore_assignees.chore_id").Where("chores.circle_id = ? AND (chores.created_by = ? OR chore_assignees.user_id = ?)", circleID, userID, userID).Group("chores.id").Order("next_due_date asc").Find(&chores, "circle_id = ? AND is_active = ?", circleID, false).Error; err != nil { return nil, err } return chores, nil