-
Notifications
You must be signed in to change notification settings - Fork 555
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
106 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
} | ||
} |