From 18a7be18d218a1b766c2d06c9dd17df405abe20b Mon Sep 17 00:00:00 2001 From: Eduard Urbach Date: Fri, 7 Jun 2019 10:50:38 +0900 Subject: [PATCH] Use AnimeID type --- arn/ActivityConsumeAnime.go | 10 +++++----- arn/AniList.go | 2 +- arn/Anime.go | 16 +++++++++++----- arn/AnimeCharacters.go | 4 ++-- arn/AnimeEpisodes.go | 2 +- arn/AnimeList.go | 8 ++++---- arn/AnimeListItem.go | 2 +- arn/AnimeRelation.go | 2 +- arn/AnimeRelations.go | 8 ++++---- arn/DraftIndex.go | 16 ++++++++-------- arn/IgnoreAnimeDifference.go | 4 ++-- arn/Quote.go | 2 +- 12 files changed, 41 insertions(+), 35 deletions(-) diff --git a/arn/ActivityConsumeAnime.go b/arn/ActivityConsumeAnime.go index 85d842e4..8ec7d8ab 100644 --- a/arn/ActivityConsumeAnime.go +++ b/arn/ActivityConsumeAnime.go @@ -4,9 +4,9 @@ import "sort" // ActivityConsumeAnime is a user activity that consumes anime. type ActivityConsumeAnime struct { - AnimeID string `json:"animeId"` - FromEpisode int `json:"fromEpisode"` - ToEpisode int `json:"toEpisode"` + AnimeID AnimeID `json:"animeId"` + FromEpisode int `json:"fromEpisode"` + ToEpisode int `json:"toEpisode"` hasID hasCreator @@ -14,7 +14,7 @@ type ActivityConsumeAnime struct { } // NewActivityConsumeAnime creates a new activity. -func NewActivityConsumeAnime(animeID string, fromEpisode int, toEpisode int, userID UserID) *ActivityConsumeAnime { +func NewActivityConsumeAnime(animeID AnimeID, fromEpisode int, toEpisode int, userID UserID) *ActivityConsumeAnime { return &ActivityConsumeAnime{ hasID: hasID{ ID: GenerateID("ActivityConsumeAnime"), @@ -46,7 +46,7 @@ func (activity *ActivityConsumeAnime) Self() Loggable { } // LastActivityConsumeAnime returns the last activity for the given anime. -func (user *User) LastActivityConsumeAnime(animeID string) *ActivityConsumeAnime { +func (user *User) LastActivityConsumeAnime(animeID AnimeID) *ActivityConsumeAnime { activities := FilterActivitiesConsumeAnime(func(activity *ActivityConsumeAnime) bool { return activity.AnimeID == animeID && activity.CreatedBy == user.ID }) diff --git a/arn/AniList.go b/arn/AniList.go index d764cf19..3c0f3e3e 100644 --- a/arn/AniList.go +++ b/arn/AniList.go @@ -39,7 +39,7 @@ func NewAniListAnimeFinder() *AniListAnimeFinder { } // GetAnime tries to find an AniList anime in our anime database. -func (finder *AniListAnimeFinder) GetAnime(id string, malID string) *Anime { +func (finder *AniListAnimeFinder) GetAnime(id AnimeID, malID string) *Anime { animeByID, existsByID := finder.idToAnime[id] animeByMALID, existsByMALID := finder.malIDToAnime[malID] diff --git a/arn/Anime.go b/arn/Anime.go index 90b7bc17..49f22424 100644 --- a/arn/Anime.go +++ b/arn/Anime.go @@ -62,8 +62,12 @@ func init() { } } +// AnimeID represents an anime ID. +type AnimeID = string + // Anime represents an anime. type Anime struct { + ID AnimeID `json:"id"` Type string `json:"type" editable:"true" datalist:"anime-types"` Title *MediaTitle `json:"title" editable:"true"` Summary string `json:"summary" editable:"true" type:"textarea"` @@ -81,7 +85,6 @@ type Anime struct { Trailers []*ExternalMedia `json:"trailers" editable:"true"` // Mixins - hasID hasMappings hasPosts hasLikes @@ -104,9 +107,7 @@ type Anime struct { // NewAnime creates a new anime. func NewAnime() *Anime { return &Anime{ - hasID: hasID{ - ID: GenerateID("Anime"), - }, + ID: GenerateID("Anime"), Title: &MediaTitle{}, Rating: &AnimeRating{}, Popularity: &AnimePopularity{}, @@ -121,7 +122,7 @@ func NewAnime() *Anime { } // GetAnime gets the anime with the given ID. -func GetAnime(id string) (*Anime, error) { +func GetAnime(id AnimeID) (*Anime, error) { obj, err := DB.Get("Anime", id) if err != nil { @@ -767,6 +768,11 @@ func (anime *Anime) String() string { return anime.Title.Canonical } +// GetID returns the ID. +func (anime *Anime) GetID() string { + return anime.ID +} + // TypeName returns the type name. func (anime *Anime) TypeName() string { return "Anime" diff --git a/arn/AnimeCharacters.go b/arn/AnimeCharacters.go index 76b69733..c6a79698 100644 --- a/arn/AnimeCharacters.go +++ b/arn/AnimeCharacters.go @@ -10,7 +10,7 @@ import ( // AnimeCharacters is a list of characters for an anime. type AnimeCharacters struct { - AnimeID string `json:"animeId" mainID:"true"` + AnimeID AnimeID `json:"animeId" mainID:"true"` Items []*AnimeCharacter `json:"items" editable:"true"` sync.Mutex @@ -108,7 +108,7 @@ func (characters *AnimeCharacters) First(count int) []*AnimeCharacter { } // GetAnimeCharacters ... -func GetAnimeCharacters(animeID string) (*AnimeCharacters, error) { +func GetAnimeCharacters(animeID AnimeID) (*AnimeCharacters, error) { obj, err := DB.Get("AnimeCharacters", animeID) if err != nil { diff --git a/arn/AnimeEpisodes.go b/arn/AnimeEpisodes.go index 1f70d723..3187874b 100644 --- a/arn/AnimeEpisodes.go +++ b/arn/AnimeEpisodes.go @@ -11,7 +11,7 @@ import ( // AnimeEpisodes is a list of episodes for an anime. type AnimeEpisodes struct { - AnimeID string `json:"animeId" mainID:"true"` + AnimeID AnimeID `json:"animeId" mainID:"true"` Items []*AnimeEpisode `json:"items" editable:"true"` sync.Mutex diff --git a/arn/AnimeList.go b/arn/AnimeList.go index f399b033..1bf3d2d9 100644 --- a/arn/AnimeList.go +++ b/arn/AnimeList.go @@ -18,7 +18,7 @@ type AnimeList struct { } // Add adds an anime to the list if it hasn't been added yet. -func (list *AnimeList) Add(animeID string) error { +func (list *AnimeList) Add(animeID AnimeID) error { if list.Contains(animeID) { return errors.New("Anime " + animeID + " has already been added") } @@ -45,7 +45,7 @@ func (list *AnimeList) Add(animeID string) error { } // Remove removes the anime ID from the list. -func (list *AnimeList) Remove(animeID string) bool { +func (list *AnimeList) Remove(animeID AnimeID) bool { list.Lock() defer list.Unlock() @@ -60,7 +60,7 @@ func (list *AnimeList) Remove(animeID string) bool { } // Contains checks if the list contains the anime ID already. -func (list *AnimeList) Contains(animeID string) bool { +func (list *AnimeList) Contains(animeID AnimeID) bool { list.Lock() defer list.Unlock() @@ -88,7 +88,7 @@ func (list *AnimeList) HasItemsWithStatus(status string) bool { } // Find returns the list item with the specified anime ID, if available. -func (list *AnimeList) Find(animeID string) *AnimeListItem { +func (list *AnimeList) Find(animeID AnimeID) *AnimeListItem { list.Lock() defer list.Unlock() diff --git a/arn/AnimeListItem.go b/arn/AnimeListItem.go index 06e317ff..b45736b3 100644 --- a/arn/AnimeListItem.go +++ b/arn/AnimeListItem.go @@ -11,7 +11,7 @@ const ( // AnimeListItem ... type AnimeListItem struct { - AnimeID string `json:"animeId"` + AnimeID AnimeID `json:"animeId"` Status string `json:"status" editable:"true"` Episodes int `json:"episodes" editable:"true"` Rating AnimeListItemRating `json:"rating"` diff --git a/arn/AnimeRelation.go b/arn/AnimeRelation.go index 1e9ac1fc..99180770 100644 --- a/arn/AnimeRelation.go +++ b/arn/AnimeRelation.go @@ -18,7 +18,7 @@ func init() { // AnimeRelation ... type AnimeRelation struct { - AnimeID string `json:"animeId" editable:"true"` + AnimeID AnimeID `json:"animeId" editable:"true"` Type string `json:"type" editable:"true" datalist:"anime-relation-types"` } diff --git a/arn/AnimeRelations.go b/arn/AnimeRelations.go index 4bb373c4..8ba55448 100644 --- a/arn/AnimeRelations.go +++ b/arn/AnimeRelations.go @@ -9,7 +9,7 @@ import ( // AnimeRelations is a list of relations for an anime. type AnimeRelations struct { - AnimeID string `json:"animeId" mainID:"true"` + AnimeID AnimeID `json:"animeId" mainID:"true"` Items []*AnimeRelation `json:"items" editable:"true"` sync.Mutex @@ -72,7 +72,7 @@ func (relations *AnimeRelations) Self() Loggable { } // Find returns the relation with the specified anime ID, if available. -func (relations *AnimeRelations) Find(animeID string) *AnimeRelation { +func (relations *AnimeRelations) Find(animeID AnimeID) *AnimeRelation { relations.Lock() defer relations.Unlock() @@ -86,7 +86,7 @@ func (relations *AnimeRelations) Find(animeID string) *AnimeRelation { } // Remove removes the anime ID from the relations. -func (relations *AnimeRelations) Remove(animeID string) bool { +func (relations *AnimeRelations) Remove(animeID AnimeID) bool { relations.Lock() defer relations.Unlock() @@ -101,7 +101,7 @@ func (relations *AnimeRelations) Remove(animeID string) bool { } // GetAnimeRelations ... -func GetAnimeRelations(animeID string) (*AnimeRelations, error) { +func GetAnimeRelations(animeID AnimeID) (*AnimeRelations, error) { obj, err := DB.Get("AnimeRelations", animeID) if err != nil { diff --git a/arn/DraftIndex.go b/arn/DraftIndex.go index f2cf33b7..22324c65 100644 --- a/arn/DraftIndex.go +++ b/arn/DraftIndex.go @@ -7,14 +7,14 @@ import ( // DraftIndex has references to unpublished drafts a user created. type DraftIndex struct { - UserID string `json:"userId"` - GroupID string `json:"groupId"` - SoundTrackID string `json:"soundTrackId"` - CompanyID string `json:"companyId"` - QuoteID string `json:"quoteId"` - CharacterID string `json:"characterId"` - AnimeID string `json:"animeId"` - AMVID string `json:"amvId"` + UserID string `json:"userId"` + GroupID string `json:"groupId"` + SoundTrackID string `json:"soundTrackId"` + CompanyID string `json:"companyId"` + QuoteID string `json:"quoteId"` + CharacterID string `json:"characterId"` + AnimeID AnimeID `json:"animeId"` + AMVID string `json:"amvId"` } // NewDraftIndex ... diff --git a/arn/IgnoreAnimeDifference.go b/arn/IgnoreAnimeDifference.go index 4d7dc5bd..1d56a90c 100644 --- a/arn/IgnoreAnimeDifference.go +++ b/arn/IgnoreAnimeDifference.go @@ -30,12 +30,12 @@ func GetIgnoreAnimeDifference(id string) (*IgnoreAnimeDifference, error) { } // CreateDifferenceID ... -func CreateDifferenceID(animeID string, dataProvider string, malAnimeID string, typeName string) string { +func CreateDifferenceID(animeID AnimeID, dataProvider string, malAnimeID string, typeName string) string { return fmt.Sprintf("arn:%s|%s:%s|%s", animeID, dataProvider, malAnimeID, typeName) } // IsAnimeDifferenceIgnored tells you whether the given difference is being ignored. -func IsAnimeDifferenceIgnored(animeID string, dataProvider string, malAnimeID string, typeName string, hash uint64) bool { +func IsAnimeDifferenceIgnored(animeID AnimeID, dataProvider string, malAnimeID string, typeName string, hash uint64) bool { key := CreateDifferenceID(animeID, dataProvider, malAnimeID, typeName) ignore, err := GetIgnoreAnimeDifference(key) diff --git a/arn/Quote.go b/arn/Quote.go index 404c4633..e819152c 100644 --- a/arn/Quote.go +++ b/arn/Quote.go @@ -14,7 +14,7 @@ import ( type Quote struct { Text QuoteText `json:"text" editable:"true"` CharacterID string `json:"characterId" editable:"true"` - AnimeID string `json:"animeId" editable:"true"` + AnimeID AnimeID `json:"animeId" editable:"true"` EpisodeNumber int `json:"episode" editable:"true"` Time int `json:"time" editable:"true"`