StoreController
package com.metacoding.storev2.store;
import jakarta.servlet.http.HttpServletRequest;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import java.util.List;
@RequiredArgsConstructor
@Controller
public class StoreController {
private final StoreService storeService;
@GetMapping("/store/save-form")
public String saveForm() {
return "store/save-form";
}
@PostMapping("/store/save")
public String save(StoreRequest.SaveDTO saveDTO) {
System.out.println("name: " + saveDTO.getName());
System.out.println("stock: " + saveDTO.getStock());
System.out.println("price: " + saveDTO.getPrice());
storeService.상품등록(saveDTO);
return "redirect:/";
}
@GetMapping("/store/list")
public String list(HttpServletRequest request) {
List<Store> storeList = storeService.상품목록();
request.setAttribute("models", storeList);
return "store/list";
}
@GetMapping("/store/{id}")
public String detail(@PathVariable("id") int id, HttpServletRequest request) {
Store store = storeService.상세보기(id);
request.setAttribute("model", store);
return "store/detail";
}
@PostMapping("/store/{id}/delete")
public String delete(@PathVariable("id") int id) {
storeService.상품삭제(id);
return "redirect:/store/list";
}
@GetMapping("/store/{id}/update-form")
public String updateForm(@PathVariable("id") int id, HttpServletRequest request) {
Store store = storeService.상세보기(id);
request.setAttribute("model", store);
return "store/update-form";
}
@PostMapping("/store/{id}/update")
public String update(@PathVariable("id") int id, StoreRequest.UpdateDTO updateDTO) {
storeService.상품수정(id, updateDTO);
return "redirect:/store/" + id;
}
}
StoreService
package com.metacoding.storev2.store;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
@RequiredArgsConstructor
@Service
public class StoreService {
private final StoreRepository storeRepository;
@Transactional
public void 상품등록(StoreRequest.SaveDTO saveDTO) {
storeRepository.save(saveDTO.getName(), saveDTO.getStock(), saveDTO.getPrice());
}
public List<Store> 상품목록() {
List<Store> storeList = storeRepository.findAll();
return storeList;
}
public Store 상세보기(int id) {
Store store = storeRepository.findById(id);
return store;
}
@Transactional
public void 상품삭제(int id) {
// 상품존재확인
Store store = storeRepository.findById(id);
if (store == null) {
throw new RuntimeException("삭제할 상품이 없습니다.");
}
storeRepository.deleteById(id);
}
@Transactional
public void 상품수정(int id, StoreRequest.UpdateDTO updateDTO) {
// 상품존재확인
Store store = storeRepository.findById(id);
if (store == null) {
throw new RuntimeException("수정할 상품이 없습니다.");
}
storeRepository.update(id, updateDTO);
}
}
StoreRepository
package com.metacoding.storev2.store;
import jakarta.persistence.EntityManager;
import jakarta.persistence.Query;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Repository;
import org.springframework.web.bind.annotation.PathVariable;
import java.util.List;
@RequiredArgsConstructor
@Repository
public class StoreRepository {
private final EntityManager em;
public void save(String name, int stock, int price) {
Query query = em.createNativeQuery("insert into store_tb (name, stock, price) values (?, ?, ?)");
query.setParameter(1, name);
query.setParameter(2, stock);
query.setParameter(3, price);
query.executeUpdate();
}
public List<Store> findAll() {
Query query = em.createNativeQuery("select * from store_tb order by id desc", Store.class);
return query.getResultList();
}
public Store findById(int id) {
Query query = em.createNativeQuery("select * from store_tb where id = ?", Store.class);
query.setParameter(1, id);
return (Store) query.getSingleResult();
}
public void deleteById(int id) {
Query query = em.createNativeQuery("delete from store_tb where id = ?");
query.setParameter(1, id);
query.executeUpdate();
}
public void update(@PathVariable("id") int id, StoreRequest.UpdateDTO updateDTO) {
Query query = em.createNativeQuery("update store_tb set name = ?, stock = ?, price = ? where id = ?");
query.setParameter(1, updateDTO.getName());
query.setParameter(2, updateDTO.getStock());
query.setParameter(3, updateDTO.getPrice());
query.setParameter(4, id);
query.executeUpdate();
}
}
StoreRequest
package com.metacoding.storev2.store;
import lombok.Data;
public class StoreRequest {
@Data
public static class SaveDTO {
private String name;
private Integer stock;
private Integer price;
}
@Data
public static class UpdateDTO {
private String name;
private Integer stock;
private Integer price;
}
}
detail
{{> layout/header}}
<div class="container mt-2">
<div class="mt-4 p-5 bg-light text-dark rounded-4">
<p>번호 : {{model.id}}</p>
<p>상품명 : {{model.name}}</p>
<p>상품가격 : {{model.price}}</p>
<p>상품재고 : {{model.stock}}</p>
</div>
<div class="mt-3 mb-3">
<a href="/store/{{model.id}}/update-form" class="btn btn-outline-primary">수정</a>
<form action="/store/{{model.id}}/delete" method="POST" class="d-inline">
<button type="submit" class="btn btn-outline-primary">삭제</button>
</form>
</div>
<div class="mt-4 p-5 bg-light text-dark rounded-4">
<form action="#">
<input type="hidden" value="1">
<input type="text" placeholder="개수를 입력하세요">
<button type="submit" class="btn btn-outline-primary">구매</button>
</form>
</div>
</div>
</body>
</html>
update-form
{{> layout/header}}
<div class="container mt-2">
<div class="mt-4 p-5 bg-light text-dark rounded-4">
<h1>상품수정 페이지</h1>
<form action="/store/{{model.id}}/update" method="post">
<div class="mb-3 mt-3">
<input type="text" class="form-control" name="name" value="딸기">
</div>
<div class="mb-3">
<input type="text" class="form-control" name="stock" value=45>
</div>
<div class="mb-3 mt-3">
<input type="text" class="form-control" name="price" value=2000>
</div>
<button type="submit" class="btn btn-primary">상품수정</button>
</form>
</div>
</div>
</body>
</html>
Share article