Skip to content

Commit

Permalink
Feat: Category 삭제 API 개발 (#68)
Browse files Browse the repository at this point in the history
* Refactor: convention 적용

* Feat: Category 삭제 API 구현
  • Loading branch information
ChooSeoyeon authored Sep 3, 2023
1 parent ffda2aa commit 26430ad
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public ResponseEntity<IdResponse> saveCategory(@RequestBody CategorySaveRequest
}

//Category Name 수정
@PatchMapping("{categoryId}/name")
@PatchMapping("/{categoryId}/name")
public ResponseEntity<Void> updateCategoryName(@PathVariable Long categoryId,
@RequestBody CategoryNameUpdateRequest request,
@AuthenticationPrincipal PrincipalDetails principalDetails) {
Expand All @@ -40,12 +40,21 @@ public ResponseEntity<Void> updateCategoryName(@PathVariable Long categoryId,
}

//Category Bookmark 수정
@PatchMapping("{categoryId}/bookmark")
@PatchMapping("/{categoryId}/bookmark")
public ResponseEntity<Void> updateCategoryBookmark(@PathVariable Long categoryId,
@RequestBody CategoryBookmarkUpdateRequest request,
@AuthenticationPrincipal PrincipalDetails principalDetails) {

categoryService.updateCategoryBookmark(categoryId, request, principalDetails.getMember());
return ResponseEntity.ok().build();
}

// Category 삭제
@DeleteMapping("/{categoryId}")
public ResponseEntity<Void> deleteCategory(@PathVariable Long categoryId,
@AuthenticationPrincipal PrincipalDetails principalDetails) {

categoryService.deleteCategory(categoryId, principalDetails.getMember());
return ResponseEntity.noContent().build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,17 @@ public void updateCategoryBookmark(Long categoryId, CategoryBookmarkUpdateReques
category.updateCategoryBookmark(request);
}

public void deleteCategory(Long categoryId, Member member) {

Category category = categoryRepository.findById(categoryId)
.orElseThrow(() -> new CustomException(ErrorCode.NOT_FOUND_CATEGORY));

validateCategoryOwnerShip(category, member);
categoryRepository.delete(category);
}

public void validateCategoryOwnerShip(Category category, Member member) { // TODO: 위치 혹은 이름 더 적절하게 변경하기

if (!category.isPublishedBy(member))
throw new CustomException(ErrorCode.FORBIDDEN_MEMBER);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ public Url(
}

public Boolean isPublishedBy(Member member) {

return this.getCategory().isPublishedBy(member);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,10 @@
import kdkd.youre.backend.domain.url.domain.Url;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.List;

public interface UrlRepository extends JpaRepository<Url, Long>, UrlCustomRepository {


boolean existsByUrlAddressAndCategory_Member(String address, Member member);

Url findByUrlAddressAndCategory_Member(String address, Member member);

List<Url> findByCategory_Member(Member member);
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public ResponseEntity<IdResponse> saveUrl(@RequestBody UrlSaveRequest request,
}

// url 수정
@PatchMapping("{urlId}")
@PatchMapping("/{urlId}")
public ResponseEntity<Void> updateUrl(@PathVariable Long urlId,
@RequestBody UrlUpdateRequest request,
@AuthenticationPrincipal PrincipalDetails principalDetails) {
Expand All @@ -46,9 +46,9 @@ public ResponseEntity<Void> updateUrl(@PathVariable Long urlId,
}

//Url 삭제
@DeleteMapping("{urlId}")
public ResponseEntity<?> deleteUrl(@PathVariable Long urlId,
@AuthenticationPrincipal PrincipalDetails principalDetails) {
@DeleteMapping("/{urlId}")
public ResponseEntity<Void> deleteUrl(@PathVariable Long urlId,
@AuthenticationPrincipal PrincipalDetails principalDetails) {

urlService.deleteUrl(urlId, principalDetails.getMember());
return ResponseEntity.noContent().build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public void deleteUrl(Long urlId, Member member) {
.orElseThrow(() -> new CustomException(ErrorCode.NOT_FOUND_URL));

validateUrlOwnerShip(url, member);
this.urlRepository.deleteById(urlId);
urlRepository.delete(url);
}

// url 상세 조회
Expand Down Expand Up @@ -150,6 +150,7 @@ public UrlFindAllResponse findAllUrl(UrlFindAllParam params, Pageable pageable)
}

public void validateUrlOwnerShip(Url url, Member member) {

if (!url.isPublishedBy(member))
throw new CustomException(ErrorCode.FORBIDDEN_MEMBER);
}
Expand Down

0 comments on commit 26430ad

Please sign in to comment.