-
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.
- Loading branch information
Showing
27 changed files
with
1,135 additions
and
17 deletions.
There are no files selected for viewing
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
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
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
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(); | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
my-delivery-registration/src/main/java/dev/helis/registration/rest/ResourcePaths.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,11 @@ | ||
package dev.helis.registration.rest; | ||
|
||
public class ResourcePaths { | ||
|
||
public static final String RESTAURANTS = "/restaurants"; | ||
public static final String DISHES = "/restaurants/{restaurantId}/dishes"; | ||
|
||
private ResourcePaths() { | ||
// Prevent instantiation | ||
} | ||
} |
77 changes: 77 additions & 0 deletions
77
my-delivery-registration/src/main/java/dev/helis/registration/rest/RestaurantResources.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,77 @@ | ||
package dev.helis.registration.rest; | ||
|
||
import java.net.URI; | ||
import java.util.List; | ||
import java.util.Optional; | ||
import java.util.stream.Collectors; | ||
|
||
import dev.helis.registration.entity.Restaurant; | ||
import dev.helis.registration.rest.dto.RestaurantRequest; | ||
import dev.helis.registration.rest.dto.RestaurantResponse; | ||
import dev.helis.registration.rest.dto.mappers.RestaurantMapper; | ||
import io.quarkus.hibernate.orm.panache.PanacheEntityBase; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
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.Produces; | ||
import jakarta.ws.rs.core.MediaType; | ||
import jakarta.ws.rs.core.Response; | ||
|
||
@Path(ResourcePaths.RESTAURANTS) | ||
@Produces(MediaType.APPLICATION_JSON) | ||
@Consumes(MediaType.APPLICATION_JSON) | ||
public class RestaurantResources { | ||
|
||
@GET | ||
public List<RestaurantResponse> findAll() { | ||
return Restaurant.findAll().stream().map( r -> (Restaurant)r).map(RestaurantMapper::mapToDto).collect(Collectors.toList()); | ||
} | ||
|
||
@GET | ||
@Path("/{id}") | ||
public RestaurantResponse findById(Long id) { | ||
return Restaurant.findByIdOptional(id).map( r -> (Restaurant)r).map(RestaurantMapper::mapToDto).orElseThrow(NotFoundException::new); | ||
} | ||
|
||
@POST | ||
@Transactional | ||
public Response create(@Valid RestaurantRequest restaurant) { | ||
Restaurant mapToEntity = RestaurantMapper.mapToEntity(restaurant); | ||
Restaurant.persist(mapToEntity); | ||
return Response.status(HttpServletResponse.SC_CREATED).location(URI.create(ResourcePaths.RESTAURANTS + "/" + mapToEntity.id)).build(); | ||
} | ||
|
||
|
||
@PUT | ||
@Path("/{id}") | ||
@Transactional | ||
public void update(Long id, @Valid RestaurantRequest updatedRestaurant) { | ||
Optional<Restaurant> optional = Restaurant.findByIdOptional(id); | ||
if (!optional.isPresent()) { | ||
throw new NotFoundException(); | ||
} | ||
Restaurant restaurant = optional.get(); | ||
restaurant.name = updatedRestaurant.getName(); | ||
restaurant.location.setLatitude(updatedRestaurant.getLocation().getLatitude()); | ||
restaurant.location.setLongitude(updatedRestaurant.getLocation().getLongitude()); | ||
|
||
restaurant.persist(); | ||
} | ||
|
||
@DELETE | ||
@Path("/{id}") | ||
public void delete(Long id) { | ||
|
||
Optional<Restaurant> optional = Restaurant.findByIdOptional(id); | ||
optional.ifPresentOrElse(PanacheEntityBase::delete, () -> {throw new NotFoundException();}); | ||
} | ||
|
||
|
||
} |
Oops, something went wrong.