mirror of
https://github.com/SamyRai/tercul-backend.git
synced 2025-12-27 05:11:34 +00:00
This change introduces a service layer to encapsulate the business logic for each domain aggregate. This will make the code more modular, testable, and easier to maintain. The following services have been created: - author - bookmark - category - collection - comment - like - tag - translation - user The main Application struct has been updated to use these new services. The integration test suite has also been updated to use the new Application struct and services. This is a work in progress. The next step is to fix the compilation errors and then refactor the resolvers to use the new services.
67 lines
1.6 KiB
Go
67 lines
1.6 KiB
Go
package bookmark
|
|
|
|
import (
|
|
"context"
|
|
"tercul/internal/domain"
|
|
)
|
|
|
|
// BookmarkCommands contains the command handlers for the bookmark aggregate.
|
|
type BookmarkCommands struct {
|
|
repo domain.BookmarkRepository
|
|
}
|
|
|
|
// NewBookmarkCommands creates a new BookmarkCommands handler.
|
|
func NewBookmarkCommands(repo domain.BookmarkRepository) *BookmarkCommands {
|
|
return &BookmarkCommands{repo: repo}
|
|
}
|
|
|
|
// CreateBookmarkInput represents the input for creating a new bookmark.
|
|
type CreateBookmarkInput struct {
|
|
Name string
|
|
UserID uint
|
|
WorkID uint
|
|
Notes string
|
|
}
|
|
|
|
// CreateBookmark creates a new bookmark.
|
|
func (c *BookmarkCommands) CreateBookmark(ctx context.Context, input CreateBookmarkInput) (*domain.Bookmark, error) {
|
|
bookmark := &domain.Bookmark{
|
|
Name: input.Name,
|
|
UserID: input.UserID,
|
|
WorkID: input.WorkID,
|
|
Notes: input.Notes,
|
|
}
|
|
err := c.repo.Create(ctx, bookmark)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return bookmark, nil
|
|
}
|
|
|
|
// UpdateBookmarkInput represents the input for updating an existing bookmark.
|
|
type UpdateBookmarkInput struct {
|
|
ID uint
|
|
Name string
|
|
Notes string
|
|
}
|
|
|
|
// UpdateBookmark updates an existing bookmark.
|
|
func (c *BookmarkCommands) UpdateBookmark(ctx context.Context, input UpdateBookmarkInput) (*domain.Bookmark, error) {
|
|
bookmark, err := c.repo.GetByID(ctx, input.ID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
bookmark.Name = input.Name
|
|
bookmark.Notes = input.Notes
|
|
err = c.repo.Update(ctx, bookmark)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return bookmark, nil
|
|
}
|
|
|
|
// DeleteBookmark deletes a bookmark by ID.
|
|
func (c *BookmarkCommands) DeleteBookmark(ctx context.Context, id uint) error {
|
|
return c.repo.Delete(ctx, id)
|
|
}
|