anniversary.go 3.78 KB
Newer Older
hujiebin's avatar
hujiebin committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
package cp_r

import (
	"git.hilo.cn/hilo-common/domain"
	"git.hilo.cn/hilo-common/mycontext"
	"github.com/gin-gonic/gin"
	"hilo-user/cv/cp_cv"
	"hilo-user/domain/model/cp_m"
	"hilo-user/myerr/bizerr"
	"hilo-user/req"
	"hilo-user/resp"
	"strconv"
)

type PostPutAnniversaryReq struct {
	Content  string `form:"content" binding:"required"`
	Date     string `form:"date" binding:"required"`
	IsRemind bool   `form:"isRemind"`
}

// @Tags CP v2
// @Summary 发布纪念日
// @Param token header string true "token"
// @Param nonce header string true "随机数字"
// @Param timezone header string true "时区"
// @Param content formData string true "纪念日名称"
// @Param date formData string true "纪念日时间(年月日)"
// @Param isRemind formData bool false "是否提醒"
// @Success 200
// @Router /v2/cp/anniversary [post]
func PostAnniversary(c *gin.Context) (*mycontext.MyContext, error) {
	myCtx := mycontext.CreateMyContext(c.Keys)
	userId, err := req.GetUserId(c)
	if err != nil {
		return myCtx, err
	}
	var param PostPutAnniversaryReq
	if err := c.ShouldBind(&param); err != nil {
		return myCtx, err
	}
	model := domain.CreateModelContext(myCtx)
	relation, exits := cp_m.GetCpRelation(model, userId)
	if !exits {
		return myCtx, bizerr.CpNotRelation
	} else {
		tz := c.GetHeader(mycontext.TIMEZONE)
		if err := cp_m.AddCpAnniversary(model, relation, param.Content, param.Date, tz, param.IsRemind); err != nil {
			return myCtx, err
		}
	}
	resp.ResponseOk(c, "")
	return myCtx, nil
}

// @Tags CP v2
// @Summary 修改纪念日
// @Param token header string true "token"
// @Param nonce header string true "随机数字"
// @Param timezone header string true "时区"
// @Param content formData string true "纪念日名称"
// @Param date formData string true "纪念日时间(年月日)"
// @Param isRemind formData bool false "是否提醒"
// @Param id path int true "更新的记录id"
// @Success 200
// @Router /v2/cp/anniversary/{id} [put]
func PutAnniversary(c *gin.Context) (*mycontext.MyContext, error) {
	myCtx := mycontext.CreateMyContext(c.Keys)
	var param PostPutAnniversaryReq
	if err := c.ShouldBind(&param); err != nil {
		return myCtx, err
	}
	id, _ := strconv.ParseUint(c.Param("id"), 10, 64)
	model := domain.CreateModelContext(myCtx)
	tz := c.GetHeader(mycontext.TIMEZONE)
	if err := cp_m.UpdateCpAnniversary(model, id, param.Content, param.Date, tz, param.IsRemind); err != nil {
		return myCtx, err
	}
	resp.ResponseOk(c, "")
	return myCtx, nil
}

// @Tags CP v2
// @Summary 获取纪念日
// @Param token header string true "token"
// @Param nonce header string true "随机数字"
// @Success 200 {object} []cp_cv.CvCpAnniversary
// @Router /v2/cp/anniversary [get]
func PageAnniversary(c *gin.Context) (*mycontext.MyContext, error) {
	myCtx := mycontext.CreateMyContext(c.Keys)
	userId, err := req.GetUserId(c)
	if err != nil {
		return myCtx, err
	}
	model := domain.CreateModelContext(myCtx)
	var response = make([]cp_cv.CvCpAnniversary, 0)
	anniversary := cp_m.GetAllCpAnniversary(model, userId)
	for _, v := range anniversary {
		response = append(response, cp_cv.CvCpAnniversary{
			Id:       v.ID,
			Content:  v.Content,
			Date:     v.Date,
			IsRemind: v.IsRemind,
		})
	}
	resp.ResponsePageBaseOk(c, response, 0, false)
	return myCtx, nil
}

// @Tags CP v2
// @Summary 获取纪念日
// @Param token header string true "token"
// @Param nonce header string true "随机数字"
// @Param id path int true "记录id"
// @Success 200
// @Router /v2/cp/anniversary/:id [delete]
func DelAnniversary(c *gin.Context) (*mycontext.MyContext, error) {
	myCtx := mycontext.CreateMyContext(c.Keys)
	id, _ := strconv.ParseUint(c.Param("id"), 10, 64)
	model := domain.CreateModelContext(myCtx)
	if err := cp_m.DelCpAnniversary(model, id); err != nil {
		return myCtx, err
	}
	resp.ResponseOk(c, "")
	return myCtx, nil
}