leaf-library-3/internal/services/document/collection.go
2024-12-06 23:46:45 +08:00

106 lines
3.1 KiB
Go

package collection
import (
"context"
"leafdev.top/Leaf/leaf-library-3/internal/entity"
"leafdev.top/Leaf/leaf-library-3/internal/types/dto"
"leafdev.top/Leaf/leaf-library-3/internal/types/errs"
)
func (s *Service) Get(ctx context.Context, collectionId dto.EntityId) (*entity.Collection, error) {
exists, err := s.Exists(ctx, collectionId)
if err != nil {
return nil, err
}
if !exists {
return nil, errs.ErrCollectionNotExists
}
return s.dao.WithContext(ctx).Collection.Where(s.dao.Collection.Id.Eq(collectionId.Uint())).First()
}
func (s *Service) Exists(ctx context.Context, CollectionId dto.EntityId) (bool, error) {
num, err := s.dao.WithContext(ctx).Collection.Where(s.dao.Collection.Id.Eq(CollectionId.Uint())).Count()
return num > 0, err
}
func (s *Service) List(ctx context.Context, workspaceId dto.EntityId) ([]*entity.Collection, error) {
return s.dao.WithContext(ctx).Collection.Where(s.dao.Collection.WorkspaceId.Eq(workspaceId.Uint())).Find()
}
func (s *Service) Create(ctx context.Context, workspaceId dto.EntityId, name string) (*entity.Collection, error) {
var collection = &entity.Collection{
Name: name,
WorkspaceId: workspaceId,
}
err := s.dao.WithContext(ctx).Collection.Create(collection)
if err != nil {
return nil, err
}
return collection, nil
}
//
//func (s *Service) AddMember(ctx context.Context, userId schema.UserId, CollectionEntity *entity.Collection) (*entity.CollectionMember, error) {
// memberExists, err := s.MemberExists(ctx, userId, CollectionEntity)
// if err != nil {
// return nil, err
// }
//
// if memberExists {
// return nil, errs.ErrMemberAlreadyExists
// }
//
// var CollectionMember = &entity.CollectionMember{
// UserId: userId,
// CollectionId: CollectionEntity.Id,
// }
//
// err = s.dao.WithContext(ctx).CollectionMember.Create(CollectionMember)
//
// return CollectionMember, err
//}
//
//func (s *Service) RemoveMember(ctx context.Context, userId schema.UserId, CollectionEntity *entity.Collection) error {
// memberExists, err := s.MemberExists(ctx, userId, CollectionEntity)
// if err != nil {
// return err
// }
//
// if !memberExists {
// return errs.ErrMemberNotExists
// }
//
// _, err = s.dao.WithContext(ctx).CollectionMember.
// Where(
// s.dao.CollectionMember.UserId.Eq(userId.String()),
// s.dao.CollectionMember.CollectionId.Eq(
// CollectionEntity.Id.Uint()),
// ).
// Delete()
//
// return err
//}
//
//func (s *Service) MemberExists(ctx context.Context, userId schema.UserId, CollectionEntity *entity.Collection) (bool, error) {
// if num, err := s.dao.WithContext(ctx).CollectionMember.Where(s.dao.CollectionMember.UserId.Eq(userId.String()), s.dao.CollectionMember.CollectionId.Eq(CollectionEntity.Id.Uint())).Count(); err != nil {
// return false, err
// } else {
// return num > 0, nil
// }
//}
//
//func (s *Service) DeleteCollection(ctx context.Context, CollectionId dto.EntityId) error {
// // 删除始终是一个复杂的工程,所以先标记为软删除
// _, err := s.dao.WithContext(ctx).Collection.Where(s.dao.Collection.Id.Eq(CollectionId.Uint())).Delete()
//
// return err
//}