Skip to content
This repository has been archived by the owner on Oct 30, 2023. It is now read-only.

Commit

Permalink
Merge pull request #18 from lugaru1234/issue1458-java-jersey-jaxrs
Browse files Browse the repository at this point in the history
Fixes swagger-api/swagger-core#1458: java-jersey-jaxrs sample has been modified
  • Loading branch information
webron committed Oct 16, 2015
2 parents 5809a49 + f47f072 commit 7246bc6
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public class PetData {
"url1", "url2" }, new String[] { "tag3", "tag4" }, "available"));
}

public Pet getPetbyId(long petId) {
public Pet getPetById(long petId) {
for (Pet pet : pets) {
if (pet.getId() == petId) {
return pet;
Expand All @@ -66,15 +66,17 @@ public Pet getPetbyId(long petId) {
return null;
}

public void deletePet(long petId) {
public boolean deletePet(long petId) {
if(pets.size() > 0) {
for (int i = pets.size() - 1; i >= 0; i--) {
Pet pet = pets.get(i);
if(pet.getId() == petId) {
pets.remove(i);
return true;
}
}
}
return false;
}

public List<Pet> findPetByStatus(String status) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@ public class StoreData {
orders.add(createOrder(3, 2, 2, new Date(), "placed"));
orders.add(createOrder(4, 2, 2, new Date(), "delivered"));
orders.add(createOrder(5, 3, 2, new Date(), "placed"));
orders.add(createOrder(11, 3, 2, new Date(), "placed"));
orders.add(createOrder(12, 3, 2, new Date(), "placed"));
orders.add(createOrder(13, 3, 2, new Date(), "placed"));
orders.add(createOrder(14, 3, 2, new Date(), "placed"));
orders.add(createOrder(15, 3, 2, new Date(), "placed"));
orders.add(createOrder(6, 3, 2, new Date(), "placed"));
orders.add(createOrder(7, 3, 2, new Date(), "placed"));
orders.add(createOrder(8, 3, 2, new Date(), "placed"));
orders.add(createOrder(9, 3, 2, new Date(), "placed"));
orders.add(createOrder(10, 3, 2, new Date(), "placed"));
}

public Order findOrderById(long orderId) {
Expand All @@ -59,14 +59,16 @@ public Order placeOrder(Order order) {
return order;
}

public void deleteOrder(long orderId) {
public boolean deleteOrder(long orderId) {
if (orders.size() > 0) {
for (int i = orders.size() - 1; i >= 0; i--) {
if (orders.get(i).getId() == orderId) {
orders.remove(i);
return true;
}
}
}
return false;
}

private static Order createOrder(long id, long petId, int quantity,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,14 +72,16 @@ public void addUser(User user) {
users.add(user);
}

public void removeUser(String username) {
public boolean removeUser(String username) {
if (users.size() > 0) {
for (int i = users.size() - 1; i >= 0; i--) {
if (users.get(i).getUsername().equals(username)) {
users.remove(i);
return true;
}
}
}
return false;
}

private static User createUser(long id, String username, String firstName,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@
public class PetResource {
private Logger LOGGER = LoggerFactory.getLogger(PetResource.class);
static PetData petData = new PetData();
static JavaRestResourceUtil ru = new JavaRestResourceUtil();

@GET
@Path("/{petId}")
Expand All @@ -65,7 +64,7 @@ public class PetResource {
public Response getPetById(
@ApiParam(value = "ID of pet to return") @PathParam("petId") Long petId)
throws NotFoundException {
Pet pet = petData.getPetbyId(petId);
Pet pet = petData.getPetById(petId);
if (null != pet)
return Response.ok().entity(pet).build();
else
Expand All @@ -75,12 +74,16 @@ public Response getPetById(
@DELETE
@Path("/{petId}")
@ApiOperation(value = "Deletes a pet")
@ApiResponses(value = { @ApiResponse(code = 400, message = "Invalid pet value")})
@ApiResponses(value = { @ApiResponse(code = 400, message = "Invalid ID supplied"),
@ApiResponse(code = 404, message = "Pet not found") })
public Response deletePet(
@ApiParam() @HeaderParam("api_key") String apiKey,
@ApiParam(value = "Pet id to delete", required = true)@PathParam("petId") Long petId) {
petData.deletePet(petId);
return Response.ok().build();
if (petData.deletePet(petId)) {
return Response.ok().build();
} else {
return Response.status(Response.Status.NOT_FOUND).build();
}
}

@POST
Expand Down Expand Up @@ -168,9 +171,7 @@ public Response updatePetWithForm (
@ApiParam(value = "ID of pet that needs to be updated", required = true)@PathParam("petId") Long petId,
@ApiParam(value = "Updated name of the pet", required = false)@FormParam("name") String name,
@ApiParam(value = "Updated status of the pet", required = false)@FormParam("status") String status) {
System.out.println(name);
System.out.println(status);
Pet pet = petData.getPetbyId(petId);
Pet pet = petData.getPetById(petId);
if(pet != null) {
if(name != null && !"".equals(name))
pet.setName(name);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
public class PetStoreResource {
static StoreData storeData = new StoreData();
static PetData petData = new PetData();
static JavaRestResourceUtil ru = new JavaRestResourceUtil();

@GET
@Path("/inventory")
Expand All @@ -50,12 +49,13 @@ public java.util.Map<String, Integer> getInventory() {
@GET
@Path("/order/{orderId}")
@ApiOperation(value = "Find purchase order by ID",
notes = "For valid response try integer IDs with value <= 5 or > 10. Other values will generated exceptions",
notes = "For valid response try integer IDs with value >= 1 and <= 10. Other values will generated exceptions",
response = Order.class)
@ApiResponses(value = { @ApiResponse(code = 400, message = "Invalid ID supplied"),
@ApiResponse(code = 404, message = "Order not found") })
public Response getOrderById(
@ApiParam(value = "ID of pet that needs to be fetched", allowableValues = "range[1,5]", required = true) @PathParam("orderId") Long orderId)
@ApiParam(value = "ID of pet that needs to be fetched", allowableValues = "range[1,10]", required = true)
@PathParam("orderId") Long orderId)
throws NotFoundException {
Order order = storeData.findOrderById(orderId);
if (null != order) {
Expand All @@ -79,12 +79,16 @@ public Order placeOrder(
@DELETE
@Path("/order/{orderId}")
@ApiOperation(value = "Delete purchase order by ID",
notes = "For valid response try integer IDs with value < 1000. Anything above 1000 or nonintegers will generate API errors")
notes = "For valid response try integer IDs with positive integer value. Negative or non-integer values will generate API errors")
@ApiResponses(value = { @ApiResponse(code = 400, message = "Invalid ID supplied"),
@ApiResponse(code = 404, message = "Order not found") })
public Response deleteOrder(
@ApiParam(value = "ID of the order that needs to be deleted", allowableValues = "range[1,infinity]", required = true) @PathParam("orderId") String orderId) {
storeData.deleteOrder(ru.getLong(0, 10000, 0, orderId));
return Response.ok().entity("").build();
@ApiParam(value = "ID of the order that needs to be deleted", allowableValues = "range[1,infinity]", required = true)
@PathParam("orderId") Long orderId) {
if (storeData.deleteOrder(orderId)) {
return Response.ok().entity("").build();
} else {
return Response.status(Response.Status.NOT_FOUND).entity("Order not found").build();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,11 @@ public Response updateUser(
@ApiResponse(code = 404, message = "User not found") })
public Response deleteUser(
@ApiParam(value = "The name that needs to be deleted", required = true) @PathParam("username") String username) {
userData.removeUser(username);
return Response.ok().entity("").build();
if (userData.removeUser(username)) {
return Response.ok().entity("").build();
} else {
return Response.status(Response.Status.NOT_FOUND).build();
}
}

@GET
Expand Down

0 comments on commit 7246bc6

Please sign in to comment.