version 1
Build and Deploy / test-backend (push) Successful in 1m11s
Build and Deploy / lint-frontend (push) Successful in 21s
Build and Deploy / build-backend (push) Successful in 58s
Build and Deploy / deploy (push) Canceled after 0s
Build and Deploy / build-frontend (push) Canceled after 5m38s
Build and Deploy / test-backend (push) Successful in 1m11s
Build and Deploy / lint-frontend (push) Successful in 21s
Build and Deploy / build-backend (push) Successful in 58s
Build and Deploy / deploy (push) Canceled after 0s
Build and Deploy / build-frontend (push) Canceled after 5m38s
This commit is contained in:
@@ -0,0 +1,131 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"context"
|
||||
"slices"
|
||||
|
||||
"github.com/dusiburg/fancy-api/internal/model"
|
||||
"github.com/dusiburg/fancy-api/internal/repository"
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
type CollectionService struct {
|
||||
repository *repository.CollectionRepository
|
||||
}
|
||||
|
||||
func NewCollectionService(repository *repository.CollectionRepository) *CollectionService {
|
||||
return &CollectionService{repository: repository}
|
||||
}
|
||||
|
||||
func (s *CollectionService) getOwned(ctx context.Context, userID, collectionID uuid.UUID) (*model.Collection, error) {
|
||||
existing, err := s.repository.GetCollectionByID(ctx, collectionID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if existing.UserID != userID {
|
||||
return nil, ErrCollectionNotOwned
|
||||
}
|
||||
|
||||
return existing, nil
|
||||
}
|
||||
|
||||
func (s *CollectionService) getOwnedEditable(ctx context.Context, userID, collectionID uuid.UUID) (*model.Collection, error) {
|
||||
existing, err := s.getOwned(ctx, userID, collectionID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if existing.Type == model.CollectionTypeAuto {
|
||||
return nil, ErrCollectionNotOwned
|
||||
}
|
||||
|
||||
return existing, nil
|
||||
}
|
||||
|
||||
func (s *CollectionService) GetCollections(ctx context.Context, userID uuid.UUID) ([]model.Collection, error) {
|
||||
return s.repository.GetCollections(ctx, userID)
|
||||
}
|
||||
|
||||
func (s *CollectionService) GetCollectionByID(ctx context.Context, userID, collectionID uuid.UUID) (*model.Collection, error) {
|
||||
return s.getOwned(ctx, userID, collectionID)
|
||||
}
|
||||
|
||||
func (s *CollectionService) GetCollectionsByWord(ctx context.Context, userID uuid.UUID, lang model.Language, word string) ([]model.Collection, error) {
|
||||
return s.repository.GetCollectionsByWord(ctx, userID, lang, word)
|
||||
}
|
||||
|
||||
func (s *CollectionService) CreateCollection(ctx context.Context, userID uuid.UUID, name string, lang model.Language) (*model.Collection, error) {
|
||||
c := &model.Collection{
|
||||
Name: name,
|
||||
Lang: lang,
|
||||
UserID: userID,
|
||||
Type: model.CollectionTypeUser,
|
||||
Words: []string{},
|
||||
}
|
||||
return s.repository.CreateCollection(ctx, c)
|
||||
}
|
||||
|
||||
func (s *CollectionService) UpdateCollectionDetails(ctx context.Context, userID, collectionID uuid.UUID, name string) (*model.Collection, error) {
|
||||
existing, err := s.getOwnedEditable(ctx, userID, collectionID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
existing.Name = name
|
||||
|
||||
return s.repository.UpdateCollection(ctx, existing)
|
||||
}
|
||||
|
||||
func (s *CollectionService) AddWords(ctx context.Context, userID, collectionID uuid.UUID, words []string) (*model.Collection, error) {
|
||||
existing, err := s.getOwnedEditable(ctx, userID, collectionID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
for _, word := range words {
|
||||
if !slices.Contains(existing.Words, word) {
|
||||
existing.Words = append(existing.Words, word)
|
||||
}
|
||||
}
|
||||
|
||||
return s.repository.UpdateCollection(ctx, existing)
|
||||
}
|
||||
|
||||
func (s *CollectionService) DeleteWord(ctx context.Context, userID, id uuid.UUID, word string) (*model.Collection, error) {
|
||||
existing, err := s.getOwnedEditable(ctx, userID, id)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
idx := slices.Index(existing.Words, word)
|
||||
if idx == -1 {
|
||||
return nil, ErrWordNotFound
|
||||
}
|
||||
existing.Words = slices.Delete(existing.Words, idx, idx+1)
|
||||
|
||||
return s.repository.UpdateCollection(ctx, existing)
|
||||
}
|
||||
|
||||
func (s *CollectionService) AppendToHistory(ctx context.Context, userID uuid.UUID, lang model.Language, word string) error {
|
||||
existing, err := s.repository.GetOrCreateAutoCollection(ctx, userID, lang)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if slices.Contains(existing.Words, word) {
|
||||
return nil
|
||||
}
|
||||
existing.Words = append(existing.Words, word)
|
||||
|
||||
_, err = s.repository.UpdateCollection(ctx, existing)
|
||||
return err
|
||||
}
|
||||
|
||||
func (s *CollectionService) DeleteCollection(ctx context.Context, userID, id uuid.UUID) error {
|
||||
if _, err := s.getOwnedEditable(ctx, userID, id); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return s.repository.DeleteCollection(ctx, id)
|
||||
}
|
||||
Reference in New Issue
Block a user