ProductRestController 2022/04/19
11184 ワード
ファイル名ProductCountRestControl。java
package com.example.restcontroller;
import java.util.HashMap;
import java.util.Map;
import com.example.entity.ProductCountEntity;
import com.example.repository.ProductCountRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping(value = "/api/productcount")
public class ProductCountRestController {
@Autowired ProductCountRepository pcRepository;
//127.0.0.1:9090/ROOT/api/productcount/insert.json
// {cnt : 12, type:"I" , product:{no:1007}}
// {cnt : -5, type:"O" , product:{no:1007}}
@PostMapping(
value = "/insert.json",
consumes = { MediaType.ALL_VALUE },
produces = { MediaType.APPLICATION_JSON_VALUE})
public Map<String, Object> insertPOST(
@RequestBody ProductCountEntity productCount
){
Map<String, Object> map = new HashMap<>();
try {
pcRepository.save(productCount);
map.put("status", 200);
}
catch (Exception e) {
e.printStackTrace();
map.put("status", 0);
}
return map;
}
@GetMapping( value = "/selectcount.json",
consumes = { MediaType.ALL_VALUE },
produces = { MediaType.APPLICATION_JSON_VALUE})
public Map<String, Object> selectcountGET(
@RequestParam(name = "no") long no
){
Map<String, Object> map = new HashMap<>();
try {
Long total = pcRepository.selectProductCountGroup(no);
map.put("status", 200);
map.put("total", total);
}
catch (Exception e) {
e.printStackTrace();
map.put("status", 0);
}
return map;
}
}
ファイル名ProductRestControl。java
package com.example.restcontroller;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.example.entity.ProductEntity;
import com.example.entity.ProductViewEntity;
import com.example.repository.ProductRepository;
import com.example.repository.ProductViewRepository;
import com.example.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
@RestController
@CrossOrigin("*") // 앱으로 만들경우 앱에서 데이터를 가져갈 수 없음.(포스트맨에선 사용 됨)
@RequestMapping(value = "/api/product")
public class ProductRestController {
@Autowired ProductViewRepository pvRepository;
@Autowired ProductRepository pRepository;
@Autowired ProductService pService;
// 127.0.0.1:9090/ROOT/api/product/insert.json
@PostMapping(
value = "/insert.json",
consumes = { MediaType.ALL_VALUE },
produces = { MediaType.APPLICATION_JSON_VALUE})
public Map<String, Object> insertPOST(
@ModelAttribute ProductEntity product,
// 보낼 때 for-data 받을때 @ModelAttribute
// const body = new formData();
// body.append("uemail",state.uemail)
// body.append("upw",state.upw)
// @RequestBody ProductEntity product
// // 보낼때 jaon 받을때 RequestBody
// // cosnt body = {
// // uemail:state.uemail,
// // upw : state.upw,
// // }
@RequestParam(name = "file", required = false) MultipartFile file
){
Map<String, Object> map = new HashMap<>();
try {
if(file != null){
if( !file.isEmpty() ){
product.setImagedata(file.getBytes());
product.setImagename(file.getOriginalFilename());
product.setImagesize(file.getSize());
product.setImagetype(file.getContentType());
}
}
pRepository.save(product);
map.put("status", 200);
}
catch (Exception e) {
e.printStackTrace();
map.put("status", 0);
}
return map;
}
// 127.0.0.1:9090/ROOT/api/product/insert.json
@PostMapping(
value = "/insertbatch.json",
consumes = { MediaType.ALL_VALUE },
produces = { MediaType.APPLICATION_JSON_VALUE})
public Map<String, Object> insertbatchPOST(
@RequestParam(name = "name") String[] name,
@RequestParam(name = "price") Long[] price,
@RequestParam(name = "file", required = false) MultipartFile[] file
){
Map<String, Object> map = new HashMap<>();
try {
List<ProductEntity> list = new ArrayList<>();
for(int i=0; i<name.length; i++){
// System.out.println("=====product insert batch=====");
// System.out.println(name[i]);
// System.out.println(price[i]);
// System.out.println(file[i].getBytes());
// System.out.println(file[i].getOriginalFilename());
// System.out.println(file[i].getSize());
// System.out.println(file[i].getContentType());
ProductEntity prd = new ProductEntity();
prd.setName(name[i]);
prd.setPrice(price[i]);
if(file[i] != null){
if( !file[i].isEmpty() ){
prd.setImagedata(file[i].getBytes());
prd.setImagename(file[i].getOriginalFilename());
prd.setImagesize(file[i].getSize());
prd.setImagetype(file[i].getContentType());
}
}
list.add(prd);
}
pService.insertBatch(list);
// pRepository.saveAll(list);
map.put("status", 200);
}
catch (Exception e) {
e.printStackTrace();
map.put("status", 0);
}
return map;
}
// 127.0.0.1:9090/ROOT/api/product/update.json
@PutMapping(value = "/update.json",
consumes = { MediaType.ALL_VALUE },
produces = { MediaType.APPLICATION_JSON_VALUE})
public Map<String, Object> updatePatch(
@RequestBody ProductEntity product
){
Map<String, Object> map = new HashMap<>();
try {
ProductEntity product1 = pRepository.findById(product.getNo()).orElse(null);
product1.setName( product.getName() );
product1.setPrice( product.getPrice() );
pRepository.save(product1);
map.put("status", 200);
} catch (Exception e) {
e.printStackTrace();
map.put("status", 0);
}
return map;
}
// 127.0.0.1:9090/ROOT/api/product/selectone.json?no=1007
@GetMapping( value = "/selectone.json",
consumes = { MediaType.ALL_VALUE },
produces = { MediaType.APPLICATION_JSON_VALUE})
public Map<String, Object> selectOnePOST(
@RequestParam(name = "no") long no
){
Map<String, Object> map = new HashMap<>();
try {
ProductViewEntity entity = pvRepository.findById(no).orElse(null);
map.put("result", entity);
map.put("status", 200);
}
catch (Exception e) {
e.printStackTrace();
map.put("status", 0);
}
return map;
}
}
Reference
この問題について(ProductRestController 2022/04/19), 我々は、より多くの情報をここで見つけました https://velog.io/@anrkfl/ProductRestController-20220419テキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol