Skip to content
This repository has been archived by the owner on Mar 28, 2024. It is now read-only.

Commit

Permalink
Refactor "Ayane" to "Calendar" and add recommendation dto
Browse files Browse the repository at this point in the history
  • Loading branch information
Ziedelth committed Nov 23, 2023
1 parent 7ac842c commit a9b9db3
Show file tree
Hide file tree
Showing 8 changed files with 31 additions and 21 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package fr.ziedelth.controllers

import com.google.inject.Inject
import fr.ziedelth.dtos.AyaneDto
import fr.ziedelth.events.AyaneReleaseEvent
import fr.ziedelth.dtos.CalendarDto
import fr.ziedelth.events.CalendarReleaseEvent
import fr.ziedelth.events.RecommendationEvent
import fr.ziedelth.repositories.AnimeRepository
import fr.ziedelth.services.RecommendationService
Expand All @@ -15,7 +15,7 @@ import fr.ziedelth.utils.routes.method.Post
import io.ktor.http.*
import java.time.Duration

class AyaneController : AbstractController<AyaneDto>("/ayane") {
class CalendarController : AbstractController<CalendarDto>("/calendar") {
@Inject
private lateinit var animeRepository: AnimeRepository

Expand All @@ -25,20 +25,20 @@ class AyaneController : AbstractController<AyaneDto>("/ayane") {
@Path
@Post
@Authorized
private fun save(@BodyParam ayaneDto: AyaneDto): Response {
if (ayaneDto.message.isBlank() || ayaneDto.images.isEmpty()) {
private fun save(@BodyParam calendarDto: CalendarDto): Response {
if (calendarDto.message.isBlank() || calendarDto.images.isEmpty()) {
return Response(HttpStatusCode.BadRequest, MISSING_PARAMETERS_MESSAGE_ERROR)
}

Thread {
PluginManager.callEvent(AyaneReleaseEvent(ayaneDto))
PluginManager.callEvent(CalendarReleaseEvent(calendarDto))
Thread.currentThread().join(Duration.ofHours(3).toMillis())

val randomAnime = animeRepository.getAll().random()
val recommendations = recommendationService.getRecommendations(listOf(randomAnime))
PluginManager.callEvent(RecommendationEvent(randomAnime, recommendations))
}.start()

return Response.created(ayaneDto)
return Response.created(calendarDto)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package fr.ziedelth.dtos

import java.io.Serializable

data class AyaneDto(
data class CalendarDto(
val message: String,
val images: List<String>
) : Serializable
8 changes: 8 additions & 0 deletions src/main/kotlin/fr/ziedelth/dtos/RecommendedAnimeDto.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package fr.ziedelth.dtos

import fr.ziedelth.entities.Anime

data class RecommendedAnimeDto(
val anime: Anime,
val score: Double,
)
6 changes: 0 additions & 6 deletions src/main/kotlin/fr/ziedelth/events/AyaneReleaseEvent.kt

This file was deleted.

6 changes: 6 additions & 0 deletions src/main/kotlin/fr/ziedelth/events/CalendarReleaseEvent.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package fr.ziedelth.events

import fr.ziedelth.dtos.CalendarDto
import fr.ziedelth.utils.plugins.events.Event

data class CalendarReleaseEvent(val calendarDto: CalendarDto) : Event
3 changes: 2 additions & 1 deletion src/main/kotlin/fr/ziedelth/events/RecommendationEvent.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package fr.ziedelth.events

import fr.ziedelth.dtos.RecommendedAnimeDto
import fr.ziedelth.entities.Anime
import fr.ziedelth.utils.plugins.events.Event

data class RecommendationEvent(val anime: Anime, val recommendations: Set<Pair<Anime, Double>>) : Event
data class RecommendationEvent(val anime: Anime, val recommendations: List<RecommendedAnimeDto>) : Event
4 changes: 2 additions & 2 deletions src/main/kotlin/fr/ziedelth/plugins/Routing.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import com.google.inject.Guice
import com.google.inject.Injector
import fr.ziedelth.controllers.AbstractController
import fr.ziedelth.controllers.AttachmentController
import fr.ziedelth.dtos.AyaneDto
import fr.ziedelth.dtos.CalendarDto
import fr.ziedelth.dtos.ProfileDto
import fr.ziedelth.entities.*
import fr.ziedelth.entities.Platform
Expand Down Expand Up @@ -271,7 +271,7 @@ private suspend fun callMethodWithParameters(
when (kParameter.type.javaType) {
Anime::class.java -> call.receive<Anime>()
Array<UUID>::class.java -> call.receive<Array<UUID>>()
AyaneDto::class.java -> call.receive<AyaneDto>()
CalendarDto::class.java -> call.receive<CalendarDto>()
Country::class.java -> call.receive<Country>()
Array<Episode>::class.java -> call.receive<Array<Episode>>()
EpisodeType::class.java -> call.receive<EpisodeType>()
Expand Down
9 changes: 5 additions & 4 deletions src/main/kotlin/fr/ziedelth/services/RecommendationService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package fr.ziedelth.services
import com.google.inject.Inject
import com.opencsv.CSVReader
import com.opencsv.CSVReaderBuilder
import fr.ziedelth.dtos.RecommendedAnimeDto
import fr.ziedelth.entities.Anime
import fr.ziedelth.entities.Genre
import fr.ziedelth.repositories.AnimeRepository
Expand All @@ -12,8 +13,8 @@ import java.util.*
class RecommendationService {
@Inject
private lateinit var animeRepository: AnimeRepository
fun getRecommendations(animes: List<Anime>): Set<Pair<Anime, Double>> {

fun getRecommendations(animes: List<Anime>): List<RecommendedAnimeDto> {
val csvFile = File("data/anime-genres-themes.csv")
val csvReader = CSVReaderBuilder(csvFile.reader()).withSkipLines(1).build()
val allGenres = mutableSetOf<String>()
Expand Down Expand Up @@ -41,8 +42,8 @@ class RecommendationService {
anime to matrixSum
}.sortedByDescending { it.second }.toMutableSet()
sortedRecommendations.removeIf { it.second == 0.0 }
return sortedRecommendations

return sortedRecommendations.map { RecommendedAnimeDto(it.first, it.second) }
}

private fun parseAnimeData(
Expand Down

0 comments on commit a9b9db3

Please sign in to comment.