Untitled
unknown
plain_text
a year ago
9.5 kB
1
Indexable
Never
FoodProductServiceImpl package com.org.foodapp.serviceImpl; import java.util.Locale.Category; import java.util.ArrayList; import java.util.List; import java.util.Map; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Service; import org.springframework.web.bind.annotation.RequestBody; import com.org.foodapp.JWT.JwtFilter; import com.org.foodapp.POJO.FoodProduct; import com.org.foodapp.constents.FoodConstants; import com.org.foodapp.dao.FoodProductDao; import com.org.foodapp.service.FoodProductService; import com.org.foodapp.utils.FoodUtils; import com.org.foodapp.wrapper.FoodProductWrapper; @Service public class FoodProductServiceImpl implements FoodProductService { @Autowired FoodProductDao foodProductDao; @Autowired JwtFilter jwtFilter; @Override public ResponseEntity<String>addNewProduct(Map<String , String> requestMap){ try { if(jwtFilter.isAdmin()) { if(validateProductMap(requestMap,false)) { foodProductDao.save(getProductFromMap(requestMap, false)); return FoodUtils.getResponseEntity("Product Added Successfully.", HttpStatus.OK); } return FoodUtils.getResponseEntity(FoodConstants.INVALID_DATA, HttpStatus.BAD_REQUEST); } else return FoodUtils.getResponseEntity(FoodConstants.UNAUTHORIZED_ACCESS, HttpStatus.UNAUTHORIZED); }catch(Exception ex) { ex.printStackTrace(); } return FoodUtils.getResponseEntity(FoodConstants.SOMETHING_WENT_WRONG,HttpStatus.INTERNAL_SERVER_ERROR) ; } private FoodProduct getProductFromMap(Map<String, String>requestMap, boolean isAdd) { Category category = new Category(); category.setId(Integer.parseInt(requestMap.get("categoryId"))); FoodProduct foodproduct = new FoodProduct(); if(isAdd) { foodproduct.setId(Integer.parseInt(requestMap.get("id"))); }else { foodproduct.setStatus("true"); } foodproduct.setCategory(category); foodproduct.setName(requestMap.get("name")); foodproduct.setDescription(requestMap.get("description")); foodproduct.setPrice(Integer.parseInt(requestMap.get("price"))); return foodproduct; } private boolean validateProductMap(Map<String, String> requestMap,boolean validateId) { if(requestMap.containsKey("name")) { if(requestMap.containsKey("id") && validateId) { return true; } else if(!validateId) { return true; } return false; } @Override public ResponseEntity<List<FoodProductWrapper>> getAllProduct(){ try { return new ResponseEntity<>(FoodProductDao.getAllProduct(),HttpStatus.OK); }catch (Exception ex) { ex.printStackTrace(); } return new ResponseEntity<>(new ArrayList<>(), HttpStatus.INTERNAL_SERVER_ERROR); } @Override public ResponseEntity<String> updateProduct(Map<String,String> RequestMap ){ try { return FoodProductService.updateProduct(requestMap); }catch (Exception ex) { ex.printStackTrace(); } } @Override public ResponseEntity<String>updateProduct(Map<String, String> requestMap){ try { if(jwtFilter.isAdmin()) { if(validateProductMap(requestMap,true)) { Optional<Product> optional= foodProductDao.findById(Integer.parseInt(requestMap.get("id"))); if(!optional.isEmpty()) { Product product = getProductFromMap(requestMap, true); product.setStatus(optional.get().getStatus()); productDao.save(product); return FoodUtils.getResponseEntity("Product updated successfully",HttpStatus.OK); } else { return FoodUtils.getResponseEntity("product id does not exist.",HttpStatus.OK); } }else { return FoodUtils.getResponseEntity(FoodConstants.INVALID_DATA,HttpStatus.BAD_REQUEST); } } else { return FoodUtils.getResponseEntity(FoodConstants.UNAUTHORIZED_ACCESS,HttpStatus.UNAUTHORIZED); } }catch(Exception ex) { ex.printStackTrace(); } return FoodUtils.getResponseEntity(FoodConstants.SOMETHING_WENT_WRONG,HttpStatus.INTERNAL_SERVER_ERROR); } } } FoodProductWrapper package com.org.foodapp.wrapper; import lombok.Data; @Data public class FoodProductWrapper { Integer id; String name; String description; Integer price; String status; Integer categoryId; String categoryName; public FoodProductWrapper() { } public FoodProductWrapper(Integer id,String name, String description,Integer price,String status,Integer categoryId,String categoryName) { this.id=id; this.name=name; this.description=description; this.price=price; this.status=status; this.categoryId=categoryId; this.categoryName=categoryName; } } FoodProductService package com.org.foodapp.service; import java.util.List; import java.util.Map; import org.springframework.http.ResponseEntity; import com.org.foodapp.wrapper.FoodProductWrapper; public interface FoodProductService { ResponseEntity<String>addNewProduct(Map<String , String> requestMap); ResponseEntity<List<FoodProductWrapper>>getAllProduct(); ResponseEntity<String>updateProduct(Map<String,String>requestMap); } FoodProductRestImpl package com.org.foodapp.restImpl; import java.util.ArrayList; import java.util.List; import java.util.Map; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RestController; import com.org.foodapp.constents.FoodConstants; import com.org.foodapp.rest.FoodProductRest; import com.org.foodapp.service.FoodProductService; import com.org.foodapp.utils.FoodUtils; import com.org.foodapp.wrapper.FoodProductWrapper; @RestController public class FoodProductRestImpl implements FoodProductRest{ @Autowired FoodProductService foodProductService; @Override public ResponseEntity<String> addNewProduct(@RequestBody Map<String,String> RequestMap ){ try { return FoodProductService.addNewProduct(requestMap); }catch(Exception ex) { ex.printStackTrace(); } return FoodUtils.getResponseEntity(FoodConstants.SOMETHING_WENT_WRONG,HttpStatus.INTERNAL_SERVER_ERROR); } @Override public ResponseEntity<List<FoodProductWrapper>>getAllProduct(){ try { return FoodProductService.getAllProduct(); }catch(Exception ex) { ex.printStackTrace(); } return new ResponseEntity<>(new ArrayList<>(),HttpStatus.INTERNAL_SERVER_ERROR); } } } FoodProductRest package com.org.foodapp.rest; import java.util.List; import java.util.Map; import org.springframework.http.ResponseEntity; 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 com.org.foodapp.wrapper.FoodProductWrapper; @RequestMapping(path = "/foodproduct") public interface FoodProductRest { @PostMapping(path="/add") ResponseEntity<String> addNewProduct(@RequestBody Map<String,String> RequestMap ); @GetMapping(path="/get") ResponseEntity<List<FoodProductWrapper>>getAllProduct(); @PostMapping(path="/update") ResponseEntity<String> updateProduct(@RequestBody Map<String,String> RequestMap ); } FoodProduct package com.org.foodapp.POJO; import java.io.Serializable; import java.util.Locale.Category; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.FetchType; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.JoinColumn; import javax.persistence.ManyToOne; import javax.persistence.NamedQuery; import javax.persistence.Table; import org.hibernate.annotations.DynamicInsert; import org.hibernate.annotations.DynamicUpdate; import lombok.Data; @NamedQuery(name= "product.getAllProduct", query= "select new com.org.foodapp.wrapper.FoodProductWrapper(p.id,p.name,p.description,p.price,p.status,p.category.id,p.category.name) from Product p") @Data @Entity @DynamicInsert @DynamicUpdate @Table(name= "foodproduct") public class FoodProduct implements Serializable { public static final long serialVersionUid = 1234456L; @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @Column(name = "id") private Integer id; @Column(name = "name") private String name; @ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name = "category_fk", nullable = false) private Category category; @Column(name="description") private String description; @Column(name = "price") private Integer price; @Column(name="status") private String status; } FoodProductDao package com.org.foodapp.dao; import java.util.List; import org.springframework.data.jpa.repository.JpaRepository; import com.org.foodapp.POJO.FoodProduct; import com.org.foodapp.wrapper.FoodProductWrapper; public interface FoodProductDao extends JpaRepository<FoodProduct, Integer> { List<FoodProductWrapper>getAllProduct(); } FoodProductConstants package com.org.foodapp.constents; public class FoodConstants { public static final String SOMETHING_WENT_WRONG ="Something went wrong."; public static final String INVALID_DATA = "Invalid data."; public static final String UNAUTHORIZED_ACCESS = "Unauthorized access."; }