1 package org.oxerr.viagogo.client.rescu.impl.sale; 2 3 import static jakarta.ws.rs.core.Response.Status.NOT_FOUND; 4 5 import java.io.IOException; 6 import java.time.Instant; 7 import java.util.Optional; 8 9 import org.oxerr.viagogo.client.rescu.resource.ViagogoException; 10 import org.oxerr.viagogo.client.rescu.resource.sale.SaleResource; 11 import org.oxerr.viagogo.client.sale.SaleService; 12 import org.oxerr.viagogo.model.request.sale.SaleRequest; 13 import org.oxerr.viagogo.model.response.PagedResource; 14 import org.oxerr.viagogo.model.response.sale.Sale; 15 16 import si.mazi.rescu.HttpStatusIOException; 17 18 public class SaleServiceImpl implements SaleService { 19 20 private final SaleResource saleResource; 21 22 public SaleServiceImpl(SaleResource saleResource) { 23 this.saleResource = saleResource; 24 } 25 26 @Override 27 public PagedResource<Sale> getSalesRecentUpdates(Instant updatedSince) throws IOException { 28 return this.saleResource.getSalesRecentUpdates(updatedSince); 29 } 30 31 @Override 32 public PagedResource<Sale> getSales(SaleRequest saleRequest) throws IOException { 33 return this.saleResource.getSales( 34 saleRequest.getPage(), 35 saleRequest.getPageSize(), 36 saleRequest.getUpdatedSince(), 37 saleRequest.getSort() 38 ); 39 } 40 41 @Override 42 public Optional<Sale> getSale(Integer saleId) throws IOException { 43 try { 44 return Optional.ofNullable(this.saleResource.getSale(saleId)); 45 } catch (ViagogoException | HttpStatusIOException e) { 46 if (e.getHttpStatusCode() == NOT_FOUND.getStatusCode()) { 47 return Optional.empty(); 48 } else { 49 throw e; 50 } 51 } 52 } 53 54 }