@Override public void delete(Car car) { repository.remove(car.getId()); } }
public CarRepresentation(io.github.cdelmas.spike.common.domain.Car car) { this.id = car.getId(); this.name = car.getName(); }
public CarRepresentation(Car car) { this.id = car.getId(); this.name = car.getName(); }
public CarRepresentation(Car car) { this.name = car.getName(); this.id = car.getId(); }
@POST @Consumes(MediaType.APPLICATION_JSON) public Response createCar(@Auth User user, Car car) { carRepository.save(car); return Response.created(UriBuilder.fromResource(CarsResource.class).path("/{id}").build(String.valueOf(car.getId()))) .build(); }
@GET public Response byId(@Auth User user, @PathParam("id") int carId) { Optional<Car> car = carRepository.byId(carId); return car.map(c -> { CarRepresentation carRepresentation = new CarRepresentation(c); carRepresentation.addLink(Link.self(uriInfo.getAbsolutePathBuilder().build(c.getId()).toString())); return Response.ok(carRepresentation).build(); }).orElse(Response.status(Response.Status.NOT_FOUND).build()); } }
@GET @Produces(MediaType.APPLICATION_JSON) public Response all(@Auth User user) { List<Car> cars = carRepository.all(); List<CarRepresentation> representations = cars.stream() .map(c -> { CarRepresentation representation = new CarRepresentation(c); representation.addLink(io.github.cdelmas.spike.common.hateoas.Link.self(uriInfo.getAbsolutePathBuilder().build(c.getId()).toString())); return representation; }).collect(toList()); return Response.ok(representations) .header("total-count", String.valueOf(cars.size())) .build(); }
@RequestMapping(method = RequestMethod.POST, consumes = "application/json") public ResponseEntity<CarRepresentation> create(@RequestBody Car car) { carRepository.save(car); HttpHeaders responseHeaders = new HttpHeaders(); Link link = linkTo(methodOn(CarsController.class).byId(String.valueOf(car.getId()))).withSelfRel(); responseHeaders.set("Location", link.getHref()); return new ResponseEntity<>(responseHeaders, HttpStatus.CREATED); } }
public List<CarRepresentation> all(Request request, Response response) { List<Car> cars = carRepository.all(); response.header("count", String.valueOf(cars.size())); response.type("application/json"); return cars.stream().map(c -> { CarRepresentation carRepresentation = new CarRepresentation(c); carRepresentation.addLink(self(request.url() + "/" + c.getId())); return carRepresentation; } ).collect(toList()); }
public void all(RoutingContext routingContext) { List<Car> all = carRepository.all(); HttpServerResponse response = routingContext.response(); response.putHeader("content-type", "application/json") .putHeader("total-count", String.valueOf(all.size())) .end(encode(all.stream().map(car -> { CarRepresentation carRepresentation = new CarRepresentation(car); carRepresentation.addLink(self(routingContext.request().absoluteURI() + "/" + car.getId())); return carRepresentation; }).collect(toList()))); }
@Get("json") public List<CarRepresentation> all() { List<io.github.cdelmas.spike.common.domain.Car> cars = carRepository.all(); getResponse().getHeaders().add("total-count", String.valueOf(cars.size())); return cars.stream().map(c -> { CarRepresentation carRepresentation = new CarRepresentation(c); carRepresentation.addLink(Link.self(new Reference(getReference()).addSegment(String.valueOf(c.getId())).toString())); return carRepresentation; }).collect(toList()); }
private CarRepresentation toCarRepresentation(Car c) { String carId = String.valueOf(c.getId()); CarRepresentation rep = new CarRepresentation(c); rep.add(linkTo(methodOn(CarsController.class).byId(carId)).withSelfRel()); return rep; }
@Post("json") public void createCar(io.github.cdelmas.spike.common.domain.Car car) { carRepository.save(car); setLocationRef(getReference().addSegment(String.valueOf(car.getId()))); setStatus(Status.SUCCESS_CREATED); }
public String createCar(Request request, Response response) { Car car; try { car = objectMapper.readValue(request.body(), Car.class); carRepository.save(car); response.header("Location", request.url() + "/" + car.getId()); response.status(201); } catch (IOException e) { response.status(400); } return ""; } }
public void create(RoutingContext routingContext) { Car car = Json.decodeValue(routingContext.getBodyAsString(), Car.class); carRepository.save(car); HttpServerResponse response = routingContext.response(); response.putHeader("Location", routingContext.request().absoluteURI() + "/" + car.getId()) .setStatusCode(201) .end(); } }