-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add Dishes Api with Test Integration
- Loading branch information
1 parent
bbffe93
commit 29664e6
Showing
13 changed files
with
585 additions
and
0 deletions.
There are no files selected for viewing
92 changes: 92 additions & 0 deletions
92
my-delivery-registration/src/main/java/dev/helis/registration/rest/DishResources.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
package dev.helis.registration.rest; | ||
|
||
import java.net.MalformedURLException; | ||
import java.net.URI; | ||
import java.util.List; | ||
import java.util.Optional; | ||
import java.util.stream.Collectors; | ||
|
||
import dev.helis.registration.entity.Dish; | ||
import dev.helis.registration.entity.Restaurant; | ||
import dev.helis.registration.rest.dto.DishRequest; | ||
import dev.helis.registration.rest.dto.DishResponse; | ||
import dev.helis.registration.rest.dto.mappers.DishMapper; | ||
import jakarta.transaction.Transactional; | ||
import jakarta.validation.Valid; | ||
import jakarta.ws.rs.Consumes; | ||
import jakarta.ws.rs.DELETE; | ||
import jakarta.ws.rs.GET; | ||
import jakarta.ws.rs.NotFoundException; | ||
import jakarta.ws.rs.POST; | ||
import jakarta.ws.rs.PUT; | ||
import jakarta.ws.rs.Path; | ||
import jakarta.ws.rs.PathParam; | ||
import jakarta.ws.rs.Produces; | ||
import jakarta.ws.rs.core.MediaType; | ||
import jakarta.ws.rs.core.Response; | ||
import jakarta.ws.rs.core.UriBuilderException; | ||
|
||
@Path(ResourcePaths.DISHES) | ||
@Produces(MediaType.APPLICATION_JSON) | ||
@Consumes(MediaType.APPLICATION_JSON) | ||
public class DishResources { | ||
|
||
private static final String SELECT_FROM_DISH_WHERE_RESTAURANT_ID_AND_ID = "restaurant.id = ?1 and id = ?2"; | ||
|
||
@GET | ||
public List<DishResponse> findAll(@PathParam("restaurantId") Long restaurantId) { | ||
return Dish.list("restaurant.id", restaurantId).stream().map( r -> (Dish)r).map(DishMapper::mapToDto).collect(Collectors.toList()); | ||
} | ||
|
||
@GET | ||
@Path("/{id}") | ||
public DishResponse findById(@PathParam("restaurantId") Long restaurantId, @PathParam("id") Long id) { | ||
return Dish.find(SELECT_FROM_DISH_WHERE_RESTAURANT_ID_AND_ID, restaurantId, id).singleResultOptional().map( r -> (Dish)r).map(DishMapper::mapToDto).orElseThrow(NotFoundException::new); | ||
} | ||
|
||
@POST | ||
@Transactional | ||
public Response create(@PathParam("restaurantId") Long restaurantId, @Valid DishRequest request) throws MalformedURLException, IllegalArgumentException, UriBuilderException { | ||
|
||
Optional<Restaurant> restaurant = Restaurant.findByIdOptional(restaurantId); | ||
if (!restaurant.isPresent()) { | ||
throw new NotFoundException(); | ||
} | ||
|
||
Dish mapToEntity = DishMapper.mapToEntity(restaurant.get(),request); | ||
mapToEntity.restaurant = restaurant.get(); | ||
mapToEntity.persist(); | ||
|
||
return Response.status(Response.Status.CREATED).location(URI.create(ResourcePaths.DISHES.replace("{restaurantId}", restaurantId.toString()) + "/" + mapToEntity.id)).build(); | ||
} | ||
|
||
@PUT | ||
@Path("/{id}") | ||
@Transactional | ||
public Response update(@PathParam("restaurantId") Long restaurantId, @PathParam("id") Long id, @Valid DishRequest request) throws MalformedURLException, IllegalArgumentException, UriBuilderException { | ||
Optional<Dish> optional = Dish.find(SELECT_FROM_DISH_WHERE_RESTAURANT_ID_AND_ID, restaurantId, id).singleResultOptional(); | ||
if (!optional.isPresent()) { | ||
throw new NotFoundException(); | ||
} | ||
Dish dish = optional.get(); | ||
dish.name = request.getName(); | ||
dish.description = request.getDescription(); | ||
dish.price = request.getPrice(); | ||
dish.image = null; | ||
dish.category = request.getCategory(); | ||
dish.isAvailable = request.getIsAvailable(); | ||
dish.persist(); | ||
return Response.status(Response.Status.NO_CONTENT).build(); | ||
} | ||
|
||
@DELETE | ||
@Path("/{id}") | ||
@Transactional | ||
public void delete(@PathParam("restaurantId") Long restaurantId, @PathParam("id") Long id) { | ||
Optional<Dish> optional = Dish.find(SELECT_FROM_DISH_WHERE_RESTAURANT_ID_AND_ID, restaurantId, id).singleResultOptional(); | ||
if (!optional.isPresent()) { | ||
throw new NotFoundException(); | ||
} | ||
optional.get().delete(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
91 changes: 91 additions & 0 deletions
91
my-delivery-registration/src/main/java/dev/helis/registration/rest/dto/DishRequest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
package dev.helis.registration.rest.dto; | ||
|
||
import java.io.Serializable; | ||
import java.math.BigDecimal; | ||
|
||
import dev.helis.registration.entity.Category; | ||
import dev.helis.registration.validation.OnlyCharacterAndPunctuation; | ||
import jakarta.validation.constraints.NotBlank; | ||
import jakarta.validation.constraints.NotNull; | ||
import jakarta.validation.constraints.Positive; | ||
|
||
|
||
public class DishRequest implements Serializable { | ||
|
||
private static final long serialVersionUID = 1L; | ||
|
||
@NotBlank | ||
@OnlyCharacterAndPunctuation | ||
private String name; | ||
|
||
@OnlyCharacterAndPunctuation | ||
private String description; | ||
|
||
@NotNull | ||
@Positive | ||
private BigDecimal price; | ||
|
||
private String image; | ||
|
||
@NotNull | ||
private Category category; | ||
|
||
@NotNull | ||
private Boolean isAvailable; | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public void setName(String name) { | ||
this.name = name; | ||
} | ||
|
||
public String getDescription() { | ||
return description; | ||
} | ||
|
||
public void setDescription(String description) { | ||
this.description = description; | ||
} | ||
|
||
public BigDecimal getPrice() { | ||
return price; | ||
} | ||
|
||
public void setPrice(BigDecimal price) { | ||
this.price = price; | ||
} | ||
|
||
public String getImage() { | ||
return image; | ||
} | ||
|
||
public void setImage(String image) { | ||
this.image = image; | ||
} | ||
|
||
public Category getCategory() { | ||
return category; | ||
} | ||
|
||
public void setCategory(Category category) { | ||
this.category = category; | ||
} | ||
|
||
public Boolean getIsAvailable() { | ||
return isAvailable; | ||
} | ||
|
||
public void setIsAvailable(Boolean isAvailable) { | ||
this.isAvailable = isAvailable; | ||
} | ||
|
||
|
||
@Override | ||
public String toString() { | ||
return "Dish [name=" + name + ", price=" + price + ", category=" + category | ||
+ "]"; | ||
} | ||
|
||
} |
135 changes: 135 additions & 0 deletions
135
my-delivery-registration/src/main/java/dev/helis/registration/rest/dto/DishResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
package dev.helis.registration.rest.dto; | ||
|
||
import java.io.Serializable; | ||
import java.math.BigDecimal; | ||
import java.net.URL; | ||
|
||
import dev.helis.registration.entity.Category; | ||
|
||
|
||
public class DishResponse implements Serializable { | ||
|
||
private static final long serialVersionUID = 1L; | ||
|
||
private Long id; | ||
|
||
private String name; | ||
|
||
private String description; | ||
|
||
private BigDecimal price; | ||
|
||
private URL image; | ||
|
||
private Category category; | ||
|
||
private Boolean isAvailable; | ||
|
||
private Long restaurant; | ||
|
||
|
||
public Long getId() { | ||
return id; | ||
} | ||
|
||
public void setId(Long id) { | ||
this.id = id; | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public void setName(String name) { | ||
this.name = name; | ||
} | ||
|
||
public String getDescription() { | ||
return description; | ||
} | ||
|
||
public void setDescription(String description) { | ||
this.description = description; | ||
} | ||
|
||
public BigDecimal getPrice() { | ||
return price; | ||
} | ||
|
||
public void setPrice(BigDecimal price) { | ||
this.price = price; | ||
} | ||
|
||
public URL getImage() { | ||
return image; | ||
} | ||
|
||
public void setImage(URL image) { | ||
this.image = image; | ||
} | ||
|
||
public Category getCategory() { | ||
return category; | ||
} | ||
|
||
public void setCategory(Category category) { | ||
this.category = category; | ||
} | ||
|
||
public Boolean getIsAvailable() { | ||
return isAvailable; | ||
} | ||
|
||
public void setIsAvailable(Boolean isAvailable) { | ||
this.isAvailable = isAvailable; | ||
} | ||
|
||
public Long getRestaurant() { | ||
return restaurant; | ||
} | ||
|
||
public void setRestaurant(Long restaurant) { | ||
this.restaurant = restaurant; | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
final int prime = 31; | ||
int result = 1; | ||
result = prime * result + ((name == null) ? 0 : name.hashCode()); | ||
result = prime * result + ((category == null) ? 0 : category.hashCode()); | ||
result = prime * result + ((restaurant == null) ? 0 : restaurant.hashCode()); | ||
return result; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object obj) { | ||
if (this == obj) | ||
return true; | ||
if (obj == null) | ||
return false; | ||
if (getClass() != obj.getClass()) | ||
return false; | ||
DishResponse other = (DishResponse) obj; | ||
if (name == null) { | ||
if (other.name != null) | ||
return false; | ||
} else if (!name.equals(other.name)) | ||
return false; | ||
if (category != other.category) | ||
return false; | ||
if (restaurant == null) { | ||
if (other.restaurant != null) | ||
return false; | ||
} else if (!restaurant.equals(other.restaurant)) | ||
return false; | ||
return true; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "Dish [name=" + name + ", price=" + price + ", category=" + category + ", restaurant=" + restaurant | ||
+ "]"; | ||
} | ||
|
||
} |
48 changes: 48 additions & 0 deletions
48
...livery-registration/src/main/java/dev/helis/registration/rest/dto/mappers/DishMapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package dev.helis.registration.rest.dto.mappers; | ||
|
||
import java.net.MalformedURLException; | ||
|
||
import dev.helis.registration.entity.Dish; | ||
import dev.helis.registration.entity.Restaurant; | ||
import dev.helis.registration.rest.dto.DishRequest; | ||
import dev.helis.registration.rest.dto.DishResponse; | ||
import jakarta.ws.rs.core.UriBuilderException; | ||
|
||
public class DishMapper { | ||
|
||
|
||
private DishMapper() { | ||
// Private constructor to hide the implicit public one | ||
} | ||
|
||
public static DishResponse mapToDto(Dish entity) { | ||
DishResponse dish = new DishResponse(); | ||
|
||
dish.setId(entity.id); | ||
dish.setName(entity.name); | ||
dish.setDescription(entity.description); | ||
dish.setPrice(entity.price); | ||
dish.setImage(entity.image); | ||
dish.setCategory(entity.category); | ||
dish.setIsAvailable(entity.isAvailable); | ||
dish.setRestaurant(entity.restaurant.id); | ||
|
||
|
||
return dish; | ||
} | ||
|
||
public static Dish mapToEntity(Restaurant restaurant, DishRequest dto) throws MalformedURLException, IllegalArgumentException, UriBuilderException { | ||
|
||
Dish dish = new Dish(); | ||
dish.name = dto.getName(); | ||
dish.description = dto.getDescription(); | ||
dish.price = dto.getPrice(); | ||
dish.image = null; | ||
dish.category = dto.getCategory(); | ||
dish.isAvailable = dto.getIsAvailable(); | ||
dish.restaurant = restaurant; | ||
|
||
return dish; | ||
} | ||
|
||
} |
Oops, something went wrong.