chore: initial commit of HouseholdBudgetBook (backend, frontend, desktop)
This commit is contained in:
commit
4a5db3f1f9
167 files changed
+21732
No files matched your search
@@ -0,0 +1,159 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
"household-book/model"
|
||||
"household-book/service"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
type AccountHandler struct {
|
||||
svc *service.AccountService
|
||||
}
|
||||
|
||||
func (h *AccountHandler) Reconcile(c *gin.Context) {
|
||||
id, err := strconv.ParseInt(c.Param("id"), 10, 64)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "无效的ID"})
|
||||
return
|
||||
}
|
||||
var req model.ReconcileAccountRequest
|
||||
if c.ShouldBindJSON(&req) != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "请求参数格式错误"})
|
||||
return
|
||||
}
|
||||
v, e := h.svc.Reconcile(c.Request.Context(), id, req)
|
||||
if e != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "账户对账失败"})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusCreated, v)
|
||||
}
|
||||
func (h *AccountHandler) ListReconciliations(c *gin.Context) {
|
||||
id, err := strconv.ParseInt(c.Param("id"), 10, 64)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "无效的ID"})
|
||||
return
|
||||
}
|
||||
v, e := h.svc.ListReconciliations(c.Request.Context(), id)
|
||||
if e != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "获取对账记录失败"})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, v)
|
||||
}
|
||||
|
||||
func NewAccountHandler(svc *service.AccountService) *AccountHandler {
|
||||
return &AccountHandler{svc: svc}
|
||||
}
|
||||
|
||||
func (h *AccountHandler) List(c *gin.Context) {
|
||||
accounts, err := h.svc.List(c.Request.Context())
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "获取账户列表失败"})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, accounts)
|
||||
}
|
||||
|
||||
func (h *AccountHandler) Create(c *gin.Context) {
|
||||
var req model.CreateAccountRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "请求参数格式错误"})
|
||||
return
|
||||
}
|
||||
if req.Name == "" {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "账户名称不能为空"})
|
||||
return
|
||||
}
|
||||
if req.Type == "" {
|
||||
req.Type = "other"
|
||||
}
|
||||
if req.Type == "bank_card" {
|
||||
req.Type = "debit_card"
|
||||
}
|
||||
if !validAccountType(req.Type) {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "账户类型无效"})
|
||||
return
|
||||
}
|
||||
|
||||
account, err := h.svc.Create(c.Request.Context(), req)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "添加账户失败"})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusCreated, account)
|
||||
}
|
||||
|
||||
func (h *AccountHandler) Update(c *gin.Context) {
|
||||
id, err := strconv.ParseInt(c.Param("id"), 10, 64)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "无效的ID"})
|
||||
return
|
||||
}
|
||||
|
||||
var req model.UpdateAccountRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "请求参数格式错误"})
|
||||
return
|
||||
}
|
||||
if req.Name != nil && *req.Name == "" {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "账户名称不能为空"})
|
||||
return
|
||||
}
|
||||
if req.Type != nil {
|
||||
if *req.Type == "bank_card" {
|
||||
*req.Type = "debit_card"
|
||||
}
|
||||
if !validAccountType(*req.Type) {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "账户类型无效"})
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
account, err := h.svc.Update(c.Request.Context(), id, req)
|
||||
if err != nil {
|
||||
if errors.Is(err, service.ErrNotFound) {
|
||||
c.JSON(http.StatusNotFound, gin.H{"error": "账户不存在"})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "更新账户失败"})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, account)
|
||||
}
|
||||
|
||||
func (h *AccountHandler) Delete(c *gin.Context) {
|
||||
id, err := strconv.ParseInt(c.Param("id"), 10, 64)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "无效的ID"})
|
||||
return
|
||||
}
|
||||
|
||||
if err := h.svc.Delete(c.Request.Context(), id); err != nil {
|
||||
if errors.Is(err, service.ErrNotFound) {
|
||||
c.JSON(http.StatusNotFound, gin.H{"error": "账户不存在"})
|
||||
return
|
||||
}
|
||||
if errors.Is(err, service.ErrHasTransactions) {
|
||||
c.JSON(http.StatusConflict, gin.H{"error": "该账户仍被收支记录、资产、导入批次或对账记录引用,无法删除"})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "删除账户失败"})
|
||||
return
|
||||
}
|
||||
c.Status(http.StatusNoContent)
|
||||
}
|
||||
|
||||
func validAccountType(typ string) bool {
|
||||
switch typ {
|
||||
case "alipay", "wechat", "debit_card", "credit_card", "cash", "other":
|
||||
return true
|
||||
default:
|
||||
return false
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user