Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package fr.epsi.montpellier.w;
- import javax.xml.bind.annotation.XmlRootElement;
- @XmlRootElement(name = "booktrain")
- public class BookTrain {
- private String bookNumber;
- private Train currentTrain;
- private int numberPlaces;
- public String getBookNumber() {
- return bookNumber;
- }
- public void setBookNumber(String bookNumber) {
- this.bookNumber = bookNumber;
- }
- public Train getCurrentTrain() {
- return currentTrain;
- }
- /**
- * @param currentTrain the currentTrain to set
- */
- public void setCurrentTrain(Train currentTrain) {
- this.currentTrain = currentTrain;
- }
- /**
- * @return the numberPlaces
- */
- public int getNumberPlaces() {
- return numberPlaces;
- }
- /**
- * @param numberPlaces the numberPlaces to set
- */
- public void setNumberPlaces(int numberPlaces) {
- this.numberPlaces = numberPlaces;
- }
- }
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package fr.epsi.montpellier.w;
- import java.util.List;
- import javax.ws.rs.DELETE;
- import javax.ws.rs.GET;
- import javax.ws.rs.POST;
- import javax.ws.rs.Path;
- import javax.ws.rs.PathParam;
- import javax.ws.rs.core.Response;
- import javax.ws.rs.core.Response.Status;
- /**
- *
- * @author M34LMAR }
- */
- public class BookTrainResource {
- @Path("/booktrains") // Chemin suivant /trains/booktrains pour invoquer cette méthode
- public BookTrainResource getBookTrainResource() {
- return new BookTrainResource();
- }
- @POST // Méthode HTTP utilisée pour déclencher cette méthode
- public Response createBookTrain(String numTrain, int numberPlaces) {
- Train currentTrain = null;
- for (Train current : BookTrainBD.getTrains()) {
- if (current.getNumTrain().equals(numTrain)) {
- currentTrain = current;
- }
- }
- if (currentTrain == null) {
- return Response
- .status(Status.NO_CONTENT)
- .build();
- }
- BookTrain newBookTrain = new BookTrain();
- newBookTrain.setNumberPlaces(numberPlaces);
- newBookTrain.setCurrentTrain(currentTrain);
- newBookTrain.setBookNumber(Long.toString(System.currentTimeMillis()));
- BookTrainBD.getBookTrains().add(newBookTrain);
- return Response
- .status(Status.OK)
- .entity(newBookTrain.getBookNumber()).build();
- }
- @GET // Méthode HTTP utilisée pour déclencher cette méthode
- public Response getBookTrains() {
- System.out.println("getBookTrains");
- return Response
- .status(Status.OK)
- .entity(BookTrainBD.getBookTrains()).build();
- }
- @GET // Méthode HTTP utilisée pour déclencher cette méthode
- @Path("{id}") // Chemin de façon à intégrer un template parameter (id)
- public Response getBookTrain(@PathParam("id") String bookNumber) {
- List<BookTrain> bookTrains = BookTrainBD.getBookTrains();
- for (BookTrain current : bookTrains) {
- if (current.getBookNumber().equals(bookNumber)) {
- return Response
- .status(Status.OK)
- .entity(current)
- .build();
- }
- }
- return Response
- .status(Status.NO_CONTENT).build();
- }
- @DELETE // Méthode HTTP utilisée pour déclencher cette méthode
- @Path("{id}") // Chemin de façon à intégrer un template parameter (id)
- public Response removeBookTrain(@PathParam("id") String bookNumber) {
- BookTrain currentBookTrain = null;
- for (BookTrain current : BookTrainBD.getBookTrains()) {
- if (current.getBookNumber().equals(bookNumber)) {
- currentBookTrain = current;
- }
- }
- return Response.status(Status.ACCEPTED).build();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement