书籍列表接口新增参数
This commit is contained in:
@ -7,6 +7,8 @@ import (
|
||||
"server/internal/model/do"
|
||||
"server/internal/service"
|
||||
"server/utility/ecode"
|
||||
|
||||
"github.com/gogf/gf/v2/database/gdb"
|
||||
)
|
||||
|
||||
type sUserFollowAuthor struct{}
|
||||
@ -36,63 +38,82 @@ func (s *sUserFollowAuthor) List(ctx context.Context, in *model.UserFollowAuthor
|
||||
|
||||
// Create adds a new user follow author
|
||||
func (s *sUserFollowAuthor) Create(ctx context.Context, in *model.UserFollowAuthorAddIn) (out *model.UserFollowAuthorCRUDOut, err error) {
|
||||
exist, err := dao.UserFollowAuthors.Ctx(ctx).
|
||||
Where(dao.UserFollowAuthors.Columns().UserId, in.UserId).
|
||||
Where(dao.UserFollowAuthors.Columns().AuthorId, in.AuthorId).
|
||||
Exist()
|
||||
if err != nil {
|
||||
return nil, ecode.Fail.Sub("user_follow_author_query_failed")
|
||||
}
|
||||
if exist {
|
||||
return nil, ecode.Params.Sub("user_follow_author_exists")
|
||||
}
|
||||
if _, err := dao.UserFollowAuthors.Ctx(ctx).Data(do.UserFollowAuthors{
|
||||
UserId: in.UserId,
|
||||
AuthorId: in.AuthorId,
|
||||
}).Insert(); err != nil {
|
||||
return nil, ecode.Fail.Sub("user_follow_author_create_failed")
|
||||
}
|
||||
return &model.UserFollowAuthorCRUDOut{Success: true}, nil
|
||||
}
|
||||
err = dao.UserFollowAuthors.Transaction(ctx, func(ctx context.Context, tx gdb.TX) error {
|
||||
exist, err := dao.UserFollowAuthors.Ctx(ctx).TX(tx).
|
||||
Where(dao.UserFollowAuthors.Columns().UserId, in.UserId).
|
||||
Where(dao.UserFollowAuthors.Columns().AuthorId, in.AuthorId).
|
||||
Exist()
|
||||
if err != nil {
|
||||
return ecode.Fail.Sub("user_follow_author_query_failed")
|
||||
}
|
||||
if exist {
|
||||
return ecode.Params.Sub("user_follow_author_exists")
|
||||
}
|
||||
if _, err := dao.UserFollowAuthors.Ctx(ctx).TX(tx).Data(do.UserFollowAuthors{
|
||||
UserId: in.UserId,
|
||||
AuthorId: in.AuthorId,
|
||||
}).Insert(); err != nil {
|
||||
return ecode.Fail.Sub("user_follow_author_create_failed")
|
||||
}
|
||||
|
||||
// Delete removes a user follow author by id
|
||||
func (s *sUserFollowAuthor) Delete(ctx context.Context, in *model.UserFollowAuthorDelIn) (out *model.UserFollowAuthorCRUDOut, err error) {
|
||||
exist, err := dao.UserFollowAuthors.Ctx(ctx).
|
||||
WherePri(in.Id).
|
||||
Exist()
|
||||
// 作者被关注数 +1
|
||||
if _, err := dao.Authors.Ctx(ctx).TX(tx).
|
||||
Where(dao.Authors.Columns().Id, in.AuthorId).
|
||||
Increment(dao.Authors.Columns().FollowerCount, 1); err != nil {
|
||||
return ecode.Fail.Sub("author_follow_count_update_failed")
|
||||
}
|
||||
// 用户关注数 +1
|
||||
if _, err := dao.Users.Ctx(ctx).TX(tx).
|
||||
Where(dao.Users.Columns().Id, in.UserId).
|
||||
Increment(dao.Users.Columns().AttentionCount, 1); err != nil {
|
||||
return ecode.Fail.Sub("user_attention_count_update_failed")
|
||||
}
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
return nil, ecode.Fail.Sub("user_follow_author_query_failed")
|
||||
}
|
||||
if !exist {
|
||||
return nil, ecode.NotFound.Sub("user_follow_author_not_found")
|
||||
}
|
||||
_, err = dao.UserFollowAuthors.Ctx(ctx).WherePri(in.Id).Delete()
|
||||
if err != nil {
|
||||
return nil, ecode.Fail.Sub("user_follow_author_delete_failed")
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &model.UserFollowAuthorCRUDOut{Success: true}, nil
|
||||
}
|
||||
|
||||
// Unfollow removes a user follow author by userId and authorId
|
||||
func (s *sUserFollowAuthor) Unfollow(ctx context.Context, userId int64, authorId int64) (out *model.UserFollowAuthorCRUDOut, err error) {
|
||||
if userId == 0 || authorId == 0 {
|
||||
return nil, ecode.Params.Sub("user_id_or_author_id_invalid")
|
||||
}
|
||||
// 查找关注记录
|
||||
var record struct{ Id int64 }
|
||||
err = dao.UserFollowAuthors.Ctx(ctx).
|
||||
Where(dao.UserFollowAuthors.Columns().UserId, userId).
|
||||
Where(dao.UserFollowAuthors.Columns().AuthorId, authorId).
|
||||
Fields("id").Scan(&record)
|
||||
err = dao.UserFollowAuthors.Transaction(ctx, func(ctx context.Context, tx gdb.TX) error {
|
||||
if userId == 0 || authorId == 0 {
|
||||
return ecode.Params.Sub("user_id_or_author_id_invalid")
|
||||
}
|
||||
// 查找关注记录
|
||||
result, err := dao.UserFollowAuthors.Ctx(ctx).TX(tx).
|
||||
Where(dao.UserFollowAuthors.Columns().UserId, userId).
|
||||
Where(dao.UserFollowAuthors.Columns().AuthorId, authorId).
|
||||
Delete()
|
||||
if err != nil {
|
||||
return ecode.Fail.Sub("user_follow_author_delete_failed")
|
||||
}
|
||||
affected, err := result.RowsAffected()
|
||||
if err != nil {
|
||||
return ecode.Fail.Sub("user_follow_author_delete_failed")
|
||||
}
|
||||
if affected == 0 {
|
||||
return ecode.NotFound.Sub("user_follow_author_not_found")
|
||||
}
|
||||
// 作者被关注数 -1
|
||||
if _, err := dao.Authors.Ctx(ctx).TX(tx).
|
||||
Where(dao.Authors.Columns().Id, authorId).
|
||||
Decrement(dao.Authors.Columns().FollowerCount, 1); err != nil {
|
||||
return ecode.Fail.Sub("author_follow_count_update_failed")
|
||||
}
|
||||
// 用户关注数 -1
|
||||
if _, err := dao.Users.Ctx(ctx).TX(tx).
|
||||
Where(dao.Users.Columns().Id, userId).
|
||||
Decrement(dao.Users.Columns().AttentionCount, 1); err != nil {
|
||||
return ecode.Fail.Sub("user_attention_count_update_failed")
|
||||
}
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
return nil, ecode.Fail.Sub("user_follow_author_query_failed")
|
||||
}
|
||||
if record.Id == 0 {
|
||||
return nil, ecode.NotFound.Sub("user_follow_author_not_found")
|
||||
}
|
||||
_, err = dao.UserFollowAuthors.Ctx(ctx).WherePri(record.Id).Delete()
|
||||
if err != nil {
|
||||
return nil, ecode.Fail.Sub("user_follow_author_delete_failed")
|
||||
return nil, err
|
||||
}
|
||||
return &model.UserFollowAuthorCRUDOut{Success: true}, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user