-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[#9] admin 페이지 구현
- Loading branch information
Showing
3 changed files
with
119 additions
and
0 deletions.
There are no files selected for viewing
39 changes: 39 additions & 0 deletions
39
src/main/java/com/itcontest/skhuming/admin/api/AdminController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package com.itcontest.skhuming.admin.api; | ||
|
||
import com.itcontest.skhuming.admin.api.request.NoticeSaveReqDto; | ||
import com.itcontest.skhuming.admin.application.AdminService; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.*; | ||
|
||
@RestController | ||
public class AdminController { | ||
|
||
private final AdminService adminService; | ||
|
||
public AdminController(AdminService adminService) { | ||
this.adminService = adminService; | ||
} | ||
|
||
// 공지 등록 | ||
@PostMapping("/admin/api/notice/save") | ||
public ResponseEntity<String> noticeSave(@RequestBody NoticeSaveReqDto noticeSaveReqDto) { | ||
adminService.noticeSave(noticeSaveReqDto); | ||
return new ResponseEntity<>("SUCCESS", HttpStatus.OK); | ||
} | ||
|
||
// 공지 수정 | ||
@PutMapping("/admin/api/notice/update") | ||
public ResponseEntity<String> noticeUpdate(@RequestParam("noticeId") Long noticeId, @RequestBody NoticeSaveReqDto noticeSaveReqDto) { | ||
adminService.noticeUpdate(noticeId, noticeSaveReqDto); | ||
return new ResponseEntity<>("SUCCESS", HttpStatus.OK); | ||
} | ||
|
||
// 공지 삭제 | ||
@PostMapping("/admin/api/notice/delete") | ||
public ResponseEntity<String> noticeDelete(@RequestParam("noticeId") Long noticeId) { | ||
adminService.noticeDelete(noticeId); | ||
return new ResponseEntity<>("SUCCESS", HttpStatus.OK); | ||
} | ||
|
||
} |
25 changes: 25 additions & 0 deletions
25
src/main/java/com/itcontest/skhuming/admin/api/request/NoticeSaveReqDto.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package com.itcontest.skhuming.admin.api.request; | ||
|
||
import lombok.AccessLevel; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
|
||
@Getter | ||
@NoArgsConstructor(access = AccessLevel.PROTECTED) | ||
public class NoticeSaveReqDto { | ||
|
||
private String title; | ||
|
||
private String schedule; | ||
|
||
private String contents; | ||
|
||
private int mileageScore; | ||
|
||
public NoticeSaveReqDto(String title, String schedule, String contents, int mileageScore) { | ||
this.title = title; | ||
this.schedule = schedule; | ||
this.contents = contents; | ||
this.mileageScore = mileageScore; | ||
} | ||
} |
55 changes: 55 additions & 0 deletions
55
src/main/java/com/itcontest/skhuming/admin/application/AdminService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package com.itcontest.skhuming.admin.application; | ||
|
||
import com.itcontest.skhuming.admin.api.request.NoticeSaveReqDto; | ||
import com.itcontest.skhuming.member.domain.MemberScrapNotice; | ||
import com.itcontest.skhuming.member.domain.repository.MemberScrapNoticeRepository; | ||
import com.itcontest.skhuming.notice.domain.Notice; | ||
import com.itcontest.skhuming.notice.domain.repository.NoticeRepository; | ||
import com.itcontest.skhuming.notice.exception.NotFoundNoticeException; | ||
import org.springframework.stereotype.Service; | ||
|
||
import javax.transaction.Transactional; | ||
import java.util.List; | ||
|
||
@Service | ||
@Transactional | ||
public class AdminService { | ||
|
||
private final NoticeRepository noticeRepository; | ||
private final MemberScrapNoticeRepository memberScrapNoticeRepository; | ||
|
||
public AdminService(NoticeRepository noticeRepository, MemberScrapNoticeRepository memberScrapNoticeRepository) { | ||
this.noticeRepository = noticeRepository; | ||
this.memberScrapNoticeRepository = memberScrapNoticeRepository; | ||
} | ||
|
||
// 공지 등록 | ||
public void noticeSave(NoticeSaveReqDto noticeSaveReqDto) { | ||
Notice notice = new Notice(noticeSaveReqDto.getTitle(), | ||
noticeSaveReqDto.getSchedule(), | ||
noticeSaveReqDto.getContents(), | ||
noticeSaveReqDto.getMileageScore()); | ||
|
||
noticeRepository.save(notice); | ||
} | ||
|
||
// 공지 수정 | ||
public void noticeUpdate(Long noticeId, NoticeSaveReqDto noticeSaveReqDto) { | ||
Notice notice = noticeRepository.findById(noticeId).orElseThrow(NotFoundNoticeException::new); | ||
|
||
notice.update(noticeSaveReqDto.getTitle(), | ||
noticeSaveReqDto.getSchedule(), | ||
noticeSaveReqDto.getContents(), | ||
noticeSaveReqDto.getMileageScore()); | ||
} | ||
|
||
// 공지 삭제 | ||
public void noticeDelete(Long noticeId) { | ||
Notice notice = noticeRepository.findById(noticeId).orElseThrow(NotFoundNoticeException::new); | ||
List<MemberScrapNotice> scrapNotice = memberScrapNoticeRepository.findByNotice(notice); | ||
|
||
memberScrapNoticeRepository.deleteAll(scrapNotice); | ||
noticeRepository.deleteById(noticeId); | ||
} | ||
|
||
} |