Skip to content

Instantly share code, notes, and snippets.

@FantailIO
Created February 19, 2018 09:19
Show Gist options
  • Save FantailIO/9d292bd789b49e678914853b13e6f1b1 to your computer and use it in GitHub Desktop.
Save FantailIO/9d292bd789b49e678914853b13e6f1b1 to your computer and use it in GitHub Desktop.
Implementation of the Add Location functionality
package io.fantail.location.service;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import javax.ws.rs.BadRequestException;
import javax.ws.rs.InternalServerErrorException;
import javax.ws.rs.NotFoundException;
import javax.ws.rs.ServerErrorException;
import javax.ws.rs.core.Response;
import io.fantail.location.model.Location;
public class LocationService {
private Map<String, Location> locations;
public LocationService() {
this(new HashMap<>());
}
protected LocationService(Map<String, Location> locations) {
this.locations = locations;
}
public Location getLocation(String id) {
Location loc = locations.get(id);
if(loc == null) {
throw new NotFoundException("Location with ID " + id + " not found");
}
return loc;
}
public Response addLocation(Location loc) {
if(loc.getId() != null) {
throw new BadRequestException("You can't add an exception with an ID");
}
String id = UUID.randomUUID().toString();
loc.setId(id);
locations.put(id, loc);
try {
return Response.created(new URI(id)).build();
} catch (URISyntaxException e) {
throw new InternalServerErrorException("Unexpected error making a URI", e);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment