@@ -2,9 +2,25 @@ package com.jeluchu.features.anime.mappers
2
2
3
3
import com.example.models.*
4
4
import com.jeluchu.core.extensions.*
5
- import kotlinx.serialization.json.Json
5
+ import com.jeluchu.features.anime.models.directory.AnimeDirectoryEntity
6
6
import org.bson.Document
7
7
8
+ fun documentToAnimeDirectoryEntity (doc : Document ) = AnimeDirectoryEntity (
9
+ rank = doc.getIntSafe(" rank" ),
10
+ year = doc.getIntSafe(" year" ),
11
+ url = doc.getStringSafe(" url" ),
12
+ malId = doc.getIntSafe(" malId" ),
13
+ type = doc.getStringSafe(" type" ),
14
+ score = doc.getStringSafe(" score" ),
15
+ title = doc.getStringSafe(" title" ),
16
+ status = doc.getStringSafe(" status" ),
17
+ season = doc.getStringSafe(" season" ),
18
+ poster = doc.getStringSafe(" poster" ),
19
+ airing = doc.getBooleanSafe(" airing" ),
20
+ genres = doc.getListSafe<String >(" genres" ),
21
+ episodesCount = doc.getIntSafe(" episodesCount" )
22
+ )
23
+
8
24
fun documentToMoreInfoEntity (doc : Document ): MoreInfoEntity {
9
25
return MoreInfoEntity (
10
26
id = doc.getLongSafe(" id" ),
@@ -14,138 +30,138 @@ fun documentToMoreInfoEntity(doc: Document): MoreInfoEntity {
14
30
cover = doc.getStringSafe(" cover" ),
15
31
genres = doc.getListSafe<String >(" genres" ),
16
32
synopsis = doc.getStringSafe(" synopsis" ),
17
- episodes = doc.getListSafe<Document >(" episodes" ).map { documentToMergedEpisode(it) } ? : emptyList() ,
18
- episodesCount = doc.getIntSafe(" episodesCount" , 0 ) ? : 0 ,
19
- score = doc.getStringSafe(" score" ) ? : " " ,
20
- staff = doc.getListSafe<Document >(" staff" ).map { documentToStaff(it) } ? : emptyList() ,
21
- characters = doc.getListSafe<Document >(" characters" ).map { documentToCharacter(it) } ? : emptyList() ,
22
- status = doc.getStringSafe(" status" ) ? : " " ,
23
- type = doc.getStringSafe(" type" ) ? : " " ,
24
- url = doc.getStringSafe(" url" ) ? : " " ,
33
+ episodes = doc.getListSafe<Document >(" episodes" ).map { documentToMergedEpisode(it) },
34
+ episodesCount = doc.getIntSafe(" episodesCount" , 0 ),
35
+ score = doc.getStringSafe(" score" ),
36
+ staff = doc.getListSafe<Document >(" staff" ).map { documentToStaff(it) },
37
+ characters = doc.getListSafe<Document >(" characters" ).map { documentToCharacter(it) },
38
+ status = doc.getStringSafe(" status" ),
39
+ type = doc.getStringSafe(" type" ),
40
+ url = doc.getStringSafe(" url" ),
25
41
promo = doc.getDocumentSafe(" promo" )?.let { documentToVideoPromo(it) } ? : VideoPromo (),
26
- source = doc.getStringSafe(" source" ) ? : " " ,
27
- duration = doc.getStringSafe(" duration" ) ? : " " ,
28
- rank = doc.getIntSafe(" rank" , 0 ) ? : 0 ,
29
- titles = doc.getListSafe<Document >(" titles" ).map { documentToAlternativeTitles(it) } ? : emptyList() ,
30
- airing = doc.getBooleanSafe(" airing" ) ? : false ,
42
+ source = doc.getStringSafe(" source" ),
43
+ duration = doc.getStringSafe(" duration" ),
44
+ rank = doc.getIntSafe(" rank" , 0 ),
45
+ titles = doc.getListSafe<Document >(" titles" ).map { documentToAlternativeTitles(it) },
46
+ airing = doc.getBooleanSafe(" airing" ),
31
47
aired = doc.getDocumentSafe(" aired" )?.let { documentToAiringTime(it) } ? : AiringTime (),
32
48
broadcast = doc.getDocumentSafe(" broadcast" )?.let { documentToAnimeBroadcast(it) } ? : AnimeBroadcast (),
33
- season = doc.getStringSafe(" season" ) ? : " " ,
49
+ season = doc.getStringSafe(" season" ),
34
50
year = doc.getIntSafe(" year" , 0 ),
35
- external = doc.getListSafe<Document >(" external" ).map { documentToExternalLinks(it) } ? : emptyList() ,
36
- streaming = doc.getListSafe<Document >(" streaming" ).map { documentToExternalLinks(it) } ? : emptyList() ,
37
- studios = doc.getListSafe<Document >(" studios" ).map { documentToCompanies(it) } ? : emptyList() ,
38
- licensors = doc.getListSafe<Document >(" licensors" ).map { documentToCompanies(it) } ? : emptyList() ,
39
- producers = doc.getListSafe<Document >(" producers" ).map { documentToCompanies(it) } ? : emptyList() ,
51
+ external = doc.getListSafe<Document >(" external" ).map { documentToExternalLinks(it) },
52
+ streaming = doc.getListSafe<Document >(" streaming" ).map { documentToExternalLinks(it) },
53
+ studios = doc.getListSafe<Document >(" studios" ).map { documentToCompanies(it) },
54
+ licensors = doc.getListSafe<Document >(" licensors" ).map { documentToCompanies(it) },
55
+ producers = doc.getListSafe<Document >(" producers" ).map { documentToCompanies(it) },
40
56
theme = doc.getDocumentSafe(" theme" )?.let { documentToThemes(it) } ? : Themes (),
41
- relations = doc.getListSafe<Document >(" relations" ).map { documentToRelated(it) } ? : emptyList() ,
57
+ relations = doc.getListSafe<Document >(" relations" ).map { documentToRelated(it) },
42
58
stats = doc.getDocumentSafe(" stats" )?.let { documentToStatistics(it) } ? : Statistics (),
43
- gallery = doc.getListSafe<Document >(" gallery" ).map { documentToImageMediaEntity(it) } ? : emptyList() ,
44
- episodeSource = doc.getStringSafe(" episodeSource" ) ? : " "
59
+ gallery = doc.getListSafe<Document >(" gallery" ).map { documentToImageMediaEntity(it) },
60
+ episodeSource = doc.getStringSafe(" episodeSource" )
45
61
)
46
62
}
47
63
48
64
fun documentToActor (doc : Document ): Actor {
49
65
return Actor (
50
66
person = doc.getDocumentSafe(" person" )?.let { documentToIndividual(it) } ? : Individual (),
51
- language = doc.getStringSafe(" language" ) ? : " "
67
+ language = doc.getStringSafe(" language" )
52
68
)
53
69
}
54
70
55
71
fun documentToAiringTime (doc : Document ): AiringTime {
56
72
return AiringTime (
57
- from = doc.getStringSafe(" from" ) ? : " " ,
58
- to = doc.getStringSafe(" to" ) ? : " "
73
+ from = doc.getStringSafe(" from" ),
74
+ to = doc.getStringSafe(" to" )
59
75
)
60
76
}
61
77
62
78
fun documentToAlternativeTitles (doc : Document ): AlternativeTitles {
63
79
return AlternativeTitles (
64
- title = doc.getStringSafe(" title" ) ? : " " ,
65
- type = doc.getStringSafe(" type" ) ? : " "
80
+ title = doc.getStringSafe(" title" ),
81
+ type = doc.getStringSafe(" type" )
66
82
)
67
83
}
68
84
69
85
fun documentToAnimeBroadcast (doc : Document ): AnimeBroadcast {
70
86
return AnimeBroadcast (
71
- day = doc.getStringSafe(" day" ) ? : " " ,
72
- time = doc.getStringSafe(" time" ) ? : " " ,
73
- timezone = doc.getStringSafe(" timezone" ) ? : " "
87
+ day = doc.getStringSafe(" day" ),
88
+ time = doc.getStringSafe(" time" ),
89
+ timezone = doc.getStringSafe(" timezone" )
74
90
)
75
91
}
76
92
77
93
fun documentToAnimeSource (doc : Document ): AnimeSource {
78
94
return AnimeSource (
79
- id = doc.getStringSafe(" id" ) ? : " " ,
80
- source = doc.getStringSafe(" source" ) ? : " "
95
+ id = doc.getStringSafe(" id" ),
96
+ source = doc.getStringSafe(" source" )
81
97
)
82
98
}
83
99
84
100
fun documentToCharacter (doc : Document ): Character {
85
101
return Character (
86
102
character = doc.getDocumentSafe(" character" )?.let { documentToIndividual(it) } ? : Individual (),
87
- role = doc.getStringSafe(" role" ) ? : " " ,
88
- voiceActor = doc.getListSafe<Document >(" voiceActor" ).map { documentToActor(it) } ? : emptyList()
103
+ role = doc.getStringSafe(" role" ),
104
+ voiceActor = doc.getListSafe<Document >(" voiceActor" ).map { documentToActor(it) }
89
105
)
90
106
}
91
107
92
108
fun documentToCompanies (doc : Document ): Companies {
93
109
return Companies (
94
110
malId = doc.getIntSafe(" malId" , 0 ),
95
- name = doc.getStringSafe(" name" ) ? : " " ,
96
- type = doc.getStringSafe(" type" ) ? : " " ,
97
- url = doc.getStringSafe(" url" ) ? : " "
111
+ name = doc.getStringSafe(" name" ),
112
+ type = doc.getStringSafe(" type" ),
113
+ url = doc.getStringSafe(" url" )
98
114
)
99
115
}
100
116
101
117
fun documentToExternalLinks (doc : Document ): ExternalLinks {
102
118
return ExternalLinks (
103
- url = doc.getStringSafe(" url" ) ? : " " ,
104
- name = doc.getStringSafe(" name" ) ? : " "
119
+ url = doc.getStringSafe(" url" ),
120
+ name = doc.getStringSafe(" name" )
105
121
)
106
122
}
107
123
108
124
fun documentToImageMediaEntity (doc : Document ): ImageMediaEntity {
109
125
return ImageMediaEntity (
110
- media = doc.getStringSafe(" media" ) ? : " " ,
111
- thumbnail = doc.getStringSafe(" thumbnail" ) ? : " " ,
126
+ media = doc.getStringSafe(" media" ),
127
+ thumbnail = doc.getStringSafe(" thumbnail" ),
112
128
width = doc.getIntSafe(" width" , 0 ),
113
129
height = doc.getIntSafe(" height" , 0 ),
114
- url = doc.getStringSafe(" url" ) ? : " "
130
+ url = doc.getStringSafe(" url" )
115
131
)
116
132
}
117
133
118
134
fun documentToImages (doc : Document ): Images {
119
135
return Images (
120
- generic = doc.getStringSafe(" generic" ) ? : " " ,
121
- small = doc.getStringSafe(" small" ) ? : " " ,
122
- medium = doc.getStringSafe(" medium" ) ? : " " ,
123
- large = doc.getStringSafe(" large" ) ? : " " ,
124
- maximum = doc.getStringSafe(" maximum" ) ? : " "
136
+ generic = doc.getStringSafe(" generic" ),
137
+ small = doc.getStringSafe(" small" ),
138
+ medium = doc.getStringSafe(" medium" ),
139
+ large = doc.getStringSafe(" large" ),
140
+ maximum = doc.getStringSafe(" maximum" )
125
141
)
126
142
}
127
143
128
144
fun documentToIndividual (doc : Document ): Individual {
129
145
return Individual (
130
146
malId = doc.getIntSafe(" malId" , 0 ),
131
- url = doc.getStringSafe(" url" ) ? : " " ,
132
- name = doc.getStringSafe(" name" ) ? : " " ,
133
- images = doc.getStringSafe(" images" ) ? : " "
147
+ url = doc.getStringSafe(" url" ),
148
+ name = doc.getStringSafe(" name" ),
149
+ images = doc.getStringSafe(" images" )
134
150
)
135
151
}
136
152
137
153
fun documentToMergedEpisode (doc : Document ): MergedEpisode {
138
154
return MergedEpisode (
139
155
number = doc.getIntSafe(" number" , 0 ),
140
- ids = doc.getListSafe<Document >(" ids" ).map { documentToAnimeSource(it) }.toMutableList() ? : mutableListOf () ,
141
- nextEpisodeDate = doc.getStringSafe(" nextEpisodeDate" ) ? : " "
156
+ ids = doc.getListSafe<Document >(" ids" ).map { documentToAnimeSource(it) }.toMutableList(),
157
+ nextEpisodeDate = doc.getStringSafe(" nextEpisodeDate" )
142
158
)
143
159
}
144
160
145
161
fun documentToRelated (doc : Document ): Related {
146
162
return Related (
147
- entry = doc.getListSafe<Document >(" entry" ).map { documentToCompanies(it) } ? : emptyList() ,
148
- relation = doc.getStringSafe(" relation" ) ? : " "
163
+ entry = doc.getListSafe<Document >(" entry" ).map { documentToCompanies(it) },
164
+ relation = doc.getStringSafe(" relation" )
149
165
)
150
166
}
151
167
@@ -164,19 +180,19 @@ fun documentToScore(doc: Document): Score {
164
180
fun documentToStaff (doc : Document ): Staff {
165
181
return Staff (
166
182
person = doc.get(" person" , Document ::class .java)?.let { documentToIndividual(it) } ? : Individual (),
167
- positions = doc.getListSafe<String >(" positions" ) ? : emptyList()
183
+ positions = doc.getListSafe<String >(" positions" )
168
184
)
169
185
}
170
186
171
187
fun documentToStatistics (doc : Document ): Statistics {
172
188
return Statistics (
173
- completed = doc.getIntSafe(" completed" ) ? : 0 ,
174
- dropped = doc.getIntSafe(" dropped" ) ? : 0 ,
175
- onHold = doc.getIntSafe(" onHold" ) ? : 0 ,
176
- planToWatch = doc.getIntSafe(" planToWatch" ) ? : 0 ,
177
- scores = doc.getListSafe<Document >(" scores" ).map { documentToScore(it) } ? : emptyList() ,
178
- total = doc.getIntSafe(" total" ) ? : 0 ,
179
- watching = doc.getIntSafe(" watching" ) ? : 0
189
+ completed = doc.getIntSafe(" completed" ),
190
+ dropped = doc.getIntSafe(" dropped" ),
191
+ onHold = doc.getIntSafe(" onHold" ),
192
+ planToWatch = doc.getIntSafe(" planToWatch" ),
193
+ scores = doc.getListSafe<Document >(" scores" ).map { documentToScore(it) },
194
+ total = doc.getIntSafe(" total" ),
195
+ watching = doc.getIntSafe(" watching" )
180
196
)
181
197
}
182
198
@@ -189,17 +205,9 @@ fun documentToThemes(doc: Document): Themes {
189
205
190
206
fun documentToVideoPromo (doc : Document ): VideoPromo {
191
207
return VideoPromo (
192
- embedUrl = doc.getStringSafe(" embedUrl" ) ? : " " ,
193
- url = doc.getStringSafe(" url" ) ? : " " ,
194
- youtubeId = doc.getStringSafe(" youtubeId" ) ? : " " ,
208
+ embedUrl = doc.getStringSafe(" embedUrl" ),
209
+ url = doc.getStringSafe(" url" ),
210
+ youtubeId = doc.getStringSafe(" youtubeId" ),
195
211
images = doc.get(" images" , Document ::class .java)?.let { documentToImages(it) } ? : Images ()
196
212
)
197
213
}
198
-
199
-
200
- fun List<Document>.toMoreInfoEntity (): List <MoreInfoEntity > {
201
- val json = Json { ignoreUnknownKeys = true }
202
- val jsonStrings = map { it.toJson() }
203
- return jsonStrings.map { json.decodeFromString<MoreInfoEntity >(it) }
204
- }
205
-
0 commit comments