init
This commit is contained in:
85
internal/handler/default_account_handler.go
Normal file
85
internal/handler/default_account_handler.go
Normal file
@@ -0,0 +1,85 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"errors"
|
||||
|
||||
"accounting-app/pkg/api"
|
||||
"accounting-app/internal/service"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
// DefaultAccountHandler handles HTTP requests for default account settings
|
||||
// Feature: financial-core-upgrade
|
||||
// Validates: Requirements 11.1-11.4
|
||||
type DefaultAccountHandler struct {
|
||||
userSettingsService *service.UserSettingsService
|
||||
}
|
||||
|
||||
// NewDefaultAccountHandler creates a new DefaultAccountHandler instance
|
||||
func NewDefaultAccountHandler(userSettingsService *service.UserSettingsService) *DefaultAccountHandler {
|
||||
return &DefaultAccountHandler{
|
||||
userSettingsService: userSettingsService,
|
||||
}
|
||||
}
|
||||
|
||||
// GetDefaultAccounts handles GET /api/settings/default-accounts
|
||||
// Returns the current default account settings
|
||||
// Validates: Requirements 11.1, 11.3
|
||||
func (h *DefaultAccountHandler) GetDefaultAccounts(c *gin.Context) {
|
||||
userId, exists := c.Get("user_id")
|
||||
if !exists {
|
||||
api.Unauthorized(c, "User not authenticated")
|
||||
return
|
||||
}
|
||||
|
||||
response, err := h.userSettingsService.GetDefaultAccounts(userId.(uint))
|
||||
if err != nil {
|
||||
api.InternalError(c, "Failed to get default accounts: "+err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
api.Success(c, response)
|
||||
}
|
||||
|
||||
// UpdateDefaultAccounts handles PUT /api/settings/default-accounts
|
||||
// Updates the default account settings
|
||||
// Validates: Requirements 11.2, 11.4
|
||||
func (h *DefaultAccountHandler) UpdateDefaultAccounts(c *gin.Context) {
|
||||
userId, exists := c.Get("user_id")
|
||||
if !exists {
|
||||
api.Unauthorized(c, "User not authenticated")
|
||||
return
|
||||
}
|
||||
|
||||
var input service.DefaultAccountsInput
|
||||
if err := c.ShouldBindJSON(&input); err != nil {
|
||||
api.ValidationError(c, "Invalid request body: "+err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
response, err := h.userSettingsService.UpdateDefaultAccounts(userId.(uint), input)
|
||||
if err != nil {
|
||||
if errors.Is(err, service.ErrDefaultAccountNotFound) {
|
||||
api.NotFound(c, "Specified account not found")
|
||||
return
|
||||
}
|
||||
if errors.Is(err, service.ErrInvalidDefaultAccount) {
|
||||
api.BadRequest(c, "Invalid default account")
|
||||
return
|
||||
}
|
||||
api.InternalError(c, "Failed to update default accounts: "+err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
api.Success(c, response)
|
||||
}
|
||||
|
||||
// RegisterRoutes registers all default account routes to the given router group
|
||||
func (h *DefaultAccountHandler) RegisterRoutes(rg *gin.RouterGroup) {
|
||||
settings := rg.Group("/settings")
|
||||
{
|
||||
settings.GET("/default-accounts", h.GetDefaultAccounts)
|
||||
settings.PUT("/default-accounts", h.UpdateDefaultAccounts)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user