Skip to content

Commit

Permalink
Merge branch 'dev' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
helisfreitas authored Oct 4, 2024
2 parents 1735faf + 17e443e commit b11a6dc
Show file tree
Hide file tree
Showing 27 changed files with 1,135 additions and 17 deletions.
83 changes: 66 additions & 17 deletions my-delivery-registration/pom.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<?xml version="1.0"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>dev.helis</groupId>
Expand All @@ -11,25 +13,25 @@
<version>0.0.1</version>

<name>Registration Api</name>

<properties>
<compiler-plugin.version>3.13.0</compiler-plugin.version>
<maven.compiler.release>21</maven.compiler.release>
<maven.compiler.target>21</maven.compiler.target>
<maven.compiler.source>21</maven.compiler.source>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<quarkus.platform.artifact-id>quarkus-bom</quarkus.platform.artifact-id>
<quarkus.platform.group-id>io.quarkus.platform</quarkus.platform.group-id>
<quarkus.platform.version>3.15.1</quarkus.platform.version>
<quarkus.version>3.15.1</quarkus.version>
<skipITs>true</skipITs>
<surefire-plugin.version>3.3.1</surefire-plugin.version>
<surefire-plugin.version>3.5.0</surefire-plugin.version>
<testcontainers.version>1.20.2</testcontainers.version>
</properties>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>${quarkus.platform.group-id}</groupId>
<artifactId>${quarkus.platform.artifact-id}</artifactId>
<version>${quarkus.platform.version}</version>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-bom</artifactId>
<version>${quarkus.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
Expand All @@ -54,27 +56,68 @@
<artifactId>quarkus-resteasy-jsonb</artifactId>
</dependency>
<dependency>
<groupId> io.quarkus</groupId>
<artifactId>quarkus-hibernate-validator</artifactId>
<groupId> io.quarkus</groupId>
<artifactId>quarkus-hibernate-validator</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-smallrye-openapi</artifactId>
</dependency>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-junit5</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>testcontainers</artifactId>
<version>${testcontainers.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>postgresql</artifactId>
<version>${testcontainers.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.github.database-rider</groupId>
<artifactId>rider-cdi</artifactId>
<version>1.44.0</version>
<scope>test</scope>
<classifier>jakarta</classifier>
<exclusions>
<exclusion>
<artifactId>stax-api</artifactId>
<groupId>stax</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>rest-assured</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.approvaltests</groupId>
<artifactId>approvaltests</artifactId>
<version>24.7.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.11.0</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>${quarkus.platform.group-id}</groupId>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-maven-plugin</artifactId>
<version>${quarkus.platform.version}</version>
<version>${quarkus.version}</version>
<extensions>true</extensions>
<executions>
<execution>
Expand All @@ -88,16 +131,22 @@
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${compiler-plugin.version}</version>
<configuration>
<parameters>true</parameters>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>${surefire-plugin.version}</version>
<configuration>
<includes>
<include>**/*IT.java</include>
<include>**/*Test.java</include>
</includes>
<systemPropertyVariables>
<java.util.logging.manager>org.jboss.logmanager.LogManager</java.util.logging.manager>
<maven.home>${maven.home}</maven.home>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,10 @@ public class Dish extends PanacheEntityBase {
public Long id;

@NotBlank
@OnlyCharacterAndPunctuation
public String name;

@OnlyCharacterAndPunctuation
public String description;

@NotNull
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import java.time.LocalDate;

import dev.helis.registration.validation.OnlyCharacterAndPunctuation;

import io.quarkus.hibernate.orm.panache.PanacheEntityBase;
import jakarta.persistence.CascadeType;
import jakarta.persistence.Entity;
Expand All @@ -23,6 +25,7 @@ public class Restaurant extends PanacheEntityBase {
public String owner;

@NotBlank
@OnlyCharacterAndPunctuation
public String name;

@ManyToOne(cascade = CascadeType.ALL)
Expand Down
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();
}
}
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
}
}
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();});
}


}
Loading

0 comments on commit b11a6dc

Please sign in to comment.