Skip to content
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 @@ -36,11 +36,8 @@ public User getAdminUserFromSession(HttpServletRequest request) {
}

@Override
public void createFAQ(Long userId, FAQRequestDto request) {
User user = userRepository.findById(userId)
.orElseThrow(() -> new RuntimeException("사용자를 찾을 수 없습니다."));
public void createFAQ(FAQRequestDto request) {
FAQ faq = new FAQ();
faq.setUser(user);
faq.setCategory(request.getCategory());
faq.setQuestion(request.getQuestion());
faq.setAnswer(request.getAnswer());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,8 @@ public User getAdminUserFromSession(HttpServletRequest request) {
}

@Override
public void createNotice(Long userId, NoticeRequestDto request) {
User user = userRepository.findById(userId)
.orElseThrow(() -> new RuntimeException("사용자를 찾을 수 없습니다."));
public void createNotice(NoticeRequestDto request) {
Notice notice = new Notice();
notice.setUser(user);
notice.setTitle(request.getTitle());
notice.setContent(request.getContent());
notice.setLabel(request.getLabel());
Expand Down Expand Up @@ -117,67 +114,4 @@ public Notice getNotice(Long noticeId) {
return noticeRepository.save(notice);
}

@Service
@RequiredArgsConstructor
public static class FAQServiceImpl implements FAQService {

private final FAQRepository faqRepository;
private final UserRepository userRepository;

@Override
public void createFAQ(Long userId, FAQRequestDto request) {
User user = userRepository.findById(userId)
.orElseThrow(() -> new RuntimeException("사용자를 찾을 수 없습니다."));
FAQ faq = new FAQ();
faq.setUser(user);
faq.setCategory(request.getCategory());
faq.setQuestion(request.getQuestion());
faq.setAnswer(request.getAnswer());

faqRepository.save(faq);
}

@Override
public void updateFAQ(Long faqId, FAQRequestDto request) {
FAQ faq = faqRepository.findById(faqId)
.orElseThrow(() -> new RuntimeException("FAQ를 찾을 수 없습니다."));

faq.setCategory(request.getCategory());
faq.setQuestion(request.getQuestion());
faq.setAnswer(request.getAnswer());

faqRepository.save(faq);
}

@Override
public void deleteFAQ(Long faqId) {
FAQ faq = faqRepository.findById(faqId)
.orElseThrow(() -> new RuntimeException("FAQ를 찾을 수 없습니다."));
faqRepository.delete(faq);
}

public User getAdminUserFromSession(HttpServletRequest request) {
HttpSession session = request.getSession();
Long id = (Long) session.getAttribute("id");
User user = userRepository.findByUserId(id);
if (user == null || user.getRole() != 1) {
throw new RuntimeException("이 기능은 관리자만 접근 가능합니다.");
}
return user;
}

@Override
public List<FAQResponseDto> getAllFAQs() {
List<FAQ> faqs = faqRepository.findAll();
return faqs.stream().map(FAQResponseDto::new).collect(Collectors.toList());
}

@Override
public FAQResponseDto getFAQById(Long faqId) {
FAQ faq = faqRepository.findById(faqId)
.orElseThrow(() -> new RuntimeException("FAQ를 찾을 수 없습니다."));
return new FAQResponseDto(faq);
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
public interface FAQService {
User getAdminUserFromSession(HttpServletRequest request);

void createFAQ(Long userId, FAQRequestDto request);
void createFAQ(FAQRequestDto request);
void updateFAQ(Long faqId, FAQRequestDto request);
void deleteFAQ(Long faqId);
List<FAQResponseDto> getAllFAQs();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
public interface NoticeService {
User getAdminUserFromSession(HttpServletRequest request);

void createNotice(Long userId, NoticeRequestDto request);
void createNotice(NoticeRequestDto request);
void updateNotice(Long noticeId, NoticeRequestDto request);
void deleteNotice(Long noticeId);
List<NoticeResponseDto> getAllNotices();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,33 +20,30 @@ public class FAQController {
private final FAQService faqService;

@PostMapping("/create")
public ResponseEntity<APIResponse<?>> createFAQ(@RequestBody FAQRequestDto request, HttpServletRequest httpRequest) {
public ResponseEntity<APIResponse<?>> createFAQ(@RequestBody FAQRequestDto request) {
try {
User admin = faqService.getAdminUserFromSession(httpRequest);
faqService.createFAQ(admin.getUserId(), request);
return ResponseEntity.ok().body(APIResponse.successAPI("관리자 "+admin.getUserId()+"가 FAQ를 생성하였습니다.", null));
faqService.createFAQ(request);
return ResponseEntity.ok().body(APIResponse.successAPI("관리자가 FAQ를 생성하였습니다.", null));
} catch (Exception e) {
return ResponseEntity.badRequest().body(APIResponse.errorAPI(e.getMessage()));
}
}

@PutMapping("/{faqId}")
public ResponseEntity<APIResponse<?>> updateFAQ(@PathVariable Long faqId, @RequestBody FAQRequestDto request, HttpServletRequest httpRequest) {
public ResponseEntity<APIResponse<?>> updateFAQ(@PathVariable Long faqId, @RequestBody FAQRequestDto request) {
try {
User admin = faqService.getAdminUserFromSession(httpRequest);
faqService.updateFAQ(faqId, request);
return ResponseEntity.ok().body(APIResponse.successAPI("관리자 : " + admin.getUserId() + " 가 FAQ를 수정했습니다.", null));
return ResponseEntity.ok().body(APIResponse.successAPI("관리자가 FAQ를 수정했습니다.", null));
} catch (Exception e) {
return ResponseEntity.badRequest().body(APIResponse.errorAPI(e.getMessage()));
}
}

@DeleteMapping("/{faqId}")
public ResponseEntity<APIResponse<?>> deleteFAQ(@PathVariable Long faqId, HttpServletRequest httpRequest) {
public ResponseEntity<APIResponse<?>> deleteFAQ(@PathVariable Long faqId) {
try {
User admin = faqService.getAdminUserFromSession(httpRequest);
faqService.deleteFAQ(faqId);
return ResponseEntity.ok().body(APIResponse.successAPI("관리자 : " + admin.getUserId() + " 가 FAQ를 삭제했습니다.", null));
return ResponseEntity.ok().body(APIResponse.successAPI("관리자가 FAQ를 삭제했습니다.", null));
} catch (Exception e) {
return ResponseEntity.badRequest().body(APIResponse.errorAPI(e.getMessage()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,18 @@ public class NoticeController {
private final NoticeService noticeService;

@PostMapping("/create")
public ResponseEntity<APIResponse<?>> createNotice(@RequestBody NoticeRequestDto request, HttpServletRequest httpRequest) {
public ResponseEntity<APIResponse<?>> createNotice(@RequestBody NoticeRequestDto request) {
try {
noticeService.createNotice(noticeService.getAdminUserFromSession(httpRequest).getUserId(), request);
noticeService.createNotice(request);
return ResponseEntity.ok().body(APIResponse.successAPI("공지사항이 등록되었습니다.", null));
} catch (Exception e) {
return ResponseEntity.badRequest().body(APIResponse.errorAPI(e.getMessage()));
}
}

@PutMapping("/{noticeId}")
public ResponseEntity<APIResponse<?>> updateNotice(@PathVariable Long noticeId, @RequestBody NoticeRequestDto request, HttpServletRequest httpRequest) {
public ResponseEntity<APIResponse<?>> updateNotice(@PathVariable Long noticeId, @RequestBody NoticeRequestDto request) {
try {
noticeService.getAdminUserFromSession(httpRequest);
noticeService.updateNotice(noticeId, request);
return ResponseEntity.ok().body(APIResponse.successAPI("공지사항이 수정되었습니다.", null));
} catch (Exception e) {
Expand All @@ -42,9 +41,8 @@ public ResponseEntity<APIResponse<?>> updateNotice(@PathVariable Long noticeId,
}

@DeleteMapping("/{noticeId}")
public ResponseEntity<APIResponse<?>> deleteNotice(@PathVariable Long noticeId, HttpServletRequest httpRequest) {
public ResponseEntity<APIResponse<?>> deleteNotice(@PathVariable Long noticeId) {
try {
noticeService.getAdminUserFromSession(httpRequest);
noticeService.deleteNotice(noticeId);
return ResponseEntity.ok().body(APIResponse.successAPI("공지사항이 삭제되었습니다.", null));
} catch (Exception e) {
Expand Down
4 changes: 0 additions & 4 deletions src/main/java/com/capstone/favicon/admin/domain/FAQ.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,6 @@ public class FAQ {
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long faqId;

@ManyToOne
@JoinColumn(name = "user_id", nullable = false)
private User user;

@Enumerated(EnumType.STRING)
@Column(nullable = false)
private FAQCategory category;
Expand Down
4 changes: 0 additions & 4 deletions src/main/java/com/capstone/favicon/admin/domain/Notice.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,6 @@ public class Notice {
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long noticeId;

@ManyToOne
@JoinColumn(name = "user_id", nullable = false)
private User user;

@Column(nullable = false)
private String title;

Expand Down
Loading