Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
fmartingr committed May 26, 2024
1 parent 2d88f90 commit 27ee126
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 5 deletions.
6 changes: 6 additions & 0 deletions internal/database/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,12 @@ type DB interface {
// GetTags fetch list of tags and its frequency from database.
GetTags(ctx context.Context) ([]model.Tag, error)

// UpdateTag updates tag with matching id in database.
UpdateTag(ctx context.Context, tag model.Tag) error

// DeleteTag removes tag with matching id from database.
DeleteTag(ctx context.Context, id model.DBID) error

// RenameTag change the name of a tag.
RenameTag(ctx context.Context, id int, newName string) error
}
Expand Down
69 changes: 69 additions & 0 deletions internal/domains/tags.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package domains

import (
"context"
"fmt"

"github.com/go-shiori/shiori/internal/dependencies"
"github.com/go-shiori/shiori/internal/model"
)

type TagDomain struct {
deps *dependencies.Dependencies
}

func (d *TagDomain) GetTags(ctx context.Context) ([]model.TagDTO, error) {
tags, err := d.deps.Database.GetTags(ctx)
if err != nil {
return nil, fmt.Errorf("error retrieving tags: %w", err)
}

tagDTOs := make([]model.TagDTO, len(tags))
for i, tag := range tags {
tagDTOs[i] = tag.ToDTO()
}

return tagDTOs, nil
}

func (d *TagDomain) CreateTag(ctx context.Context, tag model.TagDTO) (model.TagDTO, error) {
// Create tag
err := d.deps.Database.CreateTags(ctx, tag.ToTag())
if err != nil {
return model.TagDTO{}, fmt.Errorf("error creating tag: %w", err)
}

// Get created tag
createdTag, err := d.deps.Database.GetTag(ctx, tagID)
if err != nil {
return model.TagDTO{}, fmt.Errorf("error getting created tag: %w", err)
}

return createdTag.ToDTO(), nil
}

func (d *TagDomain) UpdateTag(ctx context.Context, tag model.TagDTO) (model.TagDTO, error) {
// Update tag
err := d.deps.Database.UpdateTag(ctx, tag.ID, tag.Name)
if err != nil {
return model.TagDTO{}, fmt.Errorf("error updating tag: %w", err)
}

// Get updated tag
updatedTag, err := d.deps.Database.GetTag(ctx, tag.ID)
if err != nil {
return model.TagDTO{}, fmt.Errorf("error getting updated tag: %w", err)
}

return updatedTag.ToDTO(), nil
}

func (d *TagDomain) DeleteTag(ctx context.Context, tagID model.DBID) error {
// Delete tag
err := d.deps.Database.DeleteTag(ctx, tagID)
if err != nil {
return fmt.Errorf("error deleting tag: %w", err)
}

return nil
}
7 changes: 7 additions & 0 deletions internal/model/domains.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@ type BookmarksDomain interface {
GetBookmark(ctx context.Context, id DBID) (*BookmarkDTO, error)
}

type TagsDomain interface {
GetTags(ctx context.Context) ([]TagDTO, error)
CreateTags(ctx context.Context, tags ...TagDTO) (TagDTO, error)
UpdateTag(ctx context.Context, tag TagDTO) (TagDTO, error)
DeleteTag(ctx context.Context, tagID DBID) error
}

type AccountsDomain interface {
CheckToken(ctx context.Context, userJWT string) (*Account, error)
GetAccountFromCredentials(ctx context.Context, username, password string) (*Account, error)
Expand Down
29 changes: 24 additions & 5 deletions internal/model/tag.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,28 @@
package model

// Tag is the tag for a bookmark.
// Tag is the database representation of a tag object
type Tag struct {
ID int `db:"id" json:"id"`
Name string `db:"name" json:"name"`
NBookmarks int `db:"n_bookmarks" json:"nBookmarks,omitempty"`
Deleted bool `json:"-"`
ID int `db:"id" json:"id"`
Name string `db:"name" json:"name"`
}

func (t *Tag) ToDTO() TagDTO {
return TagDTO{
ID: t.ID,
Name: t.Name,
}
}

// TagDTO is the data transfer object representation of a tag object
type TagDTO struct {
ID int
Name string
BookmarkCount int
}

func (tdto *TagDTO) ToTag() Tag {
return Tag{
ID: tdto.ID,
Name: tdto.Name,
}
}

0 comments on commit 27ee126

Please sign in to comment.