diff --git a/cmd/main.go b/cmd/main.go index dbaec28..0305054 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -9,6 +9,7 @@ import ( "github.com/KristianJBorgwarth/dendrite.daemon/core/handlers/vault" "github.com/KristianJBorgwarth/dendrite.daemon/core/logging" "github.com/KristianJBorgwarth/dendrite.daemon/core/server" + "github.com/KristianJBorgwarth/dendrite.daemon/core/services" "github.com/KristianJBorgwarth/dendrite.daemon/persistence/repositories" _ "modernc.org/sqlite" ) @@ -21,9 +22,10 @@ func main() { linkRepo := repositories.NewLinkRepository() tagRepo := repositories.NewTagRepository() noteRepo := repositories.NewNoteRepository() + tagService := services.NewTagService(tagRepo) server.RegisterHandler("vault/init", vault.NewInitializeHandler()) - server.RegisterHandler("note/create", note.NewCreateNoteHandler(uow, tagRepo, noteRepo)) + server.RegisterHandler("note/create", note.NewCreateNoteHandler(uow, tagService, noteRepo)) server.RegisterHandler("note/save", note.NewSaveNoteHandler(uow, linkRepo, tagRepo, noteRepo)) server.RegisterHandler("completion/link", completion.NewCompleteLinkHandler()) diff --git a/core/handlers/note/create_note_handler.go b/core/handlers/note/create_note_handler.go index 9def74a..026a89a 100644 --- a/core/handlers/note/create_note_handler.go +++ b/core/handlers/note/create_note_handler.go @@ -7,7 +7,7 @@ import ( filehandling "github.com/KristianJBorgwarth/dendrite.daemon/core/file_handling" "github.com/KristianJBorgwarth/dendrite.daemon/core/models" - "github.com/KristianJBorgwarth/dendrite.daemon/core/utils" + "github.com/KristianJBorgwarth/dendrite.daemon/core/services" "github.com/KristianJBorgwarth/dendrite.daemon/persistence/repositories" "github.com/KristianJBorgwarth/dendrite.daemon/persistence/store" ) @@ -19,14 +19,14 @@ type createNoteCommand struct { } type CreateNoteHandler struct { - uow *repositories.UnitOfWork - tagRepo repositories.ITagRepository - noteRepo repositories.NoteRepository + uow *repositories.UnitOfWork + tagService services.ITagService + noteRepo repositories.NoteRepository } func NewCreateNoteHandler( uow *repositories.UnitOfWork, - tagRepo repositories.ITagRepository, + tagRepo services.ITagService, noteRepo repositories.NoteRepository, ) *CreateNoteHandler { return &CreateNoteHandler{uow, tagRepo, noteRepo} @@ -53,38 +53,18 @@ func (h *CreateNoteHandler) Handle(ctx context.Context, raw json.RawMessage) (an defer h.uow.Rollback() - dbTags, err := h.tagRepo.GetByNames(ctx, dbCtx, template.FrontMatter.Tags) + tagModels, err := h.tagService.CreateTags(ctx, dbCtx, template.FrontMatter.Tags) if err != nil { return nil, err } - newTags := utils.Filter(template.FrontMatter.Tags, func(name string) bool { - for _, t := range dbTags { - if t.Name() == name { - return false - } - } - return true - }) - - tagModels, err := models.CreateTags(newTags) - if err != nil { - return nil, err - } - - if err = h.tagRepo.Insert(ctx, dbCtx, tagModels); err != nil { - return nil, err - } - - tagModels = append(tagModels, dbTags...) - note := models.CreateNote(notePath, cmd.Title, template.Slug) if err = h.noteRepo.Insert(ctx, dbCtx, note); err != nil { return nil, err } - if err = h.tagRepo.InsertNoteTags(ctx, dbCtx, note.ID(), utils.Select(tagModels, func(t *models.Tag) string { return t.ID() })); err != nil { + if err = h.tagService.CreateNoteTags(ctx, dbCtx, note.ID(), tagModels); err != nil { return nil, err } diff --git a/core/models/tag.go b/core/models/tag.go index ed1087e..3398a46 100644 --- a/core/models/tag.go +++ b/core/models/tag.go @@ -14,17 +14,14 @@ func NewTag(id, name string) *Tag { } } -func CreateTags(tags []string) ([]*Tag, error) { +func CreateTags(tags []string) ([]*Tag) { tagModels := make([]*Tag, len(tags)) for i, tag := range tags { - id, err := uuid.NewV7() - if err != nil { - panic(err) - } + id, _ := uuid.NewV7() tagModels[i] = NewTag(id.String(), tag) } - return tagModels, nil + return tagModels } func (t *Tag) ID() string { diff --git a/core/services/tag_service.go b/core/services/tag_service.go index 5e568ea..bb5076e 100644 --- a/core/services/tag_service.go +++ b/core/services/tag_service.go @@ -1 +1,57 @@ package services + +import ( + "context" + + "github.com/KristianJBorgwarth/dendrite.daemon/core/models" + "github.com/KristianJBorgwarth/dendrite.daemon/core/utils" + "github.com/KristianJBorgwarth/dendrite.daemon/persistence" + "github.com/KristianJBorgwarth/dendrite.daemon/persistence/repositories" +) + +type ITagService interface { + CreateTags(ctx context.Context, dbCtx persistence.IDbContext, names []string) ([]*models.Tag, error) + CreateNoteTags(ctx context.Context, dbCtx persistence.IDbContext, noteID string, tags []*models.Tag) error +} + +type tagService struct { + tagRepo repositories.ITagRepository +} + +func NewTagService(tagRepo repositories.ITagRepository) *tagService { + return &tagService{tagRepo} +} + +func (s *tagService) CreateTags(ctx context.Context, dbCtx persistence.IDbContext, names []string) ([]*models.Tag, error) { + existingTags, err := s.tagRepo.GetByNames(ctx, dbCtx, names) + if err != nil { + return nil, err + } + + newTags := utils.Filter(names, func(name string) bool { + for _, tag := range existingTags { + if tag.Name() == name { + return false + } + } + return true + }) + + newTagModels := models.CreateTags(newTags) + + if err = s.tagRepo.Insert(ctx, dbCtx, newTagModels); err != nil { + return nil, err + } + + tags := append(existingTags, newTagModels...) + + return tags, nil +} + +func(s *tagService) CreateNoteTags(ctx context.Context, dbCtx persistence.IDbContext, noteID string, tags []*models.Tag) error { + tagIds := utils.Select(tags, func(t *models.Tag) string { + return t.ID() + }); + + return s.tagRepo.InsertNoteTags(ctx, dbCtx, noteID, tagIds) +} diff --git a/dendrite b/dendrite index 42a1742..0138ab9 100755 Binary files a/dendrite and b/dendrite differ diff --git a/test/test_integration/create_note_handler_test.go b/test/test_integration/create_note_handler_test.go index 88eca92..fcbdeda 100644 --- a/test/test_integration/create_note_handler_test.go +++ b/test/test_integration/create_note_handler_test.go @@ -7,6 +7,7 @@ import ( "testing" "github.com/KristianJBorgwarth/dendrite.daemon/core/handlers/note" + "github.com/KristianJBorgwarth/dendrite.daemon/core/services" "github.com/KristianJBorgwarth/dendrite.daemon/persistence/repositories" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" @@ -15,7 +16,7 @@ import ( func newCreateNoteHandler() *note.CreateNoteHandler { return note.NewCreateNoteHandler( repositories.NewUnitOfWork(), - repositories.NewTagRepository(), + services.NewTagService(repositories.NewTagRepository()), repositories.NewNoteRepository(), ) }