Skip to content

Commit e708f74

Browse files
committed
feat: 선호물품 등록 기능 구현
- 기관이 선호하는 물품을 등록할 수 있는 API 추가 - 선호물품 테이블 추가 - 선호물품 등록 요청 DTO 구현 - 선호물품 등록 서비스 레이어 구현 - 테스트 코드 작성 및 검증 완료
1 parent 6c708a3 commit e708f74

File tree

7 files changed

+213
-0
lines changed

7 files changed

+213
-0
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.somemore.center.domain;
2+
3+
import jakarta.persistence.*;
4+
import lombok.AccessLevel;
5+
import lombok.Builder;
6+
import lombok.Getter;
7+
import lombok.NoArgsConstructor;
8+
9+
import java.util.UUID;
10+
11+
@NoArgsConstructor(access = AccessLevel.PROTECTED)
12+
@Getter
13+
@Entity
14+
public class PreferItem {
15+
16+
@Id
17+
@GeneratedValue(strategy = GenerationType.IDENTITY)
18+
private Long id;
19+
20+
@Column(name = "center_id", nullable = false)
21+
private UUID centerId;
22+
23+
@Column(name = "item_name", nullable = false)
24+
private String itemName;
25+
26+
@Builder
27+
private PreferItem(UUID centerId, String itemName) {
28+
this.centerId = centerId;
29+
this.itemName = itemName;
30+
}
31+
32+
public static PreferItem create(UUID centerId, String itemName) {
33+
return PreferItem.builder()
34+
.centerId(centerId)
35+
.itemName(itemName)
36+
.build();
37+
}
38+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.somemore.center.dto.request;
2+
3+
4+
import com.somemore.center.domain.PreferItem;
5+
6+
import java.util.UUID;
7+
8+
9+
public record PreferItemCreateRequestDto(
10+
11+
UUID centerId,
12+
String itemName
13+
14+
) {
15+
public PreferItem createPreferItem() {
16+
return PreferItem.create(centerId, itemName);
17+
}
18+
19+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.somemore.center.repository;
2+
3+
import com.somemore.center.domain.PreferItem;
4+
import org.springframework.data.jpa.repository.JpaRepository;
5+
import org.springframework.data.jpa.repository.Query;
6+
import org.springframework.data.repository.query.Param;
7+
import org.springframework.stereotype.Repository;
8+
9+
import java.util.List;
10+
import java.util.UUID;
11+
12+
@Repository
13+
public interface PreferItemRepository extends JpaRepository<PreferItem, Long> {
14+
15+
@Query("SELECT p FROM PreferItem p WHERE p.centerId = :centerId")
16+
List<PreferItem> findByCenterId(@Param("centerId") UUID centerId);
17+
18+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.somemore.center.service.command;
2+
3+
import com.somemore.center.dto.request.PreferItemCreateRequestDto;
4+
import com.somemore.center.repository.PreferItemRepository;
5+
import com.somemore.center.usecase.command.CreatePreferItemUseCase;
6+
import com.somemore.center.usecase.query.CenterQueryUseCase;
7+
import lombok.RequiredArgsConstructor;
8+
import org.springframework.stereotype.Service;
9+
10+
@RequiredArgsConstructor
11+
@Service
12+
public class CreatePreferItemService implements CreatePreferItemUseCase {
13+
14+
private final CenterQueryUseCase centerQueryUseCase;
15+
private final PreferItemRepository preferItemRepository;
16+
17+
@Override
18+
public void createPreferItem(PreferItemCreateRequestDto requestDto) {
19+
20+
centerQueryUseCase.validateCenterExists(requestDto.centerId());
21+
22+
preferItemRepository.save(requestDto.createPreferItem());
23+
}
24+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.somemore.center.usecase.command;
2+
3+
import com.somemore.center.dto.request.PreferItemCreateRequestDto;
4+
5+
public interface CreatePreferItemUseCase {
6+
7+
void createPreferItem(PreferItemCreateRequestDto requestDto);
8+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.somemore.center.repository;
2+
3+
import com.somemore.IntegrationTestSupport;
4+
import com.somemore.center.domain.PreferItem;
5+
import org.junit.jupiter.api.DisplayName;
6+
import org.junit.jupiter.api.Test;
7+
import org.springframework.beans.factory.annotation.Autowired;
8+
9+
import java.util.List;
10+
import java.util.UUID;
11+
12+
import static org.assertj.core.api.Assertions.assertThat;
13+
import static org.assertj.core.api.AssertionsForClassTypes.tuple;
14+
15+
class PreferItemRepositoryTest extends IntegrationTestSupport {
16+
17+
@Autowired
18+
private PreferItemRepository preferItemRepository;
19+
20+
@DisplayName("기관의 id로 선호물품을 검색할 수 있다.")
21+
@Test
22+
void findByCenterId() {
23+
24+
//given
25+
PreferItem preferItem = PreferItem.create(UUID.fromString("1a1a1a1a-1a1a-1a1a-1a1a-1a1a1a1a1a1"), "어린이 동화책");
26+
PreferItem preferItem1 = PreferItem.create(UUID.fromString("1a1a1a1a-1a1a-1a1a-1a1a-1a1a1a1a1a1"), "간식");
27+
PreferItem preferItem2 = PreferItem.create(UUID.fromString("1a1a1a1a-1a1a-1a1a-1a1a-1a1a1a1a1a2"), "수건");
28+
PreferItem preferItem3 = PreferItem.create(UUID.fromString("1a1a1a1a-1a1a-1a1a-1a1a-1a1a1a1a1a3"), "식재료");
29+
preferItemRepository.saveAll(List.of(preferItem, preferItem1, preferItem2, preferItem3));
30+
31+
//when
32+
List<PreferItem> preferItems = preferItemRepository.findByCenterId(UUID.fromString("1a1a1a1a-1a1a-1a1a-1a1a-1a1a1a1a1a2"));
33+
34+
//then
35+
assertThat(preferItems).hasSize(1)
36+
.extracting("centerId", "itemName")
37+
.containsExactlyInAnyOrder(
38+
tuple(UUID.fromString("1a1a1a1a-1a1a-1a1a-1a1a-1a1a1a1a1a2"), "수건")
39+
);
40+
}
41+
42+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package com.somemore.center.service.command;
2+
3+
import com.somemore.IntegrationTestSupport;
4+
import com.somemore.center.domain.Center;
5+
import com.somemore.center.domain.PreferItem;
6+
import com.somemore.center.dto.request.PreferItemCreateRequestDto;
7+
import com.somemore.center.repository.CenterRepository;
8+
import com.somemore.center.repository.PreferItemRepository;
9+
import jakarta.transaction.Transactional;
10+
import org.junit.jupiter.api.DisplayName;
11+
import org.junit.jupiter.api.Test;
12+
import org.springframework.beans.factory.annotation.Autowired;
13+
14+
import static org.assertj.core.api.Assertions.assertThat;
15+
16+
@Transactional
17+
class CreatePreferItemServiceTest extends IntegrationTestSupport {
18+
19+
@Autowired
20+
private CreatePreferItemService createPreferItemService;
21+
22+
@Autowired
23+
private PreferItemRepository preferItemRepository;
24+
25+
@Autowired
26+
private CenterRepository centerRepository;
27+
28+
@DisplayName("기관 아이디와 선호물품 이름을 받아 선호물품을 생성한다.")
29+
@Test
30+
void createPreferItem() {
31+
//given
32+
Center center = Center.create(
33+
"기본 기관 이름",
34+
"010-1234-5678",
35+
"http://example.com/image.jpg",
36+
"기관 소개 내용",
37+
"http://example.com",
38+
"account123",
39+
"password123"
40+
);
41+
centerRepository.save(center);
42+
String itemName = "어린이 도서";
43+
44+
45+
PreferItemCreateRequestDto requestDto = new PreferItemCreateRequestDto(
46+
center.getId(),
47+
itemName
48+
);
49+
50+
//when
51+
createPreferItemService.createPreferItem(requestDto);
52+
53+
//then
54+
PreferItem savedItem = preferItemRepository.findAll().stream()
55+
.filter(item -> item.getCenterId().equals(center.getId())
56+
&& item.getItemName().equals(itemName))
57+
.findFirst()
58+
.orElseThrow(() -> new AssertionError("저장된 선호물품을 찾을 수 없습니다."));
59+
60+
assertThat(savedItem.getCenterId()).isEqualTo(center.getId());
61+
assertThat(savedItem.getItemName()).isEqualTo(itemName);
62+
}
63+
64+
}

0 commit comments

Comments
 (0)