Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

πŸ„ :: (Meogo-38) question crud #39

Merged
merged 8 commits into from
Sep 25, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class CommentController(
request: CommentRequest
) = createCommentService.execute(request)

@DeleteMapping
@DeleteMapping("/delete")
@ResponseStatus(HttpStatus.NO_CONTENT)
fun delete(@RequestParam(name = "comment_id")id: Long) =
deleteCommentService.execute(id)
meltapplee marked this conversation as resolved.
Show resolved Hide resolved
Expand Down
21 changes: 20 additions & 1 deletion src/main/kotlin/org/meogo/domain/question/domain/Question.kt
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -23,7 +27,22 @@ class Question(
@Column(nullable = false)
val date: LocalDateTime,

@Column(name = "school_id", nullable = false)
val schoolId: Int,
meltapplee marked this conversation as resolved.
Show resolved Hide resolved

@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"))!!
meltapplee marked this conversation as resolved.
Show resolved Hide resolved
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,11 @@ package org.meogo.domain.question.domain
import org.springframework.data.repository.Repository

interface QuestionRepository : Repository<Question, Long> {
fun save(question: Question)

fun findById(id: Long): Question?

fun findBySchoolId(schoolId: Int): List<Question>?
meltapplee marked this conversation as resolved.
Show resolved Hide resolved

fun deleteById(id: Long)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package org.meogo.domain.question.enum

enum class QuestionType {
LIFE, ENTRANCE, FACILITIES, ETC
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,55 @@
package org.meogo.domain.question.presentation

class QuestionController
import lombok.RequiredArgsConstructor
meltapplee marked this conversation as resolved.
Show resolved Hide resolved
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

@RequiredArgsConstructor
meltapplee marked this conversation as resolved.
Show resolved Hide resolved
@RestController
@RequestMapping("/question")
class QuestionController(
private val createQuestionService: CreateQuestionService,
private val querySchoolQuestionService: QuerySchoolQuestionService,
private val modifyQuestionService: ModifyQuestionService,
private val deleteQuestionService: DeleteQuestionService
meltapplee marked this conversation as resolved.
Show resolved Hide resolved
) {
@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("/delete")
fun deleteQuestion(@RequestParam(name = "question_id")questionId: Long) =
deleteQuestionService.execute(questionId)
}
Original file line number Diff line number Diff line change
@@ -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
)
Original file line number Diff line number Diff line change
@@ -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
meltapplee marked this conversation as resolved.
Show resolved Hide resolved
)
Original file line number Diff line number Diff line change
@@ -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
)
meltapplee marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -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
)
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package org.meogo.domain.question.service

import org.meogo.domain.question.domain.QuestionRepository
import org.meogo.domain.question.exception.QuestionNotFoundException
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) ?: throw QuestionNotFoundException

if (user != question.user) throw UserMisMatchException

questionRepository.deleteById(question.id)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package org.meogo.domain.question.service

import org.meogo.domain.question.domain.QuestionRepository
import org.meogo.domain.question.exception.QuestionNotFoundException
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) ?: throw QuestionNotFoundException

if (user != question.user) throw UserMisMatchException

questionRepository.save(
question.modify(request.content, request.questionType)
)
}
}
Original file line number Diff line number Diff line change
@@ -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<QuerySchoolQuestionResponse> {
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
)
} ?: emptyList()
}
meltapplee marked this conversation as resolved.
Show resolved Hide resolved
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.meogo.domain.review.presentation.dto.response

data class ReviewPictureResponse(
val year: Int,
val image: String
)
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}

Expand Down
Loading