Skip to content

Commit

Permalink
refactor: Panel에서 Member를 @AuthenticationPrincipal로 받아오도록 한다
Browse files Browse the repository at this point in the history
  • Loading branch information
soomanbaek committed Oct 21, 2023
1 parent 4f3aac6 commit 47663e4
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 16 deletions.
15 changes: 11 additions & 4 deletions src/main/java/blacktokkies/toquiz/domain/panel/api/PanelApi.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package blacktokkies.toquiz.domain.panel.api;

import blacktokkies.toquiz.domain.activeinfo.ActiveInfoRepository;
import blacktokkies.toquiz.domain.member.domain.Member;
import blacktokkies.toquiz.domain.panel.application.PanelService;
import blacktokkies.toquiz.domain.panel.dto.request.CreatePanelRequest;
import blacktokkies.toquiz.domain.panel.dto.request.UpdatePanelRequest;
Expand All @@ -18,6 +19,7 @@
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.*;

@RestController
Expand All @@ -29,25 +31,30 @@ public class PanelApi {

@PostMapping("api/panel")
public ResponseEntity<SuccessResponse<PanelResponse>> createPanel(
@AuthenticationPrincipal Member member,
@RequestBody @Valid CreatePanelRequest createPanelRequest
){
PanelResponse response = panelService.createPanel(createPanelRequest);
PanelResponse response = panelService.createPanel(member, createPanelRequest);

return ResponseEntity.ok(new SuccessResponse<>(response));
}

@GetMapping("api/panels")
public ResponseEntity<SuccessResponse<GetMyPanelsResponse>> getMyPanels(
@AuthenticationPrincipal Member member,
@PageableDefault(size = 10, sort = "createdDate", direction = Sort.Direction.DESC) Pageable pageable
){
GetMyPanelsResponse response = panelService.getMyPanels(pageable);
GetMyPanelsResponse response = panelService.getMyPanels(member, pageable);

return ResponseEntity.ok(new SuccessResponse<>(response));
}

@DeleteMapping("api/panels/{panelSid}")
public ResponseEntity<SuccessMessage> deletePanel(@PathVariable String panelSid){
panelService.deletePanel(panelSid);
public ResponseEntity<SuccessMessage> deletePanel(
@AuthenticationPrincipal Member member,
@PathVariable String panelSid
){
panelService.deletePanel(member, panelSid);

return ResponseEntity.ok(SuccessMessage.PANEL_DELETE);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,13 @@ public class PanelService {
private final PanelRepository panelRepository;

@Transactional
public PanelResponse createPanel(CreatePanelRequest createPanelRequest) {
Member member = getMember();
public PanelResponse createPanel(Member member, CreatePanelRequest createPanelRequest) {
Panel panel = panelRepository.save(createPanelRequest.toPanelWith(member, generateSecondaryId()));

return PanelResponse.toDto(panel);
}

public GetMyPanelsResponse getMyPanels(Pageable pageable){
Member member = getMember();
public GetMyPanelsResponse getMyPanels(Member member, Pageable pageable){
Page<Panel> panels = panelRepository.findAllByMember(member, pageable);
List<PanelResponse> panelResponses = panels.stream().map(PanelResponse::toDto).toList();
int nextPage = panels.hasNext() ? pageable.getPageNumber() + 1 : -1;
Expand All @@ -53,8 +51,8 @@ public GetMyPanelsResponse getMyPanels(Pageable pageable){
}

@Transactional
public void deletePanel(String panelSid) {
checkIsAuthorizedToDelete(panelSid);
public void deletePanel(Member member, String panelSid) {
checkIsAuthorizedToDelete(member, panelSid);

panelRepository.deleteBySid(panelSid);
}
Expand Down Expand Up @@ -93,9 +91,8 @@ public String generateSecondaryId(){
return sid;
}

private void checkIsAuthorizedToDelete(String panelSid) {
private void checkIsAuthorizedToDelete(Member member, String panelSid) {
Panel panel = getPanel(panelSid);
Member member = getMember();

if(!Objects.equals(member.getId(), panel.getMember().getId())){
throw new RestApiException(NOT_AUTHORIZED_DELETE);
Expand All @@ -107,10 +104,6 @@ private void checkIsExistPanel(String panelSid){
.orElseThrow(() -> new RestApiException(NOT_EXIST_PANEL));
}

private Member getMember() {
return (Member) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
}

public Panel getPanel(String panelSid){
return panelRepository.findBySid(panelSid)
.orElseThrow(() -> new RestApiException(NOT_EXIST_PANEL));
Expand Down

0 comments on commit 47663e4

Please sign in to comment.