answer/internal/service/revision_common/revision_service.go

78 lines
2.5 KiB
Go
Raw Normal View History

2022-09-27 17:59:05 +08:00
package revision_common
import (
"context"
2022-11-23 11:07:28 +08:00
"github.com/answerdev/answer/internal/base/reason"
"github.com/answerdev/answer/internal/service/revision"
usercommon "github.com/answerdev/answer/internal/service/user_common"
2023-03-03 15:47:56 +08:00
"github.com/answerdev/answer/pkg/uid"
2022-11-23 11:07:28 +08:00
"github.com/segmentfault/pacman/errors"
"github.com/segmentfault/pacman/log"
2022-09-27 17:59:05 +08:00
"github.com/answerdev/answer/internal/entity"
"github.com/answerdev/answer/internal/schema"
2022-09-27 17:59:05 +08:00
"github.com/jinzhu/copier"
)
// RevisionService user service
type RevisionService struct {
revisionRepo revision.RevisionRepo
userRepo usercommon.UserRepo
}
2023-03-03 15:47:56 +08:00
func NewRevisionService(revisionRepo revision.RevisionRepo,
userRepo usercommon.UserRepo,
) *RevisionService {
2022-09-27 17:59:05 +08:00
return &RevisionService{
revisionRepo: revisionRepo,
userRepo: userRepo,
}
}
2022-11-25 17:15:02 +08:00
func (rs *RevisionService) GetUnreviewedRevisionCount(ctx context.Context, req *schema.RevisionSearch) (count int64, err error) {
if len(req.GetCanReviewObjectTypes()) == 0 {
return 0, nil
}
_, count, err = rs.revisionRepo.GetUnreviewedRevisionPage(ctx, req.Page, 1, req.GetCanReviewObjectTypes())
2022-11-25 17:15:02 +08:00
return count, err
}
2022-09-27 17:59:05 +08:00
// AddRevision add revision
//
// autoUpdateRevisionID bool : if autoUpdateRevisionID is true , the object.revision_id will be updated,
// if not need auto update object.revision_id, it must be false.
// example: user can edit the object, but need audit, the revision_id will be updated when admin approved
2022-11-22 19:48:27 +08:00
func (rs *RevisionService) AddRevision(ctx context.Context, req *schema.AddRevisionDTO, autoUpdateRevisionID bool) (
revisionID string, err error) {
2023-03-03 15:47:56 +08:00
req.ObjectID = uid.DeShortID(req.ObjectID)
2022-09-27 17:59:05 +08:00
rev := &entity.Revision{}
_ = copier.Copy(rev, req)
err = rs.revisionRepo.AddRevision(ctx, rev, autoUpdateRevisionID)
if err != nil {
2022-11-22 19:48:27 +08:00
return "", err
2022-09-27 17:59:05 +08:00
}
2022-11-22 19:48:27 +08:00
return rev.ID, nil
2022-09-27 17:59:05 +08:00
}
2022-11-23 11:07:28 +08:00
// GetRevision get revision
func (rs *RevisionService) GetRevision(ctx context.Context, revisionID string) (
revision *entity.Revision, err error) {
revisionInfo, exist, err := rs.revisionRepo.GetRevisionByID(ctx, revisionID)
if err != nil {
log.Error(err)
2022-11-23 11:07:28 +08:00
return nil, err
}
if !exist {
return nil, errors.BadRequest(reason.ObjectNotFound)
}
return revisionInfo, nil
}
2022-11-23 15:19:17 +08:00
// ExistUnreviewedByObjectID
func (rs *RevisionService) ExistUnreviewedByObjectID(ctx context.Context, objectID string) (revision *entity.Revision, exist bool, err error) {
2023-03-03 15:47:56 +08:00
objectID = uid.DeShortID(objectID)
2022-11-23 15:19:17 +08:00
revision, exist, err = rs.revisionRepo.ExistUnreviewedByObjectID(ctx, objectID)
return revision, exist, err
}