Include TotalSummary in CRUD responses to eliminate redundant API calls
Backend changes: - Add WithSummaryResponse wrappers for Task, TaskCompletion, and Residence CRUD - Update services to return summary with all mutations (create, update, delete) - Update handlers to pass through new response types - Add getSummaryForUser helper for fetching summary in CRUD operations - Wire ResidenceService into TaskService for summary access - Add summary field to JoinResidenceResponse This optimization eliminates the need for a separate getSummary() call after every task/residence mutation, reducing network calls from 2 to 1. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -165,8 +165,17 @@ func (s *ResidenceService) GetSummary(userID uint) (*responses.TotalSummary, err
|
||||
return summary, nil
|
||||
}
|
||||
|
||||
// CreateResidence creates a new residence
|
||||
func (s *ResidenceService) CreateResidence(req *requests.CreateResidenceRequest, ownerID uint) (*responses.ResidenceResponse, error) {
|
||||
// getSummaryForUser is a helper that returns summary for a user, or empty summary on error
|
||||
func (s *ResidenceService) getSummaryForUser(userID uint) responses.TotalSummary {
|
||||
summary, err := s.GetSummary(userID)
|
||||
if err != nil || summary == nil {
|
||||
return responses.TotalSummary{}
|
||||
}
|
||||
return *summary
|
||||
}
|
||||
|
||||
// CreateResidence creates a new residence and returns it with updated summary
|
||||
func (s *ResidenceService) CreateResidence(req *requests.CreateResidenceRequest, ownerID uint) (*responses.ResidenceWithSummaryResponse, error) {
|
||||
// TODO: Check subscription tier limits
|
||||
// count, err := s.residenceRepo.CountByOwner(ownerID)
|
||||
// if err != nil {
|
||||
@@ -217,12 +226,17 @@ func (s *ResidenceService) CreateResidence(req *requests.CreateResidenceRequest,
|
||||
return nil, err
|
||||
}
|
||||
|
||||
resp := responses.NewResidenceResponse(residence)
|
||||
return &resp, nil
|
||||
// Get updated summary
|
||||
summary := s.getSummaryForUser(ownerID)
|
||||
|
||||
return &responses.ResidenceWithSummaryResponse{
|
||||
Data: responses.NewResidenceResponse(residence),
|
||||
Summary: summary,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// UpdateResidence updates a residence
|
||||
func (s *ResidenceService) UpdateResidence(residenceID, userID uint, req *requests.UpdateResidenceRequest) (*responses.ResidenceResponse, error) {
|
||||
// UpdateResidence updates a residence and returns it with updated summary
|
||||
func (s *ResidenceService) UpdateResidence(residenceID, userID uint, req *requests.UpdateResidenceRequest) (*responses.ResidenceWithSummaryResponse, error) {
|
||||
// Check ownership
|
||||
isOwner, err := s.residenceRepo.IsOwner(residenceID, userID)
|
||||
if err != nil {
|
||||
@@ -303,22 +317,37 @@ func (s *ResidenceService) UpdateResidence(residenceID, userID uint, req *reques
|
||||
return nil, err
|
||||
}
|
||||
|
||||
resp := responses.NewResidenceResponse(residence)
|
||||
return &resp, nil
|
||||
// Get updated summary
|
||||
summary := s.getSummaryForUser(userID)
|
||||
|
||||
return &responses.ResidenceWithSummaryResponse{
|
||||
Data: responses.NewResidenceResponse(residence),
|
||||
Summary: summary,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// DeleteResidence soft-deletes a residence
|
||||
func (s *ResidenceService) DeleteResidence(residenceID, userID uint) error {
|
||||
// DeleteResidence soft-deletes a residence and returns updated summary
|
||||
func (s *ResidenceService) DeleteResidence(residenceID, userID uint) (*responses.ResidenceDeleteWithSummaryResponse, error) {
|
||||
// Check ownership
|
||||
isOwner, err := s.residenceRepo.IsOwner(residenceID, userID)
|
||||
if err != nil {
|
||||
return err
|
||||
return nil, err
|
||||
}
|
||||
if !isOwner {
|
||||
return ErrNotResidenceOwner
|
||||
return nil, ErrNotResidenceOwner
|
||||
}
|
||||
|
||||
return s.residenceRepo.Delete(residenceID)
|
||||
if err := s.residenceRepo.Delete(residenceID); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Get updated summary
|
||||
summary := s.getSummaryForUser(userID)
|
||||
|
||||
return &responses.ResidenceDeleteWithSummaryResponse{
|
||||
Data: "residence deleted",
|
||||
Summary: summary,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// GenerateShareCode generates a new share code for a residence
|
||||
@@ -427,9 +456,13 @@ func (s *ResidenceService) JoinWithCode(code string, userID uint) (*responses.Jo
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Get updated summary for the user
|
||||
summary := s.getSummaryForUser(userID)
|
||||
|
||||
return &responses.JoinResidenceResponse{
|
||||
Message: "Successfully joined residence",
|
||||
Residence: responses.NewResidenceResponse(residence),
|
||||
Summary: summary,
|
||||
}, nil
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user