Skip to content

Commit

Permalink
Merge pull request #50 from mash-up-kr/feature/group-domain
Browse files Browse the repository at this point in the history
feat: Implement domain logics of group view
  • Loading branch information
210-reverof committed Jul 31, 2024
2 parents 259e377 + 0541634 commit 736aa23
Show file tree
Hide file tree
Showing 11 changed files with 101 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,13 @@ class EventApplicationService(
private val eventService: EventService
) {
@Transactional
fun create(request: CreateEventServiceRequest): CreateEventResponse {
fun create(
userId: Long,
request: CreateEventServiceRequest
): CreateEventResponse {
return CreateEventResponse(
eventService.create(
userId = userId,
groupId = request.groupId,
description = request.description,
date = request.date,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ import com.mashup.pic.event.applicationService.EventApplicationService
import com.mashup.pic.event.applicationService.dto.toServiceRequest
import com.mashup.pic.event.controller.dto.CreateEventRequest
import com.mashup.pic.event.controller.dto.CreateEventResponse
import com.mashup.pic.security.authentication.UserInfo
import io.swagger.v3.oas.annotations.Operation
import io.swagger.v3.oas.annotations.tags.Tag
import jakarta.validation.Valid
import org.springframework.security.core.annotation.AuthenticationPrincipal
import org.springframework.web.bind.annotation.PostMapping
import org.springframework.web.bind.annotation.RequestBody
import org.springframework.web.bind.annotation.RequestMapping
Expand All @@ -22,10 +24,11 @@ class EventController(
@Operation(summary = "이벤트 생성")
@PostMapping
fun createEvent(
@AuthenticationPrincipal user: UserInfo,
@Valid @RequestBody createEventRequest: CreateEventRequest
): ApiResponse<CreateEventResponse> {
return ApiResponse.success(
eventApplicationService.create(createEventRequest.toServiceRequest())
eventApplicationService.create(user.id, createEventRequest.toServiceRequest())
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ package com.mashup.pic.group.applicationservice
import com.mashup.pic.domain.event.EventService
import com.mashup.pic.domain.event.EventStatus
import com.mashup.pic.domain.event.UploadService
import com.mashup.pic.domain.event.VoteService
import com.mashup.pic.domain.group.GroupDto
import com.mashup.pic.domain.group.GroupService
import com.mashup.pic.domain.result.ResultDto
import com.mashup.pic.domain.result.ResultService
import com.mashup.pic.domain.vote.VoteService
import com.mashup.pic.group.applicationservice.dto.CreateGroupResponse
import com.mashup.pic.group.applicationservice.dto.CreateGroupServiceRequest
import com.mashup.pic.group.applicationservice.dto.JoinGroupServiceRequest
Expand Down Expand Up @@ -42,6 +42,7 @@ class GroupApplicationService(
return CreateGroupResponse.from(groupDto, invitationCode)
}

@Transactional
fun joinGroup(request: JoinGroupServiceRequest): JoinGroupResponse {
val groupId = InviteCodeUtil.getIdFromInviteCode(request.code)
groupService.join(
Expand Down Expand Up @@ -87,7 +88,7 @@ class GroupApplicationService(
cardFrontImageUrl = cardBackImages.resultImages[0].imageUrl
} else { // 현 이벤트 O
recentEvent = RecentEvent(lastEvent.description, lastEvent.date)
cardFrontImageUrl = eventService.getRandomImageOption(lastEvent.id)
cardFrontImageUrl = eventService.getRandomImageOptionFromEvent(lastEvent.id)

status =
when (lastEvent.eventStatus) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,8 @@ package com.mashup.pic.domain.event

import org.springframework.data.jpa.repository.JpaRepository

interface EventImageOptionRepository : JpaRepository<EventImageOption, Long>
interface EventImageOptionRepository : JpaRepository<EventImageOption, Long> {
fun existsByEventJoinId(id: Long): Boolean

fun findAllByEventJoinIdIn(eventJoinIds: List<Long>): List<EventImageOption>
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,11 @@ package com.mashup.pic.domain.event

import org.springframework.data.jpa.repository.JpaRepository

interface EventJoinRepository : JpaRepository<EventJoin, Long>
interface EventJoinRepository : JpaRepository<EventJoin, Long> {
fun findByUserIdAndEventId(
userId: Long,
eventId: Long
): EventJoin?

fun findAllByEventId(eventId: Long): List<EventJoin>
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import org.springframework.data.repository.findByIdOrNull
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional
import java.time.LocalDateTime
import kotlin.random.Random

@Service
@Transactional(readOnly = true)
Expand All @@ -18,6 +19,7 @@ class EventService(
) {
@Transactional
fun create(
userId: Long,
groupId: Long,
description: String,
date: LocalDateTime,
Expand All @@ -33,7 +35,13 @@ class EventService(
)

createEventJoinsByGroup(event.id, groupId)
// TODO: 이벤트 생성하면서 생성한 사람에 대하여 이미지 후보군 바로 등록하기
val creatorEventJoin = getEventJoinByUserIdAndEventId(userId, event.id)

val eventImageOptions =
pictures.map { picture ->
EventImageOption(creatorEventJoin.id, picture)
}
eventImageOptionRepository.saveAll(eventImageOptions)

return event.id
}
Expand All @@ -42,9 +50,11 @@ class EventService(
return eventRepository.findTopByGroupIdOrderByDateDesc(groupId)?.toDto()
}

fun getRandomImageOption(id: Long): String {
// TODO: 등록된 후보 중 랜덤으로 1개 URL 반환
return "TODOTODO"
fun getRandomImageOptionFromEvent(eventId: Long): String {
val eventJoinIds = eventJoinRepository.findAllByEventId(eventId).map { it.id }
val imageOptions = eventImageOptionRepository.findAllByEventJoinIdIn(eventJoinIds)

return imageOptions[Random.nextInt(imageOptions.size)].imageUrl
}

fun endEventUploading(eventId: Long) {
Expand Down Expand Up @@ -81,4 +91,11 @@ class EventService(
private fun getEventById(eventId: Long): Event {
return eventRepository.findByIdOrNull(eventId) ?: throw PicException.of(PicExceptionType.NOT_EXIST, "$eventId 는 없는 이벤트")
}

private fun getEventJoinByUserIdAndEventId(
userId: Long,
eventId: Long
): EventJoin {
return eventJoinRepository.findByUserIdAndEventId(userId, eventId) ?: throw PicException.of(PicExceptionType.NOT_EXIST)
}
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,28 @@
package com.mashup.pic.domain.event

import com.mashup.pic.common.exception.PicException
import com.mashup.pic.common.exception.PicExceptionType
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional

@Service
@Transactional(readOnly = true)
class UploadService {
class UploadService(
private val eventJoinRepository: EventJoinRepository,
private val eventImageOptionRepository: EventImageOptionRepository
) {
fun hasUserUploaded(
userId: Long,
eventId: Long
): Boolean {
// TODO: 올린 이미지 있는지 확인하기
return true
val eventJoin = getEventJoinByUserIdAndEventId(userId, eventId)
return eventImageOptionRepository.existsByEventJoinId(eventJoin.id)
}

private fun getEventJoinByUserIdAndEventId(
userId: Long,
eventId: Long
): EventJoin {
return eventJoinRepository.findByUserIdAndEventId(userId, eventId) ?: throw PicException.of(PicExceptionType.NOT_EXIST)
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.mashup.pic.domain.event
package com.mashup.pic.domain.vote

import com.mashup.pic.domain.common.BaseEntity
import jakarta.persistence.Column
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.mashup.pic.domain.vote

import org.springframework.data.jpa.repository.JpaRepository

interface VoteRepository : JpaRepository<Vote, Long> {
fun existsByEventJoinId(id: Long): Boolean
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.mashup.pic.domain.vote

import com.mashup.pic.common.exception.PicException
import com.mashup.pic.common.exception.PicExceptionType
import com.mashup.pic.domain.event.EventImageOptionRepository
import com.mashup.pic.domain.event.EventJoin
import com.mashup.pic.domain.event.EventJoinRepository
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional

@Service
@Transactional(readOnly = true)
class VoteService(
private val voteRepository: VoteRepository,
private val eventJoinRepository: EventJoinRepository,
private val eventImageOptionRepository: EventImageOptionRepository
) {
fun hasUserVoted(
userId: Long,
eventId: Long
): Boolean {
val eventJoin = getEventJoinByUserIdAndEventId(userId, eventId)
return voteRepository.existsByEventJoinId(eventJoin.id)
}

private fun getEventJoinByUserIdAndEventId(
userId: Long,
eventId: Long
): EventJoin {
return eventJoinRepository.findByUserIdAndEventId(userId, eventId) ?: throw PicException.of(PicExceptionType.NOT_EXIST)
}
}

0 comments on commit 736aa23

Please sign in to comment.