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

feat: 매장 지역 목록 조회 API 구현 #79

Merged
merged 1 commit into from
Dec 10, 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
37 changes: 37 additions & 0 deletions src/main/java/com/verby/indp/domain/region/Region.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.verby.indp.domain.region;

import com.verby.indp.domain.region.vo.RegionName;
import jakarta.persistence.Column;
import jakarta.persistence.Embedded;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.Table;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;

@Entity
@Table(name = "region")
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class Region {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "region_id")
private Long regionId;

@Embedded
private RegionName name;

@Column(name = "sequence")
private Integer sequence = Integer.MAX_VALUE;

public Region(String name) {
this.name = new RegionName(name);
}

public String getRegion() {
return name.getName();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.verby.indp.domain.region.controller;

import com.verby.indp.domain.region.service.RegionService;
import com.verby.indp.domain.store.dto.response.FindRegionsResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api")
@RequiredArgsConstructor
public class RegionController {

private final RegionService regionService;

@GetMapping("/regions")
public ResponseEntity<FindRegionsResponse> findRegions() {
return ResponseEntity.ok(regionService.findRegions());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.verby.indp.domain.region.repository;

import com.verby.indp.domain.region.Region;
import com.verby.indp.domain.region.vo.RegionName;
import java.util.List;
import java.util.Optional;
import org.springframework.data.jpa.repository.JpaRepository;

public interface RegionRepository extends JpaRepository<Region, Long> {

Optional<Region> findByName(RegionName name);

List<Region> findAllByOrderBySequenceAsc();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.verby.indp.domain.region.service;

import com.verby.indp.domain.region.Region;
import com.verby.indp.domain.region.repository.RegionRepository;
import com.verby.indp.domain.store.dto.response.FindRegionsResponse;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@RequiredArgsConstructor
@Transactional(readOnly = true)
public class RegionService {

private final RegionRepository regionRepository;

public FindRegionsResponse findRegions() {
List<Region> regions = regionRepository.findAllByOrderBySequenceAsc();
return FindRegionsResponse.from(regions);
}
}
20 changes: 20 additions & 0 deletions src/main/java/com/verby/indp/domain/region/vo/RegionName.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.verby.indp.domain.region.vo;

import jakarta.persistence.Column;
import jakarta.persistence.Embeddable;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Getter
@Embeddable
@NoArgsConstructor(access = lombok.AccessLevel.PROTECTED)
public class RegionName {

@Column(name = "name")
private String name;

public RegionName(String name) {
this.name = name;
}

}
13 changes: 6 additions & 7 deletions src/main/java/com/verby/indp/domain/store/Store.java
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
package com.verby.indp.domain.store;

import static jakarta.persistence.EnumType.STRING;

import com.verby.indp.domain.common.entity.BaseTimeEntity;
import com.verby.indp.domain.common.vo.Address;
import com.verby.indp.domain.region.Region;
import com.verby.indp.domain.song.SongForm;
import com.verby.indp.domain.store.constant.Region;
import com.verby.indp.domain.store.vo.StoreName;
import com.verby.indp.domain.theme.Theme;
import jakarta.persistence.CascadeType;
import jakarta.persistence.Column;
import jakarta.persistence.Embedded;
import jakarta.persistence.Entity;
import jakarta.persistence.Enumerated;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.OneToMany;
import jakarta.persistence.Table;
import java.util.ArrayList;
Expand All @@ -41,8 +40,8 @@ public class Store extends BaseTimeEntity {
@Embedded
private Address address;

@Enumerated(STRING)
@Column(name = "region")
@ManyToOne
@JoinColumn(name = "region_id")
private Region region;

@OneToMany(mappedBy = "store", orphanRemoval = true, cascade = CascadeType.ALL)
Expand Down Expand Up @@ -136,7 +135,7 @@ public String getName() {
}

public String getRegion() {
return region.name();
return region.getRegion();
}

public String getAddress() {
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.verby.indp.domain.store.controller;

import com.verby.indp.domain.store.constant.Region;
import com.verby.indp.domain.store.dto.request.AddStoreByAdminRequest;
import com.verby.indp.domain.store.dto.request.UpdateStoreByAdminRequest;
import com.verby.indp.domain.store.dto.response.FindSimpleStoresResponse;
Expand Down Expand Up @@ -37,7 +36,7 @@ public ResponseEntity<FindSimpleStoresResponse> findSimpleStores(Pageable pageab
@GetMapping("/stores")
public ResponseEntity<FindStoresResponse> findStores(
Pageable pageable,
@RequestParam(name = "region", required = false) Region region
@RequestParam(name = "region", required = false) String region
) {
return ResponseEntity.ok(storeService.findStores(pageable, region));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.verby.indp.domain.store.dto.response;

import com.verby.indp.domain.region.Region;
import java.util.List;

public record FindRegionsResponse(List<String> regions) {

public static FindRegionsResponse from(List<Region> regions) {
List<String> stringRegions = regions.stream()
.map(Region::getRegion)
.toList();

return new FindRegionsResponse(stringRegions);
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.verby.indp.domain.store.repository;

import com.verby.indp.domain.region.Region;
import com.verby.indp.domain.store.Store;
import com.verby.indp.domain.store.constant.Region;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package com.verby.indp.domain.store.service;


import com.verby.indp.domain.common.exception.NotFoundException;
import com.verby.indp.domain.region.Region;
import com.verby.indp.domain.region.repository.RegionRepository;
import com.verby.indp.domain.region.vo.RegionName;
import com.verby.indp.domain.song.SongForm;
import com.verby.indp.domain.song.repository.SongFormRepository;
import com.verby.indp.domain.song.vo.SongFormName;
import com.verby.indp.domain.store.Store;
import com.verby.indp.domain.store.constant.Region;
import com.verby.indp.domain.store.dto.request.AddStoreByAdminRequest;
import com.verby.indp.domain.store.dto.request.UpdateStoreByAdminRequest;
import com.verby.indp.domain.store.dto.response.FindSimpleStoresResponse;
Expand All @@ -32,21 +35,23 @@ public class StoreService {
private final StoreRepository storeRepository;
private final ThemeRepository themeRepository;
private final SongFormRepository songFormRepository;
private final RegionRepository regionRepository;

public FindSimpleStoresResponse findSimpleStores(Pageable pageable) {
Page<Store> page = storeRepository.findAllByOrderByStoreIdAsc(pageable);

return FindSimpleStoresResponse.from(page);
}

public FindStoresResponse findStores(Pageable pageable, Region region) {
if (region == null) {
public FindStoresResponse findStores(Pageable pageable, String regionName) {
if (regionName == null) {
Page<Store> page = storeRepository.findAllByOrderByStoreIdAsc(
pageable);

return FindStoresResponse.from(page);
}

Region region = getRegionByName(regionName);
Page<Store> page = storeRepository.findAllByRegionOrderByStoreIdAsc(pageable, region);

return FindStoresResponse.from(page);
Expand Down Expand Up @@ -74,7 +79,7 @@ public long addStore(AddStoreByAdminRequest request) {
Store store = new Store(
request.name(),
request.address(),
Region.valueOf(request.region()),
getRegion(request.name()),
List.of(request.imageUrl()),
getThemes(request.themes()),
getSongForms(request.songForms())
Expand All @@ -89,7 +94,7 @@ public void updateStore(Long storeId, UpdateStoreByAdminRequest request) {
store.update(
request.name(),
request.address(),
Region.valueOf(request.region()),
getRegion(request.name()),
List.of(request.imageUrl()),
getThemes(request.themes()),
getSongForms(request.songForms())
Expand Down Expand Up @@ -121,8 +126,18 @@ private ArrayList<Theme> getThemes(List<String> names) {
return themes;
}

private Region getRegion(String name) {
return regionRepository.findByName(new RegionName(name))
.orElseGet(() -> regionRepository.save(new Region(name)));
}

private Store getStoreById(long storeId) {
return storeRepository.findById(storeId)
.orElseThrow(() -> new NotFoundException("존재하지 않는 매장입니다."));
}

private Region getRegionByName(String name) {
return regionRepository.findByName(new RegionName(name))
.orElseThrow(() -> new NotFoundException("등록되지 않은 지역입니다."));
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package com.verby.indp.acceptance;

import static com.verby.indp.domain.region.fixture.RegionFixture.region;
import static com.verby.indp.domain.store.fixture.StoreFixture.store;
import static org.assertj.core.api.Assertions.assertThat;

import com.verby.indp.acceptance.support.RecommendationSupporter;
import com.verby.indp.domain.recommendation.dto.request.RegisterRecommendationRequest;
import com.verby.indp.domain.region.Region;
import com.verby.indp.domain.region.repository.RegionRepository;
import com.verby.indp.domain.store.Store;
import com.verby.indp.domain.store.repository.StoreRepository;
import io.restassured.response.ExtractableResponse;
Expand All @@ -19,11 +22,17 @@ class RecommendationAcceptanceTest extends BaseAcceptanceTest {
@Autowired
private StoreRepository storeRepository;

@Autowired
private RegionRepository regionRepository;

@Test
@DisplayName("추천 음악 정보를 등록한다.")
void registerRecommendation() {
// given
Store store = store();
Region 서울 = region("서울");
regionRepository.save(서울);

Store store = store(서울);
storeRepository.save(store);

String information = "아이유-밤편지";
Expand Down
Loading
Loading