1 package org.oxerr.seatgeek.client.rescu;
2
3 import static jakarta.ws.rs.core.Response.Status.NOT_FOUND;
4
5 import java.io.IOException;
6 import java.util.Optional;
7
8 import org.apache.commons.lang3.ArrayUtils;
9 import org.apache.commons.lang3.BooleanUtils;
10 import org.apache.commons.lang3.StringUtils;
11 import org.oxerr.seatgeek.client.ListingService;
12 import org.oxerr.seatgeek.client.model.SeatGeekException;
13 import org.oxerr.seatgeek.client.model.request.CreateListingRequest;
14 import org.oxerr.seatgeek.client.model.request.UpdateListingRequest;
15 import org.oxerr.seatgeek.client.model.response.Listing;
16 import org.oxerr.seatgeek.client.model.response.MultipleListingsResponse;
17 import org.oxerr.seatgeek.client.model.response.Response;
18
19 import si.mazi.rescu.HttpStatusIOException;
20
21 public class ListingServiceImpl implements ListingService {
22
23 private final ListingResource listingResource;
24
25 public ListingServiceImpl(ListingResource listingResource) {
26 this.listingResource = listingResource;
27 }
28
29 @Override
30 public void createListing(String ticketId, CreateListingRequest r) throws IOException {
31 Response response = this.listingResource.createListing(ticketId, r);
32 if (!response.getOk().booleanValue()) {
33 throw new SeatGeekException();
34 }
35 }
36
37 @Override
38 public UpdateListingRequest updateListing(String ticketId, UpdateListingRequest r) throws IOException {
39 return this.listingResource.updateListing(ticketId, r);
40 }
41
42 @Override
43 public Optional<Listing> getListing(String ticketId) throws IOException {
44 try {
45 return Optional.ofNullable(this.listingResource.getListing(ticketId).getListing());
46 } catch (HttpStatusIOException e) {
47 if (e.getHttpStatusCode() == NOT_FOUND.getStatusCode()) {
48 return Optional.empty();
49 } else {
50 throw e;
51 }
52 }
53 }
54
55 @Override
56 public MultipleListingsResponse getListings(
57 Integer page,
58 Integer perPage,
59 Boolean onlyBarcode,
60 String... listingIds
61 ) throws IOException {
62 String commaSeperatedListingIds = ArrayUtils.getLength(listingIds) == 0 ? null : StringUtils.join(listingIds, ',');
63 return this.listingResource.getListings(
64 commaSeperatedListingIds,
65 BooleanUtils.toIntegerObject(onlyBarcode),
66 page,
67 perPage
68 );
69 }
70
71 @Override
72 public void deleteListing(String ticketId) throws IOException {
73 try {
74 this.listingResource.deleteListing(ticketId);
75 } catch (HttpStatusIOException e) {
76 if (e.getHttpStatusCode() != NOT_FOUND.getStatusCode()) {
77 throw e;
78 }
79 }
80 }
81
82 }