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
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.CitizenBasic
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> {
decode { values, _ ->
values.singleOrNull()?.let {

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@@ -1,6 +1,7 @@
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.citizenOrNull
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.security.assert
import io.ktor.application.call
import io.ktor.features.NotFoundException
import io.ktor.http.HttpStatusCode
import io.ktor.locations.KtorExperimentalLocationsAPI
import io.ktor.locations.Location
@@ -15,20 +17,23 @@ import io.ktor.locations.put
import io.ktor.request.receive
import io.ktor.response.respond
import io.ktor.routing.Route
import java.util.UUID
@KtorExperimentalLocationsAPI
object PutVoteOnArticle {
@Location("/articles/{article}/vote")
class ArticleVoteRequest(val article: ArticleForView) {
data class Content(var note: Int)
class ArticleVoteRequest(article: UUID) {
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> {
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(
target = it.article,
note = content.note,
target = article,
note = input.note,
createdBy = this.citizen
)
ac.assert { canCreate(vote, citizenOrNull) }

View File

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