From c98f1f61ab66d3b7698c1deabd02d38281d29968 Mon Sep 17 00:00:00 2001 From: io-uty Date: Mon, 22 Jul 2024 15:51:34 +0900 Subject: [PATCH] upload --- .../item/controller/HelloController.java | 48 ++++++++++++++ .../item/controller/HomeController.java | 14 ++++ .../item/controller/ItemController.java | 64 +++++++++++++++++++ .../item/controller/ItemCreateForm.java | 23 +++++++ .../item/controller/MemberForm.java | 23 +++++++ .../springintro/item/domain/Item.java | 32 ++++++++++ .../item/repository/ItemRepository.java | 18 ++++++ .../item/repository/MemoryItemRepository.java | 36 +++++++++++ .../springintro/item/service/ItemService.java | 50 +++++++++++++++ .../item/service/memberService.java | 36 +++++++++++ .../item/repository/MemoryItemRepository.java | 62 ++++++++++++++++++ .../item/service/ItemServiceTest.java | 49 ++++++++++++++ 12 files changed, 455 insertions(+) create mode 100644 src/main/java/landvibe/springintro/item/controller/HelloController.java create mode 100644 src/main/java/landvibe/springintro/item/controller/HomeController.java create mode 100644 src/main/java/landvibe/springintro/item/controller/ItemController.java create mode 100644 src/main/java/landvibe/springintro/item/controller/ItemCreateForm.java create mode 100644 src/main/java/landvibe/springintro/item/controller/MemberForm.java create mode 100644 src/main/java/landvibe/springintro/item/domain/Item.java create mode 100644 src/main/java/landvibe/springintro/item/repository/ItemRepository.java create mode 100644 src/main/java/landvibe/springintro/item/repository/MemoryItemRepository.java create mode 100644 src/main/java/landvibe/springintro/item/service/ItemService.java create mode 100644 src/main/java/landvibe/springintro/item/service/memberService.java create mode 100644 src/test/java/landvibe/springintro/item/repository/MemoryItemRepository.java create mode 100644 src/test/java/landvibe/springintro/item/service/ItemServiceTest.java diff --git a/src/main/java/landvibe/springintro/item/controller/HelloController.java b/src/main/java/landvibe/springintro/item/controller/HelloController.java new file mode 100644 index 0000000..a2b9755 --- /dev/null +++ b/src/main/java/landvibe/springintro/item/controller/HelloController.java @@ -0,0 +1,48 @@ +package landvibe.springintro.item.controller; +import org.springframework.stereotype.Controller; +import org.springframework.ui.Model; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.ResponseBody; + +@Controller +public class HelloController { + @GetMapping("hello") + public String hello(Model model) { + model.addAttribute("data", "LandVibe"); + return "hello"; + } + + @GetMapping("hello-mvc") + public String helloMvc( + @RequestParam(value = "name", required = false, defaultValue = + "landVibe") String name, + Model model ){ + model.addAttribute("name", name); + return "hello-template"; + } + + @GetMapping("hello-string") + @ResponseBody + public String helloString(@RequestParam("name") String name) { + return "hello " + name; + } + @GetMapping("hello-api") + @ResponseBody + public Hello helloApi(@RequestParam("name") String name) { + Hello hello = new Hello(); + hello.setName(name); + return hello; + } + static class Hello { + private String name; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + } +} \ No newline at end of file diff --git a/src/main/java/landvibe/springintro/item/controller/HomeController.java b/src/main/java/landvibe/springintro/item/controller/HomeController.java new file mode 100644 index 0000000..5ae4480 --- /dev/null +++ b/src/main/java/landvibe/springintro/item/controller/HomeController.java @@ -0,0 +1,14 @@ +package landvibe.springintro.item.controller; +import org.springframework.stereotype.Controller; +import org.springframework.ui.Model; +import org.springframework.web.bind.annotation.GetMapping; + +@Controller +public class HomeController { + @GetMapping("/") + public String home(Model model) { + model.addAttribute("data", "LandVibe"); + return "home"; + } + +} \ No newline at end of file diff --git a/src/main/java/landvibe/springintro/item/controller/ItemController.java b/src/main/java/landvibe/springintro/item/controller/ItemController.java new file mode 100644 index 0000000..c2bf66b --- /dev/null +++ b/src/main/java/landvibe/springintro/item/controller/ItemController.java @@ -0,0 +1,64 @@ +package landvibe.springintro.item.controller; + +import landvibe.springintro.item.domain.Item; +import landvibe.springintro.item.service.ItemService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Controller; +import org.springframework.ui.Model; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.ModelAttribute; +import org.springframework.web.bind.annotation.PostMapping; + +import java.util.List; + +@Controller +public class ItemController { + private final ItemService itemService; + @Autowired + public ItemController(ItemService itemService) { + this.itemService = itemService; + } + //회원등록 + + @GetMapping("/members/new") + public String createMemberForm(){ + return "members/createMemberForm"; + } + @PostMapping("members/new") + public String create(@ModelAttribute MemberForm form) { + Item item = new Item(); + item.setName(form.getName()); + item.setPrice(form.getId()); + item.setCount(form.getPwd()); + itemService.create(item); + return "redirect:/"; + } + @GetMapping("/members") + public String memberList(Model model){ + List members = itemService.findMembers(); + model.addAttribute("members", members); + return "members/memberList"; + } + +//상품등록 + @GetMapping("/items/new") + public String createItemForm(){ + return "items/createForm"; + } + @PostMapping("/items/new") + public String create(@ModelAttribute ItemCreateForm form) { + Item item = new Item(); + item.setName(form.getName()); + item.setPrice(form.getPrice()); + item.setCount(form.getCount()); + itemService.create(item); + return "redirect:/"; + } + @GetMapping("/items") + public String list(Model model){ + List items = itemService.findItems(); + model.addAttribute("items", items); + return "items/itemList"; + } + +} diff --git a/src/main/java/landvibe/springintro/item/controller/ItemCreateForm.java b/src/main/java/landvibe/springintro/item/controller/ItemCreateForm.java new file mode 100644 index 0000000..2162ebe --- /dev/null +++ b/src/main/java/landvibe/springintro/item/controller/ItemCreateForm.java @@ -0,0 +1,23 @@ +package landvibe.springintro.item.controller; +public class ItemCreateForm { + private String name; + private Integer price; + private Integer count; + 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 Integer getCount() { + return count; + } + public void setCount(Integer count) { + this.count = count; + } } \ No newline at end of file diff --git a/src/main/java/landvibe/springintro/item/controller/MemberForm.java b/src/main/java/landvibe/springintro/item/controller/MemberForm.java new file mode 100644 index 0000000..7272bc0 --- /dev/null +++ b/src/main/java/landvibe/springintro/item/controller/MemberForm.java @@ -0,0 +1,23 @@ +package landvibe.springintro.item.controller; +public class MemberForm { + private String name; + private Integer id; + private Integer pwd; + public String getName() { + return name; + } + public void setName(String name) { + this.name = name; + } + public Integer getId() { + return id; + } + public void setId(Integer id) { + this.id = id; + } + public Integer getPwd() { + return pwd; + } + public void setPwd(Integer pwd) { + this.pwd = pwd; + } } \ No newline at end of file diff --git a/src/main/java/landvibe/springintro/item/domain/Item.java b/src/main/java/landvibe/springintro/item/domain/Item.java new file mode 100644 index 0000000..3309099 --- /dev/null +++ b/src/main/java/landvibe/springintro/item/domain/Item.java @@ -0,0 +1,32 @@ +package landvibe.springintro.item.domain; + +public class Item { + private Long id; + private String name; + private Integer price; + private Integer count; + 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 Integer getCount() { + return count; + } + public void setCount(Integer count) { + this.count = count; + } +} diff --git a/src/main/java/landvibe/springintro/item/repository/ItemRepository.java b/src/main/java/landvibe/springintro/item/repository/ItemRepository.java new file mode 100644 index 0000000..df1317d --- /dev/null +++ b/src/main/java/landvibe/springintro/item/repository/ItemRepository.java @@ -0,0 +1,18 @@ +package landvibe.springintro.item.repository; + +import landvibe.springintro.item.domain.Item; + +import java.util.List; +import java.util.Optional; + +public interface ItemRepository { + + Item save(Item item); + + Optional findById(Long id); + + Optional findByName(String name); + + List findAll(); + +} \ No newline at end of file diff --git a/src/main/java/landvibe/springintro/item/repository/MemoryItemRepository.java b/src/main/java/landvibe/springintro/item/repository/MemoryItemRepository.java new file mode 100644 index 0000000..b27ddf8 --- /dev/null +++ b/src/main/java/landvibe/springintro/item/repository/MemoryItemRepository.java @@ -0,0 +1,36 @@ +package landvibe.springintro.item.repository; + +import landvibe.springintro.item.domain.Item; +import org.springframework.stereotype.Repository; + +import java.util.*; + +@Repository +public class MemoryItemRepository implements ItemRepository { + private static Map store = new HashMap<>(); + private static long sequence = 0L; + @Override + public Item save(Item item) { + item.setId(++sequence); + store.put(item.getId(), item); + return item; + } + @Override + public Optional findById(Long id) { + Item item = store.get(id); + return Optional.ofNullable(item); + } + @Override + public Optional findByName(String name) { + return store.values().stream() + .filter(item -> item.getName().equals(name)) + .findAny(); + } + @Override + public List findAll() { + return new ArrayList<>(store.values()); + } + public void clearStore() { + store.clear(); + } +} diff --git a/src/main/java/landvibe/springintro/item/service/ItemService.java b/src/main/java/landvibe/springintro/item/service/ItemService.java new file mode 100644 index 0000000..51d34e3 --- /dev/null +++ b/src/main/java/landvibe/springintro/item/service/ItemService.java @@ -0,0 +1,50 @@ +package landvibe.springintro.item.service; +import landvibe.springintro.item.domain.Item; +import landvibe.springintro.item.repository.ItemRepository; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import java.util.List; + +@Service +public class ItemService { + private final ItemRepository repository; + @Autowired + public ItemService(ItemRepository repository) { + this.repository = repository; + } + /** + * 상품 등록 */ + public Long create(Item item) { + validateDuplicateItem(item.getName()); + repository.save(item); + + return item.getId(); + } + private void validateDuplicateMember(Item item) { + repository.findByName(item.getName()) + .ifPresent(m->{ + throw new IllegalStateException("이미 존재하는 회원입니다."); + }); + } + /** + * 상품 전체조회 */ + public List findItems() { + return repository.findAll(); + } + private void validateDuplicateItem(String itemName) { + repository.findByName(itemName) + .ifPresent(i -> { + throw new IllegalArgumentException("이미 존재하는 상품입니다."); + }); + } + public List findMembers() { + return repository.findAll(); + } + private void validateDuplicateMember(String itemName) { + repository.findByName(itemName) + .ifPresent(i -> { + throw new IllegalArgumentException("이미 존재하는 상품입니다."); + }); + } +} diff --git a/src/main/java/landvibe/springintro/item/service/memberService.java b/src/main/java/landvibe/springintro/item/service/memberService.java new file mode 100644 index 0000000..b7d7871 --- /dev/null +++ b/src/main/java/landvibe/springintro/item/service/memberService.java @@ -0,0 +1,36 @@ +package landvibe.springintro.item.service; +import landvibe.springintro.item.domain.Item; +import landvibe.springintro.item.repository.ItemRepository; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import java.util.List; + +@Service +public class memberService { + private ItemRepository repository; + @Autowired + public memberService(ItemRepository repository) { + this.repository = repository; + } + + + /** + * 회원 등록 */ + public Long create(Item item) { + validateDuplicateItem(item.getName()); + repository.save(item); + return item.getId(); + } + /** + * 회원 전체조회 */ + public List findItems() { + return repository.findAll(); + } + private void validateDuplicateItem(String itemName) { + repository.findByName(itemName) + .ifPresent(i -> { + throw new IllegalArgumentException("이미 존재하는 상품입니다."); + }); + } +} diff --git a/src/test/java/landvibe/springintro/item/repository/MemoryItemRepository.java b/src/test/java/landvibe/springintro/item/repository/MemoryItemRepository.java new file mode 100644 index 0000000..d496413 --- /dev/null +++ b/src/test/java/landvibe/springintro/item/repository/MemoryItemRepository.java @@ -0,0 +1,62 @@ +package landvibe.springintro.item.repository; + +import landvibe.springintro.item.domain.Item; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.Test; + +import java.util.List; + +import static org.assertj.core.api.Assertions.assertThat; +class MemoryItemRepositoryTest { + MemoryItemRepository repository = new MemoryItemRepository(); + + @AfterEach + void afterEach() { + repository.clearStore(); + } + + @Test + void save() { +// given + Item item = createItem("과자", 10, 1000); // when + repository.save(item); +// then + Item foundItem = repository.findById(item.getId()).get(); + assertThat(foundItem).isEqualTo(item); + } + + @Test + void findByName() { +// given + Item 눈을감자 = createItem("눈을감자", 10, 1000); + Item 프링글스 = createItem("프링글스", 10, 1000); + repository.save(눈을감자); + repository.save(프링글스); +// when + Item foundItem = repository.findByName("눈을감자").get(); +// then + assertThat(foundItem).isEqualTo(눈을감자); + } + + @Test + void findAll() { +// given + Item 눈을감자 = createItem("눈을감자", 10, 1000); + Item 프링글스 = createItem("프링글스", 10, 1000); + repository.save(눈을감자); + repository.save(프링글스); +// when + List foundItems = repository.findAll(); +// then + assertThat(foundItems.size()).isEqualTo(2); + assertThat(foundItems).contains(눈을감자, 프링글스); + } + + private static Item createItem(String name, int count, int price) { + Item item = new Item(); + item.setName(name); + item.setCount(count); + item.setPrice(price); + return item; + } +} \ No newline at end of file diff --git a/src/test/java/landvibe/springintro/item/service/ItemServiceTest.java b/src/test/java/landvibe/springintro/item/service/ItemServiceTest.java new file mode 100644 index 0000000..734c362 --- /dev/null +++ b/src/test/java/landvibe/springintro/item/service/ItemServiceTest.java @@ -0,0 +1,49 @@ +package landvibe.springintro.item.service; + +import landvibe.springintro.item.domain.Item; +import landvibe.springintro.item.repository.MemoryItemRepository; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.jupiter.api.Assertions.assertThrows; +class ItemServiceTest { + ItemService service; + MemoryItemRepository repository; + @BeforeEach + void setUp() { + repository = new MemoryItemRepository(); + service = new ItemService(repository); + } + @AfterEach + void tearDown() { + repository.clearStore(); + } + @Test + public void 상품생성() throws Exception { +// given + Item item = createItem("눈을감자", 10, 1000); // when + Long id = service.create(item); +// then + Item foundItem = repository.findById(id).get(); + assertThat(foundItem).isEqualTo(item); + } + @Test + void 중복이름_상품예외() { +// given + Item item = createItem("눈을감자", 10, 1000); + Item duplicatedItem = createItem("눈을감자", 10, 1000); service.create(item); +// when & then + IllegalArgumentException ex = + assertThrows(IllegalArgumentException.class, + () -> service.create(duplicatedItem));// 예외 발생 assertThat(ex.getMessage()).isEqualTo("이미 존재하는 상품입니다."); + } + private static Item createItem(String name, int count, int price) { + Item item = new Item(); + item.setName(name); + item.setCount(count); + item.setPrice(price); + return item; + } +}