-
Notifications
You must be signed in to change notification settings - Fork 0
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
feat: controller, service 수정 #14 #19
Changes from 2 commits
4ff6fc6
876d1f7
c0d317b
19bc2ee
5e224bd
1ea93ee
dee9296
1fa15e4
a11145c
ecd9e87
9da9053
f55ac6d
b71334d
57ee438
f900859
5d85709
f7453a0
215421d
c84fca7
fbb36b1
d248227
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package HookKiller.server.notice.controller; | ||
|
||
import HookKiller.server.common.type.LanguageType; | ||
import HookKiller.server.notice.dto.NoticeArticleDto; | ||
import HookKiller.server.notice.dto.NoticeContentDto; | ||
import HookKiller.server.notice.entity.NoticeArticle; | ||
import HookKiller.server.repository.NoticeArticleRepository; | ||
import HookKiller.server.repository.NoticeContentRepository; | ||
import HookKiller.server.service.NoticeService; | ||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.validation.Valid; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.web.bind.annotation.*; | ||
|
||
import java.util.List; | ||
import java.util.Optional; | ||
|
||
@RestController | ||
@RequiredArgsConstructor | ||
@RequestMapping("/notice") | ||
public class NoticeController { | ||
|
||
private final NoticeService noticeService; | ||
private final NoticeArticleRepository articleRepository; | ||
private final NoticeContentRepository contentRepository; | ||
|
||
/** | ||
* 단건 조회 | ||
* @param noticeArticleId | ||
* @param request | ||
* @param languageType | ||
* @return | ||
*/ | ||
@GetMapping("{/noticeArticleId}") | ||
public NoticeArticleDto getNoticeArticle(@PathVariable Long noticeArticleId, HttpServletRequest request, LanguageType languageType) { | ||
request.getHeader("languageType"); | ||
return noticeService.getNoticeArticleByArticleId(noticeArticleId, languageType); | ||
} | ||
|
||
/** | ||
* 리스트 조회 | ||
* @param request | ||
* @param languageType | ||
* @param requestData | ||
* @return | ||
*/ | ||
@GetMapping | ||
public List<NoticeArticleDto> getNoticeArticleList(HttpServletRequest request, LanguageType languageType, NoticeArticleDto requestData) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 마찬가지로 |
||
request.getHeader("id"); | ||
Long id = requestData.getId(); | ||
List<NoticeArticleDto> articleDto = noticeService.getNoticeArticleList(id, languageType); | ||
return articleDto; | ||
} | ||
|
||
/** | ||
* 공지사항 등록 | ||
*/ | ||
@PostMapping | ||
public void addNotice(@RequestBody @Valid NoticeArticleDto articleRequest, NoticeContentDto contentRequest) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
noticeService.saveNotice(articleRequest, contentRequest); | ||
} | ||
|
||
/** | ||
* 공지사항 수정 | ||
*/ | ||
@PutMapping("/{id}") | ||
public void updateNotice(@PathVariable Long id, | ||
@RequestBody @Valid NoticeArticleDto articleDto, NoticeContentDto contentDto) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 마찬가지로 |
||
noticeService.update(id, articleDto); | ||
Optional<NoticeArticle> findNotice = articleRepository.findById(id); | ||
} | ||
|
||
/** | ||
* 공지사항 삭제 | ||
*/ | ||
@DeleteMapping("/{noticeArticleId}") | ||
public void deleteNotice(@PathVariable Long noticeArticleId) { | ||
noticeService.deleteNotice(noticeArticleId); | ||
} | ||
Comment on lines
+82
to
+85
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package HookKiller.server.notice.dto; | ||
|
||
import HookKiller.server.common.type.LanguageType; | ||
import HookKiller.server.common.type.NoticeArticleStatus; | ||
import HookKiller.server.notice.entity.NoticeArticle; | ||
import HookKiller.server.notice.entity.NoticeContent; | ||
import lombok.Builder; | ||
import lombok.Getter; | ||
|
||
import java.sql.Timestamp; | ||
|
||
@Getter | ||
@Builder | ||
public class NoticeArticleDto { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. dev을 머지하시고 |
||
|
||
private Long id; | ||
private LanguageType language; | ||
private NoticeArticleStatus status; | ||
private Timestamp createdAt; | ||
private Long createdUser; | ||
private Timestamp updatedAt; | ||
private Long updatedUser; | ||
|
||
public static NoticeArticleDto of(NoticeArticle noticeArticle, NoticeContent noticeContent) { | ||
return NoticeArticleDto.builder() | ||
.id(noticeArticle.getId()) | ||
.language(noticeArticle.getLanguage()) | ||
.status(noticeArticle.getStatus()) | ||
.createdAt(noticeArticle.getCreatedAt()) | ||
.createdUser(noticeArticle.getCreatedUser()) | ||
.updatedAt(noticeArticle.getUpdatedAt()) | ||
.updatedUser(noticeArticle.getUpdatedUser()) | ||
.build(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package HookKiller.server.notice.dto; | ||
|
||
import HookKiller.server.common.type.LanguageType; | ||
import HookKiller.server.notice.entity.NoticeArticle; | ||
import HookKiller.server.notice.entity.NoticeContent; | ||
import lombok.Builder; | ||
import lombok.Getter; | ||
|
||
@Getter | ||
@Builder | ||
public class NoticeContentDto { | ||
|
||
private Long id; | ||
private LanguageType language; | ||
private String title; | ||
private String content; | ||
|
||
public NoticeContentDto of(NoticeArticle noticeArticle, NoticeContent noticeContent) { | ||
|
||
return NoticeContentDto.builder() | ||
.id(noticeContent.getId()) | ||
.language(noticeContent.getLanguage()) | ||
.title(noticeContent.getTitle()) | ||
.content(noticeContent.getContent()) | ||
.build(); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package HookKiller.server.notice.exception; | ||
|
||
import HookKiller.server.common.exception.BaseException; | ||
|
||
import static HookKiller.server.notice.exception.NoticeException.NOTICE_ARTICLE_NOT_FOUND_EXCEPTION; | ||
|
||
public class NoticeArticleNotFoundException extends BaseException { | ||
public static final BaseException EXCEPTION = new NoticeArticleNotFoundException(); | ||
|
||
private NoticeArticleNotFoundException() { | ||
super(NOTICE_ARTICLE_NOT_FOUND_EXCEPTION); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package HookKiller.server.notice.exception; | ||
|
||
import HookKiller.server.common.dto.ErrorDetail; | ||
import HookKiller.server.common.exception.BaseErrorCode; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
import org.springframework.http.HttpStatus; | ||
|
||
import static org.springframework.http.HttpStatus.NOT_FOUND; | ||
|
||
@Getter | ||
@AllArgsConstructor | ||
public enum NoticeException implements BaseErrorCode { | ||
NOTICE_ARTICLE_NOT_FOUND_EXCEPTION(NOT_FOUND.value(), "404-1", "공지사항 게시물이 존재하지 않습니다."); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 굿굿 좋습니다 |
||
|
||
private final Integer statusCode; | ||
private final String errorCode; | ||
private final String reason; | ||
|
||
@Override | ||
public ErrorDetail getErrorDetail() { | ||
return ErrorDetail.of(statusCode, errorCode, reason); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,7 @@ | |
|
||
@Repository | ||
public interface NoticeArticleRepository extends JpaRepository<NoticeArticle, Long> { | ||
NoticeArticle findAllById(NoticeArticle noticeArticle); | ||
NoticeArticle findAll(NoticeArticle noticeArticle); | ||
NoticeArticle getById(Long id); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 레포지토리에서 |
||
|
||
} |
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,90 @@ | ||||||||||||||||||||||||||||||||
package HookKiller.server.service; | ||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
import HookKiller.server.common.type.LanguageType; | ||||||||||||||||||||||||||||||||
import HookKiller.server.notice.dto.NoticeArticleDto; | ||||||||||||||||||||||||||||||||
import HookKiller.server.notice.dto.NoticeContentDto; | ||||||||||||||||||||||||||||||||
import HookKiller.server.notice.entity.NoticeArticle; | ||||||||||||||||||||||||||||||||
import HookKiller.server.notice.entity.NoticeContent; | ||||||||||||||||||||||||||||||||
import HookKiller.server.notice.exception.NoticeArticleNotFoundException; | ||||||||||||||||||||||||||||||||
import HookKiller.server.repository.NoticeArticleRepository; | ||||||||||||||||||||||||||||||||
import HookKiller.server.repository.NoticeContentRepository; | ||||||||||||||||||||||||||||||||
import lombok.RequiredArgsConstructor; | ||||||||||||||||||||||||||||||||
import org.springframework.stereotype.Service; | ||||||||||||||||||||||||||||||||
import org.springframework.transaction.annotation.Transactional; | ||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
import java.util.ArrayList; | ||||||||||||||||||||||||||||||||
import java.util.List; | ||||||||||||||||||||||||||||||||
import java.util.Optional; | ||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
@Service | ||||||||||||||||||||||||||||||||
@RequiredArgsConstructor | ||||||||||||||||||||||||||||||||
public class NoticeService { | ||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
private final NoticeArticleRepository noticeArticleRepository; | ||||||||||||||||||||||||||||||||
private final NoticeContentRepository noticeContentRepository; | ||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
@Transactional | ||||||||||||||||||||||||||||||||
public void saveNotice(NoticeArticleDto articleDto, NoticeContentDto contentDto) { | ||||||||||||||||||||||||||||||||
NoticeArticle noticeArticle = NoticeArticle.builder() | ||||||||||||||||||||||||||||||||
.id(articleDto.getId()) | ||||||||||||||||||||||||||||||||
.build(); | ||||||||||||||||||||||||||||||||
NoticeContent noticeContent = NoticeContent.builder() | ||||||||||||||||||||||||||||||||
.id(contentDto.getId()) | ||||||||||||||||||||||||||||||||
.build(); | ||||||||||||||||||||||||||||||||
noticeArticleRepository.save(noticeArticle); | ||||||||||||||||||||||||||||||||
noticeContentRepository.save(noticeContent); | ||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
@Transactional(readOnly = true) | ||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. readonly 좋습니다 |
||||||||||||||||||||||||||||||||
public List<NoticeArticleDto> getNoticeList() { | ||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
List<NoticeArticle> all = noticeArticleRepository.findAll(); | ||||||||||||||||||||||||||||||||
List<NoticeArticleDto> noticeArticleDtoList = new ArrayList<>(); | ||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
for (NoticeArticle noticeArticle : all) { | ||||||||||||||||||||||||||||||||
NoticeArticleDto articleDto = NoticeArticleDto.builder() | ||||||||||||||||||||||||||||||||
.id(noticeArticle.getId()) | ||||||||||||||||||||||||||||||||
.language(noticeArticle.getLanguage()) | ||||||||||||||||||||||||||||||||
.status(noticeArticle.getStatus()) | ||||||||||||||||||||||||||||||||
.createdAt(noticeArticle.getCreatedAt()) | ||||||||||||||||||||||||||||||||
.createdUser(noticeArticle.getCreatedUser()) | ||||||||||||||||||||||||||||||||
.updatedAt(noticeArticle.getUpdatedAt()) | ||||||||||||||||||||||||||||||||
.updatedUser(noticeArticle.getUpdatedUser()) | ||||||||||||||||||||||||||||||||
.build(); | ||||||||||||||||||||||||||||||||
noticeArticleDtoList.add(articleDto); | ||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||||||||||||||||||||||||||||||||
return noticeArticleDtoList; | ||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
@Transactional | ||||||||||||||||||||||||||||||||
public NoticeContentDto getNotice(Long id) { | ||||||||||||||||||||||||||||||||
NoticeContent noticeContent = noticeContentRepository.findById(id).orElseThrow(() -> NoticeArticleNotFoundException.EXCEPTION); | ||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
return NoticeContentDto.builder() | ||||||||||||||||||||||||||||||||
.id(noticeContent.getId()) | ||||||||||||||||||||||||||||||||
.language(noticeContent.getLanguage()) | ||||||||||||||||||||||||||||||||
.title(noticeContent.getTitle()) | ||||||||||||||||||||||||||||||||
.content(noticeContent.getContent()) | ||||||||||||||||||||||||||||||||
.build(); | ||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 이 코드는 나중에 또 쓸 수 있으니
Suggested change
처럼 나오도록 한번 만들어보세요! |
||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
@Transactional | ||||||||||||||||||||||||||||||||
public void deleteNotice(Long id) { | ||||||||||||||||||||||||||||||||
noticeArticleRepository.deleteById(id); | ||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
@Transactional | ||||||||||||||||||||||||||||||||
public void update(Long id, NoticeArticleDto articleDto) { | ||||||||||||||||||||||||||||||||
Optional<NoticeArticle> byId = noticeArticleRepository.findById(id); | ||||||||||||||||||||||||||||||||
// TODO : 수정 받고 다시 작업 예정 | ||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. JPA에서 |
||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
public NoticeArticleDto getNoticeArticleByArticleId(Long noticeArticleId, LanguageType languageType) { | ||||||||||||||||||||||||||||||||
return null; | ||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
public List<NoticeArticleDto> getNoticeArticleList(Long noticeArticleId, LanguageType languageType) { | ||||||||||||||||||||||||||||||||
return null; | ||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 이런 것들을 서비스에서 따로 정의하는것도 괜찮지만, There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 그러면 서비스에서 이것들은 지워버리고 컨트롤러에서 바로 가져오도록 하면 되나요? |
||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Controller에서 LanguageType을 받을 수 있을 지가 확실하지않네요
아마
requestHeader
에 넣어주고 받을 것 같습니다이미 밑에서 해준 것 같으니
LanguageType
을 빼도 될 것 같아요또
requestHeader
를 사용하기 위해서는@RequestHeader
어노테이션에 대해서도 알아보시면 좋을 것 같네요