Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 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 @@ -4,17 +4,15 @@
import com.somemore.center.dto.request.PreferItemCreateRequestDto;
import com.somemore.center.dto.response.PreferItemCreateResponseDto;
import com.somemore.center.usecase.command.CreatePreferItemUseCase;
import com.somemore.center.usecase.command.DeletePreferItemUseCase;
import com.somemore.global.common.response.ApiResponse;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import java.util.UUID;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.annotation.Secured;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;

@RequiredArgsConstructor
@RestController
Expand All @@ -23,6 +21,7 @@
public class PreferItemCommandApiController {

private final CreatePreferItemUseCase createPreferItemUseCase;
private final DeletePreferItemUseCase deletePreferItemUseCase;

@Secured("ROLE_CENTER")
@Operation(summary = "기관 선호물품 등록 API")
Expand All @@ -36,4 +35,14 @@ public ApiResponse<PreferItemCreateResponseDto> registerPreferItem(

return ApiResponse.ok(200, responseDto, "관심 기관 등록 성공");
}

@Secured("ROLE_CENTER")
@Operation(summary = "기관 선호물품 삭제 API")
@DeleteMapping("/{preferItemId}")
public ApiResponse<String> deletePreferItem(@CurrentUser UUID centerId, @PathVariable Long preferItemId) {

deletePreferItemUseCase.deletePreferItem(centerId, preferItemId);

return ApiResponse.ok("선호 물품 삭제 성공");
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.somemore.center.repository;
package com.somemore.center.repository.center;

import com.somemore.center.domain.Center;
import org.springframework.data.jpa.repository.JpaRepository;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.somemore.center.repository;
package com.somemore.center.repository.center;

import com.somemore.center.domain.Center;
import com.somemore.center.repository.mapper.CenterOverviewInfo;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.somemore.center.repository;
package com.somemore.center.repository.center;

import com.querydsl.core.types.Projections;
import com.querydsl.core.types.dsl.BooleanExpression;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
package com.somemore.center.repository;
package com.somemore.center.repository.preferitem;

import com.somemore.center.domain.PreferItem;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.UUID;

@Repository
public interface PreferItemRepository extends JpaRepository<PreferItem, Long> {

public interface PreferItemJpaRepository extends JpaRepository<PreferItem, Long> {
List<PreferItem> findByCenterId(UUID centerId);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.somemore.center.repository.preferitem;

import com.somemore.center.domain.PreferItem;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Optional;
import java.util.UUID;

@Repository
public interface PreferItemRepository {
void save(PreferItem preferItem);
void deleteById(Long preferItemId);
Optional<PreferItem> findById(Long preferItemId);
List<PreferItem> findByCenterId(UUID centerId);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.somemore.center.repository.preferitem;

import com.somemore.center.domain.PreferItem;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Optional;
import java.util.UUID;

@RequiredArgsConstructor
@Repository
public class PreferItemRepositoryImpl implements PreferItemRepository{

private final PreferItemJpaRepository preferItemJpaRepository;

@Override
public void save(PreferItem preferItem) {
preferItemJpaRepository.save(preferItem);
}

@Override
public void deleteById(Long preferItemId) {
preferItemJpaRepository.deleteById(preferItemId);
}

@Override
public Optional<PreferItem> findById(Long preferItemId) {
return preferItemJpaRepository.findById(preferItemId);
}

@Override
public List<PreferItem> findByCenterId(UUID centerId) {
return preferItemJpaRepository.findByCenterId(centerId);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import com.somemore.center.domain.PreferItem;
import com.somemore.center.dto.request.PreferItemCreateRequestDto;
import com.somemore.center.dto.response.PreferItemCreateResponseDto;
import com.somemore.center.repository.PreferItemRepository;
import com.somemore.center.repository.preferitem.PreferItemRepository;
import com.somemore.center.usecase.command.CreatePreferItemUseCase;
import com.somemore.center.usecase.query.CenterQueryUseCase;
import lombok.RequiredArgsConstructor;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.somemore.center.service.command;

import com.somemore.center.domain.PreferItem;
import com.somemore.center.repository.preferitem.PreferItemRepository;
import com.somemore.center.usecase.command.DeletePreferItemUseCase;
import com.somemore.global.exception.BadRequestException;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.UUID;

import static com.somemore.global.exception.ExceptionMessage.*;

@RequiredArgsConstructor
@Service
public class DeletePreferItemService implements DeletePreferItemUseCase {

private final PreferItemRepository preferItemRepository;

@Override
public void deletePreferItem(UUID centerId, Long preferItemId) {
PreferItem preferItem = preferItemRepository.findById(preferItemId)
.orElseThrow(() -> new BadRequestException(NOT_EXISTS_PREFER_ITEM));

validatePreferItemOwnership(centerId, preferItem);

preferItemRepository.deleteById(preferItemId);
}

private void validatePreferItemOwnership(UUID centerId, PreferItem preferItem) {
if (preferItem.getCenterId().equals(centerId)) {
return;
}
throw new BadRequestException(UNAUTHORIZED_PREFER_ITEM);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import com.somemore.center.domain.Center;
import com.somemore.center.dto.request.CenterProfileUpdateRequestDto;
import com.somemore.center.repository.CenterRepository;
import com.somemore.center.repository.center.CenterRepository;
import com.somemore.center.usecase.command.UpdateCenterProfileUseCase;
import com.somemore.global.exception.BadRequestException;
import lombok.RequiredArgsConstructor;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import com.somemore.center.repository.mapper.CenterOverviewInfo;
import com.somemore.center.dto.response.CenterProfileResponseDto;
import com.somemore.center.dto.response.PreferItemResponseDto;
import com.somemore.center.repository.CenterRepository;
import com.somemore.center.repository.center.CenterRepository;
import com.somemore.center.usecase.query.CenterQueryUseCase;
import com.somemore.center.usecase.query.PreferItemQueryUseCase;
import com.somemore.global.exception.BadRequestException;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.somemore.center.service.query;

import com.somemore.center.repository.CenterRepository;
import com.somemore.center.repository.center.CenterRepository;
import com.somemore.center.usecase.query.CenterSignUseCase;
import com.somemore.global.exception.BadRequestException;
import lombok.RequiredArgsConstructor;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import com.somemore.center.domain.PreferItem;
import com.somemore.center.dto.response.PreferItemResponseDto;
import com.somemore.center.repository.PreferItemRepository;
import com.somemore.center.repository.preferitem.PreferItemRepository;
import com.somemore.center.usecase.query.PreferItemQueryUseCase;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.somemore.center.usecase.command;

import java.util.UUID;

public interface DeletePreferItemUseCase {
void deletePreferItem(UUID centerId, Long preferItemId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ public enum ExceptionMessage {
UNAUTHORIZED_NOTIFICATION("해당 알림에 권한이 없습니다."),
VOLUNTEER_APPLY_LIST_MISMATCH("봉사 지원 목록과 요청된 봉사 지원 목록이 일치하지 않습니다."),
RECRUIT_BOARD_ID_MISMATCH("모든 봉사 신청이 동일한 모집글 ID를 가져야 합니다."),
NOT_EXISTS_NOTE("존재하지 않는 쪽지입니다.")
NOT_EXISTS_NOTE("존재하지 않는 쪽지입니다."),
NOT_EXISTS_PREFER_ITEM("존재하지 않는 선호물품 Id입니다"),
UNAUTHORIZED_PREFER_ITEM("해당 선호물품에 대한 권한이 없습니다.")

;
private final String message;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.somemore.center.dto.request.PreferItemCreateRequestDto;
import com.somemore.center.dto.response.PreferItemCreateResponseDto;
import com.somemore.center.usecase.command.CreatePreferItemUseCase;
import com.somemore.center.usecase.command.DeletePreferItemUseCase;
import com.somemore.global.exception.BadRequestException;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
Expand All @@ -14,7 +15,9 @@
import java.util.UUID;

import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.verify;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
Expand All @@ -24,6 +27,9 @@ class PreferItemCommandApiControllerTest extends ControllerTestSupport {
@MockBean
private CreatePreferItemUseCase createPreferItemUseCase;

@MockBean
private DeletePreferItemUseCase deletePreferItemUseCase;


@DisplayName("기관은 선호물품을 등록할 수 있다. (controller)")
@Test
Expand Down Expand Up @@ -80,4 +86,69 @@ void registerPreferItem_Fail_WhenCenterNotExists() throws Exception {
// verify
verify(createPreferItemUseCase).createPreferItem(userId, requestDto);
}

@DisplayName("기관은 등록한 선호물품을 삭제할 수 있다. (controller)")
@Test
@WithMockCustomUser(role = "CENTER")
void deletePreferItem() throws Exception {
// given
UUID userId = UUID.fromString("123e4567-e89b-12d3-a456-426614174000");
Long preferItemId = 1L;

// when & then
mockMvc.perform(delete("/api/preferItem/{preferItemId}", preferItemId)
.principal(userId::toString))
.andExpect(status().isOk())
.andExpect(jsonPath("$.code").value(200))
.andExpect(jsonPath("$.message").value("선호 물품 삭제 성공"));

// verify
verify(deletePreferItemUseCase).deletePreferItem(userId, preferItemId);
}

@DisplayName("선호물품을 등록한 기관이 아니라면 선호물품을 삭제할 수 없다. (controller)")
@Test
@WithMockCustomUser(role = "CENTER")
void deletePreferItem_Fail_WhenUnauthorized() throws Exception {
// given
UUID userId = UUID.fromString("123e4567-e89b-12d3-a456-426614174000");
Long preferItemId = 1L;

doThrow(new BadRequestException("다른 기관의 선호 물품은 삭제할 수 없습니다."))
.when(deletePreferItemUseCase)
.deletePreferItem(userId, preferItemId);

// when & then
mockMvc.perform(delete("/api/preferItem/{preferItemId}", preferItemId)
.principal(userId::toString))
.andExpect(status().isBadRequest())
.andExpect(jsonPath("$.status").value(400))
.andExpect(jsonPath("$.detail").value("다른 기관의 선호 물품은 삭제할 수 없습니다."));

// verify
verify(deletePreferItemUseCase).deletePreferItem(userId, preferItemId);
}

@DisplayName("존재하지 않는 선호물품을 삭제할 수 없다. (controller)")
@Test
@WithMockCustomUser(role = "CENTER")
void deletePreferItem_Fail_WhenPreferItemNotExists() throws Exception {
// given
UUID userId = UUID.fromString("123e4567-e89b-12d3-a456-426614174000");
Long preferItemId = 1L;

doThrow(new BadRequestException("존재하지 않는 선호 물품입니다."))
.when(deletePreferItemUseCase)
.deletePreferItem(userId, preferItemId);

// when & then
mockMvc.perform(delete("/api/preferItem/{preferItemId}", preferItemId)
.principal(userId::toString))
.andExpect(status().isBadRequest())
.andExpect(jsonPath("$.status").value(400))
.andExpect(jsonPath("$.detail").value("존재하지 않는 선호 물품입니다."));

// verify
verify(deletePreferItemUseCase).deletePreferItem(userId, preferItemId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.somemore.IntegrationTestSupport;
import com.somemore.center.domain.Center;
import com.somemore.center.repository.center.CenterRepository;
import jakarta.transaction.Transactional;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import com.somemore.IntegrationTestSupport;
import com.somemore.center.domain.PreferItem;
import com.somemore.center.repository.preferitem.PreferItemJpaRepository;
import com.somemore.center.repository.preferitem.PreferItemRepository;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
Expand All @@ -17,6 +19,8 @@ class PreferItemRepositoryTest extends IntegrationTestSupport {
@Autowired
private PreferItemRepository preferItemRepository;

@Autowired
private PreferItemJpaRepository preferItemJpaRepository;

private UUID centerId = UUID.fromString("1a1a1a1a-1a1a-1a1a-1a1a-1a1a1a1a1a1");
private UUID centerId1 = UUID.fromString("1a1a1a1a-1a1a-1a1a-1a1a-1a1a1a1a1a1");
Expand All @@ -32,7 +36,7 @@ void findByCenterId() {
PreferItem preferItem1 = PreferItem.create(centerId1, "간식");
PreferItem preferItem2 = PreferItem.create(centerId2, "수건");
PreferItem preferItem3 = PreferItem.create(centerId3, "식재료");
preferItemRepository.saveAll(List.of(preferItem, preferItem1, preferItem2, preferItem3));
preferItemJpaRepository.saveAll(List.of(preferItem, preferItem1, preferItem2, preferItem3));

//when
List<PreferItem> preferItems = preferItemRepository.findByCenterId(UUID.fromString("1a1a1a1a-1a1a-1a1a-1a1a-1a1a1a1a1a2"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@
import com.somemore.center.domain.Center;
import com.somemore.center.domain.PreferItem;
import com.somemore.center.dto.request.PreferItemCreateRequestDto;
import com.somemore.center.repository.CenterRepository;
import com.somemore.center.repository.PreferItemRepository;
import com.somemore.center.repository.center.CenterRepository;
import com.somemore.center.repository.preferitem.PreferItemJpaRepository;
import com.somemore.center.repository.preferitem.PreferItemRepository;
import com.somemore.global.exception.BadRequestException;
import jakarta.transaction.Transactional;
import org.junit.jupiter.api.DisplayName;
Expand All @@ -26,6 +27,9 @@ class CreatePreferItemServiceTest extends IntegrationTestSupport {
@Autowired
private PreferItemRepository preferItemRepository;

@Autowired
private PreferItemJpaRepository preferItemJpaRepository;

@Autowired
private CenterRepository centerRepository;

Expand All @@ -45,7 +49,7 @@ void createPreferItem() {
createPreferItemService.createPreferItem(center.getId(), requestDto);

//then
PreferItem savedItem = preferItemRepository.findAll().stream()
PreferItem savedItem = preferItemJpaRepository.findAll().stream()
.filter(item -> item.getCenterId().equals(center.getId())
&& item.getItemName().equals(itemName))
.findFirst()
Expand Down
Loading
Loading