Commit 320af826 by Patryk Czarnik

Produktu jako JAX-RS w Spring Jersey

parent 47c762b0
......@@ -8,6 +8,8 @@ import com.demo.proste.Kalkulator;
import com.demo.proste.Kontekst;
import com.demo.proste.Parametry;
import com.demo.proste.Time;
import com.demo.sklep.rest.ROrder;
import com.demo.sklep.rest.RProduct;
@Configuration
public class JerseyConfig extends ResourceConfig {
......@@ -18,6 +20,8 @@ public class JerseyConfig extends ResourceConfig {
register(Parametry.class);
register(Kalkulator.class);
register(Time.class);
register(RProduct.class);
register(ROrder.class);
}
}
[{"description":"Pralka wolnoobrotowa","price":2300.00,"productId":1,"productName":"praleczka","vat":0.23},{"description":"Odkurzacz automatyczny","price":499.00,"productId":2,"productName":"odkurzacz","vat":0.23},{"description":"Telewizor 55 cali 4K","price":2998.88,"productId":3,"productName":"telewizor 55","vat":0.23},{"description":"Telewizor 40 Full HD","price":2202.00,"productId":4,"productName":"telewiz","vat":0.23},{"price":444.00,"productId":5,"productName":"myszka gejmerska","vat":0.23},{"description":"Kawa Capuccino","price":12.90,"productId":10,"productName":"kawa","vat":0.08},{"description":"","price":9.90,"productId":11,"productName":"sok pomarańczowy","vat":0.05},{"description":"laptop co się zawiesza czasami","price":3456.00,"productId":12,"productName":"komputer","vat":0.23},{"description":"Głośniczek do Zooma","price":24.56,"productId":13,"productName":"głośniczek","vat":0.23},{"description":"","price":1999.99,"productId":14,"productName":"monitor","vat":0.23},{"description":"","price":12.90,"productId":15,"productName":"napój","vat":0.23},{"description":"","price":33.33,"productId":16,"productName":"wiatrak","vat":0.23},{"description":"Produkt arg0","price":999.00,"productId":17,"productName":"arg0name","vat":0.22},{"description":"Parkiet drewniany","price":190.00,"productId":18,"productName":"parkiet","vat":0.23},{"description":"Panele podłogowe","price":190.00,"productId":21,"productName":"panel","vat":0.23},{"description":"Klimatyzacja dobrze działająca","price":190.00,"productId":22,"productName":"Klimatyzacja","vat":0.23},{"price":19.00,"productId":23,"productName":"kosz na śmieci","vat":0.23},{"price":19.00,"productId":24,"productName":"kosz na śmieci","vat":0.23},{"price":19.00,"productId":25,"productName":"kosz na śmieci","vat":0.23},{"price":19.00,"productId":26,"productName":"kosz na śmieci","vat":0.23}]
\ No newline at end of file
package com.demo.sklep.rest;
import java.util.List;
import java.util.Optional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.server.ResponseStatusException;
import com.demo.sklep.model.Order;
import com.demo.sklep.repository.OrderRepository;
import jakarta.ws.rs.Consumes;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.PathParam;
import jakarta.ws.rs.Produces;
@Path("/orders")
@Produces("application/json")
@Consumes("application/json")
public class ROrder {
@Autowired
private OrderRepository orderRepository;
@GET
public List<Order> readAll() {
return orderRepository.findAll();
}
@Path("/{id}")
@GET
public Order readOne(@PathParam("id") int id) {
Optional<Order> order = orderRepository.findById(id);
if(order.isPresent()) {
return order.get();
} else {
throw new ResponseStatusException(HttpStatus.NOT_FOUND, "Nie ma zamówienia o numerze " + id);
}
}
}
package com.demo.sklep.rest;
import java.math.BigDecimal;
import java.util.List;
import java.util.Optional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.http.HttpStatus;
import org.springframework.web.server.ResponseStatusException;
import com.demo.sklep.model.Product;
import com.demo.sklep.photo.PhotoUtil;
import com.demo.sklep.repository.ProductRepository;
import jakarta.ws.rs.Consumes;
import jakarta.ws.rs.DELETE;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.POST;
import jakarta.ws.rs.PUT;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.PathParam;
import jakarta.ws.rs.Produces;
@Path("/products")
@Produces("application/json")
@Consumes("application/json")
public class RProduct {
@Autowired
private ProductRepository productRepository;
@Autowired
private PhotoUtil photoUtil;
@GET
public List<Product> readAll() {
return productRepository.findAll();
}
@Path("/{id}")
@GET
public Product readOne(@PathParam("id") int id) {
return productRepository.findById(id).orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND));
}
@Path("/{id}/price")
@GET
public BigDecimal getPrice(@PathParam("id") Integer id) {
Optional<Product> product = productRepository.findById(id);
if(product.isPresent()) {
return product.get().getPrice();
} else {
throw new ResponseStatusException(HttpStatus.NOT_FOUND);
}
}
@Path("/{id}/price")
@PUT
public void setPrice(@PathParam("id") Integer id, BigDecimal newPrice) {
Optional<Product> product = productRepository.findById(id);
if(product.isPresent()) {
Product realProduct = product.get();
realProduct.setPrice(newPrice);
productRepository.save(realProduct);
} else {
throw new ResponseStatusException(HttpStatus.NOT_FOUND);
}
}
@Path("/{id}")
@PUT
public void update(@PathParam("id") Integer productId, Product product) {
// Aby mieć pewność, że zapisujemu produkt o typ ID, wpisuję productId z URL-a.
// Ewentualnie możnaby jeszcze sprawdzić czy rekord istnieje, czy ID się zgadza
// i jeśli coś jest nie tak, to wyrzucić wyjątek.
product.setProductId(productId);
productRepository.save(product);
}
@POST
public Product insert(Product product) {
product.setProductId(null);
productRepository.save(product);
return product;
}
@Path("/{id}")
@DELETE
public void delete(@PathParam("id") Integer productId) {
try {
productRepository.deleteById(productId);
} catch (EmptyResultDataAccessException e) {
throw new ResponseStatusException(HttpStatus.NOT_FOUND, "Brak produktu nr " + productId);
}
}
@GET
@Path("/{id}/photo")
@Produces("image/jpeg")
public byte[] getPhoto(@PathParam("id") int productId) {
return photoUtil.readBytes(productId);
}
@PUT
@Path("/{id}/photo")
@Consumes("image/jpeg")
public void uploadPhoto(@PathParam("id") int productId, byte[] bytes) {
photoUtil.writeBytes(productId, bytes);
}
}
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