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,157 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
"household-book/model"
|
||||
"household-book/service"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
type PolicyHandler struct{ svc *service.PolicyService }
|
||||
|
||||
func NewPolicyHandler(svc *service.PolicyService) *PolicyHandler { return &PolicyHandler{svc: svc} }
|
||||
|
||||
func (h *PolicyHandler) List(c *gin.Context) {
|
||||
items, err := h.svc.List(c.Request.Context())
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "获取保单列表失败"})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, items)
|
||||
}
|
||||
|
||||
func (h *PolicyHandler) ListPayments(c *gin.Context) {
|
||||
id, ok := parseID(c)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
v, e := h.svc.ListPayments(c.Request.Context(), id)
|
||||
if e != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "获取缴费记录失败"})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, v)
|
||||
}
|
||||
func (h *PolicyHandler) CreatePayment(c *gin.Context) {
|
||||
id, ok := parseID(c)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
var req model.CreatePolicyPaymentRequest
|
||||
if c.ShouldBindJSON(&req) != nil || req.DueDate == "" || req.Amount < 0 {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "缴费日期或金额无效"})
|
||||
return
|
||||
}
|
||||
v, e := h.svc.CreatePayment(c.Request.Context(), id, req)
|
||||
if e != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "创建缴费记录失败"})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusCreated, v)
|
||||
}
|
||||
func (h *PolicyHandler) UpdatePayment(c *gin.Context) {
|
||||
id, ok := parseID(c)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
var req model.UpdatePolicyPaymentRequest
|
||||
if c.ShouldBindJSON(&req) != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "请求参数格式错误"})
|
||||
return
|
||||
}
|
||||
v, e := h.svc.UpdatePayment(c.Request.Context(), id, req)
|
||||
if errors.Is(e, service.ErrInvalidPaymentStatus) {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "缴费状态无效"})
|
||||
return
|
||||
}
|
||||
if e != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "更新缴费记录失败"})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, v)
|
||||
}
|
||||
|
||||
func (h *PolicyHandler) Upcoming(c *gin.Context) {
|
||||
days, _ := strconv.Atoi(c.DefaultQuery("days", "30"))
|
||||
items, err := h.svc.Upcoming(c.Request.Context(), days)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "获取续费提醒失败"})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, items)
|
||||
}
|
||||
|
||||
func (h *PolicyHandler) Create(c *gin.Context) {
|
||||
var req model.CreatePolicyRequest
|
||||
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.Status == "" {
|
||||
req.Status = "active"
|
||||
}
|
||||
if req.PaymentFrequency == "" {
|
||||
req.PaymentFrequency = "annual"
|
||||
}
|
||||
if !validPaymentFrequency(req.PaymentFrequency) {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "无效的缴费频率"})
|
||||
return
|
||||
}
|
||||
item, err := h.svc.Create(c.Request.Context(), req)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "添加保单失败"})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusCreated, item)
|
||||
}
|
||||
|
||||
func (h *PolicyHandler) Update(c *gin.Context) {
|
||||
id, ok := parseID(c)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
var req model.UpdatePolicyRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "请求参数格式错误"})
|
||||
return
|
||||
}
|
||||
if req.PaymentFrequency != nil && !validPaymentFrequency(*req.PaymentFrequency) {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "无效的缴费频率"})
|
||||
return
|
||||
}
|
||||
item, err := h.svc.Update(c.Request.Context(), id, req)
|
||||
if err != nil {
|
||||
writeEntityError(c, err, "更新保单失败", "保单不存在")
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, item)
|
||||
}
|
||||
|
||||
func validPaymentFrequency(value string) bool {
|
||||
switch value {
|
||||
case "monthly", "quarterly", "semiannual", "annual", "single":
|
||||
return true
|
||||
default:
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
func (h *PolicyHandler) Delete(c *gin.Context) {
|
||||
id, ok := parseID(c)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
if err := h.svc.Delete(c.Request.Context(), id); err != nil {
|
||||
writeEntityError(c, err, "删除保单失败", "保单不存在")
|
||||
return
|
||||
}
|
||||
c.Status(http.StatusNoContent)
|
||||
}
|
||||
Reference in new issue
Block a user