Commit f3e6d072 authored by hujiebin's avatar hujiebin

feat:纪念日接口done

parent 4d02c6d0
......@@ -23,3 +23,10 @@ type CvCpAchievement struct {
Score uint32 `json:"score"` // 分值
TimeUnix int64 `json:"timeUnix"` // 达成成就时间戳
}
type CvCpAnniversary struct {
Id uint64 `json:"id"` // 记录id
Content string `json:"content"` // 纪念日内容
Date string `json:"date"` // 纪念日日期
IsRemind bool `json:"isRemind"` // 是否提醒
}
package cp_m
import (
"git.hilo.cn/hilo-common/domain"
"git.hilo.cn/hilo-common/resource/mysql"
)
// CpAnniversary cp纪念日
type CpAnniversary struct {
mysql.Entity
CpId mysql.ID
UserId1 mysql.ID
UserId2 mysql.ID
Content string
Date string
Timezone string
IsRemind bool
}
// 添加cp纪念日
func AddCpAnniversary(model *domain.Model, cp CpRelationTmp, content, date, tz string, isRemind bool) error {
return model.DB().Model(CpAnniversary{}).Create(&CpAnniversary{
CpId: cp.ID,
UserId1: cp.UserId1,
UserId2: cp.UserId2,
Content: content,
Date: date,
Timezone: tz,
IsRemind: isRemind,
}).Error
}
// 更新cp纪念日
func UpdateCpAnniversary(model *domain.Model, id mysql.ID, content, date, tz string, isRemind bool) error {
updates := map[string]interface{}{
"content": content,
"date": date,
"timezone": tz,
"is_remind": isRemind,
}
return model.DB().Model(CpAnniversary{}).Where("id = ?", id).Updates(updates).Error
}
func DelCpAnniversary(model *domain.Model, id mysql.ID) error {
return model.DB().Model(CpAnniversary{}).Where("id = ? ", id).Delete(&CpAnniversary{}).Error
}
// 根据用户id获取所有纪念日
func GetAllCpAnniversary(model *domain.Model, userId mysql.ID) []CpAnniversary {
var res []CpAnniversary
relation, exists := GetCpRelation(model, userId)
if !exists {
return res
}
if err := model.DB().Model(CpAnniversary{}).Where("cp_id = ?", relation.ID).Order("id").Find(&res).Error; err != nil {
model.Log.Errorf("GetAllCpAnniversary fail:%v", err)
}
return res
}
......@@ -21,4 +21,6 @@ var (
DiamondAccountFrozen = myerr.NewBusinessCode(4004, "Diamond Account Frozen", myerr.BusinessData{})
ResPropertyDiamondNoUse = myerr.NewBusinessCode(5004, "Property can not buy", myerr.BusinessData{}) //头饰不能买
CpNotRelation = myerr.NewBusinessCode(6000, "cp not relation", myerr.BusinessData{})
)
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
}
......@@ -198,5 +198,5 @@ func CpAchievement(c *gin.Context) (*mycontext.MyContext, error) {
} else {
}
resp.ResponsePageBaseOk(c, response, 0, false)
return nil, nil
return myCtx, nil
}
......@@ -36,6 +36,11 @@ func InitRouter() *gin.Engine {
cp.GET("/rank/:queryType", wrapper(cp_r.CpRank))
cp.GET("/my/:queryType", wrapper(cp_r.CpMy))
cp.GET("/achievement", wrapper(cp_r.CpAchievement))
cp.POST("/anniversary", wrapper(cp_r.PostAnniversary))
cp.PUT("/anniversary/:id", wrapper(cp_r.PutAnniversary))
cp.GET("/anniversary", wrapper(cp_r.PageAnniversary))
cp.DELETE("/anniversary/:id", wrapper(cp_r.DelAnniversary))
}
inner := r.Group("/inner")
inner.Use(ExceptionHandle, LoggerHandle)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment