Skip to content

Commit

Permalink
[#9] admin 페이지 구현
Browse files Browse the repository at this point in the history
[#9] admin 페이지 구현
  • Loading branch information
giwoong01 authored Sep 8, 2023
2 parents 761b219 + b30cb8f commit 329dac7
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 0 deletions.
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);
}

}
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;
}
}
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);
}

}

0 comments on commit 329dac7

Please sign in to comment.