101 lines
2.6 KiB
Go
101 lines
2.6 KiB
Go
// Code generated by hertz generator.
|
|
|
|
package user
|
|
|
|
import (
|
|
"acquaintances/biz/dal/mysql"
|
|
"acquaintances/biz/model"
|
|
"context"
|
|
"github.com/cloudwego/hertz/pkg/common/hlog"
|
|
|
|
user "acquaintances/biz/model/user"
|
|
"github.com/cloudwego/hertz/pkg/app"
|
|
"github.com/cloudwego/hertz/pkg/protocol/consts"
|
|
)
|
|
|
|
// CreateFriendRelationship .
|
|
// @router /v1/user/friendRelationship/ [POST]
|
|
func CreateFriendRelationship(ctx context.Context, c *app.RequestContext) {
|
|
var err error
|
|
var req user.CreateFriendRelationshipReq
|
|
err = c.BindAndValidate(&req)
|
|
if err != nil {
|
|
c.JSON(consts.StatusBadRequest, err.Error())
|
|
return
|
|
}
|
|
err = mysql.CreateFriendApplication(req.ApplicantID, req.TargetUserID, req.ApplicationMessage)
|
|
if err != nil {
|
|
c.JSON(consts.StatusBadRequest, err.Error())
|
|
return
|
|
}
|
|
resp := new(user.CreateFriendRelationshipRes)
|
|
|
|
c.JSON(consts.StatusOK, resp)
|
|
}
|
|
|
|
// UpdateFriendRelationship .
|
|
// @router /v1/user/friendRelationship/ [PUT]
|
|
func UpdateFriendRelationship(ctx context.Context, c *app.RequestContext) {
|
|
var err error
|
|
var req user.UpdateFriendRelationshipReq
|
|
err = c.BindAndValidate(&req)
|
|
if err != nil {
|
|
c.JSON(consts.StatusBadRequest, err.Error())
|
|
return
|
|
}
|
|
|
|
if req.Status == 1 {
|
|
//接受
|
|
err = mysql.HandleFriendApplication(req.ID, model.ApplicationAccepted)
|
|
if err != nil {
|
|
c.JSON(consts.StatusBadRequest, err.Error())
|
|
return
|
|
}
|
|
//添加好友
|
|
var reqC user.CreateUserRelationsReq
|
|
reqC.UserID = req.ApplicantID
|
|
reqC.FriendID = req.TargetUserID
|
|
err := mysql.CreateUserRelations(reqC)
|
|
if err != nil {
|
|
hlog.Errorf("CreateUserRelations err=%s", err.Error())
|
|
c.JSON(consts.StatusBadRequest, err.Error())
|
|
return
|
|
}
|
|
} else if req.Status == 2 {
|
|
//拒绝
|
|
err = mysql.HandleFriendApplication(req.ID, model.ApplicationRejected)
|
|
if err != nil {
|
|
c.JSON(consts.StatusBadRequest, err.Error())
|
|
return
|
|
}
|
|
} else {
|
|
c.JSON(consts.StatusBadRequest, "invalid status")
|
|
return
|
|
}
|
|
|
|
resp := new(user.UpdateFriendRelationshipRes)
|
|
|
|
c.JSON(consts.StatusOK, resp)
|
|
}
|
|
|
|
// ListFriendRelationship .
|
|
// @router /v1/user/friendRelationship/ [GET]
|
|
func ListFriendRelationship(ctx context.Context, c *app.RequestContext) {
|
|
var err error
|
|
var req user.ListFriendRelationshipReq
|
|
err = c.BindAndValidate(&req)
|
|
if err != nil {
|
|
c.JSON(consts.StatusBadRequest, err.Error())
|
|
return
|
|
}
|
|
data, err := mysql.GetApplications(req, model.ApplicationPending, model.ApplicationAccepted, model.ApplicationRejected)
|
|
if err != nil {
|
|
c.JSON(consts.StatusBadRequest, err.Error())
|
|
return
|
|
}
|
|
resp := new(user.ListFriendRelationshipRes)
|
|
resp.FriendRelationshipInfos = data
|
|
resp.Total = int64(len(data))
|
|
c.JSON(consts.StatusOK, resp)
|
|
}
|