parent
7062c0c68b
commit
74b2423754
@ -0,0 +1,116 @@
|
||||
package biz
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
pb "moredoc/api/v1"
|
||||
"moredoc/middleware/auth"
|
||||
"moredoc/model"
|
||||
"moredoc/util"
|
||||
|
||||
"go.uber.org/zap"
|
||||
"google.golang.org/grpc/codes"
|
||||
"google.golang.org/grpc/status"
|
||||
"google.golang.org/protobuf/types/known/emptypb"
|
||||
)
|
||||
|
||||
type FavoriteAPIService struct {
|
||||
pb.UnimplementedFavoriteAPIServer
|
||||
dbModel *model.DBModel
|
||||
logger *zap.Logger
|
||||
}
|
||||
|
||||
func NewFavoriteAPIService(dbModel *model.DBModel, logger *zap.Logger) (service *FavoriteAPIService) {
|
||||
return &FavoriteAPIService{dbModel: dbModel, logger: logger.Named("FavoriteAPIService")}
|
||||
}
|
||||
|
||||
func (s *FavoriteAPIService) checkLogin(ctx context.Context) (*auth.UserClaims, error) {
|
||||
return checkGRPCLogin(s.dbModel, ctx)
|
||||
}
|
||||
|
||||
func (s *FavoriteAPIService) CreateFavorite(ctx context.Context, req *pb.Favorite) (*pb.Favorite, error) {
|
||||
userClaims, err := s.checkLogin(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
favorite := &model.Favorite{
|
||||
UserId: userClaims.UserId,
|
||||
DocumentId: req.DocumentId,
|
||||
}
|
||||
|
||||
exsit, _ := s.dbModel.GetUserFavorite(favorite.UserId, favorite.DocumentId)
|
||||
if exsit.Id > 0 {
|
||||
return nil, status.Errorf(codes.AlreadyExists, "您已经收藏过了")
|
||||
}
|
||||
|
||||
err = s.dbModel.CreateFavorite(favorite)
|
||||
if err != nil {
|
||||
return nil, status.Errorf(codes.Internal, "收藏失败:"+err.Error())
|
||||
}
|
||||
|
||||
pbFavorite := &pb.Favorite{}
|
||||
util.CopyStruct(favorite, pbFavorite)
|
||||
|
||||
return pbFavorite, nil
|
||||
}
|
||||
|
||||
func (s *FavoriteAPIService) DeleteFavorite(ctx context.Context, req *pb.DeleteFavoriteRequest) (*emptypb.Empty, error) {
|
||||
userClaims, err := s.checkLogin(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
err = s.dbModel.DeleteFavorite(userClaims.UserId, req.Id)
|
||||
if err != nil {
|
||||
return nil, status.Errorf(codes.Internal, "删除失败:"+err.Error())
|
||||
}
|
||||
|
||||
return &emptypb.Empty{}, nil
|
||||
}
|
||||
|
||||
func (s *FavoriteAPIService) GetFavorite(ctx context.Context, req *pb.GetFavoriteRequest) (*pb.Favorite, error) {
|
||||
userClaims, err := s.checkLogin(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
favorite, _ := s.dbModel.GetUserFavorite(userClaims.UserId, req.DocumentId)
|
||||
|
||||
pbFavorite := &pb.Favorite{}
|
||||
if favorite.Id > 0 {
|
||||
util.CopyStruct(&favorite, pbFavorite)
|
||||
}
|
||||
|
||||
return pbFavorite, nil
|
||||
}
|
||||
|
||||
// 获取用户自身的收藏列表
|
||||
func (s *FavoriteAPIService) ListFavorite(ctx context.Context, req *pb.ListFavoriteRequest) (*pb.ListFavoriteReply, error) {
|
||||
articleStatus := []int{model.DocumentStatusConverted, model.DocumentStatusConverting, model.DocumentStatusPending, model.DocumentStatusFailed}
|
||||
userId := req.UserId
|
||||
userClaims, err := s.checkLogin(ctx)
|
||||
if err == nil {
|
||||
if req.UserId == userClaims.UserId {
|
||||
articleStatus = []int{} // 如果是获取自己的收藏列表,可以获取所有状态的文章
|
||||
}
|
||||
userId = userClaims.UserId
|
||||
}
|
||||
|
||||
favorites, total, err := s.dbModel.GetFavoriteList(&model.OptionGetFavoriteList{
|
||||
Page: int(req.Page),
|
||||
Size: int(req.Size_),
|
||||
WithCount: true,
|
||||
QueryIn: map[string][]interface{}{
|
||||
"user_id": {userId},
|
||||
},
|
||||
}, articleStatus...)
|
||||
if err != nil {
|
||||
return nil, status.Errorf(codes.Internal, "获取失败:"+err.Error())
|
||||
}
|
||||
resp := &pb.ListFavoriteReply{
|
||||
Total: total,
|
||||
Favorite: favorites,
|
||||
}
|
||||
return resp, nil
|
||||
}
|
@ -0,0 +1,167 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
pb "moredoc/api/v1"
|
||||
"time"
|
||||
|
||||
"go.uber.org/zap"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type Favorite struct {
|
||||
Id int64 `form:"id" json:"id,omitempty" gorm:"primaryKey;autoIncrement;column:id;comment:自增主键;"`
|
||||
UserId int64 `form:"user_id" json:"user_id,omitempty" gorm:"column:user_id;type:bigint(20);size:20;default:0;comment:用户id;index:idx_user_id;index:idx_user_document,unique"`
|
||||
DocumentId int64 `form:"document_id" json:"document_id,omitempty" gorm:"column:document_id;type:bigint(20);size:20;default:0;comment:;index:idx_user_document,unique"`
|
||||
CreatedAt time.Time `form:"created_at" json:"created_at,omitempty" gorm:"column:created_at;type:datetime;comment:;"`
|
||||
UpdatedAt time.Time `form:"updated_at" json:"updated_at,omitempty" gorm:"column:updated_at;type:datetime;comment:;"`
|
||||
}
|
||||
|
||||
func (Favorite) TableName() string {
|
||||
return tablePrefix + "favorite"
|
||||
}
|
||||
|
||||
// CreateFavorite 创建Favorite
|
||||
func (m *DBModel) CreateFavorite(favorite *Favorite) (err error) {
|
||||
tx := m.db.Begin()
|
||||
defer func() {
|
||||
if err != nil {
|
||||
tx.Rollback()
|
||||
} else {
|
||||
tx.Commit()
|
||||
}
|
||||
}()
|
||||
|
||||
// 添加收藏
|
||||
err = tx.Create(favorite).Error
|
||||
if err != nil {
|
||||
m.logger.Error("CreateFavorite", zap.Error(err))
|
||||
return
|
||||
}
|
||||
|
||||
// 文档收藏数量增加
|
||||
err = tx.Model(&Document{}).Where("id = ?", favorite.DocumentId).Update("favorite_count", gorm.Expr("favorite_count + ?", 1)).Error
|
||||
if err != nil {
|
||||
m.logger.Error("CreateFavorite", zap.Error(err))
|
||||
return
|
||||
}
|
||||
|
||||
// 用户收藏数量增加
|
||||
err = tx.Model(&User{}).Where("id = ?", favorite.UserId).Update("favorite_count", gorm.Expr("favorite_count + ?", 1)).Error
|
||||
if err != nil {
|
||||
m.logger.Error("CreateFavorite", zap.Error(err))
|
||||
return
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func (m *DBModel) GetUserFavorite(userId int64, DocumentId int64) (favorite Favorite, err error) {
|
||||
err = m.db.Where("user_id = ? and document_id = ?", userId, DocumentId).First(&favorite).Error
|
||||
return
|
||||
}
|
||||
|
||||
// GetFavorite 根据id获取Favorite
|
||||
func (m *DBModel) GetFavorite(id int64, fields ...string) (favorite Favorite, err error) {
|
||||
db := m.db
|
||||
|
||||
fields = m.FilterValidFields(Favorite{}.TableName(), fields...)
|
||||
if len(fields) > 0 {
|
||||
db = db.Select(fields)
|
||||
}
|
||||
|
||||
err = db.Where("id = ?", id).First(&favorite).Error
|
||||
return
|
||||
}
|
||||
|
||||
type OptionGetFavoriteList struct {
|
||||
Page int
|
||||
Size int
|
||||
WithCount bool
|
||||
SelectFields []string // 查询字段
|
||||
QueryIn map[string][]interface{} // map[field][]{value1,value2,...}
|
||||
}
|
||||
|
||||
// GetFavoriteList 获取Favorite列表
|
||||
func (m *DBModel) GetFavoriteList(opt *OptionGetFavoriteList, documentStatus ...int) (favoriteList []*pb.Favorite, total int64, err error) {
|
||||
tableFavorite := Favorite{}.TableName() + " f"
|
||||
db := m.db.
|
||||
Table(tableFavorite).
|
||||
Joins(
|
||||
fmt.Sprintf("left join %s a on f.document_id = a.id", Document{}.TableName()),
|
||||
)
|
||||
db = m.generateQueryIn(db, tableFavorite, opt.QueryIn)
|
||||
|
||||
if len(documentStatus) > 0 {
|
||||
db = db.Where("a.status in (?)", documentStatus)
|
||||
}
|
||||
|
||||
if opt.WithCount {
|
||||
err = db.Count(&total).Error
|
||||
if err != nil {
|
||||
m.logger.Error("GetFavoriteList", zap.Error(err))
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
opt.SelectFields = m.FilterValidFields(tableFavorite, opt.SelectFields...)
|
||||
if len(opt.SelectFields) > 0 {
|
||||
db = db.Select(opt.SelectFields)
|
||||
}
|
||||
|
||||
db = db.Order("id desc").Offset((opt.Page - 1) * opt.Size).Limit(opt.Size)
|
||||
err = db.Select("f.*, a.title").Find(&favoriteList).Error
|
||||
if err != nil && err != gorm.ErrRecordNotFound {
|
||||
m.logger.Error("GetFavoriteList", zap.Error(err))
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// DeleteFavorite 取消收藏
|
||||
func (m *DBModel) DeleteFavorite(userId int64, ids []int64) (err error) {
|
||||
if len(ids) == 0 || userId == 0 {
|
||||
return
|
||||
}
|
||||
|
||||
var favorites []Favorite
|
||||
m.db.Where("id in (?) and user_id = ?", ids, userId).Select("id", "user_id", "document_id").Find(&favorites)
|
||||
if len(favorites) == 0 {
|
||||
return
|
||||
}
|
||||
|
||||
tx := m.db.Begin()
|
||||
defer func() {
|
||||
if err != nil {
|
||||
tx.Rollback()
|
||||
} else {
|
||||
tx.Commit()
|
||||
}
|
||||
}()
|
||||
|
||||
// 删除收藏
|
||||
document := &Document{}
|
||||
user := &User{}
|
||||
for _, favorite := range favorites {
|
||||
err = tx.Delete(&favorite).Error
|
||||
if err != nil {
|
||||
m.logger.Error("DeleteFavorite", zap.Error(err))
|
||||
return
|
||||
}
|
||||
|
||||
// 文档收藏数量减少
|
||||
err = tx.Model(document).Where("id = ?", favorite.DocumentId).Update("favorite_count", gorm.Expr("favorite_count - ?", 1)).Error
|
||||
if err != nil {
|
||||
m.logger.Error("DeleteFavorite", zap.Error(err))
|
||||
return
|
||||
}
|
||||
|
||||
// 用户收藏数量减少
|
||||
err = tx.Model(user).Where("id = ?", favorite.UserId).Update("favorite_count", gorm.Expr("favorite_count - ?", 1)).Error
|
||||
if err != nil {
|
||||
m.logger.Error("DeleteFavorite", zap.Error(err))
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
return
|
||||
}
|
@ -0,0 +1,35 @@
|
||||
import service from '~/utils/request'
|
||||
|
||||
export const createFavorite = (data) => {
|
||||
return service({
|
||||
url: '/api/v1/favorite',
|
||||
method: 'post',
|
||||
data,
|
||||
})
|
||||
}
|
||||
|
||||
export const deleteFavorite = (params) => {
|
||||
return service({
|
||||
url: '/api/v1/favorite',
|
||||
method: 'delete',
|
||||
params,
|
||||
})
|
||||
}
|
||||
|
||||
export const getFavorite = (params) => {
|
||||
return service({
|
||||
url: '/api/v1/favorite',
|
||||
method: 'get',
|
||||
params,
|
||||
})
|
||||
}
|
||||
|
||||
export const listFavorite = (params) => {
|
||||
return service({
|
||||
url: '/api/v1/favorite/list',
|
||||
method: 'get',
|
||||
params,
|
||||
})
|
||||
}
|
||||
|
||||
|
Loading…
Reference in new issue