feat(store): 매장 생성 API 구현

This commit is contained in:
bum12ark
2022-03-18 20:46:02 +09:00
parent a22ca6cd4b
commit be795e9c6a
8 changed files with 161 additions and 29 deletions

View File

@@ -0,0 +1,75 @@
package com.justpickup.storeservice.domain.store.dto;
import com.justpickup.storeservice.domain.map.entity.Map;
import com.justpickup.storeservice.domain.store.entity.Store;
import com.justpickup.storeservice.global.entity.Address;
import lombok.AccessLevel;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
@Getter @NoArgsConstructor(access = AccessLevel.PROTECTED)
public class PostStoreDto {
private String name;
private String phoneNumber;
private Long userId;
private _PostStoreAddress address;
private _PostStoreMap map;
@Builder
public PostStoreDto(String name, String phoneNumber, Long userId, _PostStoreAddress address, _PostStoreMap map) {
this.name = name;
this.phoneNumber = phoneNumber;
this.userId = userId;
this.address = address;
this.map = map;
}
public static PostStoreDto of(Store store) {
PostStoreDto postStoreDto = new PostStoreDto();
postStoreDto.name = store.getName();
postStoreDto.phoneNumber = store.getPhoneNumber();
postStoreDto.userId = store.getUserId();
postStoreDto.address = _PostStoreAddress.of(store.getAddress());
postStoreDto.map = _PostStoreMap.of(store.getMap());
return postStoreDto;
}
@Getter @NoArgsConstructor(access = AccessLevel.PROTECTED)
public static class _PostStoreAddress {
private String address;
private String zipcode;
@Builder
public _PostStoreAddress(String address, String zipcode) {
this.address = address;
this.zipcode = zipcode;
}
public static _PostStoreAddress of(Address address) {
_PostStoreAddress postStoreAddress = new _PostStoreAddress();
postStoreAddress.address = address.getAddress();
postStoreAddress.zipcode = address.getAddress();
return postStoreAddress;
}
}
@Getter @NoArgsConstructor(access = AccessLevel.PROTECTED)
public static class _PostStoreMap {
private Double latitude;
private Double longitude;
@Builder
public _PostStoreMap(Double latitude, Double longitude) {
this.latitude = latitude;
this.longitude = longitude;
}
public static _PostStoreMap of(Map map) {
_PostStoreMap postStoreMap = new _PostStoreMap();
postStoreMap.latitude = map.getLatitude();
postStoreMap.longitude = map.getLongitude();
return postStoreMap;
}
}
}

View File

@@ -68,12 +68,13 @@ public class Store extends BaseEntity {
item.setStore(this);
}
public static Store of(Address address, Map map, Long userId, String name) {
public static Store of(Address address, Map map, Long userId, String name, String phoneNumber) {
Store store = new Store();
store.address = address;
store.map = map;
store.userId = userId;
store.name = name;
store.phoneNumber = phoneNumber;
return store;
}
}

View File

@@ -1,9 +1,6 @@
package com.justpickup.storeservice.domain.store.service;
import com.justpickup.storeservice.domain.store.dto.SearchStoreCondition;
import com.justpickup.storeservice.domain.store.dto.SearchStoreResult;
import com.justpickup.storeservice.domain.store.dto.StoreByUserIdDto;
import com.justpickup.storeservice.domain.store.dto.StoreDto;
import com.justpickup.storeservice.domain.store.dto.*;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.SliceImpl;
@@ -15,4 +12,5 @@ public interface StoreService {
StoreDto findStoreById(Long storeId);
StoreByUserIdDto findStoreByUserId(Long userId);
List<StoreDto> findStoreAllById(Iterable<Long> storeIds);
PostStoreDto saveStore(PostStoreDto postStoreDto);
}

View File

@@ -1,14 +1,13 @@
package com.justpickup.storeservice.domain.store.service;
import com.justpickup.storeservice.domain.favoritestore.repository.FavoriteStoreCustom;
import com.justpickup.storeservice.domain.store.dto.SearchStoreCondition;
import com.justpickup.storeservice.domain.store.dto.SearchStoreResult;
import com.justpickup.storeservice.domain.store.dto.StoreByUserIdDto;
import com.justpickup.storeservice.domain.store.dto.StoreDto;
import com.justpickup.storeservice.domain.map.entity.Map;
import com.justpickup.storeservice.domain.store.dto.*;
import com.justpickup.storeservice.domain.store.entity.Store;
import com.justpickup.storeservice.domain.store.exception.NotExistStoreException;
import com.justpickup.storeservice.domain.store.repository.StoreRepository;
import com.justpickup.storeservice.domain.store.repository.StoreRepositoryCustom;
import com.justpickup.storeservice.global.entity.Address;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.SliceImpl;
@@ -71,4 +70,19 @@ public class StoreServiceImpl implements StoreService {
.map(StoreDto::of)
.collect(Collectors.toList());
}
@Override
public PostStoreDto saveStore(PostStoreDto postStoreDto) {
PostStoreDto._PostStoreAddress postStoreAddress = postStoreDto.getAddress();
Address address = new Address(postStoreAddress.getAddress(), postStoreAddress.getZipcode());
PostStoreDto._PostStoreMap postStoreMap = postStoreDto.getMap();
Map map = Map.of(postStoreMap.getLatitude(), postStoreMap.getLongitude());
Store store =
Store.of(address, map, postStoreDto.getUserId(), postStoreDto.getName(), postStoreDto.getPhoneNumber());
Store savedStore = storeRepository.save(store);
return PostStoreDto.of(savedStore);
}
}

View File

@@ -7,9 +7,7 @@ import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.stream.Collectors;

View File

@@ -1,16 +1,18 @@
package com.justpickup.storeservice.domain.store.web;
import com.justpickup.storeservice.domain.store.dto.PostStoreDto;
import com.justpickup.storeservice.domain.store.dto.StoreByUserIdDto;
import com.justpickup.storeservice.domain.store.service.StoreService;
import com.justpickup.storeservice.global.dto.Result;
import lombok.Data;
import lombok.RequiredArgsConstructor;
import lombok.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
@RestController
@RequiredArgsConstructor
@@ -38,6 +40,47 @@ public class StoreOwnerApiController {
this.id = dto.getId();
this.name = dto.getName();
}
}
@PostMapping("/owner/store")
public ResponseEntity<Result> postStore(@Valid @RequestBody PostStoreRequest postStoreRequest,
@RequestHeader(value="user-id") String userHeader) {
Long userId = Long.valueOf(userHeader);
storeService.saveStore(postStoreRequest.toPostStoreDto(userId));
return ResponseEntity.status(HttpStatus.CREATED).build();
}
@Data @NoArgsConstructor @AllArgsConstructor @Builder
static class PostStoreRequest {
@NotEmpty
private String name;
@NotEmpty
private String phoneNumber;
@NotEmpty
private String address;
@NotEmpty
private String zipcode;
@NotNull
private Double latitude;
@NotNull
private Double longitude;
public PostStoreDto toPostStoreDto(Long userId) {
PostStoreDto._PostStoreAddress address =
PostStoreDto._PostStoreAddress.builder().address(this.address).zipcode(this.zipcode).build();
PostStoreDto._PostStoreMap map =
PostStoreDto._PostStoreMap.builder().latitude(this.latitude).longitude(this.longitude).build();
return PostStoreDto.builder()
.name(this.name)
.phoneNumber(this.phoneNumber)
.userId(userId)
.address(address)
.map(map)
.build();
}
}
}

View File

@@ -116,7 +116,7 @@ public class SqlCommandLineRunner implements CommandLineRunner {
List<Item> items = List.of(아메리카노, 카페라떼, 카페모카, 콜드브루, 녹차라떼, 딸기라떼, 녹차, 히비스커스);
itemRepository.saveAll(items);
items.forEach(item -> store.addItem(item));
items.forEach(store::addItem);
});
}
@@ -136,37 +136,41 @@ public class SqlCommandLineRunner implements CommandLineRunner {
void createStores(StoreRepository storeRepository, List<Store> stores) {
stores.add(
Store.of(
new Address("서울시", "마포구 도화동", "201-20"),
new Address("서울시 마포구 도화동", "201-20"),
Map.of(37.5398271003404, 126.94769672415691),
1L,
"커피온리 마포역점"
"커피온리 마포역점",
"010-1234-5678"
)
);
stores.add(
Store.of(
new Address("서울시", "마포구 도화동", "50-10"),
new Address("서울시 마포구 도화동", "50-10"),
Map.of(37.54010719003089, 126.94556661330861),
2L,
"만랩커피 마포점"
"만랩커피 마포점",
"010-1234-5678"
)
);
stores.add(
Store.of(
new Address("서울시", "마포구 도화동", "555"),
new Address("서울시 마포구 도화동", "555"),
Map.of(37.539797393793755, 126.9453578838543),
3L,
"이디야커피 마포오벨리스크점"
"이디야커피 마포오벨리스크점",
"010-1234-5678"
)
);
stores.add(
Store.of(
new Address("서울시", "영등포구 도림로", "31길 2"),
new Address("서울시 영등포구 도림로", "31길 2"),
Map.of(37.493033141569505, 126.89593667847592),
4L,
"이디야커피 대림역점"
"이디야커피 대림역점",
"010-1234-5678"
)
);

View File

@@ -11,7 +11,6 @@ import javax.persistence.Embeddable;
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@AllArgsConstructor @Getter
public class Address {
private String city;
private String street;
private String address;
private String zipcode;
}