Skip to content

Commit 1976c82

Browse files
committed
Improve directory response, reduce verbose log and refactoring
1 parent d411542 commit 1976c82

File tree

7 files changed

+153
-106
lines changed

7 files changed

+153
-106
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.jeluchu.core.extensions
2+
3+
import io.ktor.http.*
4+
import io.ktor.server.routing.*
5+
import kotlinx.coroutines.Dispatchers
6+
import kotlinx.coroutines.withContext
7+
8+
fun Route.getToJson(
9+
path: String,
10+
request: suspend RoutingContext.() -> Unit
11+
): Route = get(path) {
12+
call.response.headers.append(HttpHeaders.ContentType, ContentType.Application.Json.toString())
13+
withContext(Dispatchers.IO) { request() }
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package com.jeluchu.core.utils
2+
3+
object Routes {
4+
const val DIRECTORY = "/directory"
5+
const val ANIME_DETAILS = "/anime/{id}"
6+
}

src/main/kotlin/com/jeluchu/features/anime/mappers/AnimeMappers.kt

+82-74
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,25 @@ package com.jeluchu.features.anime.mappers
22

33
import com.example.models.*
44
import com.jeluchu.core.extensions.*
5-
import kotlinx.serialization.json.Json
5+
import com.jeluchu.features.anime.models.directory.AnimeDirectoryEntity
66
import org.bson.Document
77

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+
824
fun documentToMoreInfoEntity(doc: Document): MoreInfoEntity {
925
return MoreInfoEntity(
1026
id = doc.getLongSafe("id"),
@@ -14,138 +30,138 @@ fun documentToMoreInfoEntity(doc: Document): MoreInfoEntity {
1430
cover = doc.getStringSafe("cover"),
1531
genres = doc.getListSafe<String>("genres"),
1632
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"),
2541
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"),
3147
aired = doc.getDocumentSafe("aired")?.let { documentToAiringTime(it) } ?: AiringTime(),
3248
broadcast = doc.getDocumentSafe("broadcast")?.let { documentToAnimeBroadcast(it) } ?: AnimeBroadcast(),
33-
season = doc.getStringSafe("season") ?: "",
49+
season = doc.getStringSafe("season"),
3450
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) },
4056
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) },
4258
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")
4561
)
4662
}
4763

4864
fun documentToActor(doc: Document): Actor {
4965
return Actor(
5066
person = doc.getDocumentSafe("person")?.let { documentToIndividual(it) } ?: Individual(),
51-
language = doc.getStringSafe("language") ?: ""
67+
language = doc.getStringSafe("language")
5268
)
5369
}
5470

5571
fun documentToAiringTime(doc: Document): AiringTime {
5672
return AiringTime(
57-
from = doc.getStringSafe("from") ?: "",
58-
to = doc.getStringSafe("to") ?: ""
73+
from = doc.getStringSafe("from"),
74+
to = doc.getStringSafe("to")
5975
)
6076
}
6177

6278
fun documentToAlternativeTitles(doc: Document): AlternativeTitles {
6379
return AlternativeTitles(
64-
title = doc.getStringSafe("title") ?: "",
65-
type = doc.getStringSafe("type") ?: ""
80+
title = doc.getStringSafe("title"),
81+
type = doc.getStringSafe("type")
6682
)
6783
}
6884

6985
fun documentToAnimeBroadcast(doc: Document): AnimeBroadcast {
7086
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")
7490
)
7591
}
7692

7793
fun documentToAnimeSource(doc: Document): AnimeSource {
7894
return AnimeSource(
79-
id = doc.getStringSafe("id") ?: "",
80-
source = doc.getStringSafe("source") ?: ""
95+
id = doc.getStringSafe("id"),
96+
source = doc.getStringSafe("source")
8197
)
8298
}
8399

84100
fun documentToCharacter(doc: Document): Character {
85101
return Character(
86102
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) }
89105
)
90106
}
91107

92108
fun documentToCompanies(doc: Document): Companies {
93109
return Companies(
94110
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")
98114
)
99115
}
100116

101117
fun documentToExternalLinks(doc: Document): ExternalLinks {
102118
return ExternalLinks(
103-
url = doc.getStringSafe("url") ?: "",
104-
name = doc.getStringSafe("name") ?: ""
119+
url = doc.getStringSafe("url"),
120+
name = doc.getStringSafe("name")
105121
)
106122
}
107123

108124
fun documentToImageMediaEntity(doc: Document): ImageMediaEntity {
109125
return ImageMediaEntity(
110-
media = doc.getStringSafe("media") ?: "",
111-
thumbnail = doc.getStringSafe("thumbnail") ?: "",
126+
media = doc.getStringSafe("media"),
127+
thumbnail = doc.getStringSafe("thumbnail"),
112128
width = doc.getIntSafe("width", 0),
113129
height = doc.getIntSafe("height", 0),
114-
url = doc.getStringSafe("url") ?: ""
130+
url = doc.getStringSafe("url")
115131
)
116132
}
117133

118134
fun documentToImages(doc: Document): Images {
119135
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")
125141
)
126142
}
127143

128144
fun documentToIndividual(doc: Document): Individual {
129145
return Individual(
130146
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")
134150
)
135151
}
136152

137153
fun documentToMergedEpisode(doc: Document): MergedEpisode {
138154
return MergedEpisode(
139155
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")
142158
)
143159
}
144160

145161
fun documentToRelated(doc: Document): Related {
146162
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")
149165
)
150166
}
151167

@@ -164,19 +180,19 @@ fun documentToScore(doc: Document): Score {
164180
fun documentToStaff(doc: Document): Staff {
165181
return Staff(
166182
person = doc.get("person", Document::class.java)?.let { documentToIndividual(it) } ?: Individual(),
167-
positions = doc.getListSafe<String>("positions") ?: emptyList()
183+
positions = doc.getListSafe<String>("positions")
168184
)
169185
}
170186

171187
fun documentToStatistics(doc: Document): Statistics {
172188
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")
180196
)
181197
}
182198

@@ -189,17 +205,9 @@ fun documentToThemes(doc: Document): Themes {
189205

190206
fun documentToVideoPromo(doc: Document): VideoPromo {
191207
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"),
195211
images = doc.get("images", Document::class.java)?.let { documentToImages(it) } ?: Images()
196212
)
197213
}
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-
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.jeluchu.features.anime.models.directory
2+
3+
import kotlinx.serialization.Serializable
4+
5+
@Serializable
6+
data class AnimeDirectoryEntity(
7+
val rank: Int = 0,
8+
val year: Int = 0,
9+
var malId: Int = 0,
10+
val url: String = "",
11+
var type: String = "",
12+
var score: String = "",
13+
var title: String = "",
14+
var status: String = "",
15+
val season: String = "",
16+
var poster: String = "",
17+
var episodesCount: Int = 0,
18+
val airing: Boolean = false,
19+
var genres: List<String> = emptyList()
20+
)
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.jeluchu.features.anime.routes
22

3+
import com.jeluchu.core.extensions.getToJson
4+
import com.jeluchu.core.utils.Routes
35
import com.jeluchu.features.anime.services.AnimeService
46
import com.mongodb.client.MongoDatabase
57
import io.ktor.server.routing.*
@@ -8,6 +10,6 @@ fun Route.animeEndpoints(
810
mongoDatabase: MongoDatabase,
911
service: AnimeService = AnimeService(mongoDatabase)
1012
) {
11-
get("/directory") { service.getDirectory(call) }
12-
get("/anime/{id}") { service.getAnimeByMalId(call) }
13+
getToJson(Routes.DIRECTORY) { service.getDirectory(call) }
14+
getToJson(Routes.ANIME_DETAILS) { service.getAnimeByMalId(call) }
1315
}

0 commit comments

Comments
 (0)