diff --git a/src/main/java/com/landvibe/summercoding/SummerCodingApplication.java b/src/main/java/com/landvibe/SummerCodingApplication.java similarity index 89% rename from src/main/java/com/landvibe/summercoding/SummerCodingApplication.java rename to src/main/java/com/landvibe/SummerCodingApplication.java index a16d3b1..7ed0d7c 100644 --- a/src/main/java/com/landvibe/summercoding/SummerCodingApplication.java +++ b/src/main/java/com/landvibe/SummerCodingApplication.java @@ -1,4 +1,4 @@ -package com.landvibe.summercoding; +package com.landvibe; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; diff --git a/src/main/java/com/landvibe/homework/AppConfig.java b/src/main/java/com/landvibe/homework/AppConfig.java new file mode 100644 index 0000000..dd4af93 --- /dev/null +++ b/src/main/java/com/landvibe/homework/AppConfig.java @@ -0,0 +1,47 @@ +package com.landvibe.homework; + +import com.landvibe.homework.repository.MenuRepository; +import com.landvibe.homework.repository.OrderRepository; +import com.landvibe.homework.repository.UserRepository; +import com.landvibe.homework.repository.impl.MemoryMenuRepository; +import com.landvibe.homework.repository.impl.MemoryOrderRepository; +import com.landvibe.homework.repository.impl.MemoryUserRepository; +import com.landvibe.homework.service.MenuService; +import com.landvibe.homework.service.OrderService; +import com.landvibe.homework.service.UserService; +import com.landvibe.homework.service.impl.MenuServiceImpl; +import com.landvibe.homework.service.impl.OrderServiceImpl; +import com.landvibe.homework.service.impl.UserServiceImpl; + +public class AppConfig { + private static final MenuRepository menuRepository = new MemoryMenuRepository(); + private static final OrderRepository orderRepository = new MemoryOrderRepository(); + private static final UserRepository userRepository = new MemoryUserRepository(); + private static final MenuService menuService = new MenuServiceImpl(menuRepository); + private static final OrderService orderService = new OrderServiceImpl(orderRepository,menuRepository); + private static final UserService userService = new UserServiceImpl(userRepository); + + public MenuRepository getMenuRepository() { + return menuRepository; + } + + public OrderRepository getOrderRepository() { + return orderRepository; + } + + public UserRepository getUserRepository() { + return userRepository; + } + + public MenuService getMenuService() { + return menuService; + } + + public OrderService getOrderService() { + return orderService; + } + + public UserService getUserService() { + return userService; + } +} diff --git a/src/main/java/com/landvibe/homework/HomeworkMain.java b/src/main/java/com/landvibe/homework/HomeworkMain.java index 2c47a1f..f092407 100644 --- a/src/main/java/com/landvibe/homework/HomeworkMain.java +++ b/src/main/java/com/landvibe/homework/HomeworkMain.java @@ -1,7 +1,105 @@ package com.landvibe.homework; +import com.landvibe.homework.entity.User; +import com.landvibe.homework.service.MenuService; +import com.landvibe.homework.service.OrderService; +import com.landvibe.homework.service.UserService; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; + public class HomeworkMain { - public static void main(String[] args) { - System.out.println("hello world"); + private static final BufferedReader bf = new BufferedReader(new InputStreamReader(System.in)); + private static final AppConfig appconfig = new AppConfig(); + private static final UserService userService = appconfig.getUserService(); + private static final OrderService orderService = appconfig.getOrderService(); + private static final MenuService menuService = appconfig.getMenuService(); + + public static void main(String[] args) throws IOException { + init(); + userOperation(); + } + + static void init() { + menuService.createMenu("짜장면", 6000); + menuService.createMenu("간짜장", 7000); + menuService.createMenu("짬뽕", 6500); + menuService.createMenu("볶음밥", 7000); + menuService.createMenu("탕수육", 12000); + + } + + static void userOperation() throws IOException { + while (true) { + System.out.println("무엇을 할까요 ?"); + System.out.println("1. 유저 선택"); + System.out.println("2. 유저 목록"); + System.out.println("3. 유저 생성"); + System.out.println("4. 프로그램 종료"); + System.out.println("원하는 작업의 숫자를 입력해 주세요"); + int op = Integer.parseInt(bf.readLine()); + if (op == 1) { + System.out.println("선택하실 유저의 이름을 입력해 주세요"); + String name = bf.readLine(); + User user = userService.findByName(name); + if (user == null) { + System.out.println("존재하지 않는 유저입니다\n"); + } else { + menuOperation(user); + } + } else if (op == 2) { + userService.printUserList(); + } else if (op == 3) { + System.out.println("생성할 유저 이름을 입력해 주세요"); + String name = bf.readLine(); + System.out.println("초기 금액을 입력해 주세요"); + int credit = Integer.parseInt(bf.readLine()); + userService.createUser(name, credit); + } else if (op == 4) { + System.out.println("안녕히가세요 ~"); + return; + } else { + System.out.println("ERROR : 1에서 4사이의 수를 입력해 주세요\n"); + } + } + } + + static void menuOperation(User user) throws IOException { + while (true) { + System.out.println("무엇을 할까요 ?"); + System.out.println("1. 메뉴판 보기"); + System.out.println("2. 주문하기"); + System.out.println("3. 내 주문 내역 보기"); + System.out.println("4. 전체 주문 내역 보기"); + System.out.println("5. 내 잔고 보기"); + System.out.println("6. 돈벌기"); + System.out.println("7. 유저 선택 화면으로"); + + int cmd = Integer.parseInt(bf.readLine()); + if (cmd == 1) { + menuService.printAllMenu(); + } else if (cmd == 2) { + System.out.println("어떤 메뉴를 주문하시겠습니까? 주문번호 혹은 메뉴명을 적어주세요"); + String menuName = bf.readLine(); + orderService.createOrder(user, menuService.findMenu(menuName)); + } else if (cmd == 3) { + orderService.printOrderList(user); + } else if (cmd == 4) { + orderService.printOrderList(); + } else if (cmd == 5) { + userService.checkCredit(user); + } else if (cmd == 6) { + System.out.println("얼마나 버시겠어요 ? 숫자를 입력해 주세요"); + int value = Integer.parseInt(bf.readLine()); + userService.earnCredit(user, value); + } else if (cmd == 7) { + System.out.println("유저 선택 화면으로 이동합니다 \n"); + return; + } else { + System.out.println("올바르지 않은 입력입니다"); + } + } } } + diff --git a/src/main/java/com/landvibe/homework/entity/Menu.java b/src/main/java/com/landvibe/homework/entity/Menu.java new file mode 100644 index 0000000..d235bd8 --- /dev/null +++ b/src/main/java/com/landvibe/homework/entity/Menu.java @@ -0,0 +1,46 @@ +package com.landvibe.homework.entity; + +public class Menu { + private Long id; + private String name; + private Integer price; + + public Menu(Long id, String name, Integer price) { + this.id = id; + this.name = name; + this.price = price; + } + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public Integer getPrice() { + return price; + } + + public void setPrice(Integer price) { + this.price = price; + } + + public void print() { + System.out.println(this.name + " " + this.price + "원"); + } + + @Override + public String toString() { + return id + ". " + name + " " + price + "원"; + } +} diff --git a/src/main/java/com/landvibe/homework/entity/Order.java b/src/main/java/com/landvibe/homework/entity/Order.java new file mode 100644 index 0000000..e45e504 --- /dev/null +++ b/src/main/java/com/landvibe/homework/entity/Order.java @@ -0,0 +1,43 @@ +package com.landvibe.homework.entity; + +public class Order { + + private Long id; + private Menu menu; + private User user; + + public Order(Long id, Menu menu, User user) { + this.id = id; + this.menu = menu; + this.user = user; + } + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public Menu getMenu() { + return menu; + } + + public void setMenu(Menu menu) { + this.menu = menu; + } + + public User getUser() { + return user; + } + + public void setUser(User user) { + this.user = user; + } + + @Override + public String toString() { + return "이름 : " + user.getName() + "\n메뉴 : " + menu.getName() + ", 가격 : " + menu.getPrice(); + } +} diff --git a/src/main/java/com/landvibe/homework/entity/User.java b/src/main/java/com/landvibe/homework/entity/User.java new file mode 100644 index 0000000..6ac8cd2 --- /dev/null +++ b/src/main/java/com/landvibe/homework/entity/User.java @@ -0,0 +1,65 @@ +package com.landvibe.homework.entity; + +import java.util.ArrayList; +import java.util.List; + +public class User { + + private Long id; + private String name; + private Integer credit; + private List orderList; + + public User(Long id, String name, Integer credit) { + this.id = id; + this.name = name; + this.credit = credit; + this.orderList = new ArrayList<>(); + } + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public Integer getCredit() { + return credit; + } + + public void setCredit(Integer credit) { + this.credit = credit; + } + + public List getOrderList() { + return orderList; + } + + public void setOrderList(List orderList) { + this.orderList = orderList; + } + + public void work(int value) { + this.credit += value; + } + + @Override + public String toString() { + return "이름 : " + name + "\n잔액 : " + credit; + } + + public void addOrder(Order order) { + orderList.add(order); + credit -= order.getMenu().getPrice(); + } +} diff --git a/src/main/java/com/landvibe/homework/repository/MenuRepository.java b/src/main/java/com/landvibe/homework/repository/MenuRepository.java new file mode 100644 index 0000000..281a602 --- /dev/null +++ b/src/main/java/com/landvibe/homework/repository/MenuRepository.java @@ -0,0 +1,17 @@ +package com.landvibe.homework.repository; + +import com.landvibe.homework.entity.Menu; + +import java.util.List; + +public interface MenuRepository { + Long getCount(); + + void save(Menu menu); + + Menu findById(Long menuId); + + Menu findByName(String menuName); + + List findAll(); +} diff --git a/src/main/java/com/landvibe/homework/repository/OrderRepository.java b/src/main/java/com/landvibe/homework/repository/OrderRepository.java new file mode 100644 index 0000000..325ff9a --- /dev/null +++ b/src/main/java/com/landvibe/homework/repository/OrderRepository.java @@ -0,0 +1,16 @@ +package com.landvibe.homework.repository; + +import com.landvibe.homework.entity.Order; + +import java.util.List; + +public interface OrderRepository { + + Long getCount(); + + void save(Order order); + + List findAll(); + + List findOrderByUser(Long userId); +} diff --git a/src/main/java/com/landvibe/homework/repository/UserRepository.java b/src/main/java/com/landvibe/homework/repository/UserRepository.java new file mode 100644 index 0000000..48b66d5 --- /dev/null +++ b/src/main/java/com/landvibe/homework/repository/UserRepository.java @@ -0,0 +1,18 @@ +package com.landvibe.homework.repository; + +import com.landvibe.homework.entity.User; + +import java.util.List; + +public interface UserRepository { + + void save(User user); + + User findById(Long userId); + + User findByName(String userName); + + Long getCount(); + + List findAll(); +} diff --git a/src/main/java/com/landvibe/homework/repository/impl/MemoryMenuRepository.java b/src/main/java/com/landvibe/homework/repository/impl/MemoryMenuRepository.java new file mode 100644 index 0000000..ef084f2 --- /dev/null +++ b/src/main/java/com/landvibe/homework/repository/impl/MemoryMenuRepository.java @@ -0,0 +1,41 @@ +package com.landvibe.homework.repository.impl; + +import com.landvibe.homework.entity.Menu; +import com.landvibe.homework.repository.MenuRepository; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +public class MemoryMenuRepository implements MenuRepository { + private static final Map store = new HashMap<>(); + + @Override + public void save(Menu menu) { + store.put(menu.getId(), menu); + } + + @Override + public Menu findById(Long menuId) { + return store.get(menuId); + } + + @Override + public Menu findByName(String menuName) { + for (Menu menu : store.values()) { + if (menu.getName().equals(menuName)) return menu; + } + return null; + } + + @Override + public List findAll() { + return new ArrayList<>(store.values()); + } + + @Override + public Long getCount() { + return (long) store.size() + 1; + } +} diff --git a/src/main/java/com/landvibe/homework/repository/impl/MemoryOrderRepository.java b/src/main/java/com/landvibe/homework/repository/impl/MemoryOrderRepository.java new file mode 100644 index 0000000..42c42c4 --- /dev/null +++ b/src/main/java/com/landvibe/homework/repository/impl/MemoryOrderRepository.java @@ -0,0 +1,40 @@ +package com.landvibe.homework.repository.impl; + +import com.landvibe.homework.entity.Order; +import com.landvibe.homework.repository.OrderRepository; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +public class MemoryOrderRepository implements OrderRepository { + + private static final Map store = new HashMap<>(); + + @Override + public void save(Order order) { + store.put(order.getId(), order); + } + + @Override + public List findAll() { + return new ArrayList<>(store.values()); + } + + @Override + public List findOrderByUser(Long userId) { + List orderList = new ArrayList<>(); + for (Order order : store.values()) { + if (order.getUser().getId().equals(userId)) + orderList.add(order); + } + + return orderList; + } + + @Override + public Long getCount() { + return (long) store.size() + 1; + } +} diff --git a/src/main/java/com/landvibe/homework/repository/impl/MemoryUserRepository.java b/src/main/java/com/landvibe/homework/repository/impl/MemoryUserRepository.java new file mode 100644 index 0000000..6b86ca2 --- /dev/null +++ b/src/main/java/com/landvibe/homework/repository/impl/MemoryUserRepository.java @@ -0,0 +1,43 @@ +package com.landvibe.homework.repository.impl; + +import com.landvibe.homework.entity.User; +import com.landvibe.homework.repository.UserRepository; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +public class MemoryUserRepository implements UserRepository { + + private static final Map store = new HashMap<>(); + + @Override + public void save(User user) { + store.put(user.getId(), user); + } + + @Override + public User findById(Long userId) { + return store.get(userId); + } + + @Override + public User findByName(String userName) { + for (User user : store.values()) { + if (user.getName().equals(userName)) + return user; + } + return null; + } + + @Override + public Long getCount() { + return (long) store.size() + 1; + } + + @Override + public List findAll() { + return new ArrayList<>(store.values()); + } +} diff --git a/src/main/java/com/landvibe/homework/service/MenuService.java b/src/main/java/com/landvibe/homework/service/MenuService.java new file mode 100644 index 0000000..83c219d --- /dev/null +++ b/src/main/java/com/landvibe/homework/service/MenuService.java @@ -0,0 +1,12 @@ +package com.landvibe.homework.service; + +import com.landvibe.homework.entity.Menu; + +public interface MenuService { + void createMenu(String name, int price); + + void printAllMenu(); + + Menu findMenu(String menuName); + +} diff --git a/src/main/java/com/landvibe/homework/service/OrderService.java b/src/main/java/com/landvibe/homework/service/OrderService.java new file mode 100644 index 0000000..681331a --- /dev/null +++ b/src/main/java/com/landvibe/homework/service/OrderService.java @@ -0,0 +1,12 @@ +package com.landvibe.homework.service; + +import com.landvibe.homework.entity.Menu; +import com.landvibe.homework.entity.User; + +public interface OrderService { + void createOrder(User user, Menu menu); + + void printOrderList(User user); + + void printOrderList(); +} diff --git a/src/main/java/com/landvibe/homework/service/UserService.java b/src/main/java/com/landvibe/homework/service/UserService.java new file mode 100644 index 0000000..c772ef0 --- /dev/null +++ b/src/main/java/com/landvibe/homework/service/UserService.java @@ -0,0 +1,15 @@ +package com.landvibe.homework.service; + +import com.landvibe.homework.entity.User; + +public interface UserService { + void createUser(String name, int credit); + + void checkCredit(User user); + + void earnCredit(User user, int value); + + void printUserList(); + + User findByName(String name); +} diff --git a/src/main/java/com/landvibe/homework/service/impl/MenuServiceImpl.java b/src/main/java/com/landvibe/homework/service/impl/MenuServiceImpl.java new file mode 100644 index 0000000..effe176 --- /dev/null +++ b/src/main/java/com/landvibe/homework/service/impl/MenuServiceImpl.java @@ -0,0 +1,41 @@ +package com.landvibe.homework.service.impl; + +import com.landvibe.homework.entity.Menu; +import com.landvibe.homework.repository.MenuRepository; +import com.landvibe.homework.service.MenuService; + +import java.util.List; + +public class MenuServiceImpl implements MenuService { + + private final MenuRepository menuRepository; + + public MenuServiceImpl(MenuRepository menuRepository) { + this.menuRepository = menuRepository; + } + + @Override + public void createMenu(String name, int price) { + Menu menu = new Menu(menuRepository.getCount(), name, price); + menuRepository.save(menu); + } + + @Override + public void printAllMenu() { + List menuList = menuRepository.findAll(); + for (Menu menu : menuList) { + System.out.println(menu.toString()); + } + System.out.println(); + } + + @Override + public Menu findMenu(String menuName) { + if (Character.isDigit(menuName.charAt(0))) { + Long menuId = Long.parseLong(menuName); + return menuRepository.findById(menuId); + } else { + return menuRepository.findByName(menuName); + } + } +} diff --git a/src/main/java/com/landvibe/homework/service/impl/OrderServiceImpl.java b/src/main/java/com/landvibe/homework/service/impl/OrderServiceImpl.java new file mode 100644 index 0000000..f929875 --- /dev/null +++ b/src/main/java/com/landvibe/homework/service/impl/OrderServiceImpl.java @@ -0,0 +1,52 @@ +package com.landvibe.homework.service.impl; + +import com.landvibe.homework.entity.Menu; +import com.landvibe.homework.entity.Order; +import com.landvibe.homework.entity.User; +import com.landvibe.homework.repository.MenuRepository; +import com.landvibe.homework.repository.OrderRepository; +import com.landvibe.homework.service.OrderService; + +import java.util.List; + +public class OrderServiceImpl implements OrderService { + + private final OrderRepository orderRepository; + private final MenuRepository menuRepository; + + public OrderServiceImpl(OrderRepository orderRepository, MenuRepository menuRepository) { + this.orderRepository = orderRepository; + this.menuRepository = menuRepository; + } + + @Override + public void createOrder(User user, Menu menu) { + if (user.getCredit() < menu.getPrice()) { + System.out.println("잔액이 부족해 주문에 실패하였습니다\n"); + return; + } + Order order = new Order(orderRepository.getCount(), menu, user); + orderRepository.save(order); + user.addOrder(order); + + System.out.println("주문이 완료되었습니다\n"); + } + + @Override + public void printOrderList(User user) { + List orderList = user.getOrderList(); + for (Order order : orderList) { + System.out.println(order.toString()); + } + System.out.println(); + } + + @Override + public void printOrderList() { + List orderList = orderRepository.findAll(); + for (Order order : orderList) { + System.out.println(order.toString()); + } + System.out.println(); + } +} diff --git a/src/main/java/com/landvibe/homework/service/impl/UserServiceImpl.java b/src/main/java/com/landvibe/homework/service/impl/UserServiceImpl.java new file mode 100644 index 0000000..4a65ef9 --- /dev/null +++ b/src/main/java/com/landvibe/homework/service/impl/UserServiceImpl.java @@ -0,0 +1,56 @@ +package com.landvibe.homework.service.impl; + +import com.landvibe.homework.entity.User; +import com.landvibe.homework.repository.UserRepository; +import com.landvibe.homework.service.UserService; + +import java.util.List; + +public class UserServiceImpl implements UserService { + + private final UserRepository userRepository; + + public UserServiceImpl(UserRepository userRepository) { + this.userRepository = userRepository; + } + + @Override + public void createUser(String name, int credit) { + if (userRepository.findByName(name) != null) { + System.out.println("유저 생성 실패 - 이미 존재하는 이름입니다!! \n"); + return; + } + User user = new User(userRepository.getCount(), name, credit); + userRepository.save(user); + System.out.println("유저 생성 완료!!\n"); + } + + @Override + public void checkCredit(User user) { + System.out.println("잔액 : " + user.getCredit() + "원\n"); + } + + @Override + public void earnCredit(User user, int value) { + if (value > 0) { + user.work(value); + System.out.println("돈 벌기 완료!!\n"); + } else { + System.out.println("0보다 큰 정수만 입력해 주세요\n"); + } + } + + @Override + public void printUserList() { + List userList = userRepository.findAll(); + for (User user : userList) { + System.out.println(user.toString()); + } + System.out.println(); + } + + @Override + public User findByName(String name) { + return userRepository.findByName(name); + } +} diff --git a/src/main/java/com/landvibe/week3/controller/CategoryController.java b/src/main/java/com/landvibe/week3/controller/CategoryController.java new file mode 100644 index 0000000..30b52f7 --- /dev/null +++ b/src/main/java/com/landvibe/week3/controller/CategoryController.java @@ -0,0 +1,31 @@ +package com.landvibe.week3.controller; + +import com.landvibe.week3.dto.GetCategoryResDto; +import com.landvibe.week3.dto.PostCategoryReqDto; +import com.landvibe.week3.dto.PostCommonResDto; +import com.landvibe.week3.service.CategoryService; +import lombok.RequiredArgsConstructor; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.*; + +@RequiredArgsConstructor +@RestController +@RequestMapping(path = "/api") +public class CategoryController { + + private final CategoryService categoryService; + + @PostMapping(path = "/category") + public ResponseEntity postCategory(@RequestBody PostCategoryReqDto reqDto) { + PostCommonResDto resDto = categoryService.create(reqDto.getName()); + + return ResponseEntity.ok(resDto); + } + + @GetMapping(path = "/categories") + public ResponseEntity getCategories() { + GetCategoryResDto resDto = categoryService.getAllCategories(); + + return ResponseEntity.ok(resDto); + } +} diff --git a/src/main/java/com/landvibe/week3/controller/ProductController.java b/src/main/java/com/landvibe/week3/controller/ProductController.java new file mode 100644 index 0000000..4ab6b95 --- /dev/null +++ b/src/main/java/com/landvibe/week3/controller/ProductController.java @@ -0,0 +1,39 @@ +package com.landvibe.week3.controller; + +import com.landvibe.week3.dto.GetProductDetailResDto; +import com.landvibe.week3.dto.GetProductResDto; +import com.landvibe.week3.dto.PostCommonResDto; +import com.landvibe.week3.dto.PostProductReqDto; +import com.landvibe.week3.service.ProductService; +import lombok.RequiredArgsConstructor; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.*; + +@RequiredArgsConstructor +@RestController +@RequestMapping(path = "/api") +public class ProductController { + + private final ProductService productService; + + @PostMapping(path = "/product") + public ResponseEntity postProduct(@RequestBody PostProductReqDto reqDto) { + PostCommonResDto resDto = productService.create(reqDto); + + return ResponseEntity.ok(resDto); + } + + @GetMapping(path = "/products") + public ResponseEntity getProducts() { + GetProductResDto resDto = productService.getAll(); + + return ResponseEntity.ok(resDto); + } + + @GetMapping(path = "/product/{productId}") + public ResponseEntity getProductDetail(@PathVariable(name = "productId") Long productId) { + GetProductDetailResDto resDto = productService.getDetail(productId); + + return ResponseEntity.ok(resDto); + } +} diff --git a/src/main/java/com/landvibe/week3/dto/GetCategoryResDto.java b/src/main/java/com/landvibe/week3/dto/GetCategoryResDto.java new file mode 100644 index 0000000..8baced8 --- /dev/null +++ b/src/main/java/com/landvibe/week3/dto/GetCategoryResDto.java @@ -0,0 +1,36 @@ +package com.landvibe.week3.dto; + +import com.landvibe.week3.entity.Category; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Getter; + +import java.util.List; + +@Getter +@Builder +@AllArgsConstructor +public class GetCategoryResDto { + + private Integer size; + private List categories; + + @Getter + @Builder + @AllArgsConstructor + public static class Category { + private Long id; + private String name; + private Integer count; + } + + public void convert(com.landvibe.week3.entity.Category categoryEntity) { + Category category = Category.builder() + .id(categoryEntity.getId()) + .name(categoryEntity.getName()) + .count(categoryEntity.getProductList().size()) + .build(); + + categories.add(category); + } +} diff --git a/src/main/java/com/landvibe/week3/dto/GetProductDetailResDto.java b/src/main/java/com/landvibe/week3/dto/GetProductDetailResDto.java new file mode 100644 index 0000000..19aa4fe --- /dev/null +++ b/src/main/java/com/landvibe/week3/dto/GetProductDetailResDto.java @@ -0,0 +1,35 @@ +package com.landvibe.week3.dto; + +import com.landvibe.week3.entity.Product; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Getter; + +import java.time.LocalDateTime; + +@Getter +@AllArgsConstructor +@Builder +public class GetProductDetailResDto { + + private Product product; + + @Getter + public static class Product { + private Long id; + private Long categoryId; + private String categoryName; + private String name; + private String description; + private LocalDateTime createdAt; + + public Product(com.landvibe.week3.entity.Product product) { + this.id = product.getId(); + this.categoryId = product.getCategory().getId(); + this.categoryName = product.getCategory().getName(); + this.name = product.getName(); + this.description = product.getDescription(); + this.createdAt = product.getCreateTime(); + } + } +} diff --git a/src/main/java/com/landvibe/week3/dto/GetProductResDto.java b/src/main/java/com/landvibe/week3/dto/GetProductResDto.java new file mode 100644 index 0000000..10acda8 --- /dev/null +++ b/src/main/java/com/landvibe/week3/dto/GetProductResDto.java @@ -0,0 +1,38 @@ +package com.landvibe.week3.dto; + +import com.landvibe.week3.entity.Product; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Getter; + +import java.time.LocalDateTime; +import java.util.List; + +@Getter +@AllArgsConstructor +@Builder +public class GetProductResDto { + private Integer size; + private List products; + + @Getter + @AllArgsConstructor + @Builder + public static class Product { + private Long id; + private Long categoryId; + private String name; + private LocalDateTime createdAt; + } + + public void convert(com.landvibe.week3.entity.Product productEntity) { + Product product = Product.builder() + .id(productEntity.getId()) + .categoryId(productEntity.getCategory().getId()) + .name(productEntity.getName()) + .createdAt(productEntity.getCreateTime()) + .build(); + + products.add(product); + } +} diff --git a/src/main/java/com/landvibe/week3/dto/PostCategoryReqDto.java b/src/main/java/com/landvibe/week3/dto/PostCategoryReqDto.java new file mode 100644 index 0000000..0fade2d --- /dev/null +++ b/src/main/java/com/landvibe/week3/dto/PostCategoryReqDto.java @@ -0,0 +1,15 @@ +package com.landvibe.week3.dto; + +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.NoArgsConstructor; +import lombok.Setter; + +@Getter +@Setter +@AllArgsConstructor +@NoArgsConstructor +public class PostCategoryReqDto { + + private String name; +} diff --git a/src/main/java/com/landvibe/week3/dto/PostCommonResDto.java b/src/main/java/com/landvibe/week3/dto/PostCommonResDto.java new file mode 100644 index 0000000..78ff7d5 --- /dev/null +++ b/src/main/java/com/landvibe/week3/dto/PostCommonResDto.java @@ -0,0 +1,21 @@ +package com.landvibe.week3.dto; + +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Getter; + +@Getter +@Builder +@AllArgsConstructor +public class PostCommonResDto { + + private Integer code; + private Result result; + + @Getter + @Builder + @AllArgsConstructor + public static class Result { + private Long id; + } +} diff --git a/src/main/java/com/landvibe/week3/dto/PostProductReqDto.java b/src/main/java/com/landvibe/week3/dto/PostProductReqDto.java new file mode 100644 index 0000000..61cc033 --- /dev/null +++ b/src/main/java/com/landvibe/week3/dto/PostProductReqDto.java @@ -0,0 +1,17 @@ +package com.landvibe.week3.dto; + +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.NoArgsConstructor; +import lombok.Setter; + +@Getter +@Setter +@NoArgsConstructor +@AllArgsConstructor +public class PostProductReqDto { + + private Long categoryId; + private String name; + private String description; +} diff --git a/src/main/java/com/landvibe/week3/entity/Category.java b/src/main/java/com/landvibe/week3/entity/Category.java new file mode 100644 index 0000000..daabd59 --- /dev/null +++ b/src/main/java/com/landvibe/week3/entity/Category.java @@ -0,0 +1,22 @@ +package com.landvibe.week3.entity; + +import lombok.Getter; +import lombok.Setter; + +import java.util.ArrayList; +import java.util.List; + +@Setter +@Getter +public class Category { + + private Long id; + + private String name; + + private List productList = new ArrayList<>(); + + public void addProduct(Product product) { + productList.add(product); + } +} diff --git a/src/main/java/com/landvibe/week3/entity/Product.java b/src/main/java/com/landvibe/week3/entity/Product.java new file mode 100644 index 0000000..8d2dd9f --- /dev/null +++ b/src/main/java/com/landvibe/week3/entity/Product.java @@ -0,0 +1,24 @@ +package com.landvibe.week3.entity; + +import lombok.Getter; +import lombok.Setter; +import org.hibernate.annotations.CreationTimestamp; + +import java.time.LocalDateTime; + +@Getter +@Setter +public class Product { + + private Long id; + + private Category category; + + private String name; + + private String description; + + @CreationTimestamp + private LocalDateTime createTime; + +} diff --git a/src/main/java/com/landvibe/week3/error/ExceptionHandlers.java b/src/main/java/com/landvibe/week3/error/ExceptionHandlers.java new file mode 100644 index 0000000..936a5ad --- /dev/null +++ b/src/main/java/com/landvibe/week3/error/ExceptionHandlers.java @@ -0,0 +1,21 @@ +package com.landvibe.week3.error; + +import com.landvibe.week3.dto.PostCommonResDto; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.ControllerAdvice; +import org.springframework.web.bind.annotation.ExceptionHandler; + +@ControllerAdvice +public class ExceptionHandlers { + + @ExceptionHandler({IllegalArgumentException.class, IllegalStateException.class}) + public ResponseEntity handleArgumentException(Exception e) { + PostCommonResDto resDto = PostCommonResDto.builder() + .code(-1) + .build(); + + e.printStackTrace(); + + return ResponseEntity.ok(resDto); + } +} diff --git a/src/main/java/com/landvibe/week3/repository/CategoryRepository.java b/src/main/java/com/landvibe/week3/repository/CategoryRepository.java new file mode 100644 index 0000000..7f2cba2 --- /dev/null +++ b/src/main/java/com/landvibe/week3/repository/CategoryRepository.java @@ -0,0 +1,19 @@ +package com.landvibe.week3.repository; + +import com.landvibe.week3.entity.Category; +import org.springframework.stereotype.Repository; + +import java.util.List; +import java.util.Optional; + +@Repository +public interface CategoryRepository { + + Category save(Category category); + + Optional findById(Long id); + + List getAll(); + + boolean existName(String name); +} diff --git a/src/main/java/com/landvibe/week3/repository/MemoryCategoryRepository.java b/src/main/java/com/landvibe/week3/repository/MemoryCategoryRepository.java new file mode 100644 index 0000000..29a1444 --- /dev/null +++ b/src/main/java/com/landvibe/week3/repository/MemoryCategoryRepository.java @@ -0,0 +1,36 @@ +package com.landvibe.week3.repository; + +import com.landvibe.week3.entity.Category; +import org.springframework.stereotype.Repository; + +import java.util.*; + +@Repository +public class MemoryCategoryRepository implements CategoryRepository{ + + private static Map db = new HashMap<>(); + private static Long sequence = 0L; + + @Override + public Category save(Category category) { + category.setId(++sequence); + db.put(sequence, category); + return category; + } + + @Override + public Optional findById(Long id) { + return Optional.ofNullable(db.get(id)); + } + + @Override + public List getAll() { + return new ArrayList<>(db.values()); + } + + @Override + public boolean existName(String name) { + return db.values().stream() + .anyMatch(category -> name.equals(category.getName())); + } +} diff --git a/src/main/java/com/landvibe/week3/repository/MemoryProductRepository.java b/src/main/java/com/landvibe/week3/repository/MemoryProductRepository.java new file mode 100644 index 0000000..243ffff --- /dev/null +++ b/src/main/java/com/landvibe/week3/repository/MemoryProductRepository.java @@ -0,0 +1,36 @@ +package com.landvibe.week3.repository; + +import com.landvibe.week3.entity.Product; +import org.springframework.stereotype.Repository; + +import java.util.*; + +@Repository +public class MemoryProductRepository implements ProductRepository { + + private final Map db = new HashMap<>(); + private Long sequence = 0L; + + @Override + public Product save(Product product) { + product.setId(++sequence); + db.put(sequence, product); + return product; + } + + @Override + public Optional findById(Long id) { + return Optional.of(db.get(id)); + } + + @Override + public List getAll() { + return new ArrayList<>(db.values()); + } + + @Override + public boolean existName(String name) { + return db.values().stream() + .anyMatch(product -> name.equals(product.getName())); + } +} diff --git a/src/main/java/com/landvibe/week3/repository/ProductRepository.java b/src/main/java/com/landvibe/week3/repository/ProductRepository.java new file mode 100644 index 0000000..842a52d --- /dev/null +++ b/src/main/java/com/landvibe/week3/repository/ProductRepository.java @@ -0,0 +1,19 @@ +package com.landvibe.week3.repository; + +import com.landvibe.week3.entity.Product; +import org.springframework.stereotype.Repository; + +import java.util.List; +import java.util.Optional; + +@Repository +public interface ProductRepository { + + Product save(Product product); + + Optional findById(Long id); + + List getAll(); + + boolean existName(String name); +} diff --git a/src/main/java/com/landvibe/week3/service/CategoryService.java b/src/main/java/com/landvibe/week3/service/CategoryService.java new file mode 100644 index 0000000..5c2d87a --- /dev/null +++ b/src/main/java/com/landvibe/week3/service/CategoryService.java @@ -0,0 +1,55 @@ +package com.landvibe.week3.service; + +import com.landvibe.week3.dto.GetCategoryResDto; +import com.landvibe.week3.dto.PostCommonResDto; +import com.landvibe.week3.entity.Category; +import com.landvibe.week3.repository.CategoryRepository; +import lombok.AllArgsConstructor; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Service; + +import java.util.ArrayList; +import java.util.List; + +@Service +@RequiredArgsConstructor +public class CategoryService { + + private final CategoryRepository categoryRepository; + + public PostCommonResDto create(String name) { + validate(name); + + Category category = new Category(); + category.setName(name); + categoryRepository.save(category); + + return PostCommonResDto.builder() + .code(0) + .result(PostCommonResDto.Result.builder() + .id(category.getId()) + .build()) + .build(); + } + + private void validate(String name) { + if (name == null) + throw new IllegalArgumentException("Name Cannot Be Null"); + + if (categoryRepository.existName(name)) + throw new IllegalArgumentException("Already Exist Name"); + } + + public GetCategoryResDto getAllCategories() { + List categories = categoryRepository.getAll(); + + GetCategoryResDto resDto = GetCategoryResDto.builder() + .size(categories.size()) + .categories(new ArrayList<>()) + .build(); + + categories.forEach(resDto::convert); + + return resDto; + } +} diff --git a/src/main/java/com/landvibe/week3/service/ProductService.java b/src/main/java/com/landvibe/week3/service/ProductService.java new file mode 100644 index 0000000..9fe4e97 --- /dev/null +++ b/src/main/java/com/landvibe/week3/service/ProductService.java @@ -0,0 +1,82 @@ +package com.landvibe.week3.service; + +import com.landvibe.week3.dto.GetProductDetailResDto; +import com.landvibe.week3.dto.GetProductResDto; +import com.landvibe.week3.dto.PostCommonResDto; +import com.landvibe.week3.dto.PostProductReqDto; +import com.landvibe.week3.entity.Category; +import com.landvibe.week3.entity.Product; +import com.landvibe.week3.repository.CategoryRepository; +import com.landvibe.week3.repository.ProductRepository; +import lombok.AllArgsConstructor; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Service; + +import java.time.LocalDateTime; +import java.util.ArrayList; +import java.util.List; +import java.util.Optional; + +@Service +@RequiredArgsConstructor +public class ProductService { + + private final ProductRepository productRepository; + private final CategoryRepository categoryRepository; + + public PostCommonResDto create(PostProductReqDto reqDto) { + Category category = validate(reqDto); + + Product product = new Product(); + product.setName(reqDto.getName()); + product.setDescription(reqDto.getDescription()); + product.setCategory(category); + product.setCreateTime(LocalDateTime.now().withNano(0)); + category.addProduct(product); + + productRepository.save(product); + + return PostCommonResDto.builder() + .code(0) + .result(PostCommonResDto.Result.builder() + .id(product.getId()) + .build()) + .build(); + } + + private Category validate(PostProductReqDto reqDto) { + if (reqDto.getName()== null) + throw new IllegalArgumentException("Name Cannot Be Null"); + + if (productRepository.existName(reqDto.getName())) + throw new IllegalArgumentException("Already Exist Name"); + + Optional category = categoryRepository.findById(reqDto.getCategoryId()); + if (category.isEmpty()) + throw new IllegalStateException("Cannot Find Category"); + + return category.get(); + } + + public GetProductDetailResDto getDetail(Long productId) { + Optional product = productRepository.findById(productId); + if (product.isEmpty()) throw new IllegalStateException("Cannot Find Product"); + + return GetProductDetailResDto.builder() + .product(new GetProductDetailResDto.Product(product.get())) + .build(); + } + + public GetProductResDto getAll() { + List products = productRepository.getAll(); + + GetProductResDto resDto = GetProductResDto.builder() + .size(products.size()) + .products(new ArrayList<>()) + .build(); + + products.forEach(resDto::convert); + + return resDto; + } +}