This commit is contained in:
ivamp 2024-12-06 23:46:45 +08:00
parent df6592a81e
commit d9ccbf13f7
8 changed files with 391 additions and 0 deletions

View File

@ -0,0 +1,105 @@
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
//}

View File

@ -0,0 +1,18 @@
package collection
import (
"leafdev.top/Leaf/leaf-library-3/internal/base/conf"
"leafdev.top/Leaf/leaf-library-3/internal/dao"
)
type Service struct {
config *conf.Config
dao *dao.Query
}
func NewService(config *conf.Config, dao *dao.Query) *Service {
return &Service{
config,
dao,
}
}

View File

@ -0,0 +1,105 @@
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
//}

View File

@ -0,0 +1,18 @@
package collection
import (
"leafdev.top/Leaf/leaf-library-3/internal/base/conf"
"leafdev.top/Leaf/leaf-library-3/internal/dao"
)
type Service struct {
config *conf.Config
dao *dao.Query
}
func NewService(config *conf.Config, dao *dao.Query) *Service {
return &Service{
config,
dao,
}
}

View File

@ -0,0 +1,18 @@
package workspace
import (
"leafdev.top/Leaf/leaf-library-3/internal/base/conf"
"leafdev.top/Leaf/leaf-library-3/internal/dao"
)
type Service struct {
config *conf.Config
dao *dao.Query
}
func NewService(config *conf.Config, dao *dao.Query) *Service {
return &Service{
config,
dao,
}
}

View File

@ -0,0 +1,111 @@
package workspace
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"
"leafdev.top/Leaf/leaf-library-3/internal/types/user"
)
func (s *Service) Get(ctx context.Context, workspaceId dto.EntityId) (*entity.Workspace, error) {
exists, err := s.Exists(ctx, workspaceId)
if err != nil {
return nil, err
}
if !exists {
return nil, errs.ErrWorkspaceNotExists
}
return s.dao.WithContext(ctx).Workspace.Where(s.dao.Workspace.Id.Eq(workspaceId.Uint())).First()
}
func (s *Service) Exists(ctx context.Context, workspaceId dto.EntityId) (bool, error) {
num, err := s.dao.WithContext(ctx).Workspace.Where(s.dao.Workspace.Id.Eq(workspaceId.Uint())).Count()
return num > 0, err
}
func (s *Service) List(ctx context.Context, userId user.Id) ([]*entity.Workspace, error) {
return s.dao.WithContext(ctx).Workspace.Where(s.dao.Workspace.UserId.Eq(userId.String())).Find()
}
func (s *Service) Create(ctx context.Context, userId user.Id, name string) (*entity.Workspace, error) {
var workspace = &entity.Workspace{
Name: name,
UserId: userId,
}
err := s.dao.WithContext(ctx).Workspace.Create(workspace)
if err != nil {
return nil, err
}
_, err = s.AddMember(ctx, userId, workspace)
if err != nil {
return nil, err
}
return workspace, nil
}
func (s *Service) AddMember(ctx context.Context, userId user.Id, workspaceEntity *entity.Workspace) (*entity.WorkspaceMember, error) {
memberExists, err := s.MemberExists(ctx, userId, workspaceEntity)
if err != nil {
return nil, err
}
if memberExists {
return nil, errs.ErrMemberAlreadyExists
}
var workspaceMember = &entity.WorkspaceMember{
UserId: userId,
WorkspaceId: workspaceEntity.Id,
}
err = s.dao.WithContext(ctx).WorkspaceMember.Create(workspaceMember)
return workspaceMember, err
}
func (s *Service) RemoveMember(ctx context.Context, userId user.Id, workspaceEntity *entity.Workspace) error {
memberExists, err := s.MemberExists(ctx, userId, workspaceEntity)
if err != nil {
return err
}
if !memberExists {
return errs.ErrMemberNotExists
}
_, err = s.dao.WithContext(ctx).WorkspaceMember.
Where(
s.dao.WorkspaceMember.UserId.Eq(userId.String()),
s.dao.WorkspaceMember.WorkspaceId.Eq(
workspaceEntity.Id.Uint()),
).
Delete()
return err
}
func (s *Service) MemberExists(ctx context.Context, userId user.Id, workspaceEntity *entity.Workspace) (bool, error) {
if num, err := s.dao.WithContext(ctx).WorkspaceMember.Where(s.dao.WorkspaceMember.UserId.Eq(userId.String()), s.dao.WorkspaceMember.WorkspaceId.Eq(workspaceEntity.Id.Uint())).Count(); err != nil {
return false, err
} else {
return num > 0, nil
}
}
func (s *Service) DeleteWorkspace(ctx context.Context, workspaceId dto.EntityId) error {
// 删除始终是一个复杂的工程,所以先标记为软删除
_, err := s.dao.WithContext(ctx).Workspace.Where(s.dao.Workspace.Id.Eq(workspaceId.Uint())).Delete()
return err
}

View File

@ -0,0 +1,7 @@
package errs
import "errors"
var (
ErrCollectionNotExists = errors.New("collection not exists")
)

View File

@ -0,0 +1,9 @@
package errs
import "errors"
var (
ErrWorkspaceNotExists = errors.New("workspace not exists")
ErrMemberAlreadyExists = errors.New("member already exists")
ErrMemberNotExists = errors.New("member not exists")
)