Skip to content

Commit

Permalink
Merge pull request #14 from Dae-Hwa/BE/preparation/mockup-api-detail
Browse files Browse the repository at this point in the history
[Be] mockup api-detail
  • Loading branch information
Jiwon-JJW committed Apr 23, 2021
2 parents d4b7763 + a06cbe6 commit 535cdec
Show file tree
Hide file tree
Showing 10 changed files with 1,940 additions and 366 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.codesquad.sidedish.web.sidedish.DTO.ItemDTO;
import com.codesquad.sidedish.web.sidedish.DTO.SidedishDTO;
import com.codesquad.sidedish.web.sidedish.DTO.DetailDTO;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.PropertyNamingStrategy;
Expand All @@ -10,6 +11,8 @@
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class SampleDataFactory {

Expand Down Expand Up @@ -42,6 +45,13 @@ public static List<ItemDTO> createSideSidedishes() {
});
}

public static Map<String, DetailDTO> createDetails() {
List<DetailDTO> detailDTOs = jsonToObject("sample-data/details.json", new TypeReference<List<DetailDTO>>() {
});

return detailDTOs.stream().collect(Collectors.toMap(DetailDTO::getHash, detailDTO -> detailDTO));
}

private static <E> E jsonToObject(String jsonFilePath, TypeReference<E> typeReference) {
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.setPropertyNamingStrategy(PropertyNamingStrategy.SNAKE_CASE);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,216 @@
package com.codesquad.sidedish.web.sidedish.DTO;

import com.codesquad.sidedish.web.sidedish.Price;

import java.util.List;
import java.util.stream.Collectors;

public class DetailDTO {
private String hash;
private Data data;

public DetailDTO() {
}

public DetailDTO(String hash, Data data) {
this.hash = hash;
this.data = data;
}

public String getHash() {
return hash;
}

public void setHash(String hash) {
this.hash = hash;
}

public Data getData() {
return data;
}

public void setData(Data data) {
this.data = data;
}


public static final class DetailDTOBuilder {
private String hash;
private Data data;

private DetailDTOBuilder() {
}

public static DetailDTOBuilder create() {
return new DetailDTOBuilder();
}

public DetailDTOBuilder hash(String hash) {
this.hash = hash;
return this;
}

public DetailDTOBuilder data(Data data) {
this.data = data;
return this;
}

public DetailDTO build() {
return new DetailDTO(hash, data);
}
}

public static class Data {
private String topImage;
private List<String> thumbImages;
private String productDescription;
private Price point;
private String deliveryInfo;
private Price deliveryFee;
private List<Price> prices;
private List<String> detailSection;

public Data() {
}

public Data(String topImage, List<String> thumbImages, String productDescription, Price point, String deliveryInfo, Price deliveryFee, List<Price> prices, List<String> detailSection) {
this.topImage = topImage;
this.thumbImages = thumbImages;
this.productDescription = productDescription;
this.point = point;
this.deliveryInfo = deliveryInfo;
this.deliveryFee = deliveryFee;
this.prices = prices;
this.detailSection = detailSection;
}

public String getTopImage() {
return topImage;
}

public void setTopImage(String topImage) {
this.topImage = topImage;
}

public List<String> getThumbImages() {
return thumbImages;
}

public void setThumbImages(List<String> thumbImages) {
this.thumbImages = thumbImages;
}

public String getProductDescription() {
return productDescription;
}

public void setProductDescription(String productDescription) {
this.productDescription = productDescription;
}

public String getPoint() {
return point.getFormattedPrice();
}

public void setPoint(Price point) {
this.point = point;
}

public String getDeliveryInfo() {
return deliveryInfo;
}

public void setDeliveryInfo(String deliveryInfo) {
this.deliveryInfo = deliveryInfo;
}

public String getDeliveryFee() {
return deliveryFee.getFormattedPrice() + " (40,000원 이상 구매 시 무료)";
}

public void setDeliveryFee(Price deliveryFee) {
this.deliveryFee = deliveryFee;
}

public List<String> getPrices() {
return prices.stream()
.map(Price::getFormattedPrice)
.collect(Collectors.toList());
}

public void setPrices(List<Price> prices) {
this.prices = prices;
}

public List<String> getDetailSection() {
return detailSection;
}

public void setDetailSection(List<String> detailSection) {
this.detailSection = detailSection;
}

public static final class DataBuilder {
private String topImage;
private List<String> thumbImages;
private String productDescription;
private Price point;
private String deliveryInfo;
private Price deliveryFee;
private List<Price> prices;
private List<String> detailSection;

private DataBuilder() {
}

public static DataBuilder create() {
return new DataBuilder();
}

public DataBuilder topImage(String topImage) {
this.topImage = topImage;
return this;
}

public DataBuilder thumbImages(List<String> thumbImages) {
this.thumbImages = thumbImages;
return this;
}

public DataBuilder productDescription(String productDescription) {
this.productDescription = productDescription;
return this;
}

public DataBuilder point(Price point) {
this.point = point;
return this;
}

public DataBuilder deliveryInfo(String deliveryInfo) {
this.deliveryInfo = deliveryInfo;
return this;
}

public DataBuilder deliveryFee(Price deliveryFee) {
this.deliveryFee = deliveryFee;
return this;
}

public DataBuilder prices(List<Price> prices) {
this.prices = prices;
return this;
}

public DataBuilder detailSection(List<String> detailSection) {
this.detailSection = detailSection;
return this;
}

public Data build() {
return new Data(topImage, thumbImages, productDescription, point, deliveryInfo, deliveryFee, prices, detailSection);
}
}

}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.codesquad.sidedish.web.sidedish.DTO;

import java.text.NumberFormat;
import java.text.ParseException;
import com.codesquad.sidedish.web.sidedish.Price;

import java.util.List;

public class ItemDTO {
Expand All @@ -11,14 +11,14 @@ public class ItemDTO {
private List<String> deliveryType;
private String title;
private String description;
private long nPrice;
private long sPrice;
private Price nPrice;
private Price sPrice;
private List<String> badge;

public ItemDTO() {
}

public ItemDTO(String detailHash, String image, String alt, List<String> deliveryType, String title, String description, long nPrice, long sPrice, List<String> badge) {
public ItemDTO(String detailHash, String image, String alt, List<String> deliveryType, String title, String description, Price nPrice, Price sPrice, List<String> badge) {
this.detailHash = detailHash;
this.image = image;
this.alt = alt;
Expand Down Expand Up @@ -79,19 +79,19 @@ public void setDescription(String description) {
}

public String getnPrice() {
return NumberFormat.getInstance().format(nPrice);
return nPrice != null ? nPrice.getFormattedPrice() : "";
}

public void setnPrice(String nPrice) throws ParseException {
this.nPrice = NumberFormat.getInstance().parse(nPrice).longValue();
public void setnPrice(Price nPrice) {
this.nPrice = nPrice;
}

public String getsPrice() {
return NumberFormat.getInstance().format(sPrice);
return sPrice != null ? sPrice.getFormattedPrice() : "";
}

public void setsPrice(String sPrice) throws ParseException {
this.sPrice = NumberFormat.getInstance().parse(sPrice).longValue();
public void setsPrice(Price sPrice) {
this.sPrice = sPrice;
}

public List<String> getBadge() {
Expand All @@ -109,8 +109,8 @@ public static final class ItemDTOBuilder {
private List<String> deliveryType;
private String title;
private String description;
private long nPrice;
private long sPrice;
private Price nPrice;
private Price sPrice;
private List<String> badge;

private ItemDTOBuilder() {
Expand Down Expand Up @@ -150,12 +150,12 @@ public ItemDTOBuilder description(String description) {
return this;
}

public ItemDTOBuilder nPrice(long nPrice) {
public ItemDTOBuilder nPrice(Price nPrice) {
this.nPrice = nPrice;
return this;
}

public ItemDTOBuilder sPrice(long sPrice) {
public ItemDTOBuilder sPrice(Price sPrice) {
this.sPrice = sPrice;
return this;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.codesquad.sidedish.web.sidedish;

import java.text.NumberFormat;
import java.text.ParseException;

public class Price {
private static final String POSTFIX = "원";

private long price;

public Price(String price) {
this.price = parsePrice(price);
}

public String getFormattedPrice() {
return NumberFormat.getInstance().format(price) + POSTFIX;
}

private long parsePrice(String price) {
try {
return NumberFormat.getInstance().parse(price).longValue();
} catch (ParseException e) {
throw new IllegalArgumentException("Error occurred while parsing price, price : " + price, e);
}
}

@Override
public String toString() {
return "Price{" +
"price=" + price +
'}';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.codesquad.sidedish.web.sidedish.controller;

import com.codesquad.sidedish.utils.SampleDataFactory;
import com.codesquad.sidedish.web.sidedish.DTO.DetailDTO;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class DetailController {
@GetMapping("/detail/{hash}")
public DetailDTO readOne(@PathVariable String hash) {
return SampleDataFactory.createDetails().get(hash);
}
}
Loading

0 comments on commit 535cdec

Please sign in to comment.