Commit 52393c61 authored by Simhadri Guntreddi's avatar Simhadri Guntreddi

second commit for new changes

parent 99c55f7a
package com.training.catalog.controller; package com.training.catalog.controller;
import java.util.List; import java.util.List;
import java.util.Optional;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
...@@ -25,10 +26,15 @@ public class CatalogController { ...@@ -25,10 +26,15 @@ public class CatalogController {
return new ResponseEntity<List<Product>>(catalogService.getProducts(), HttpStatus.OK); return new ResponseEntity<List<Product>>(catalogService.getProducts(), HttpStatus.OK);
} }
@GetMapping("/{name}") @GetMapping("/{name}")
public ResponseEntity<Product> getProductWith(@PathVariable("name") String name) { public ResponseEntity<Optional<Product>> getProductWith(@PathVariable("name") String name) {
// System.out.println("name-->" + name);
return new ResponseEntity<Product>(catalogService.getProduct(name), HttpStatus.OK); Optional<Product> productName =catalogService.getProduct(name);
if(productName.isPresent()) {
return new ResponseEntity<Optional<Product> >(productName, HttpStatus.OK);
}
return new ResponseEntity<Optional<Product> >(HttpStatus.NOT_FOUND);
} }
} }
package com.training.catalog.service; package com.training.catalog.service;
import java.util.List; import java.util.List;
import java.util.Optional;
import com.training.catalog.model.Product; import com.training.catalog.model.Product;
public interface CatalogService { public interface CatalogService {
public List<Product> getProducts(); public List<Product> getProducts();
public Product getProduct(String name) ; public Optional<Product> getProduct(String name) ;
} }
...@@ -3,6 +3,7 @@ package com.training.catalog.service; ...@@ -3,6 +3,7 @@ package com.training.catalog.service;
import java.util.List; import java.util.List;
import java.util.Optional; import java.util.Optional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import com.training.catalog.model.Product; import com.training.catalog.model.Product;
...@@ -11,8 +12,9 @@ import com.training.catalog.respository.CatalogRepository; ...@@ -11,8 +12,9 @@ import com.training.catalog.respository.CatalogRepository;
@Service @Service
public class CatalogServiceImpl implements CatalogService { public class CatalogServiceImpl implements CatalogService {
@Autowired
private CatalogRepository catalogRepository; private CatalogRepository catalogRepository;
@Override @Override
public List<Product> getProducts() { public List<Product> getProducts() {
List<Product> listOfProducts = catalogRepository.findAll(); List<Product> listOfProducts = catalogRepository.findAll();
...@@ -20,12 +22,15 @@ public class CatalogServiceImpl implements CatalogService { ...@@ -20,12 +22,15 @@ public class CatalogServiceImpl implements CatalogService {
} }
@Override @Override
public Product getProduct(String name) { public Optional<Product> getProduct(String name) {
Optional<Product> findById = catalogRepository.findById(name);
if (findById.isPresent()) { if (catalogRepository.existsById(name)) {
return findById.get(); Optional<Product> findById = catalogRepository.findById(name);
if (findById.isPresent()) {
return findById;
}
} }
return null; return Optional.empty();
} }
} }
package com.training.product.service; package com.training.product.service;
import org.springframework.stereotype.Service;
import com.training.product.model.Product; import com.training.product.model.Product;
public interface BagService { public interface BagService {
......
...@@ -30,16 +30,19 @@ public class BagServiceImpl implements BagService { ...@@ -30,16 +30,19 @@ public class BagServiceImpl implements BagService {
product.setPrice(pro.getPrice()); product.setPrice(pro.getPrice());
product.setQuantity(pro.getQuantity()); product.setQuantity(pro.getQuantity());
bagRepository.save(product); bagRepository.save(product);
return "updated..!"; return "Product Updated..!";
} }
return "Product Updated "; return "There is a no product on this name";
} }
@Override @Override
public String deleteProduct(String name) { public String deleteProduct(String name) {
if (bagRepository.existsById(name)) {
bagRepository.deleteById(name); bagRepository.deleteById(name);
return "Product Deleted "; return "Product Deleted ";
}
return "Please give correct product name/there is a no product on this name";
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment