Skip to content

Commit

Permalink
[FIX] E7-S3 Account 연관관계 설정 #99
Browse files Browse the repository at this point in the history
- MenuServiceTest 에서 오류 납니다. 로그인된 상황을 어떻게 테스트해야 하는지 몰라서 성욱님 허락 하에 오류난 채로 커밋하였습니다.
  • Loading branch information
nrudev committed Nov 17, 2021
1 parent 477aeb4 commit 412c5ed
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
import com.infp.ciat.category.controller.dto.MenuUpdateRequestDto;
import com.infp.ciat.category.entity.Menu;
import com.infp.ciat.category.service.MenuService;
import com.infp.ciat.config.auth.PrincipalDetails;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.*;

import java.util.List;
Expand All @@ -19,8 +21,8 @@ public class MenuController {
MenuService menuService;

@PostMapping("/menu")
public ResponseEntity<MenuDto> newMenu(@RequestBody MenuSaveRequestDto requestDto) {
MenuDto newMenu = menuService.create(requestDto);
public ResponseEntity<MenuDto> newMenu(@RequestBody MenuSaveRequestDto requestDto, @AuthenticationPrincipal PrincipalDetails principalDetails) {
MenuDto newMenu = menuService.create(requestDto, principalDetails.getAccount());
return new ResponseEntity<>(newMenu, HttpStatus.CREATED);
}

Expand All @@ -35,8 +37,8 @@ public ResponseEntity<MenuDto> getOneMenu(@PathVariable Long id) {
}

@PutMapping("/menu/{id}")
public ResponseEntity<Long> updateMenu(@PathVariable Long id, @RequestBody MenuUpdateRequestDto requestDto) {
return new ResponseEntity<>(menuService.update(id, requestDto), HttpStatus.OK);
public ResponseEntity<Long> updateMenu(@PathVariable Long id, @RequestBody MenuUpdateRequestDto requestDto, @AuthenticationPrincipal PrincipalDetails principalDetails) {
return new ResponseEntity<>(menuService.update(id, requestDto, principalDetails.getAccount()), HttpStatus.OK);
}

@PatchMapping("/menu/{id}")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.infp.ciat.category.entity.Category;
import com.infp.ciat.category.entity.Menu;
import com.infp.ciat.user.entity.Account;
import lombok.Builder;
import lombok.Getter;
import lombok.Setter;
Expand All @@ -20,7 +21,8 @@ public class MenuDto {
private Long orders;
private String showYn;
private List<Category> categoryList;
// private Account account;
private Account account;
private Account updater;

public MenuDto(Menu menu) {
this.id = menu.getId();
Expand All @@ -31,10 +33,12 @@ public MenuDto(Menu menu) {
this.orders = menu.getOrders();
this.showYn = menu.getShowYn();
this.categoryList = menu.getCategoryList();
this.account = menu.getAccount();
this.updater = menu.getUpdater();
}

@Builder
public MenuDto(Long id, String uid, String name, String icon, String url, Long orders, String showYn, List<Category> categoryList) {
public MenuDto(Long id, String uid, String name, String icon, String url, Long orders, String showYn, List<Category> categoryList, Account account, Account updater) {
this.id = id;
this.uid = uid;
this.name = name;
Expand All @@ -43,5 +47,7 @@ public MenuDto(Long id, String uid, String name, String icon, String url, Long o
this.orders = orders;
this.showYn = showYn;
this.categoryList = categoryList;
this.account = account;
this.updater = updater;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.infp.ciat.category.entity.Category;
import com.infp.ciat.category.entity.Menu;
import com.infp.ciat.user.entity.Account;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
Expand All @@ -16,15 +17,17 @@ public class MenuSaveRequestDto {
private String url;
private Long orders;
private String showYn;
private Account account;

@Builder
public MenuSaveRequestDto(String uid, String name, String icon, String url, Long orders, String showYn) {
public MenuSaveRequestDto(String uid, String name, String icon, String url, Long orders, String showYn, Account account) {
this.uid = uid;
this.name = name;
this.icon = icon;
this.url = url;
this.orders = orders;
this.showYn = showYn;
this.account = account;
}

public Menu toEntity() {
Expand All @@ -35,6 +38,12 @@ public Menu toEntity() {
.url(url)
.orders(orders)
.showYn(showYn)
.account(account)
.build();
}

public void insertAccount(Account account) {
this.account = account;
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.infp.ciat.category.controller.dto;

import com.infp.ciat.user.entity.Account;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
Expand All @@ -15,14 +16,20 @@ public class MenuUpdateRequestDto {
private String url;
private Long orders;
private String showYn;
private Account updater;

@Builder
public MenuUpdateRequestDto(String name, String icon, String url, Long orders, String showYn) {
public MenuUpdateRequestDto(String name, String icon, String url, Long orders, String showYn, Account updater) {
this.name = name;
this.icon = icon;
this.url = url;
this.orders = orders;
this.showYn = showYn;
this.updater = updater;
}

public void addUpdater(Account updater) {
this.updater = updater;
}

}
20 changes: 16 additions & 4 deletions backend/src/main/java/com/infp/ciat/category/entity/Menu.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package com.infp.ciat.category.entity;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.infp.ciat.category.controller.dto.CategoryDto;
import com.infp.ciat.category.controller.dto.CategoryUpdateRequestDto;
import com.infp.ciat.category.controller.dto.MenuDto;
import com.infp.ciat.category.controller.dto.MenuUpdateRequestDto;
import com.infp.ciat.common.BaseTimeEntity;
import com.infp.ciat.user.entity.Account;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
Expand Down Expand Up @@ -44,18 +46,25 @@ public class Menu extends BaseTimeEntity {
@JsonIgnoreProperties({"menu"})
private List<Category> categoryList;

// @ManyToOne
// @JoinColumn(name = "accountId")
// private Account account;
@ManyToOne
@JoinColumn(name = "accountId")
@JsonIgnore
private Account account;

@ManyToOne
@JoinColumn(name = "updated_account_id")
@JsonIgnore
private Account updater;

@Builder
public Menu(String uid, String name, String icon, String url, Long orders, String showYn) {
public Menu(String uid, String name, String icon, String url, Long orders, String showYn, Account account) {
this.uid = uid;
this.name = name;
this.icon = icon;
this.url = url;
this.orders = orders;
this.showYn = showYn;
this.account = account;
}

public MenuDto fromEntity() {
Expand All @@ -68,6 +77,8 @@ public MenuDto fromEntity() {
.orders(orders)
.showYn(showYn)
.categoryList(categoryList)
.account(account)
.updater(updater)
.build();
}

Expand All @@ -77,6 +88,7 @@ public void update(MenuUpdateRequestDto requestDto) {
this.url = requestDto.getUrl();
this.orders = requestDto.getOrders();
this.showYn = requestDto.getShowYn();
this.updater = requestDto.getUpdater();
}

public void delete() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.infp.ciat.category.entity.Category;
import com.infp.ciat.category.entity.Menu;
import com.infp.ciat.category.repository.MenuRepository;
import com.infp.ciat.user.entity.Account;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.http.HttpStatus;
Expand All @@ -23,7 +24,8 @@ public class MenuService {
MenuRepository menuRepository;

@Transactional
public MenuDto create(MenuSaveRequestDto requestDto) {
public MenuDto create(MenuSaveRequestDto requestDto, Account account) {
requestDto.insertAccount(account);
return new MenuDto(menuRepository.save(requestDto.toEntity()));
}

Expand Down Expand Up @@ -61,8 +63,9 @@ public MenuDto getDetail(Long id) {
}

@Transactional
public Long update(Long id, MenuUpdateRequestDto requestDto) {
public Long update(Long id, MenuUpdateRequestDto requestDto, Account account) {
Menu targetMenu = menuRepository.findById(id).orElseThrow(()-> new ResponseStatusException(HttpStatus.NOT_FOUND, "해당 메뉴가 없습니다."));
requestDto.addUpdater(account);
targetMenu.update(requestDto);

return targetMenu.getId();
Expand Down

0 comments on commit 412c5ed

Please sign in to comment.