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,99 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
"household-book/model"
|
||||
"household-book/service"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
type MemberHandler struct {
|
||||
svc *service.MemberService
|
||||
}
|
||||
|
||||
func NewMemberHandler(svc *service.MemberService) *MemberHandler {
|
||||
return &MemberHandler{svc: svc}
|
||||
}
|
||||
|
||||
func (h *MemberHandler) List(c *gin.Context) {
|
||||
members, err := h.svc.List(c.Request.Context())
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "获取成员列表失败"})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, members)
|
||||
}
|
||||
|
||||
func (h *MemberHandler) Create(c *gin.Context) {
|
||||
var req model.CreateMemberRequest
|
||||
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 len(req.Name) > 50 {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "姓名不能超过50个字符"})
|
||||
return
|
||||
}
|
||||
|
||||
member, err := h.svc.Create(c.Request.Context(), req)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "添加成员失败"})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusCreated, member)
|
||||
}
|
||||
|
||||
func (h *MemberHandler) 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.UpdateMemberRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "请求参数格式错误"})
|
||||
return
|
||||
}
|
||||
|
||||
member, 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, member)
|
||||
}
|
||||
|
||||
func (h *MemberHandler) 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)
|
||||
}
|
||||
Reference in new issue
Block a user