Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Issue] 매거진 수정시 categorySlug가 null을 허용하도록 변경 #169

Merged
merged 4 commits into from
May 15, 2024
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 @@ -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());
}

}
Loading