Skip to content

Commit f0b11f1

Browse files
authored
Merge pull request #18 from SOPT-all/feat/#12
Feat/#12 리스트조회 구현
2 parents 240cbfa + f15220c commit f0b11f1

File tree

15 files changed

+157
-73
lines changed

15 files changed

+157
-73
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,28 @@
11
package com.sopkathon.domain.place.controller;
22

3+
import org.springframework.http.ResponseEntity;
4+
import org.springframework.web.bind.annotation.GetMapping;
5+
import org.springframework.web.bind.annotation.RequestMapping;
6+
import org.springframework.web.bind.annotation.RequestParam;
37
import org.springframework.web.bind.annotation.RestController;
48

9+
import com.sopkathon.domain.place.dto.GetPlaceListResponse;
10+
import com.sopkathon.domain.place.service.PlaceService;
11+
import com.sopkathon.global.error.code.SuccessCode;
12+
import com.sopkathon.global.error.dto.SuccessResponse;
13+
14+
import lombok.RequiredArgsConstructor;
15+
516
@RestController
17+
@RequiredArgsConstructor
18+
@RequestMapping("/api/v1/places")
619
public class PlaceController {
20+
private final PlaceService placeService;
21+
22+
@GetMapping
23+
public ResponseEntity<SuccessResponse<?>> getPlaceList(@RequestParam(name = "category", defaultValue = "ALL") String category,
24+
@RequestParam(name = "subway") String subway){
25+
GetPlaceListResponse getPlaceListResponse = placeService.getPlaceList(category, subway);
26+
return ResponseEntity.ok(SuccessResponse.of(SuccessCode.SUCCESS_FETCH, getPlaceListResponse));
27+
}
728
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.sopkathon.domain.place.dto;
2+
3+
import java.util.List;
4+
5+
public record GetPlaceListResponse(
6+
List<GetPlaceResponse> getPlaceResponseList
7+
) {
8+
public static GetPlaceListResponse of(List<GetPlaceResponse> getPlaceResponseList) {
9+
return new GetPlaceListResponse(getPlaceResponseList);
10+
}
11+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.sopkathon.domain.place.dto;
2+
3+
4+
import com.sopkathon.domain.place.entity.PlaceEntity;
5+
6+
import lombok.Builder;
7+
8+
@Builder
9+
public record GetPlaceResponse(
10+
Long placeId,
11+
String name,
12+
String description,
13+
String mapLink,
14+
String photoUrl
15+
) {
16+
17+
public static GetPlaceResponse of(PlaceEntity placeEntity) {
18+
return GetPlaceResponse.builder()
19+
.placeId(placeEntity.getId())
20+
.name(placeEntity.getName())
21+
.description(placeEntity.getDescription())
22+
.mapLink(placeEntity.getMapLink())
23+
.photoUrl(placeEntity.getPhotoUrl())
24+
.build();
25+
}
26+
}
Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.sopkathon.domain.place.entity;
22

3+
import java.util.Arrays;
4+
35
import com.sopkathon.global.error.code.ErrorCode;
46
import com.sopkathon.global.error.exception.BusinessException;
57

@@ -9,18 +11,27 @@
911

1012
@Getter
1113
public enum Category {
12-
NATURAL("자연");
14+
ORCHARD("과수원"),
15+
CROPS("밭작물"),
16+
LIVESTOCK("축산"),
17+
PLANTS_AND_FLOWERS("식물·꽃"),
18+
OTHER("기타");
19+
20+
private String korName;
1321

14-
private String name;
22+
Category(String korName) {
23+
this.korName = korName;
24+
}
1525

16-
Category(String name) {}
1726

18-
public static Category fromName(String name) {
19-
for (Category category : values()) {
20-
if(category.name.equals(name)) {
21-
return category;
22-
}
23-
}
24-
throw new BusinessException(ErrorCode.DATA_NOT_FOUND);
27+
public String getKorName() {
28+
return korName;
29+
}
30+
31+
public static Category fromKorName(String korName) {
32+
return Arrays.stream(Category.values())
33+
.filter(c -> c.korName.equals(korName))
34+
.findFirst()
35+
.orElseThrow(() -> new IllegalArgumentException("Unknown category: " + korName));
2536
}
2637
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.sopkathon.domain.place.entity;
2+
3+
import jakarta.persistence.AttributeConverter;
4+
import jakarta.persistence.Converter;
5+
6+
@Converter(autoApply = true)
7+
public class CategoryConverter implements AttributeConverter<Category, String> {
8+
@Override
9+
public String convertToDatabaseColumn(Category attribute) {
10+
return attribute.getKorName(); // 한글로 저장
11+
}
12+
13+
@Override
14+
public Category convertToEntityAttribute(String dbData) {
15+
return Category.fromKorName(dbData); // 한글 → Enum으로 복원
16+
}
17+
}

src/main/java/com/sopkathon/domain/place/entity/PlaceEntity.java

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
package com.sopkathon.domain.place.entity;
22

33
import com.sopkathon.domain.review.entity.ReviewEntity;
4-
import com.sopkathon.domain.similarPlace.entity.SimilarPlaceEntity;
54
import com.sopkathon.domain.subway.entity.SubwayEntity;
65

76
import jakarta.persistence.Column;
7+
import jakarta.persistence.Convert;
88
import jakarta.persistence.Entity;
99
import jakarta.persistence.EnumType;
1010
import jakarta.persistence.Enumerated;
@@ -29,6 +29,9 @@ public class PlaceEntity {
2929
@GeneratedValue(strategy = GenerationType.IDENTITY)
3030
private Long id;
3131

32+
@Column(name = "name", nullable = false)
33+
private String name;
34+
3235
@Column(name = "duration", nullable = false)
3336
private int duration;
3437

@@ -41,33 +44,23 @@ public class PlaceEntity {
4144
@Column(name = "photo_url", nullable = false)
4245
private String photoUrl;
4346

44-
@Enumerated(EnumType.STRING)
47+
@Convert(converter = CategoryConverter.class)
4548
@Column(name = "category", nullable = false)
4649
private Category category;
4750

4851
@ManyToOne(targetEntity = SubwayEntity.class, fetch = FetchType.LAZY)
4952
@JoinColumn(name = "subway_id", nullable = false)
5053
private SubwayEntity subwayEntity;
5154

52-
@ManyToOne(targetEntity = ReviewEntity.class, fetch = FetchType.LAZY)
53-
@JoinColumn(name = "review_id", nullable = false)
54-
private ReviewEntity reviewEntity;
55-
56-
@ManyToOne(targetEntity = SimilarPlaceEntity.class, fetch = FetchType.LAZY)
57-
@JoinColumn(name = "similar_place_id", nullable = false)
58-
private SimilarPlaceEntity similarPlaceEntity;
5955

6056
@Builder
61-
6257
public PlaceEntity(int duration, String description, String mapLink, String photoUrl, Category category,
63-
SubwayEntity subwayEntity, ReviewEntity reviewEntity, SimilarPlaceEntity similarPlaceEntity) {
58+
SubwayEntity subwayEntity) {
6459
this.duration = duration;
6560
this.description = description;
6661
this.mapLink = mapLink;
6762
this.photoUrl = photoUrl;
6863
this.category = category;
6964
this.subwayEntity = subwayEntity;
70-
this.reviewEntity = reviewEntity;
71-
this.similarPlaceEntity = similarPlaceEntity;
7265
}
7366
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
11
package com.sopkathon.domain.place.repository;
22

3+
import java.util.List;
4+
35
import org.springframework.data.jpa.repository.JpaRepository;
46
import org.springframework.stereotype.Repository;
57

8+
import com.sopkathon.domain.place.entity.Category;
69
import com.sopkathon.domain.place.entity.PlaceEntity;
10+
import com.sopkathon.domain.subway.entity.SubwayEntity;
711

812
@Repository
913
public interface PlaceRepository extends JpaRepository<PlaceEntity,Long> {
14+
List<PlaceEntity> findAllBySubwayEntity(SubwayEntity subwayEntity);
15+
List<PlaceEntity> findAllBySubwayEntityAndCategory(SubwayEntity subwayEntity, Category category);
1016
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,40 @@
11
package com.sopkathon.domain.place.service;
22

3+
import java.util.List;
4+
5+
import org.springframework.stereotype.Service;
6+
7+
import com.sopkathon.domain.place.dto.GetPlaceListResponse;
8+
import com.sopkathon.domain.place.dto.GetPlaceResponse;
9+
import com.sopkathon.domain.place.entity.Category;
10+
import com.sopkathon.domain.place.entity.PlaceEntity;
11+
import com.sopkathon.domain.place.repository.PlaceRepository;
12+
import com.sopkathon.domain.subway.entity.SubwayEntity;
13+
import com.sopkathon.domain.subway.service.SubwayService;
14+
15+
import lombok.RequiredArgsConstructor;
16+
17+
@Service
18+
@RequiredArgsConstructor
319
public class PlaceService {
20+
private final PlaceRepository placeRepository;
21+
private final SubwayService SubwayService;
22+
23+
public GetPlaceListResponse getPlaceList(String category, String subway){
24+
SubwayEntity subwayEntity = SubwayService.getSubwayEntity(subway);
25+
List<PlaceEntity> placeEntityList = getPlacesByCategory(subwayEntity, category);
26+
27+
List<GetPlaceResponse> getPlaceResponseList = placeEntityList.stream()
28+
.map(GetPlaceResponse::of)
29+
.toList();
30+
31+
return new GetPlaceListResponse(getPlaceResponseList);
32+
}
33+
34+
private List<PlaceEntity> getPlacesByCategory(SubwayEntity subwayEntity, String category) {
35+
if ("ALL".equals(category)) {
36+
return placeRepository.findAllBySubwayEntity(subwayEntity);
37+
}
38+
return placeRepository.findAllBySubwayEntityAndCategory(subwayEntity, Category.fromKorName(category));
39+
}
440
}

src/main/java/com/sopkathon/domain/review/entity/ReviewEntity.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

88
import java.time.LocalDate;
99

10+
import com.sopkathon.domain.place.entity.PlaceEntity;
11+
1012
@Entity
1113
@Getter
1214
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@@ -17,7 +19,7 @@ public class ReviewEntity {
1719
private Long id;
1820

1921
@Column(name = "createdAt", nullable = false)
20-
private LocalDate createdAt;
22+
private String createdAt;
2123

2224
@Column(name = "author", nullable = false)
2325
private String author;
@@ -27,4 +29,8 @@ public class ReviewEntity {
2729

2830
@Column(name = "profile_image_url", nullable = false)
2931
private String profileImageUrl;
32+
33+
@ManyToOne(targetEntity = PlaceEntity.class, fetch = FetchType.LAZY)
34+
@JoinColumn(name = "place_id", nullable = false)
35+
private PlaceEntity placeEntity;
3036
}

src/main/java/com/sopkathon/domain/similarPlace/controller/SimilarPlaceController.java

Lines changed: 0 additions & 8 deletions
This file was deleted.

0 commit comments

Comments
 (0)