This repository has been archived by the owner on Sep 22, 2021. It is now read-only.
/
anime.go
143 lines (128 loc) · 3.88 KB
/
anime.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package database
import (
"errors"
"fmt"
"net/http"
"github.com/rl404/mal-db/internal/constant"
_errors "github.com/rl404/mal-db/internal/errors"
"github.com/rl404/mal-db/internal/model"
"github.com/rl404/mal-db/internal/model/join"
"github.com/rl404/mal-db/internal/model/raw"
"github.com/rl404/mal-db/internal/pkg/utils"
"gorm.io/gorm"
)
// GetAnime to get anime from db.
func (d *Database) GetAnime(id int) (*model.Anime, map[string]interface{}, int, error) {
// Is empty.
if d.isEntryEmpty(constant.AnimeType, id) {
return nil, nil, http.StatusNotFound, _errors.ErrInvalidID
}
// Retrieve from db.
var animeRaw raw.Anime
if errors.Is(d.db.Where("id = ?", id).First(&animeRaw).Error, gorm.ErrRecordNotFound) {
// Enqueue if not exists.
if err := d.enqueue(constant.AnimeType, id); err != nil {
return nil, nil, http.StatusInternalServerError, err
}
return nil, nil, http.StatusAccepted, _errors.ErrQueueEntry
}
// Fill data.
anime := &model.Anime{
ID: animeRaw.ID,
Title: animeRaw.Title,
AlternativeTitles: model.AlternativeTitle{
English: animeRaw.TitleEnglish,
Japanese: animeRaw.TitleJapanese,
Synonym: animeRaw.TitleSynonym,
},
Image: animeRaw.ImageURL,
Video: animeRaw.VideoURL,
Synopsis: animeRaw.Synopsis,
Score: animeRaw.Score,
Voter: animeRaw.Voter,
Rank: animeRaw.Rank,
Popularity: animeRaw.Popularity,
Member: animeRaw.Member,
Favorite: animeRaw.Favorite,
Type: animeRaw.AnimeTypeID,
Episode: animeRaw.Episode,
Status: animeRaw.AnimeStatusID,
Airing: model.Airing{
Start: model.Date{
Year: animeRaw.StartYear,
Month: animeRaw.StartMonth,
Day: animeRaw.StartDay,
},
End: model.Date{
Year: animeRaw.EndYear,
Month: animeRaw.EndMonth,
Day: animeRaw.EndDay,
},
Day: animeRaw.AiringDay,
Time: animeRaw.AiringTime,
},
Duration: utils.SecondToString(animeRaw.Duration),
Premiered: animeRaw.Premiered,
Source: animeRaw.AnimeSourceID,
Rating: animeRaw.AnimeRatingID,
Genres: d.getMediaGenre(constant.AnimeType, id),
Related: d.getRelated(constant.AnimeType, id),
Songs: d.getAnimeSong(id),
}
anime.Producers, anime.Studios, anime.Licensors = d.getAnimeProducer(id)
// Prepare meta.
meta := map[string]interface{}{
"parsedAt": animeRaw.UpdatedAt,
}
return anime, meta, http.StatusOK, nil
}
func (d *Database) getAnimeProducer(id int) (producers, studios, licensors []model.Item) {
var animeProducer []join.AnimeProducer
err := d.db.Table(fmt.Sprintf("%s as ap", raw.AnimeProducer{}.TableName())).
Select("pm.id, pm.name, ap.is_licensor, ap.is_studio").
Joins(fmt.Sprintf("left join %s as pm on pm.id = ap.producer_id", raw.ProducerMagazine{}.TableName())).
Where("ap.anime_id = ? and pm.type = ?", id, constant.AnimeType).
Find(&animeProducer).Error
if err != nil {
d.log.Error(err.Error())
}
producers, studios, licensors = []model.Item{}, []model.Item{}, []model.Item{}
for _, p := range animeProducer {
if !p.IsLicensor && !p.IsStudio {
producers = append(producers, model.Item{
ID: p.ID,
Name: p.Name,
})
} else if !p.IsLicensor && p.IsStudio {
studios = append(studios, model.Item{
ID: p.ID,
Name: p.Name,
})
} else if p.IsLicensor && !p.IsStudio {
licensors = append(licensors, model.Item{
ID: p.ID,
Name: p.Name,
})
}
}
return producers, studios, licensors
}
func (d *Database) getAnimeSong(id int) model.Song {
var songs []raw.Song
if err := d.db.Select("type, song").Where("anime_id = ?", id).Find(&songs).Error; err != nil {
d.log.Error(err.Error())
}
result := model.Song{
Opening: []string{},
Ending: []string{},
}
for _, s := range songs {
if s.Type == constant.OpeningSong {
result.Opening = append(result.Opening, s.Song)
}
if s.Type == constant.EndingSong {
result.Ending = append(result.Ending, s.Song)
}
}
return result
}