Skip to content

Commit

Permalink
refactor(app.domain.song): refine structure
Browse files Browse the repository at this point in the history
  • Loading branch information
alebabai committed Feb 9, 2024
1 parent da9127e commit a5a3825
Show file tree
Hide file tree
Showing 10 changed files with 146 additions and 172 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ module github.com/linden-honey/linden-honey-api-go

go 1.21

require github.com/linden-honey/linden-honey-sdk-go v0.1.0
require github.com/linden-honey/linden-honey-sdk-go v0.1.4
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
github.com/linden-honey/linden-honey-sdk-go v0.1.0 h1:lE4AQK+LnMTW4s0B/LAsxwlridjpUvB0NG9vemBPOmY=
github.com/linden-honey/linden-honey-sdk-go v0.1.0/go.mod h1:Of0QNaySkZignSgTMuIgKAM3OjffUsyoL8oekHKAw10=
github.com/linden-honey/linden-honey-sdk-go v0.1.4 h1:W8OY56kRQ+Itakm8VH65p7RLh4/wCy4/Dq7i6sCAAkI=
github.com/linden-honey/linden-honey-sdk-go v0.1.4/go.mod h1:Id4XBwvUN6dbbnkDmfRBTntkG6nYMT+z+eM6w2Sl+4Y=
4 changes: 4 additions & 0 deletions pkg/application/domain/song/dto.go
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
package song

type GetQuoteResponse struct {
Quote Quote `json:"quote"`
}
12 changes: 12 additions & 0 deletions pkg/application/domain/song/dto_validation.go
Original file line number Diff line number Diff line change
@@ -1 +1,13 @@
package song

import (
sdkerrors "github.com/linden-honey/linden-honey-sdk-go/errors"
)

func (dto GetQuoteResponse) Validate() error {
if err := dto.Quote.Validate(); err != nil {
return sdkerrors.NewInvalidValueError("Quote", err)
}

return nil
}
40 changes: 40 additions & 0 deletions pkg/application/domain/song/dto_validation_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package song

import (
"testing"
)

func TestGetQuoteResponse_Validate(t *testing.T) {
type fields struct {
Quote Quote
}
tests := []struct {
name string
fields fields
wantErr bool
}{
{
name: "ok",
fields: fields{
Quote: "some quote",
},
},
{
name: "err invalid quote",
fields: fields{
Quote: "",
},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
dto := GetQuoteResponse{
Quote: tt.fields.Quote,
}
if err := dto.Validate(); (err != nil) != tt.wantErr {
t.Errorf("GetQuoteResponse.Validate() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
46 changes: 3 additions & 43 deletions pkg/application/domain/song/entity.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
package song

import (
"errors"
"math/rand"
)

// Entity is a main domain object.
type Entity struct {
Metadata
Lyrics `json:"lyrics"`
Lyrics Lyrics `json:"lyrics"`
}

// Metadata is a domain helper object.
Expand All @@ -30,43 +25,8 @@ type Tag struct {
// Lyrics is a domain object.
type Lyrics []Verse

// GetRandomVerse returns a random verse from lyrics or an error if there are no verses.
func (l Lyrics) GetRandomVerse() (*Verse, error) {
versesCount := len(l)
if versesCount == 0 {
return nil, errors.New("no verses")
}

return &l[rand.Intn(versesCount)], nil
}

// GetRandomQuote returns a random quote from lyrics or an error if there are no quotes.
func (l Lyrics) GetRandomQuote() (*Quote, error) {
quotes := l.GetQuotes()
quotesCount := len(quotes)
if quotesCount == 0 {
return nil, errors.New("no quotes")
}

return &quotes[rand.Intn(quotesCount)], nil
}

// GetQuotes returns all quotes from the song.
func (l Lyrics) GetQuotes() []Quote {
quotes := make([]Quote, 0)
for _, v := range l {
quotes = append(quotes, v.Quotes...)
}

return quotes
}

// Verse is a domain object.
type Verse struct {
Quotes []Quote `json:"quotes"`
}
type Verse []Quote

// Quote is a domain object.
type Quote struct {
Phrase string `json:"phrase"`
}
type Quote string
37 changes: 37 additions & 0 deletions pkg/application/domain/song/entity_accessors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package song

import (
"errors"
"math/rand"
)

// GetRandomVerse returns a random verse from lyrics or an error if there are no verses.
func (l Lyrics) GetRandomVerse() (Verse, error) {
versesCount := len(l)
if versesCount == 0 {
return nil, errors.New("no verses")
}

return l[rand.Intn(versesCount)], nil
}

// GetRandomQuote returns a random quote from lyrics or an error if there are no quotes.
func (l Lyrics) GetRandomQuote() (Quote, error) {
quotes := l.GetQuotes()
quotesCount := len(quotes)
if quotesCount == 0 {
return "", errors.New("no quotes")
}

return quotes[rand.Intn(quotesCount)], nil
}

// GetQuotes returns all quotes from the song.
func (l Lyrics) GetQuotes() []Quote {
quotes := make([]Quote, 0)
for _, v := range l {
quotes = append(quotes, v...)
}

return quotes
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,37 +9,25 @@ func TestLyrics_GetRandomVerse(t *testing.T) {
tests := []struct {
name string
l Lyrics
want *Verse
want Verse
wantErr bool
}{
{
name: "ok",
l: Lyrics{
{
Quotes: []Quote{
{
Phrase: "some phrase",
},
},
"some quote",
},
{
Quotes: []Quote{
{
Phrase: "some phrase",
},
},
"some quote",
},
},
want: &Verse{
Quotes: []Quote{
{
Phrase: "some phrase",
},
},
want: Verse{
"some quote",
},
},
{
name: "empty",
name: "err empty lyrics",
l: Lyrics{},
wantErr: true,
},
Expand All @@ -62,33 +50,23 @@ func TestLyrics_GetRandomQuote(t *testing.T) {
tests := []struct {
name string
l Lyrics
want *Quote
want Quote
wantErr bool
}{
{
name: "ok",
l: Lyrics{
{
Quotes: []Quote{
{
Phrase: "some phrase",
},
{
Phrase: "some phrase",
},
},

"some quote",
"some quote",
},
{
Quotes: []Quote{
{
Phrase: "some phrase",
},
},

"some quote",
},
},
want: &Quote{
Phrase: "some phrase",
},
want: "some quote",
},
{
name: "empty",
Expand Down Expand Up @@ -120,33 +98,19 @@ func TestLyrics_GetQuotes(t *testing.T) {
name: "ok",
l: Lyrics{
{
Quotes: []Quote{
{
Phrase: "some phrase",
},
{
Phrase: "another phrase",
},
},

"some quote",
"another quote",
},
{
Quotes: []Quote{
{
Phrase: "one more phrase",
},
},

"one more quote",
},
},
want: []Quote{
{
Phrase: "some phrase",
},
{
Phrase: "another phrase",
},
{
Phrase: "one more phrase",
},
"some quote",
"another quote",
"one more quote",
},
},
{
Expand Down
14 changes: 7 additions & 7 deletions pkg/application/domain/song/entity_validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,20 +47,20 @@ func (ts Tags) Validate() error {
errs := make([]error, 0)

unique := make(map[Tag]struct{}, len(ts))
for i, t := range ts {
for _, t := range ts {
if _, ok := unique[t]; !ok {
unique[t] = struct{}{}
} else {
errs = append(
errs,
sdkerrors.NewInvalidValueError(fmt.Sprintf("Tags[%d]", i), errors.New("duplicate tag")),
sdkerrors.NewInvalidValueError(fmt.Sprintf("%#v", t), errors.New("duplicate tag")),
)

continue
}

if err := t.Validate(); err != nil {
errs = append(errs, sdkerrors.NewInvalidValueError(fmt.Sprintf("Tags[%d]", i), err))
errs = append(errs, sdkerrors.NewInvalidValueError(fmt.Sprintf("%#v", t), err))
}
}

Expand Down Expand Up @@ -103,11 +103,11 @@ func (l Lyrics) Validate() error {
func (v Verse) Validate() error {
errs := make([]error, 0)

if len(v.Quotes) == 0 {
if len(v) == 0 {
errs = append(errs, sdkerrors.NewRequiredValueError("Quotes"))
}

for i, q := range v.Quotes {
for i, q := range v {
if err := q.Validate(); err != nil {
errs = append(errs, sdkerrors.NewInvalidValueError(fmt.Sprintf("Quotes[%d]", i), err))
}
Expand All @@ -118,8 +118,8 @@ func (v Verse) Validate() error {

// Validate a Quote and returns an error if validation is failed
func (q Quote) Validate() error {
if strings.TrimSpace(q.Phrase) == "" {
return sdkerrors.NewRequiredValueError("Phrase")
if strings.TrimSpace(string(q)) == "" {
return sdkerrors.ErrEmptyValue
}

return nil
Expand Down
Loading

0 comments on commit a5a3825

Please sign in to comment.