I am new to programming and I am trying to build a small dietary tracking app. I use quarkus RESTeasy JAX-RS and java to build my REST API. The user should be able to add foods he ate in days of the week.
The user must be able to retrieve the foods he ate based on the date. My issue is that I can't retrieve the food based on the date. When I use the Timestamp: "2021-06-10T08:44:45.9328079Z[UTC]" as the input date on my end point for GET Method, I get 400 BAD REQUEST in postman. When I retrieve foods based on userId it works fine.
Here is my code with the GET and POST methods:
@Path("/foods") public class Controller {
public static ArrayList<Object> foods = new ArrayList<>();
@GET
@Produces(MediaType.APPLICATION_JSON)
public Response getAllFood(){
return Response.ok(foods).build();
}
@POST
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public Response addFood() throws ParseException {
foods.add(new Food("Garlic", 30, Timestamp.from(Instant.now()), 1));
foods.add(new Food("Onions", 20, Timestamp.from(Instant.now()), 2));
return Response.ok(foods).build();
}
@Path("{userId}")
@GET
@Produces(MediaType.APPLICATION_JSON)
public static Response getFoodById(@PathParam("userId") int userId){
for (Object food : foods){
if (((Food)food).getUserId()==(userId)){
return Response.ok(food).build();
}
}
return null;
}
@Path("/second/{time}")
@GET
@Produces(MediaType.APPLICATION_JSON)
public Response getFoodByDate(@PathParam("time") Timestamp time){
for (Object food : foods){
if (((Food)food).getTime().toString().equals(time.toString())){
return Response.ok(food).build();
}
}
return null;
}
}
And here is the Food Class:
package org.acme;
import java.sql.Timestamp;
public class Food {
private String foodType;
private int portion;
private Timestamp time;
private int userId;
public Food(){}
public Food(String foodType, int portion, Timestamp time, int userId){
this.foodType = foodType;
this.portion = portion;
this.time = time;
this.userId = userId;
}
public String getFoodType() {
return foodType;
}
public void setFoodType(String foodType) {
this.foodType = foodType;
}
public int getPortion() {
return portion;
}
public void setPortion(int portion) {
this.portion = portion;
}
public Timestamp getTime() {
return time;
}
public void setTime(Timestamp time) {
this.time = time;
}
public int getUserId() {
return userId;
}
public void setUserId(int userId) {
this.userId = userId;
}
@Override
public String toString(){
return foodType + ", " + portion + ", " + time + ", " + userId;
}
}