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

공지사항 작성 api를 개발했습니다 #300

Merged
merged 7 commits into from
Mar 28, 2024
Merged
Show file tree
Hide file tree
Changes from all 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 @@ -2,6 +2,8 @@ package com.msg.gcms.domain.notice.domain.entity

import com.msg.gcms.domain.club.domain.entity.Club
import com.msg.gcms.domain.user.domain.entity.User
import org.springframework.data.annotation.CreatedDate
import java.time.LocalDateTime
import javax.persistence.*

@Entity
Expand All @@ -22,5 +24,9 @@ class Notice (

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "user_id")
val user: User
val user: User,

@CreatedDate
@Column(columnDefinition = "DATETIME(6)")
val createdAt: LocalDateTime
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.msg.gcms.domain.notice.presentation

import com.msg.gcms.domain.notice.presentation.data.request.CreateNoticeRequestDto
import com.msg.gcms.domain.notice.service.CreateNoticeService
import com.msg.gcms.domain.notice.utils.NoticeConverter
import com.msg.gcms.global.annotation.RequestController
import org.springframework.http.HttpStatus
import org.springframework.http.ResponseEntity
import org.springframework.web.bind.annotation.PathVariable
import org.springframework.web.bind.annotation.PostMapping
import org.springframework.web.bind.annotation.RequestBody
import javax.validation.Valid

@RequestController("/notification")
class NoticeController(
private val createNoticeService: CreateNoticeService,
private val noticeConverter: NoticeConverter
) {
@PostMapping("/{club_id}")
fun createNotice(
@PathVariable("club_id") clubId: Long,
@RequestBody @Valid requestDto: CreateNoticeRequestDto
): ResponseEntity<Unit> =
noticeConverter.toDto(requestDto)
.let { createNoticeService.execute(clubId, it) }
.let { ResponseEntity.status(HttpStatus.CREATED).build() }

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.msg.gcms.domain.notice.presentation.data.dto

import java.time.LocalDateTime

data class NoticeDto(
val id: Long = 0,
val title: String,
val content: String,
val createdAt: LocalDateTime = LocalDateTime.now()
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.msg.gcms.domain.notice.presentation.data.request

import org.hibernate.validator.constraints.Length
import javax.validation.constraints.NotBlank

data class CreateNoticeRequestDto(
@field:NotBlank
@field:Length(max = 100)
val title: String,

@field:NotBlank
@field:Length(max = 2000)
val content: String
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.msg.gcms.domain.notice.service

import com.msg.gcms.domain.notice.presentation.data.dto.NoticeDto

interface CreateNoticeService {
fun execute(clubId: Long, dto: NoticeDto)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.msg.gcms.domain.notice.service.impl

import com.msg.gcms.domain.auth.domain.Role
import com.msg.gcms.domain.club.domain.repository.ClubRepository
import com.msg.gcms.domain.club.exception.ClubNotFoundException
import com.msg.gcms.domain.club.exception.HeadNotSameException
import com.msg.gcms.domain.notice.domain.repository.NoticeRepository
import com.msg.gcms.domain.notice.presentation.data.dto.NoticeDto
import com.msg.gcms.domain.notice.service.CreateNoticeService
import com.msg.gcms.domain.notice.utils.NoticeConverter
import com.msg.gcms.global.annotation.ServiceWithTransaction
import com.msg.gcms.global.util.UserUtil
import org.springframework.data.repository.findByIdOrNull

@ServiceWithTransaction
class CreateNoticeServiceImpl(
private val userUtil: UserUtil,
private val clubRepository: ClubRepository,
private val noticeConverter: NoticeConverter,
private val noticeRepository: NoticeRepository
) : CreateNoticeService {
override fun execute(clubId: Long, dto: NoticeDto) {
val user = userUtil.fetchCurrentUser()

val club = clubRepository.findByIdOrNull(clubId)
?: throw ClubNotFoundException()

if(user.roles.contains(Role.ROLE_STUDENT)) {
if (user != club.user) throw HeadNotSameException()
}

noticeConverter.toEntity(dto, user, club)
.let { noticeRepository.save(it) }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.msg.gcms.domain.notice.utils

import com.msg.gcms.domain.club.domain.entity.Club
import com.msg.gcms.domain.notice.domain.entity.Notice
import com.msg.gcms.domain.notice.presentation.data.dto.NoticeDto
import com.msg.gcms.domain.notice.presentation.data.request.CreateNoticeRequestDto
import com.msg.gcms.domain.user.domain.entity.User

interface NoticeConverter {
fun toDto(requestDto: CreateNoticeRequestDto): NoticeDto

fun toEntity(dto: NoticeDto, user: User, club: Club): Notice
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.msg.gcms.domain.notice.utils.impl

import com.msg.gcms.domain.club.domain.entity.Club
import com.msg.gcms.domain.notice.domain.entity.Notice
import com.msg.gcms.domain.notice.presentation.data.dto.NoticeDto
import com.msg.gcms.domain.notice.presentation.data.request.CreateNoticeRequestDto
import com.msg.gcms.domain.notice.utils.NoticeConverter
import com.msg.gcms.domain.user.domain.entity.User
import org.springframework.stereotype.Component

@Component
class NoticeConverterImpl : NoticeConverter {
override fun toDto(requestDto: CreateNoticeRequestDto): NoticeDto = NoticeDto(
title = requestDto.title,
content = requestDto.content
)

override fun toEntity(dto: NoticeDto, user: User, club: Club): Notice = Notice(
id = dto.id,
title = dto.title,
content = dto.content,
createdAt = dto.createdAt,
user = user,
club = club
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ class SecurityConfig(
.antMatchers(HttpMethod.PATCH, "/attend/").authenticated()
.antMatchers(HttpMethod.GET, "/attend/excel").hasRole("ADMIN")

.antMatchers(HttpMethod.POST, "/notification/{club_id}").authenticated()

.antMatchers(HttpMethod.GET, "/club-member/{club_id}").authenticated()
.antMatchers(HttpMethod.POST, "/club-member/{club_id}").authenticated()
.antMatchers(HttpMethod.PATCH, "/club-member/{club_id}").authenticated()
Expand Down
Loading