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,173 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
"household-book/model"
|
||||
"household-book/service"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
type AssetHandler struct{ svc *service.AssetService }
|
||||
|
||||
func NewAssetHandler(svc *service.AssetService) *AssetHandler { return &AssetHandler{svc: svc} }
|
||||
|
||||
func (h *AssetHandler) ListAssets(c *gin.Context) {
|
||||
items, err := h.svc.ListAssets(c.Request.Context())
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "获取资产列表失败"})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, items)
|
||||
}
|
||||
|
||||
func (h *AssetHandler) CreateAsset(c *gin.Context) {
|
||||
var req model.CreateAssetRequest
|
||||
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
|
||||
}
|
||||
item, err := h.svc.CreateAsset(c.Request.Context(), req)
|
||||
if err != nil {
|
||||
if errors.Is(err, service.ErrAccountAlreadyBound) {
|
||||
c.JSON(http.StatusConflict, gin.H{"error": "该账户已绑定其他资产"})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "添加资产失败"})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusCreated, item)
|
||||
}
|
||||
|
||||
func (h *AssetHandler) UpdateAsset(c *gin.Context) {
|
||||
id, ok := parseID(c)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
var req model.UpdateAssetRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "请求参数格式错误"})
|
||||
return
|
||||
}
|
||||
item, err := h.svc.UpdateAsset(c.Request.Context(), id, req)
|
||||
if err != nil {
|
||||
if errors.Is(err, service.ErrAccountAlreadyBound) {
|
||||
c.JSON(http.StatusConflict, gin.H{"error": "该账户已绑定其他资产"})
|
||||
return
|
||||
}
|
||||
writeEntityError(c, err, "更新资产失败", "资产不存在")
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, item)
|
||||
}
|
||||
|
||||
func (h *AssetHandler) DeleteAsset(c *gin.Context) {
|
||||
id, ok := parseID(c)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
if err := h.svc.DeleteAsset(c.Request.Context(), id); err != nil {
|
||||
writeEntityError(c, err, "删除资产失败", "资产不存在")
|
||||
return
|
||||
}
|
||||
c.Status(http.StatusNoContent)
|
||||
}
|
||||
|
||||
func (h *AssetHandler) ListLiabilities(c *gin.Context) {
|
||||
items, err := h.svc.ListLiabilities(c.Request.Context())
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "获取负债列表失败"})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, items)
|
||||
}
|
||||
|
||||
func (h *AssetHandler) CreateLiability(c *gin.Context) {
|
||||
var req model.CreateLiabilityRequest
|
||||
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
|
||||
}
|
||||
item, err := h.svc.CreateLiability(c.Request.Context(), req)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "添加负债失败"})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusCreated, item)
|
||||
}
|
||||
|
||||
func (h *AssetHandler) UpdateLiability(c *gin.Context) {
|
||||
id, ok := parseID(c)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
var req model.UpdateLiabilityRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "请求参数格式错误"})
|
||||
return
|
||||
}
|
||||
item, err := h.svc.UpdateLiability(c.Request.Context(), id, req)
|
||||
if err != nil {
|
||||
writeEntityError(c, err, "更新负债失败", "负债不存在")
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, item)
|
||||
}
|
||||
|
||||
func (h *AssetHandler) DeleteLiability(c *gin.Context) {
|
||||
id, ok := parseID(c)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
if err := h.svc.DeleteLiability(c.Request.Context(), id); err != nil {
|
||||
writeEntityError(c, err, "删除负债失败", "负债不存在")
|
||||
return
|
||||
}
|
||||
c.Status(http.StatusNoContent)
|
||||
}
|
||||
|
||||
func (h *AssetHandler) NetWorth(c *gin.Context) {
|
||||
data, err := h.svc.NetWorth(c.Request.Context())
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "计算净值失败"})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, data)
|
||||
}
|
||||
|
||||
func (h *AssetHandler) ListSnapshots(c *gin.Context) {
|
||||
data, err := h.svc.ListSnapshots(c.Request.Context())
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "获取净值快照失败"})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, data)
|
||||
}
|
||||
|
||||
func parseID(c *gin.Context) (int64, bool) {
|
||||
id, err := strconv.ParseInt(c.Param("id"), 10, 64)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "无效的ID"})
|
||||
return 0, false
|
||||
}
|
||||
return id, true
|
||||
}
|
||||
|
||||
func writeEntityError(c *gin.Context, err error, generic, notFound string) {
|
||||
if errors.Is(err, service.ErrNotFound) {
|
||||
c.JSON(http.StatusNotFound, gin.H{"error": notFound})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": generic})
|
||||
}
|
||||
Reference in new issue
Block a user