diff --git a/src/main/kotlin/org/meogo/domain/comment/presentation/CommentController.kt b/src/main/kotlin/org/meogo/domain/comment/presentation/CommentController.kt index be44918..b6561f7 100644 --- a/src/main/kotlin/org/meogo/domain/comment/presentation/CommentController.kt +++ b/src/main/kotlin/org/meogo/domain/comment/presentation/CommentController.kt @@ -1,6 +1,5 @@ package org.meogo.domain.comment.presentation -import lombok.RequiredArgsConstructor import org.meogo.domain.comment.presentation.dto.request.CommentRequest import org.meogo.domain.comment.service.CreateCommentService import org.meogo.domain.comment.service.DeleteCommentService @@ -14,7 +13,6 @@ import org.springframework.web.bind.annotation.ResponseStatus import org.springframework.web.bind.annotation.RestController import javax.validation.Valid -@RequiredArgsConstructor @RestController @RequestMapping("/comment") class CommentController( @@ -28,7 +26,7 @@ class CommentController( request: CommentRequest ) = createCommentService.execute(request) - @DeleteMapping + @DeleteMapping() @ResponseStatus(HttpStatus.NO_CONTENT) fun delete(@RequestParam(name = "comment_id")id: Long) = deleteCommentService.execute(id) diff --git a/src/main/kotlin/org/meogo/domain/post/presentation/PostController.kt b/src/main/kotlin/org/meogo/domain/post/presentation/PostController.kt index 4415d50..35adf95 100644 --- a/src/main/kotlin/org/meogo/domain/post/presentation/PostController.kt +++ b/src/main/kotlin/org/meogo/domain/post/presentation/PostController.kt @@ -45,7 +45,7 @@ class PostController( ) = createPostService.execute(request, image) - @DeleteMapping("/delete") + @DeleteMapping() @ResponseStatus(HttpStatus.NO_CONTENT) fun delete(@RequestParam("post_id") id: Long) = deletePostService.execute(id) diff --git a/src/main/kotlin/org/meogo/domain/question/domain/Question.kt b/src/main/kotlin/org/meogo/domain/question/domain/Question.kt index 1c02474..729b808 100644 --- a/src/main/kotlin/org/meogo/domain/question/domain/Question.kt +++ b/src/main/kotlin/org/meogo/domain/question/domain/Question.kt @@ -1,9 +1,13 @@ package org.meogo.domain.question.domain +import org.meogo.domain.question.enum.QuestionType import org.meogo.domain.user.domain.User import java.time.LocalDateTime +import java.time.format.DateTimeFormatter import javax.persistence.Column import javax.persistence.Entity +import javax.persistence.EnumType +import javax.persistence.Enumerated import javax.persistence.FetchType import javax.persistence.GeneratedValue import javax.persistence.GenerationType @@ -23,7 +27,22 @@ class Question( @Column(nullable = false) val date: LocalDateTime, + @Column(name = "school_id", nullable = false) + val schoolId: Int, + + @Enumerated(EnumType.STRING) + @Column(name = "question_type", nullable = false) + var questionType: QuestionType, + @ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name = "user_id", nullable = false) val user: User -) +) { + fun modify(content: String, questionType: QuestionType): Question { + this.content = content + this.questionType = questionType + return this + } + + fun format(date: LocalDateTime) = date.format(DateTimeFormatter.ofPattern("yy.MM.dd HH:mm"))!! +} diff --git a/src/main/kotlin/org/meogo/domain/question/domain/QuestionRepository.kt b/src/main/kotlin/org/meogo/domain/question/domain/QuestionRepository.kt index 91f83eb..bf89229 100644 --- a/src/main/kotlin/org/meogo/domain/question/domain/QuestionRepository.kt +++ b/src/main/kotlin/org/meogo/domain/question/domain/QuestionRepository.kt @@ -3,5 +3,11 @@ package org.meogo.domain.question.domain import org.springframework.data.repository.Repository interface QuestionRepository : Repository { - fun findById(id: Long): Question? + fun save(question: Question) + + fun findById(id: Long): Question + + fun findBySchoolId(schoolId: Int): List + + fun deleteById(id: Long) } diff --git a/src/main/kotlin/org/meogo/domain/question/enum/QuestionType.kt b/src/main/kotlin/org/meogo/domain/question/enum/QuestionType.kt new file mode 100644 index 0000000..f3cc110 --- /dev/null +++ b/src/main/kotlin/org/meogo/domain/question/enum/QuestionType.kt @@ -0,0 +1,5 @@ +package org.meogo.domain.question.enum + +enum class QuestionType { + LIFE, ENTRANCE, FACILITIES, ETC +} diff --git a/src/main/kotlin/org/meogo/domain/question/presentation/QuestionController.kt b/src/main/kotlin/org/meogo/domain/question/presentation/QuestionController.kt index 3c7e24e..e433fb0 100644 --- a/src/main/kotlin/org/meogo/domain/question/presentation/QuestionController.kt +++ b/src/main/kotlin/org/meogo/domain/question/presentation/QuestionController.kt @@ -1,3 +1,53 @@ package org.meogo.domain.question.presentation -class QuestionController +import org.meogo.domain.question.presentation.dto.request.ModifyQuestionRequest +import org.meogo.domain.question.presentation.dto.request.QuestionRequest +import org.meogo.domain.question.service.CreateQuestionService +import org.meogo.domain.question.service.DeleteQuestionService +import org.meogo.domain.question.service.ModifyQuestionService +import org.meogo.domain.question.service.QuerySchoolQuestionService +import org.springframework.http.HttpStatus +import org.springframework.web.bind.annotation.DeleteMapping +import org.springframework.web.bind.annotation.GetMapping +import org.springframework.web.bind.annotation.PatchMapping +import org.springframework.web.bind.annotation.PostMapping +import org.springframework.web.bind.annotation.RequestBody +import org.springframework.web.bind.annotation.RequestMapping +import org.springframework.web.bind.annotation.RequestParam +import org.springframework.web.bind.annotation.ResponseStatus +import org.springframework.web.bind.annotation.RestController +import javax.validation.Valid + +@RestController +@RequestMapping("/question") +class QuestionController( + private val createQuestionService: CreateQuestionService, + private val querySchoolQuestionService: QuerySchoolQuestionService, + private val modifyQuestionService: ModifyQuestionService, + private val deleteQuestionService: DeleteQuestionService +) { + @ResponseStatus(HttpStatus.CREATED) + @PostMapping + fun createQuestion( + @Valid @RequestBody + request: QuestionRequest + ) = createQuestionService.execute(request) + + @PatchMapping("/modify") + fun modifyQuestion( + @RequestParam(name = "question_id") + questionId: Long, + @Valid @RequestBody + request: ModifyQuestionRequest + ) = + modifyQuestionService.execute(questionId, request) + + @GetMapping("/query") + fun querySchoolQuestion(@RequestParam(name = "school_id")schoolId: Int) = + querySchoolQuestionService.execute(schoolId) + + @ResponseStatus(HttpStatus.NO_CONTENT) + @DeleteMapping() + fun deleteQuestion(@RequestParam(name = "question_id")questionId: Long) = + deleteQuestionService.execute(questionId) +} diff --git a/src/main/kotlin/org/meogo/domain/question/presentation/dto/request/ModifyQuestionRequest.kt b/src/main/kotlin/org/meogo/domain/question/presentation/dto/request/ModifyQuestionRequest.kt new file mode 100644 index 0000000..c9fab54 --- /dev/null +++ b/src/main/kotlin/org/meogo/domain/question/presentation/dto/request/ModifyQuestionRequest.kt @@ -0,0 +1,10 @@ +package org.meogo.domain.question.presentation.dto.request + +import org.meogo.domain.question.enum.QuestionType +import javax.validation.constraints.Size + +data class ModifyQuestionRequest( + @field: Size(min = 1, max = 100) + val content: String, + val questionType: QuestionType +) diff --git a/src/main/kotlin/org/meogo/domain/question/presentation/dto/request/QuestionRequest.kt b/src/main/kotlin/org/meogo/domain/question/presentation/dto/request/QuestionRequest.kt new file mode 100644 index 0000000..967279c --- /dev/null +++ b/src/main/kotlin/org/meogo/domain/question/presentation/dto/request/QuestionRequest.kt @@ -0,0 +1,11 @@ +package org.meogo.domain.question.presentation.dto.request + +import org.meogo.domain.question.enum.QuestionType +import javax.validation.constraints.Size + +data class QuestionRequest( + val schoolId: Int, + @field: Size(min = 1, max = 100) + val content: String, + val questionType: QuestionType +) diff --git a/src/main/kotlin/org/meogo/domain/question/presentation/dto/response/QuerySchoolQuestionResponse.kt b/src/main/kotlin/org/meogo/domain/question/presentation/dto/response/QuerySchoolQuestionResponse.kt new file mode 100644 index 0000000..7f7790d --- /dev/null +++ b/src/main/kotlin/org/meogo/domain/question/presentation/dto/response/QuerySchoolQuestionResponse.kt @@ -0,0 +1,11 @@ +package org.meogo.domain.question.presentation.dto.response + +import org.meogo.domain.question.enum.QuestionType + +data class QuerySchoolQuestionResponse( + val id: Long, + val content: String, + val date: String, + val questionType: QuestionType, + val name: String +) diff --git a/src/main/kotlin/org/meogo/domain/question/service/CreateQuestionService.kt b/src/main/kotlin/org/meogo/domain/question/service/CreateQuestionService.kt new file mode 100644 index 0000000..ed6204b --- /dev/null +++ b/src/main/kotlin/org/meogo/domain/question/service/CreateQuestionService.kt @@ -0,0 +1,32 @@ +package org.meogo.domain.question.service + +import org.meogo.domain.question.domain.Question +import org.meogo.domain.question.domain.QuestionRepository +import org.meogo.domain.question.presentation.dto.request.QuestionRequest +import org.meogo.domain.user.exception.UserNotFoundException +import org.meogo.domain.user.facade.UserFacade +import org.springframework.stereotype.Service +import org.springframework.transaction.annotation.Transactional +import java.time.LocalDateTime + +@Service +class CreateQuestionService( + private val questionRepository: QuestionRepository, + private val userFacade: UserFacade +) { + + @Transactional + fun execute(request: QuestionRequest) { + val user = userFacade.currentUser() ?: throw UserNotFoundException + + questionRepository.save( + Question( + content = request.content, + date = LocalDateTime.now(), + schoolId = request.schoolId, + questionType = request.questionType, + user = user + ) + ) + } +} diff --git a/src/main/kotlin/org/meogo/domain/question/service/DeleteQuestionService.kt b/src/main/kotlin/org/meogo/domain/question/service/DeleteQuestionService.kt new file mode 100644 index 0000000..8e61359 --- /dev/null +++ b/src/main/kotlin/org/meogo/domain/question/service/DeleteQuestionService.kt @@ -0,0 +1,25 @@ +package org.meogo.domain.question.service + +import org.meogo.domain.question.domain.QuestionRepository +import org.meogo.domain.user.exception.UserMisMatchException +import org.meogo.domain.user.exception.UserNotFoundException +import org.meogo.domain.user.facade.UserFacade +import org.springframework.stereotype.Service +import org.springframework.transaction.annotation.Transactional + +@Service +class DeleteQuestionService( + private val questionRepository: QuestionRepository, + private val userFacade: UserFacade +) { + + @Transactional + fun execute(questionId: Long) { + val user = userFacade.currentUser() ?: throw UserNotFoundException + val question = questionRepository.findById(questionId) + + if (user != question.user) throw UserMisMatchException + + questionRepository.deleteById(question.id) + } +} diff --git a/src/main/kotlin/org/meogo/domain/question/service/ModifyQuestionService.kt b/src/main/kotlin/org/meogo/domain/question/service/ModifyQuestionService.kt new file mode 100644 index 0000000..1b4111c --- /dev/null +++ b/src/main/kotlin/org/meogo/domain/question/service/ModifyQuestionService.kt @@ -0,0 +1,28 @@ +package org.meogo.domain.question.service + +import org.meogo.domain.question.domain.QuestionRepository +import org.meogo.domain.question.presentation.dto.request.ModifyQuestionRequest +import org.meogo.domain.user.exception.UserMisMatchException +import org.meogo.domain.user.exception.UserNotFoundException +import org.meogo.domain.user.facade.UserFacade +import org.springframework.stereotype.Service +import org.springframework.transaction.annotation.Transactional + +@Service +class ModifyQuestionService( + private val questionRepository: QuestionRepository, + private val userFacade: UserFacade +) { + + @Transactional + fun execute(questionId: Long, request: ModifyQuestionRequest) { + val user = userFacade.currentUser() ?: throw UserNotFoundException + val question = questionRepository.findById(questionId) + + if (user != question.user) throw UserMisMatchException + + questionRepository.save( + question.modify(request.content, request.questionType) + ) + } +} diff --git a/src/main/kotlin/org/meogo/domain/question/service/QuerySchoolQuestionService.kt b/src/main/kotlin/org/meogo/domain/question/service/QuerySchoolQuestionService.kt new file mode 100644 index 0000000..cc86026 --- /dev/null +++ b/src/main/kotlin/org/meogo/domain/question/service/QuerySchoolQuestionService.kt @@ -0,0 +1,27 @@ +package org.meogo.domain.question.service + +import org.meogo.domain.question.domain.QuestionRepository +import org.meogo.domain.question.presentation.dto.response.QuerySchoolQuestionResponse +import org.springframework.stereotype.Service +import org.springframework.transaction.annotation.Transactional + +@Service +class QuerySchoolQuestionService( + private val questionRepository: QuestionRepository +) { + + @Transactional(readOnly = true) + fun execute(schoolId: Int): List { + val questions = questionRepository.findBySchoolId(schoolId) + + return questions?.map { question -> + QuerySchoolQuestionResponse( + id = question.id, + content = question.content, + date = question.format(question.date), + questionType = question.questionType, + name = question.user.accountId + ) + }?.sortedByDescending { it.id } ?: emptyList() + } +} diff --git a/src/main/kotlin/org/meogo/domain/review/presentation/ReviewController.kt b/src/main/kotlin/org/meogo/domain/review/presentation/ReviewController.kt index 78f88a7..f9c8340 100644 --- a/src/main/kotlin/org/meogo/domain/review/presentation/ReviewController.kt +++ b/src/main/kotlin/org/meogo/domain/review/presentation/ReviewController.kt @@ -1,6 +1,5 @@ package org.meogo.domain.review.presentation -import lombok.RequiredArgsConstructor import org.meogo.domain.review.presentation.dto.request.ModifyReviewRequest import org.meogo.domain.review.presentation.dto.request.ReviewRequest import org.meogo.domain.review.service.CreateReviewService @@ -25,7 +24,6 @@ import org.springframework.web.multipart.MultipartFile import javax.validation.Valid @RestController -@RequiredArgsConstructor @RequestMapping("/review") class ReviewController( private val createReviewService: CreateReviewService, @@ -54,7 +52,7 @@ class ReviewController( fun modify(@RequestParam(name = "review_id") reviewId: Long, @RequestBody request: ModifyReviewRequest) = modifyReviewService.modifyReview(reviewId, request) - @DeleteMapping("/delete") + @DeleteMapping() @ResponseStatus(value = HttpStatus.NO_CONTENT) fun delete(@RequestParam(name = "review_id") reviewId: Long) = deleteReviewService.deleteReview(reviewId) diff --git a/src/main/kotlin/org/meogo/domain/review/presentation/dto/response/ReviewPictureResponse.kt b/src/main/kotlin/org/meogo/domain/review/presentation/dto/response/ReviewPictureResponse.kt index bdfd271..40f97a8 100644 --- a/src/main/kotlin/org/meogo/domain/review/presentation/dto/response/ReviewPictureResponse.kt +++ b/src/main/kotlin/org/meogo/domain/review/presentation/dto/response/ReviewPictureResponse.kt @@ -1,5 +1,6 @@ package org.meogo.domain.review.presentation.dto.response data class ReviewPictureResponse( + val year: Int, val image: String ) diff --git a/src/main/kotlin/org/meogo/domain/review/service/QueryReviewPictureService.kt b/src/main/kotlin/org/meogo/domain/review/service/QueryReviewPictureService.kt index 4d88210..d471daa 100644 --- a/src/main/kotlin/org/meogo/domain/review/service/QueryReviewPictureService.kt +++ b/src/main/kotlin/org/meogo/domain/review/service/QueryReviewPictureService.kt @@ -19,9 +19,10 @@ class QueryReviewPictureService( val pictures = reviews .flatMap { review -> + val year = review.date.year review.picture?.split(",")?.map { pic -> val pictureUrl = fileUtil.generateObjectUrl(pic.trim(), Path.REVIEW) - ReviewPictureResponse(pictureUrl) + ReviewPictureResponse(year, pictureUrl) } ?: emptyList() } diff --git a/src/main/kotlin/org/meogo/domain/user/presentation/UserController.kt b/src/main/kotlin/org/meogo/domain/user/presentation/UserController.kt index fd0b586..d63fb24 100644 --- a/src/main/kotlin/org/meogo/domain/user/presentation/UserController.kt +++ b/src/main/kotlin/org/meogo/domain/user/presentation/UserController.kt @@ -1,6 +1,5 @@ package org.meogo.domain.user.presentation -import lombok.RequiredArgsConstructor import org.meogo.domain.user.presentation.dto.request.UserCheckRequest import org.meogo.domain.user.presentation.dto.request.UserSignInRequest import org.meogo.domain.user.presentation.dto.request.UserSignUpRequest @@ -24,7 +23,6 @@ import org.springframework.web.multipart.MultipartFile import javax.validation.Valid @RestController -@RequiredArgsConstructor @RequestMapping("/user") class UserController( private val userSignUpService: UserSignUpService,