Big refactoring #77

Merged
flecomte merged 166 commits from refactoring-component-and-immutable into master 2021-03-24 19:06:07 +01:00
10 changed files with 45 additions and 48 deletions
Showing only changes of commit aeaab860b2 - Show all commits

View File

@@ -1,8 +1,5 @@
package fr.dcproject.application package fr.dcproject.application
import fr.dcproject.component.article.ArticleForView
import fr.dcproject.component.article.ArticleRef
import fr.dcproject.component.article.ArticleRepository
import fr.dcproject.component.citizen.Citizen import fr.dcproject.component.citizen.Citizen
import fr.dcproject.component.citizen.CitizenBasic import fr.dcproject.component.citizen.CitizenBasic
import fr.dcproject.component.citizen.CitizenRef import fr.dcproject.component.citizen.CitizenRef
@@ -46,22 +43,6 @@ val converters: ConverterDeclaration = {
} }
} }
convert<ArticleForView> {
decode { values, _ ->
values.singleOrNull()?.let {
get<ArticleRepository>().findById(UUID.fromString(it))
?: throw NotFoundException("Article $values not found")
} ?: throw NotFoundException("Article $values not found")
}
}
convert<ArticleRef> {
decode { values, _ ->
values.singleOrNull()?.let {
ArticleRef(UUID.fromString(it))
} ?: throw NotFoundException("""UUID "$values" is not valid for Article""")
}
}
convert<CommentRef> { convert<CommentRef> {
decode { values, _ -> decode { values, _ ->
values.singleOrNull()?.let { values.singleOrNull()?.let {

View File

@@ -1,9 +1,10 @@
package fr.dcproject.component.comment.article.routes package fr.dcproject.component.comment.article.routes
import fr.dcproject.component.article.ArticleForView import fr.dcproject.component.article.ArticleRef
import fr.dcproject.component.auth.citizen import fr.dcproject.component.auth.citizen
import fr.dcproject.component.auth.citizenOrNull import fr.dcproject.component.auth.citizenOrNull
import fr.dcproject.component.comment.article.CommentArticleRepository import fr.dcproject.component.comment.article.CommentArticleRepository
import fr.dcproject.component.comment.article.routes.CreateCommentArticle.PostArticleCommentRequest.Input
import fr.dcproject.component.comment.generic.CommentAccessControl import fr.dcproject.component.comment.generic.CommentAccessControl
import fr.dcproject.component.comment.generic.CommentForUpdate import fr.dcproject.component.comment.generic.CommentForUpdate
import fr.dcproject.security.assert import fr.dcproject.security.assert
@@ -16,25 +17,23 @@ import io.ktor.locations.post
import io.ktor.request.receive import io.ktor.request.receive
import io.ktor.response.respond import io.ktor.response.respond
import io.ktor.routing.Route import io.ktor.routing.Route
import java.util.UUID
@KtorExperimentalLocationsAPI @KtorExperimentalLocationsAPI
object CreateCommentArticle { object CreateCommentArticle {
@Location("/articles/{article}/comments") @Location("/articles/{article}/comments")
class PostArticleCommentRequest( class PostArticleCommentRequest(article: UUID) {
val article: ArticleForView val article = ArticleRef(article)
) { class Input(val content: String)
class Comment( }
val content: String
)
suspend fun getComment(call: ApplicationCall) = call.receive<Comment>().run { suspend fun PostArticleCommentRequest.getComment(call: ApplicationCall) = call.receive<Input>().run {
CommentForUpdate( CommentForUpdate(
target = article, target = article,
createdBy = call.citizen, createdBy = call.citizen,
content = content content = content
) )
} }
}
fun Route.createCommentArticle(repo: CommentArticleRepository, ac: CommentAccessControl) { fun Route.createCommentArticle(repo: CommentArticleRepository, ac: CommentAccessControl) {
post<PostArticleCommentRequest> { post<PostArticleCommentRequest> {

View File

@@ -14,17 +14,19 @@ import io.ktor.locations.Location
import io.ktor.locations.get import io.ktor.locations.get
import io.ktor.response.respond import io.ktor.response.respond
import io.ktor.routing.Route import io.ktor.routing.Route
import java.util.UUID
@KtorExperimentalLocationsAPI @KtorExperimentalLocationsAPI
object GetArticleComments { object GetArticleComments {
@Location("/articles/{article}/comments") @Location("/articles/{article}/comments")
class ArticleCommentsRequest( class ArticleCommentsRequest(
val article: ArticleRef, article: UUID,
page: Int = 1, page: Int = 1,
limit: Int = 50, limit: Int = 50,
val search: String? = null, val search: String? = null,
sort: String = CommentArticleRepository.Sort.CREATED_AT.sql sort: String = CommentArticleRepository.Sort.CREATED_AT.sql
) : PaginatedRequestI by PaginatedRequest(page, limit) { ) : PaginatedRequestI by PaginatedRequest(page, limit) {
val article = ArticleRef(article)
val sort: CommentArticleRepository.Sort = CommentArticleRepository.Sort.fromString(sort) ?: CommentArticleRepository.Sort.CREATED_AT val sort: CommentArticleRepository.Sort = CommentArticleRepository.Sort.fromString(sort) ?: CommentArticleRepository.Sort.CREATED_AT
} }

View File

@@ -69,7 +69,7 @@ abstract class CommentRepositoryAbs<T : TargetI>(override var requester: Request
} }
} }
fun <I : T, C : CitizenRef> comment(comment: CommentForUpdate<I, C>) { fun <I : TargetI, C : CitizenRef> comment(comment: CommentForUpdate<I, C>) {
requester requester
.getFunction("comment") .getFunction("comment")
.sendQuery( .sendQuery(

View File

@@ -14,11 +14,14 @@ import io.ktor.locations.Location
import io.ktor.locations.post import io.ktor.locations.post
import io.ktor.response.respond import io.ktor.response.respond
import io.ktor.routing.Route import io.ktor.routing.Route
import java.util.UUID
@KtorExperimentalLocationsAPI @KtorExperimentalLocationsAPI
object FollowArticle { object FollowArticle {
@Location("/articles/{article}/follows") @Location("/articles/{article}/follows")
class ArticleFollowRequest(val article: ArticleRef) class ArticleFollowRequest(article: UUID) {
val article = ArticleRef(article)
}
fun Route.followArticle(repo: FollowArticleRepository, ac: FollowAccessControl) { fun Route.followArticle(repo: FollowArticleRepository, ac: FollowAccessControl) {
post<ArticleFollowRequest> { post<ArticleFollowRequest> {

View File

@@ -13,11 +13,14 @@ import io.ktor.locations.Location
import io.ktor.locations.get import io.ktor.locations.get
import io.ktor.response.respond import io.ktor.response.respond
import io.ktor.routing.Route import io.ktor.routing.Route
import java.util.UUID
@KtorExperimentalLocationsAPI @KtorExperimentalLocationsAPI
object GetFollowArticle { object GetFollowArticle {
@Location("/articles/{article}/follows") @Location("/articles/{article}/follows")
class ArticleFollowRequest(val article: ArticleRef) class ArticleFollowRequest(article: UUID) {
val article = ArticleRef(article)
}
fun Route.getFollowArticle(repo: FollowArticleRepository, ac: FollowAccessControl) { fun Route.getFollowArticle(repo: FollowArticleRepository, ac: FollowAccessControl) {
get<ArticleFollowRequest> { get<ArticleFollowRequest> {

View File

@@ -14,11 +14,14 @@ import io.ktor.locations.Location
import io.ktor.locations.delete import io.ktor.locations.delete
import io.ktor.response.respond import io.ktor.response.respond
import io.ktor.routing.Route import io.ktor.routing.Route
import java.util.UUID
@KtorExperimentalLocationsAPI @KtorExperimentalLocationsAPI
object UnfollowArticle { object UnfollowArticle {
@Location("/articles/{article}/follows") @Location("/articles/{article}/follows")
class ArticleFollowRequest(val article: ArticleRef) class ArticleFollowRequest(article: UUID) {
val article = ArticleRef(article)
}
fun Route.unfollowArticle(repo: FollowArticleRepository, ac: FollowAccessControl) { fun Route.unfollowArticle(repo: FollowArticleRepository, ac: FollowAccessControl) {
delete<ArticleFollowRequest> { delete<ArticleFollowRequest> {

View File

@@ -1,6 +1,6 @@
package fr.dcproject.component.opinion.routes package fr.dcproject.component.opinion.routes
import fr.dcproject.component.article.ArticleForView import fr.dcproject.component.article.ArticleRef
import fr.dcproject.component.auth.citizen import fr.dcproject.component.auth.citizen
import fr.dcproject.component.auth.citizenOrNull import fr.dcproject.component.auth.citizenOrNull
import fr.dcproject.component.opinion.OpinionAccessControl import fr.dcproject.component.opinion.OpinionAccessControl
@@ -25,7 +25,8 @@ object OpinionArticle {
* Put an opinion on one article * Put an opinion on one article
*/ */
@Location("/articles/{article}/opinions") @Location("/articles/{article}/opinions")
class ArticleOpinion(val article: ArticleForView) { class ArticleOpinion(article: UUID) {
val article = ArticleRef(article)
class Body(ids: List<String>) { class Body(ids: List<String>) {
val ids: List<UUID> = ids.map { it.toUUID() } val ids: List<UUID> = ids.map { it.toUUID() }
} }

View File

@@ -1,6 +1,7 @@
package fr.dcproject.component.vote.routes package fr.dcproject.component.vote.routes
import fr.dcproject.component.article.ArticleForView import fr.dcproject.component.article.ArticleRef
import fr.dcproject.component.article.ArticleRepository
import fr.dcproject.component.auth.citizen import fr.dcproject.component.auth.citizen
import fr.dcproject.component.auth.citizenOrNull import fr.dcproject.component.auth.citizenOrNull
import fr.dcproject.component.vote.VoteAccessControl import fr.dcproject.component.vote.VoteAccessControl
@@ -8,6 +9,7 @@ import fr.dcproject.component.vote.VoteArticleRepository
import fr.dcproject.component.vote.entity.VoteForUpdate import fr.dcproject.component.vote.entity.VoteForUpdate
import fr.dcproject.security.assert import fr.dcproject.security.assert
import io.ktor.application.call import io.ktor.application.call
import io.ktor.features.NotFoundException
import io.ktor.http.HttpStatusCode import io.ktor.http.HttpStatusCode
import io.ktor.locations.KtorExperimentalLocationsAPI import io.ktor.locations.KtorExperimentalLocationsAPI
import io.ktor.locations.Location import io.ktor.locations.Location
@@ -15,20 +17,23 @@ import io.ktor.locations.put
import io.ktor.request.receive import io.ktor.request.receive
import io.ktor.response.respond import io.ktor.response.respond
import io.ktor.routing.Route import io.ktor.routing.Route
import java.util.UUID
@KtorExperimentalLocationsAPI @KtorExperimentalLocationsAPI
object PutVoteOnArticle { object PutVoteOnArticle {
@Location("/articles/{article}/vote") @Location("/articles/{article}/vote")
class ArticleVoteRequest(val article: ArticleForView) { class ArticleVoteRequest(article: UUID) {
data class Content(var note: Int) val article = ArticleRef(article)
data class Input(var note: Int)
} }
fun Route.putVoteOnArticle(repo: VoteArticleRepository, ac: VoteAccessControl) { fun Route.putVoteOnArticle(repo: VoteArticleRepository, ac: VoteAccessControl, articleRepo: ArticleRepository) {
put<ArticleVoteRequest> { put<ArticleVoteRequest> {
val content = call.receive<ArticleVoteRequest.Content>() val input = call.receive<ArticleVoteRequest.Input>()
val article = articleRepo.findById(it.article.id) ?: throw NotFoundException("Article ${it.article.id} not found")
val vote = VoteForUpdate( val vote = VoteForUpdate(
target = it.article, target = article,
note = content.note, note = input.note,
createdBy = this.citizen createdBy = this.citizen
) )
ac.assert { canCreate(vote, citizenOrNull) } ac.assert { canCreate(vote, citizenOrNull) }

View File

@@ -15,7 +15,7 @@ fun Routing.installVoteRoutes() {
authenticate(optional = true) { authenticate(optional = true) {
getCitizenVote(get(), get()) getCitizenVote(get(), get())
getCitizenVotesOnArticle(get(), get()) getCitizenVotesOnArticle(get(), get())
putVoteOnArticle(get(), get()) putVoteOnArticle(get(), get(), get())
putVoteOnComment(get(), get(), get()) putVoteOnComment(get(), get(), get())
voteConstitution(get(), get()) voteConstitution(get(), get())
} }