init
This commit is contained in:
198
internal/handler/sub_account_handler.go
Normal file
198
internal/handler/sub_account_handler.go
Normal file
@@ -0,0 +1,198 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"strconv"
|
||||
|
||||
"accounting-app/pkg/api"
|
||||
"accounting-app/internal/service"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
// SubAccountHandler handles HTTP requests for sub-account operations
|
||||
// Feature: financial-core-upgrade
|
||||
// Validates: Requirements 9.1-9.8
|
||||
type SubAccountHandler struct {
|
||||
subAccountService *service.SubAccountService
|
||||
}
|
||||
|
||||
// NewSubAccountHandler creates a new SubAccountHandler instance
|
||||
func NewSubAccountHandler(subAccountService *service.SubAccountService) *SubAccountHandler {
|
||||
return &SubAccountHandler{
|
||||
subAccountService: subAccountService,
|
||||
}
|
||||
}
|
||||
|
||||
// ListSubAccounts handles GET /api/accounts/:id/sub-accounts
|
||||
// Returns all sub-accounts for a parent account
|
||||
// Validates: Requirements 9.1, 9.5
|
||||
func (h *SubAccountHandler) ListSubAccounts(c *gin.Context) {
|
||||
userId, exists := c.Get("user_id")
|
||||
if !exists {
|
||||
api.Unauthorized(c, "User not authenticated")
|
||||
return
|
||||
}
|
||||
|
||||
parentID, err := strconv.ParseUint(c.Param("id"), 10, 32)
|
||||
if err != nil {
|
||||
api.BadRequest(c, "Invalid parent account ID")
|
||||
return
|
||||
}
|
||||
|
||||
subAccounts, err := h.subAccountService.ListSubAccounts(userId.(uint), uint(parentID))
|
||||
if err != nil {
|
||||
if errors.Is(err, service.ErrParentAccountNotFound) {
|
||||
api.NotFound(c, "Parent account not found")
|
||||
return
|
||||
}
|
||||
api.InternalError(c, "Failed to get sub-accounts: "+err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
api.Success(c, subAccounts)
|
||||
}
|
||||
|
||||
// CreateSubAccount handles POST /api/accounts/:id/sub-accounts
|
||||
// Creates a new sub-account under the specified parent account
|
||||
// Validates: Requirements 9.2, 9.6
|
||||
func (h *SubAccountHandler) CreateSubAccount(c *gin.Context) {
|
||||
userId, exists := c.Get("user_id")
|
||||
if !exists {
|
||||
api.Unauthorized(c, "User not authenticated")
|
||||
return
|
||||
}
|
||||
|
||||
parentID, err := strconv.ParseUint(c.Param("id"), 10, 32)
|
||||
if err != nil {
|
||||
api.BadRequest(c, "Invalid parent account ID")
|
||||
return
|
||||
}
|
||||
|
||||
var input service.CreateSubAccountInput
|
||||
if err := c.ShouldBindJSON(&input); err != nil {
|
||||
api.ValidationError(c, "Invalid request body: "+err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
subAccount, err := h.subAccountService.CreateSubAccount(userId.(uint), uint(parentID), input)
|
||||
if err != nil {
|
||||
if errors.Is(err, service.ErrParentAccountNotFound) {
|
||||
api.NotFound(c, "Parent account not found")
|
||||
return
|
||||
}
|
||||
if errors.Is(err, service.ErrParentIsSubAccount) {
|
||||
api.BadRequest(c, "Cannot create sub-account under another sub-account")
|
||||
return
|
||||
}
|
||||
if errors.Is(err, service.ErrInvalidSubAccountType) {
|
||||
api.BadRequest(c, "Invalid sub-account type")
|
||||
return
|
||||
}
|
||||
api.InternalError(c, "Failed to create sub-account: "+err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
api.Created(c, subAccount)
|
||||
}
|
||||
|
||||
// UpdateSubAccount handles PUT /api/accounts/:id/sub-accounts/:subId
|
||||
// Updates an existing sub-account
|
||||
// Validates: Requirements 9.3, 9.7
|
||||
func (h *SubAccountHandler) UpdateSubAccount(c *gin.Context) {
|
||||
userId, exists := c.Get("user_id")
|
||||
if !exists {
|
||||
api.Unauthorized(c, "User not authenticated")
|
||||
return
|
||||
}
|
||||
|
||||
parentID, err := strconv.ParseUint(c.Param("id"), 10, 32)
|
||||
if err != nil {
|
||||
api.BadRequest(c, "Invalid parent account ID")
|
||||
return
|
||||
}
|
||||
|
||||
subID, err := strconv.ParseUint(c.Param("subId"), 10, 32)
|
||||
if err != nil {
|
||||
api.BadRequest(c, "Invalid sub-account ID")
|
||||
return
|
||||
}
|
||||
|
||||
var input service.UpdateSubAccountInput
|
||||
if err := c.ShouldBindJSON(&input); err != nil {
|
||||
api.ValidationError(c, "Invalid request body: "+err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
subAccount, err := h.subAccountService.UpdateSubAccount(userId.(uint), uint(parentID), uint(subID), input)
|
||||
if err != nil {
|
||||
if errors.Is(err, service.ErrSubAccountNotFound) {
|
||||
api.NotFound(c, "Sub-account not found")
|
||||
return
|
||||
}
|
||||
if errors.Is(err, service.ErrParentAccountNotFound) {
|
||||
api.NotFound(c, "Parent account not found")
|
||||
return
|
||||
}
|
||||
if errors.Is(err, service.ErrSubAccountNotBelongTo) {
|
||||
api.BadRequest(c, "Sub-account does not belong to the specified parent account")
|
||||
return
|
||||
}
|
||||
api.InternalError(c, "Failed to update sub-account: "+err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
api.Success(c, subAccount)
|
||||
}
|
||||
|
||||
// DeleteSubAccount handles DELETE /api/accounts/:id/sub-accounts/:subId
|
||||
// Deletes a sub-account and transfers its balance back to the parent account
|
||||
// Validates: Requirements 9.4, 9.8
|
||||
func (h *SubAccountHandler) DeleteSubAccount(c *gin.Context) {
|
||||
userId, exists := c.Get("user_id")
|
||||
if !exists {
|
||||
api.Unauthorized(c, "User not authenticated")
|
||||
return
|
||||
}
|
||||
|
||||
parentID, err := strconv.ParseUint(c.Param("id"), 10, 32)
|
||||
if err != nil {
|
||||
api.BadRequest(c, "Invalid parent account ID")
|
||||
return
|
||||
}
|
||||
|
||||
subID, err := strconv.ParseUint(c.Param("subId"), 10, 32)
|
||||
if err != nil {
|
||||
api.BadRequest(c, "Invalid sub-account ID")
|
||||
return
|
||||
}
|
||||
|
||||
err = h.subAccountService.DeleteSubAccount(userId.(uint), uint(parentID), uint(subID))
|
||||
if err != nil {
|
||||
if errors.Is(err, service.ErrSubAccountNotFound) {
|
||||
api.NotFound(c, "Sub-account not found")
|
||||
return
|
||||
}
|
||||
if errors.Is(err, service.ErrParentAccountNotFound) {
|
||||
api.NotFound(c, "Parent account not found")
|
||||
return
|
||||
}
|
||||
if errors.Is(err, service.ErrSubAccountNotBelongTo) {
|
||||
api.BadRequest(c, "Sub-account does not belong to the specified parent account")
|
||||
return
|
||||
}
|
||||
api.InternalError(c, "Failed to delete sub-account: "+err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
api.NoContent(c)
|
||||
}
|
||||
|
||||
// RegisterRoutes registers all sub-account routes to the given router group
|
||||
func (h *SubAccountHandler) RegisterRoutes(rg *gin.RouterGroup) {
|
||||
// Sub-account routes under /accounts/:id/sub-accounts
|
||||
rg.GET("/accounts/:id/sub-accounts", h.ListSubAccounts)
|
||||
rg.POST("/accounts/:id/sub-accounts", h.CreateSubAccount)
|
||||
rg.PUT("/accounts/:id/sub-accounts/:subId", h.UpdateSubAccount)
|
||||
rg.DELETE("/accounts/:id/sub-accounts/:subId", h.DeleteSubAccount)
|
||||
}
|
||||
Reference in New Issue
Block a user