-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add possibility to have watchlist for a user
- Loading branch information
Showing
31 changed files
with
1,336 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
138 changes: 138 additions & 0 deletions
138
src/main/kotlin/fr/shikkanime/controllers/api/MemberController.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
package fr.shikkanime.controllers.api | ||
|
||
import com.google.inject.Inject | ||
import fr.shikkanime.converters.AbstractConverter | ||
import fr.shikkanime.dtos.AllFollowedEpisodeDto | ||
import fr.shikkanime.dtos.GenericDto | ||
import fr.shikkanime.dtos.MemberDto | ||
import fr.shikkanime.services.MemberFollowAnimeService | ||
import fr.shikkanime.services.MemberFollowEpisodeService | ||
import fr.shikkanime.services.MemberService | ||
import fr.shikkanime.utils.StringUtils | ||
import fr.shikkanime.utils.routes.* | ||
import fr.shikkanime.utils.routes.method.Delete | ||
import fr.shikkanime.utils.routes.method.Post | ||
import fr.shikkanime.utils.routes.method.Put | ||
import fr.shikkanime.utils.routes.openapi.OpenAPI | ||
import fr.shikkanime.utils.routes.openapi.OpenAPIResponse | ||
import fr.shikkanime.utils.routes.param.BodyParam | ||
import java.util.* | ||
|
||
@Controller("/api/v1/members") | ||
class MemberController { | ||
@Inject | ||
private lateinit var memberService: MemberService | ||
|
||
@Inject | ||
private lateinit var memberFollowAnimeService: MemberFollowAnimeService | ||
|
||
@Inject | ||
private lateinit var memberFollowEpisodeService: MemberFollowEpisodeService | ||
|
||
@Path("/private-register") | ||
@Post | ||
@OpenAPI( | ||
description = "Register a private member", | ||
responses = [ | ||
OpenAPIResponse(201, "Private member registered", Map::class), | ||
] | ||
) | ||
private fun registerPrivateMember(): Response { | ||
var identifier: String | ||
|
||
do { | ||
identifier = StringUtils.generateRandomString(12) | ||
} while (memberService.findPrivateMember(identifier) != null) | ||
|
||
memberService.savePrivateMember(identifier) | ||
return Response.created(mapOf("identifier" to identifier)) | ||
} | ||
|
||
@Path("/private-login") | ||
@Post | ||
@OpenAPI( | ||
description = "Login a private member", | ||
responses = [ | ||
OpenAPIResponse(200, "Private member logged in"), | ||
] | ||
) | ||
private fun loginPrivateMember(@BodyParam identifier: String): Response { | ||
val privateMember = memberService.findPrivateMember(identifier) ?: return Response.notFound() | ||
return Response.ok(AbstractConverter.convert(privateMember, MemberDto::class.java)) | ||
} | ||
|
||
@Path("/animes") | ||
@Put | ||
@JWTAuthenticated | ||
@OpenAPI( | ||
description = "Follow an anime", | ||
responses = [ | ||
OpenAPIResponse(200, "Anime followed successfully"), | ||
OpenAPIResponse(401, "Unauthorized") | ||
], | ||
security = true | ||
) | ||
private fun followAnime(@JWTUser uuidUser: UUID, @BodyParam anime: GenericDto): Response { | ||
return memberFollowAnimeService.follow(uuidUser, anime) | ||
} | ||
|
||
@Path("/animes") | ||
@Delete | ||
@JWTAuthenticated | ||
@OpenAPI( | ||
description = "Unfollow an anime", | ||
responses = [ | ||
OpenAPIResponse(200, "Anime unfollowed successfully"), | ||
OpenAPIResponse(401, "Unauthorized") | ||
], | ||
security = true | ||
) | ||
private fun unfollowAnime(@JWTUser uuidUser: UUID, @BodyParam anime: GenericDto): Response { | ||
return memberFollowAnimeService.unfollow(uuidUser, anime) | ||
} | ||
|
||
@Path("/follow-all-episodes") | ||
@Put | ||
@JWTAuthenticated | ||
@OpenAPI( | ||
description = "Follow all episodes of an anime", | ||
responses = [ | ||
OpenAPIResponse(200, "Episodes followed successfully", AllFollowedEpisodeDto::class), | ||
OpenAPIResponse(401, "Unauthorized") | ||
], | ||
security = true | ||
) | ||
private fun followAllEpisodes(@JWTUser uuidUser: UUID, @BodyParam anime: GenericDto): Response { | ||
return memberFollowEpisodeService.followAll(uuidUser, anime) | ||
} | ||
|
||
@Path("/episodes") | ||
@Put | ||
@JWTAuthenticated | ||
@OpenAPI( | ||
description = "Follow an episode", | ||
responses = [ | ||
OpenAPIResponse(200, "Episode followed successfully"), | ||
OpenAPIResponse(401, "Unauthorized") | ||
], | ||
security = true | ||
) | ||
private fun followEpisode(@JWTUser uuidUser: UUID, @BodyParam episode: GenericDto): Response { | ||
return memberFollowEpisodeService.follow(uuidUser, episode) | ||
} | ||
|
||
@Path("/episodes") | ||
@Delete | ||
@JWTAuthenticated | ||
@OpenAPI( | ||
description = "Unfollow an episode", | ||
responses = [ | ||
OpenAPIResponse(200, "Episode unfollowed successfully"), | ||
OpenAPIResponse(401, "Unauthorized") | ||
], | ||
security = true | ||
) | ||
private fun unfollowEpisode(@JWTUser uuidUser: UUID, @BodyParam episode: GenericDto): Response { | ||
return memberFollowEpisodeService.unfollow(uuidUser, episode) | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
src/main/kotlin/fr/shikkanime/converters/member/MemberToMemberDtoConverter.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package fr.shikkanime.converters.member | ||
|
||
import com.google.inject.Inject | ||
import fr.shikkanime.converters.AbstractConverter | ||
import fr.shikkanime.dtos.MemberDto | ||
import fr.shikkanime.dtos.TokenDto | ||
import fr.shikkanime.entities.Member | ||
import fr.shikkanime.services.MemberFollowAnimeService | ||
import fr.shikkanime.services.MemberFollowEpisodeService | ||
import fr.shikkanime.utils.withUTCString | ||
|
||
class MemberToMemberDtoConverter : AbstractConverter<Member, MemberDto>() { | ||
@Inject | ||
private lateinit var memberFollowAnimeService: MemberFollowAnimeService | ||
|
||
@Inject | ||
private lateinit var memberFollowEpisodeService: MemberFollowEpisodeService | ||
|
||
override fun convert(from: Member): MemberDto { | ||
val tokenDto = convert(from, TokenDto::class.java) | ||
val followedEpisodes = memberFollowEpisodeService.getAllFollowedEpisodes(from) | ||
|
||
return MemberDto( | ||
uuid = from.uuid!!, | ||
token = tokenDto.token!!, | ||
creationDateTime = from.creationDateTime.withUTCString(), | ||
lastUpdateDateTime = from.lastUpdateDateTime.withUTCString(), | ||
isPrivate = from.isPrivate, | ||
followedAnimes = memberFollowAnimeService.getAllFollowedAnimes(from).mapNotNull { it.uuid }, | ||
followedEpisodes = followedEpisodes.mapNotNull { it.uuid }, | ||
totalDuration = followedEpisodes.sumOf { it.duration } | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package fr.shikkanime.dtos | ||
|
||
import java.util.* | ||
|
||
data class AllFollowedEpisodeDto( | ||
val data: Set<UUID>, | ||
val duration: Long, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package fr.shikkanime.dtos | ||
|
||
import java.io.Serializable | ||
import java.util.* | ||
|
||
data class GenericDto( | ||
val uuid: UUID, | ||
) : Serializable |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package fr.shikkanime.dtos | ||
|
||
import java.util.* | ||
|
||
data class MemberDto( | ||
val uuid: UUID, | ||
val token: String, | ||
val creationDateTime: String, | ||
val lastUpdateDateTime: String, | ||
val isPrivate: Boolean, | ||
val followedAnimes: List<UUID>, | ||
val followedEpisodes: List<UUID>, | ||
val totalDuration: Long, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
src/main/kotlin/fr/shikkanime/entities/MemberFollowAnime.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package fr.shikkanime.entities | ||
|
||
import jakarta.persistence.* | ||
import org.hibernate.annotations.Cache | ||
import org.hibernate.annotations.CacheConcurrencyStrategy | ||
import java.time.ZonedDateTime | ||
import java.util.* | ||
|
||
@Entity | ||
@Table(name = "member_follow_anime") | ||
@Cacheable | ||
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE) | ||
class MemberFollowAnime( | ||
override val uuid: UUID? = null, | ||
@Column(nullable = false, name = "follow_date_time") | ||
val followDateTime: ZonedDateTime = ZonedDateTime.now(), | ||
@ManyToOne(optional = false) | ||
val member: Member? = null, | ||
@ManyToOne(optional = false) | ||
val anime: Anime? = null, | ||
) : ShikkEntity(uuid) |
21 changes: 21 additions & 0 deletions
21
src/main/kotlin/fr/shikkanime/entities/MemberFollowEpisode.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package fr.shikkanime.entities | ||
|
||
import jakarta.persistence.* | ||
import org.hibernate.annotations.Cache | ||
import org.hibernate.annotations.CacheConcurrencyStrategy | ||
import java.time.ZonedDateTime | ||
import java.util.* | ||
|
||
@Entity | ||
@Table(name = "member_follow_episode") | ||
@Cacheable | ||
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE) | ||
class MemberFollowEpisode( | ||
override val uuid: UUID? = null, | ||
@Column(nullable = false, name = "follow_date_time") | ||
val followDateTime: ZonedDateTime = ZonedDateTime.now(), | ||
@ManyToOne(optional = false) | ||
val member: Member? = null, | ||
@ManyToOne(optional = false) | ||
val episode: EpisodeMapping? = null, | ||
) : ShikkEntity(uuid) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.