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

[3주차 과제] 박수지 3주차 과제 제출합니다. #6

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
@@ -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;
}
}
}
Original file line number Diff line number Diff line change
@@ -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";
}

}
Original file line number Diff line number Diff line change
@@ -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<Item> 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<Item> items = itemService.findItems();
model.addAttribute("items", items);
return "items/itemList";
}

}
Original file line number Diff line number Diff line change
@@ -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;
} }
23 changes: 23 additions & 0 deletions src/main/java/landvibe/springintro/item/controller/MemberForm.java
Original file line number Diff line number Diff line change
@@ -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;
} }
32 changes: 32 additions & 0 deletions src/main/java/landvibe/springintro/item/domain/Item.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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<Item> findById(Long id);

Optional<Item> findByName(String name);

List<Item> findAll();

}
Original file line number Diff line number Diff line change
@@ -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<Long, Item> 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<Item> findById(Long id) {
Item item = store.get(id);
return Optional.ofNullable(item);
}
@Override
public Optional<Item> findByName(String name) {
return store.values().stream()
.filter(item -> item.getName().equals(name))
.findAny();
}
@Override
public List<Item> findAll() {
return new ArrayList<>(store.values());
}
public void clearStore() {
store.clear();
}
}
50 changes: 50 additions & 0 deletions src/main/java/landvibe/springintro/item/service/ItemService.java
Original file line number Diff line number Diff line change
@@ -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<Item> findItems() {
return repository.findAll();
}
private void validateDuplicateItem(String itemName) {
repository.findByName(itemName)
.ifPresent(i -> {
throw new IllegalArgumentException("이미 존재하는 상품입니다.");
});
}
public List<Item> findMembers() {
return repository.findAll();
}
private void validateDuplicateMember(String itemName) {
repository.findByName(itemName)
.ifPresent(i -> {
throw new IllegalArgumentException("이미 존재하는 상품입니다.");
});
}
}
36 changes: 36 additions & 0 deletions src/main/java/landvibe/springintro/item/service/memberService.java
Original file line number Diff line number Diff line change
@@ -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<Item> findItems() {
return repository.findAll();
}
private void validateDuplicateItem(String itemName) {
repository.findByName(itemName)
.ifPresent(i -> {
throw new IllegalArgumentException("이미 존재하는 상품입니다.");
});
}
}
Loading