answer/internal/service/collection_service.go

148 lines
4.1 KiB
Go
Raw Normal View History

2022-09-27 17:59:05 +08:00
package service
import (
"context"
"fmt"
"github.com/answerdev/answer/internal/entity"
"github.com/answerdev/answer/internal/schema"
collectioncommon "github.com/answerdev/answer/internal/service/collection_common"
questioncommon "github.com/answerdev/answer/internal/service/question_common"
2022-09-27 17:59:05 +08:00
"github.com/segmentfault/pacman/errors"
"github.com/segmentfault/pacman/log"
)
// CollectionService user service
type CollectionService struct {
collectionRepo collectioncommon.CollectionRepo
collectionGroupRepo CollectionGroupRepo
questionCommon *questioncommon.QuestionCommon
}
func NewCollectionService(
collectionRepo collectioncommon.CollectionRepo,
collectionGroupRepo CollectionGroupRepo,
questionCommon *questioncommon.QuestionCommon,
) *CollectionService {
return &CollectionService{
collectionRepo: collectionRepo,
collectionGroupRepo: collectionGroupRepo,
questionCommon: questionCommon,
}
}
func (cs *CollectionService) CollectionSwitch(ctx context.Context, dto *schema.CollectionSwitchDTO) (resp *schema.CollectionSwitchResp, err error) {
resp = &schema.CollectionSwitchResp{}
dbData, has, err := cs.collectionRepo.GetOneByObjectIDAndUser(ctx, dto.UserID, dto.ObjectID)
if err != nil {
return
}
if has {
err = cs.collectionRepo.RemoveCollection(ctx, dbData.ID)
if err != nil {
return nil, err
}
err = cs.questionCommon.UpdateCollectionCount(ctx, dto.ObjectID, -1)
if err != nil {
log.Error("UpdateCollectionCount", err.Error())
}
count, err := cs.objectCollectionCount(ctx, dto.ObjectID)
if err != nil {
return resp, err
}
resp.ObjectCollectionCount = fmt.Sprintf("%v", count)
resp.Switch = false
return resp, err
}
if dto.GroupID == "" || dto.GroupID == "0" {
defaultGroup, has, err := cs.collectionGroupRepo.GetDefaultID(ctx, dto.UserID)
if err != nil {
return nil, err
}
if !has {
2022-09-30 16:18:04 +08:00
dbdefaultGroup, err := cs.collectionGroupRepo.AddCollectionDefaultGroup(ctx, dto.UserID)
2022-09-27 17:59:05 +08:00
if err != nil {
return nil, err
}
2022-09-30 16:18:04 +08:00
dto.GroupID = dbdefaultGroup.ID
2022-09-27 17:59:05 +08:00
} else {
dto.GroupID = defaultGroup.ID
}
}
collection := &entity.Collection{
UserCollectionGroupID: dto.GroupID,
UserID: dto.UserID,
ObjectID: dto.ObjectID,
}
err = cs.collectionRepo.AddCollection(ctx, collection)
if err != nil {
return
}
err = cs.questionCommon.UpdateCollectionCount(ctx, dto.ObjectID, 1)
if err != nil {
log.Error("UpdateCollectionCount", err.Error())
}
count, err := cs.objectCollectionCount(ctx, dto.ObjectID)
if err != nil {
return
}
resp.ObjectCollectionCount = fmt.Sprintf("%d", count)
resp.Switch = true
return
}
func (cs *CollectionService) objectCollectionCount(ctx context.Context, objectId string) (int64, error) {
count, err := cs.collectionRepo.CountByObjectID(ctx, objectId)
return count, err
}
func (cs *CollectionService) add(ctx context.Context, collection *entity.Collection) error {
_, has, err := cs.collectionRepo.GetOneByObjectIDAndUser(ctx, collection.UserID, collection.ObjectID)
if err != nil {
return err
}
if has {
return errors.BadRequest("already collected")
}
if collection.UserCollectionGroupID == "" || collection.UserCollectionGroupID == "0" {
defaultGroup, has, err := cs.collectionGroupRepo.GetDefaultID(ctx, collection.UserID)
if err != nil {
return err
}
if !has {
defaultGroup, err := cs.collectionGroupRepo.AddCollectionDefaultGroup(ctx, collection.UserID)
if err != nil {
return err
}
collection.UserCollectionGroupID = defaultGroup.ID
} else {
collection.UserCollectionGroupID = defaultGroup.ID
}
}
err = cs.collectionRepo.AddCollection(ctx, collection)
if err != nil {
return err
}
return nil
}
// Cancel
func (cs *CollectionService) cancel(ctx context.Context, collection *entity.Collection) error {
dbData, has, err := cs.collectionRepo.GetOneByObjectIDAndUser(ctx, collection.UserID, collection.ObjectID)
if err != nil {
return err
}
if !has {
return errors.BadRequest("collected record does not exist")
}
err = cs.collectionRepo.RemoveCollection(ctx, dbData.ID)
if err != nil {
return err
}
return nil
}