Skip to content

Commit

Permalink
Merge branch 'dev' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
phjppo0918 committed Nov 20, 2023
2 parents e1b0fca + b1735ea commit d2c098c
Show file tree
Hide file tree
Showing 23 changed files with 360 additions and 80 deletions.
2 changes: 2 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,9 @@ koverReport {
classes("**.QboxServerApplicationKt")
classes("**.**Config")
classes("**.**Query")
classes("**.local.**")
classes("site.qbox.qboxserver.domain.member.email.svc.MailSvc")
classes("site.qbox.qboxserver.domain.image.local.LocalStorageImageRepo")
classes("**.dto.**")
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package site.qbox.qboxserver.domain.image

import org.springframework.web.multipart.MultipartFile

interface ImageRepo {
fun save(file: MultipartFile, name: String) : String
}
23 changes: 23 additions & 0 deletions src/main/kotlin/site/qbox/qboxserver/domain/image/ImageSvc.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package site.qbox.qboxserver.domain.image

import org.springframework.web.multipart.MultipartFile
import site.qbox.qboxserver.domain.image.dto.ImageRes
import site.qbox.qboxserver.global.annotation.CommandService
import java.util.*

@CommandService
class ImageSvc(
private val imageRepo: ImageRepo,
private val imageValidator: ImageValidator,
) {
fun save(file: MultipartFile): ImageRes {
imageValidator.validate(file)
return ImageRes(imageRepo.save(file, generateName(file)))
}

private fun generateName(file: MultipartFile): String =
"${UUID.randomUUID()}.${extractExtension(file)}"

private fun extractExtension(file: MultipartFile): String =
file.name.substringAfterLast(".")
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package site.qbox.qboxserver.domain.image

import org.springframework.stereotype.Component
import org.springframework.web.multipart.MultipartFile
import site.qbox.qboxserver.domain.image.exception.NotAllowedImageException

@Component
class ImageValidator {
companion object {
private val ALLOW_EXTENSIONS = listOf("png", "jpg", "jpeg", "gif", "bmp", "webp", "ico", "tiff", "tif", "svg")
}

fun validate(file: MultipartFile) {
require(extractExtension(file) in ALLOW_EXTENSIONS) { throw NotAllowedImageException() }
}

private fun extractExtension(file: MultipartFile): String =
file.name.substringAfterLast(".")
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package site.qbox.qboxserver.domain.image.dto

data class ImageRes(val location: String)
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package site.qbox.qboxserver.domain.image.exception

class NotAllowedImageException : RuntimeException()
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package site.qbox.qboxserver.domain.image.local

import org.springframework.beans.factory.annotation.Value
import org.springframework.stereotype.Repository
import org.springframework.web.multipart.MultipartFile
import site.qbox.qboxserver.domain.image.ImageRepo
import java.io.File

@Repository
class LocalStorageImageRepo(
@Value("\${local-storage.directory}") private val localStorageDirectory: String,
@Value("\${local-storage.url}") private val localStorageUrl: String,
) : ImageRepo {
override fun save(file: MultipartFile, name: String): String {
file.transferTo(File(localStorageDirectory, name))
return "${localStorageUrl}/${name}"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import com.querydsl.core.types.dsl.StringPath
import site.qbox.qboxserver.domain.member.command.entity.QMember.member
import site.qbox.qboxserver.domain.member.query.dto.QMemberSummary

class MemberQuery {
interface MemberQuery {
companion object {
val id: StringPath = member.email.email
val summary = QMemberSummary(id, member.nickname)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,53 +2,58 @@ package site.qbox.qboxserver.domain.question.query

import com.querydsl.jpa.impl.JPAQueryFactory
import org.springframework.data.domain.Pageable
import site.qbox.qboxserver.domain.lecture.command.entity.LectureId
import site.qbox.qboxserver.domain.lecture.command.entity.QLecture.lecture
import site.qbox.qboxserver.domain.lecture.query.dto.QLectureRes
import site.qbox.qboxserver.domain.member.command.entity.QMember.member
import site.qbox.qboxserver.domain.member.query.MemberQuery
import site.qbox.qboxserver.domain.question.command.entity.QQuestion.question
import site.qbox.qboxserver.domain.question.query.dto.QQuestionRes
import site.qbox.qboxserver.domain.question.query.dto.QQuestionSummary
import site.qbox.qboxserver.domain.question.query.dto.QuestionRes
import site.qbox.qboxserver.domain.question.query.dto.QuestionSummary
import site.qbox.qboxserver.domain.question.query.dto.*
import site.qbox.qboxserver.global.annotation.QueryService

@QueryService
class QuestionDao (
class QuestionDao(
private val queryFactory: JPAQueryFactory,
){
fun findAllByLecture(code: String, depart: Long, pageable: Pageable): List<QuestionSummary> {
) {
fun findAllBy(condition: QuestionCondition, pageable: Pageable): List<QuestionSummary> {
return queryFactory
.select(QQuestionSummary(
question.id,
question.title,
MemberQuery.summary
))
.select(questionSummary())
.from(question)
.where(question.lecture.eq(LectureId(code, depart)))
.where(condition.toFilter())
.join(member).on(question.writerId.eq(MemberQuery.id))
.offset(pageable.offset)
.limit(pageable.pageSize.toLong())
.orderBy(question.createdAt.desc())
.fetch()
}

private fun questionSummary() = QQuestionSummary(
question.id,
question.title,
MemberQuery.summary,
question.createdAt,
)

fun getBy(id: Long): QuestionRes {
return queryFactory
.select(QQuestionRes(
question.id,
question.title,
question.body,
QLectureRes(
lecture.name,
lecture.id.code,
lecture.id.departId),
MemberQuery.summary
))
.select(questionRes())
.from(question)
.where(question.id.eq(id))
.join(member).on(question.writerId.eq(MemberQuery.id))
.join(lecture).on(question.lecture.eq(lecture.id))
.fetchFirst()
}

private fun questionRes() = QQuestionRes(
question.id,
question.title,
question.body,
QLectureRes(
lecture.name,
lecture.id.code,
lecture.id.departId
),
MemberQuery.summary,
question.createdAt,
question.updatedAt
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.PathVariable
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController
import site.qbox.qboxserver.domain.question.query.dto.QuestionCondition
import site.qbox.qboxserver.domain.question.query.dto.QuestionRes
import site.qbox.qboxserver.domain.question.query.dto.QuestionSummary

Expand All @@ -15,8 +16,8 @@ class QuestionQueryCtrl (
private val questionDao: QuestionDao,
){
@GetMapping
fun getAllByLecture(code: String, depart: Long, @PageableDefault(page = 10) pageable: Pageable) : List<QuestionSummary> =
questionDao.findAllByLecture(code, depart, pageable)
fun getAllByLecture(condition: QuestionCondition, @PageableDefault(page = 10) pageable: Pageable) : List<QuestionSummary> =
questionDao.findAllBy(condition, pageable)

@GetMapping("{id}")
fun getById(@PathVariable id: Long) : QuestionRes =
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package site.qbox.qboxserver.domain.question.query.dto

import com.querydsl.core.types.dsl.BooleanExpression
import site.qbox.qboxserver.domain.lecture.command.entity.LectureId
import site.qbox.qboxserver.domain.member.command.entity.QMember.member
import site.qbox.qboxserver.domain.question.command.entity.QQuestion.question

data class QuestionCondition(
val title: String = "",
val body: String = "",
val writerNickname: String = "",
val lectureCode: String,
val lectureDepart: Long,
) {

fun toFilter(): BooleanExpression =
question.lecture.eq(LectureId(lectureCode, lectureDepart))
.and(question.title.contains(title))
.and(question.body.contains(body))
.and(member.nickname.contains(writerNickname))
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@ package site.qbox.qboxserver.domain.question.query.dto
import com.querydsl.core.annotations.QueryProjection
import site.qbox.qboxserver.domain.lecture.query.dto.LectureRes
import site.qbox.qboxserver.domain.member.query.dto.MemberSummary
import java.time.LocalDateTime

data class QuestionRes @QueryProjection constructor (
val id: Long,
val title: String,
val body: String,
val lecture: LectureRes,
val writer: MemberSummary,
val createdAt: LocalDateTime,
val updatedAt: LocalDateTime,
)
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ package site.qbox.qboxserver.domain.question.query.dto

import com.querydsl.core.annotations.QueryProjection
import site.qbox.qboxserver.domain.member.query.dto.MemberSummary
import java.time.LocalDateTime

data class QuestionSummary @QueryProjection constructor (
val id: Long,
val title: String,
val writer: MemberSummary,
val createdAt: LocalDateTime,
)
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
package site.qbox.qboxserver.global.exception

class NotFoundException : RuntimeException("Not found") {
}
class NotFoundException : RuntimeException("Not found")
4 changes: 4 additions & 0 deletions src/main/resources/application-local.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,7 @@ logging:

service:
url: http://localhost:5172

local-storage:
directory: http://localhost/qbox/image/
url: http://localhost:5172
6 changes: 5 additions & 1 deletion src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,8 @@ logging:
config: classpath:logback-spring.xml

service:
url: ${SERVICE_URL}
url: ${SERVICE_URL}

local-storage:
directory: ${LOCAL_STORAGE_DIRECTORY}
url: ${LOCAL_STORAGE_URL}
Loading

0 comments on commit d2c098c

Please sign in to comment.