init
This commit is contained in:
84
internal/handler/settings_handler.go
Normal file
84
internal/handler/settings_handler.go
Normal file
@@ -0,0 +1,84 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"errors"
|
||||
|
||||
"accounting-app/pkg/api"
|
||||
"accounting-app/internal/service"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
// SettingsHandler handles HTTP requests for user settings operations
|
||||
// Feature: accounting-feature-upgrade
|
||||
// Validates: Requirements 5.4, 6.5, 8.25-8.27
|
||||
type SettingsHandler struct {
|
||||
settingsService service.UserSettingsServiceInterface
|
||||
}
|
||||
|
||||
// NewSettingsHandler creates a new SettingsHandler instance
|
||||
func NewSettingsHandler(settingsService service.UserSettingsServiceInterface) *SettingsHandler {
|
||||
return &SettingsHandler{
|
||||
settingsService: settingsService,
|
||||
}
|
||||
}
|
||||
|
||||
// GetSettings handles GET /api/v1/settings
|
||||
// Retrieves user settings, creating default settings if not found
|
||||
// Feature: accounting-feature-upgrade
|
||||
// Validates: Requirements 5.4, 6.5, 8.25-8.27
|
||||
func (h *SettingsHandler) GetSettings(c *gin.Context) {
|
||||
userId, exists := c.Get("user_id")
|
||||
if !exists {
|
||||
api.Unauthorized(c, "User not authenticated")
|
||||
return
|
||||
}
|
||||
|
||||
settings, err := h.settingsService.GetSettings(userId.(uint))
|
||||
if err != nil {
|
||||
api.InternalError(c, "Failed to get settings: "+err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
api.Success(c, settings)
|
||||
}
|
||||
|
||||
// UpdateSettings handles PUT /api/v1/settings
|
||||
// Updates user settings with validation
|
||||
// Feature: accounting-feature-upgrade
|
||||
// Validates: Requirements 5.4, 6.5, 8.25-8.27
|
||||
func (h *SettingsHandler) UpdateSettings(c *gin.Context) {
|
||||
userId, exists := c.Get("user_id")
|
||||
if !exists {
|
||||
api.Unauthorized(c, "User not authenticated")
|
||||
return
|
||||
}
|
||||
|
||||
var input service.UserSettingsInput
|
||||
if err := c.ShouldBindJSON(&input); err != nil {
|
||||
api.ValidationError(c, "Invalid request body: "+err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
settings, err := h.settingsService.UpdateSettings(userId.(uint), input)
|
||||
if err != nil {
|
||||
if errors.Is(err, service.ErrInvalidIconLayout) {
|
||||
api.BadRequest(c, "Invalid icon layout, must be one of: four, five, six")
|
||||
return
|
||||
}
|
||||
if errors.Is(err, service.ErrInvalidImageCompression) {
|
||||
api.BadRequest(c, "Invalid image compression, must be one of: low, medium, high")
|
||||
return
|
||||
}
|
||||
api.InternalError(c, "Failed to update settings: "+err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
api.Success(c, settings)
|
||||
}
|
||||
|
||||
// RegisterRoutes registers all settings routes to the given router group
|
||||
func (h *SettingsHandler) RegisterRoutes(rg *gin.RouterGroup) {
|
||||
rg.GET("/settings", h.GetSettings)
|
||||
rg.PUT("/settings", h.UpdateSettings)
|
||||
}
|
||||
Reference in New Issue
Block a user