Skip to content

Commit

Permalink
Merge pull request #169 from Media-XI/ART-187-매거진-수정시-문제-해결
Browse files Browse the repository at this point in the history
[Issue] 매거진 수정시 categorySlug가 null을 허용하도록 변경
  • Loading branch information
haroya01 authored May 15, 2024
2 parents 5a0715a + b161625 commit d20d988
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -163,8 +163,7 @@ public ResponseEntity updateMagazine(
String loginUsername = SecurityUtil.getCurrentUsername()
.orElseThrow(LoginRequiredException::new);

MagazineCategory category = magazineCategoryService.getEntity(magazineRequest.getCategorySlug());
MagazineResponse.Get magazine = magazineService.update(id, loginUsername, magazineRequest, category);
MagazineResponse.Get magazine = magazineService.update(id, loginUsername, magazineRequest);

return new ResponseEntity(magazine, HttpStatus.OK);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ public static class Update {
@NotEmpty
private String content;

@NotNull
private String categorySlug;

private Map<String, String> metadata;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,15 @@ public class MagazineService {

private final MagazineMediaRepository magazineMediaRepository;

private final MagazineCategoryRepository magazineCategoryRepository;


@Autowired
public MagazineService(MagazineRepository magazineRepository, MagazineCommentRepository magazineCommentRepository, MagazineMediaRepository magazineMediaRepository) {
public MagazineService(MagazineRepository magazineRepository, MagazineCommentRepository magazineCommentRepository, MagazineMediaRepository magazineMediaRepository, MagazineCategoryRepository magazineCategoryRepository) {
this.magazineRepository = magazineRepository;
this.magazineCommentRepository = magazineCommentRepository;
this.magazineMediaRepository = magazineMediaRepository;
this.magazineCategoryRepository = magazineCategoryRepository;
}

@Transactional
Expand Down Expand Up @@ -81,17 +84,27 @@ public void delete(String loginUsername, Long id) {
}

@Transactional
public MagazineResponse.Get update(Long id, String loginUsername, MagazineRequest.Update magazineRequest, MagazineCategory magazineCategory) {
public MagazineResponse.Get update(Long id, String loginUsername, MagazineRequest.Update magazineRequest) {
Magazine magazine = magazineRepository.findById(id)
.orElseThrow(() -> new NotFoundException("해당 매거진이 존재하지 않습니다."));

MagazineCategory magazineCategory = getCategory(magazineRequest.getCategorySlug(), magazine.getCategory());

validateOwner(loginUsername, magazine);

magazine.update(magazineRequest, magazineCategory);

return MagazineResponse.Get.from(magazine);
}

private MagazineCategory getCategory(String categorySlug, MagazineCategory defaultCategory) {
if (categorySlug != null) {
return magazineCategoryRepository.findBySlug(categorySlug)
.orElseThrow(() -> new NotFoundException("해당 카테고리가 존재하지 않습니다."));
}
return defaultCategory;
}

private void validateOwner(String loginUsername, Magazine magazine) {
if (!magazine.isOwner(loginUsername)) {
throw new IllegalArgumentException("해당 매거진의 소유자가 아닙니다.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1025,4 +1025,37 @@ public void createAndInviteMember(TeamUser loginUser, Member inviteMember) {
//then
assertTrue(response.contains("잠시 후 다시 시도해주세요."));
}

@WithMockCustomUser(username = "testid", role = "USER")
@DisplayName("매거진 수정시 slug가 없는 경우")
@Test
void 매거진_수정시_slug가_없는경우() throws Exception {
// given
Member author = createMember("testid");
MagazineResponse.Get magazine = createMagaizne(author);
String originalCategorySlug = magazine.getCategorySlug();

MagazineRequest.Update magazineRequest = new MagazineRequest.Update();
magazineRequest.setTitle("수정된 제목");
magazineRequest.setContent("수정된 내용");
magazineRequest.setCategorySlug(null);

// when
String response = mockMvc.perform(
put("/api/magazines/" + magazine.getId())
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(magazineRequest))
)
.andDo(print())
.andExpect(status().isOk())
.andReturn().getResponse().getContentAsString(StandardCharsets.UTF_8);

// then
MagazineResponse.Get magazineResponse = objectMapper.readValue(response, MagazineResponse.Get.class);
assertEquals(magazine.getId(), magazineResponse.getId());
assertEquals(magazineRequest.getTitle(), magazineResponse.getTitle());
assertEquals(magazineRequest.getContent(), magazineResponse.getContent());
assertEquals(originalCategorySlug, magazineResponse.getCategorySlug());
}

}

0 comments on commit d20d988

Please sign in to comment.