Add lightweight summary endpoint for task statistics
- Add GET /api/residences/summary/ endpoint - Returns task statistics without full residence data - Enables efficient refresh of home screen summary counts 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -56,6 +56,20 @@ func (h *ResidenceHandler) GetMyResidences(c *gin.Context) {
|
||||
c.JSON(http.StatusOK, response)
|
||||
}
|
||||
|
||||
// GetSummary handles GET /api/residences/summary/
|
||||
// Returns just the task statistics summary without full residence data
|
||||
func (h *ResidenceHandler) GetSummary(c *gin.Context) {
|
||||
user := c.MustGet(middleware.AuthUserKey).(*models.User)
|
||||
|
||||
summary, err := h.residenceService.GetSummary(user.ID)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, summary)
|
||||
}
|
||||
|
||||
// GetResidence handles GET /api/residences/:id/
|
||||
func (h *ResidenceHandler) GetResidence(c *gin.Context) {
|
||||
user := c.MustGet(middleware.AuthUserKey).(*models.User)
|
||||
|
||||
@@ -244,6 +244,7 @@ func setupResidenceRoutes(api *gin.RouterGroup, residenceHandler *handlers.Resid
|
||||
residences.GET("/", residenceHandler.ListResidences)
|
||||
residences.POST("/", residenceHandler.CreateResidence)
|
||||
residences.GET("/my-residences/", residenceHandler.GetMyResidences)
|
||||
residences.GET("/summary/", residenceHandler.GetSummary)
|
||||
residences.POST("/join-with-code/", residenceHandler.JoinWithCode)
|
||||
|
||||
residences.GET("/:id/", residenceHandler.GetResidence)
|
||||
|
||||
@@ -120,6 +120,40 @@ func (s *ResidenceService) GetMyResidences(userID uint) (*responses.MyResidences
|
||||
}, nil
|
||||
}
|
||||
|
||||
// GetSummary returns just the task summary statistics for a user's residences
|
||||
// This is a lightweight endpoint for refreshing summary counts without full residence data
|
||||
func (s *ResidenceService) GetSummary(userID uint) (*responses.TotalSummary, error) {
|
||||
residences, err := s.residenceRepo.FindByUser(userID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
summary := &responses.TotalSummary{
|
||||
TotalResidences: len(residences),
|
||||
}
|
||||
|
||||
// Get task statistics if task repository is available
|
||||
if s.taskRepo != nil && len(residences) > 0 {
|
||||
// Collect residence IDs
|
||||
residenceIDs := make([]uint, len(residences))
|
||||
for i, r := range residences {
|
||||
residenceIDs[i] = r.ID
|
||||
}
|
||||
|
||||
// Get aggregated statistics
|
||||
stats, err := s.taskRepo.GetTaskStatistics(residenceIDs)
|
||||
if err == nil && stats != nil {
|
||||
summary.TotalTasks = stats.TotalTasks
|
||||
summary.TotalPending = stats.TotalPending
|
||||
summary.TotalOverdue = stats.TotalOverdue
|
||||
summary.TasksDueNextWeek = stats.TasksDueNextWeek
|
||||
summary.TasksDueNextMonth = stats.TasksDueNextMonth
|
||||
}
|
||||
}
|
||||
|
||||
return summary, nil
|
||||
}
|
||||
|
||||
// CreateResidence creates a new residence
|
||||
func (s *ResidenceService) CreateResidence(req *requests.CreateResidenceRequest, ownerID uint) (*responses.ResidenceResponse, error) {
|
||||
// TODO: Check subscription tier limits
|
||||
|
||||
Reference in New Issue
Block a user