Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ target/
.settings
.springBeans
.sts4-cache
.env

### IntelliJ IDEA ###
.idea
Expand Down
14 changes: 14 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
FROM openjdk:17-slim AS builder
WORKDIR application
ARG JAR_FILE=target/*.jar
COPY ${JAR_FILE} application.jar
RUN java -Djarmode=layertools -jar application.jar extract

FROM openjdk:17-slim
WORKDIR application
COPY --from=builder application/dependencies/ ./
COPY --from=builder application/spring-boot-loader/ ./
COPY --from=builder application/snapshot-dependencies/ ./
COPY --from=builder application/application/ ./
ENTRYPOINT ["java", "org.springframework.boot.loader.launch.JarLauncher"]
EXPOSE 8080
39 changes: 39 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
version: "3.8"

services:
mysqldb:
image: mysql:8
platform: linux/amd64
restart: unless-stopped
env_file: ./.env
environment:
MYSQL_ROOT_PASSWORD: "${MYSQLDB_ROOT_PASSWORD}"
MYSQL_DATABASE: "${MYSQLDB_DATABASE}"
MYSQL_USER: "${MYSQLDB_USER}"
MYSQL_PASSWORD: "${MYSQLDB_PASSWORD}"
ports:
- "${MYSQLDB_LOCAL_PORT}:3306"
healthcheck:
test: ["CMD", "mysqladmin", "ping", "-h", "localhost"]
interval: 30s
timeout: 30s
retries: 3

bookstore-service:
build:
context: .
dockerfile: Dockerfile
env_file: ./.env
restart: on-failure
ports:
- "${SPRING_LOCAL_PORT}:${SPRING_DOCKER_PORT}"
- "${DEBUG_PORT}:${DEBUG_PORT}"
environment:
SPRING_DATASOURCE_URL: "jdbc:mysql://mysqldb:3306/${MYSQLDB_DATABASE}"
SPRING_DATASOURCE_USERNAME: "${MYSQLDB_USER}"
SPRING_DATASOURCE_PASSWORD: "${MYSQLDB_PASSWORD}"
SPRING_JPA_PROPERTIES_HIBERNATE_DIALECT: "org.hibernate.dialect.MySQL8Dialect"
JAVA_TOOL_OPTIONS: "-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=*:5005"

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
JAVA_TOOL_OPTIONS: "-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=*:5005"
JAVA_TOOL_OPTIONS: "-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=*:{DEBUG_PORT}"

depends_on:
mysqldb:
condition: service_healthy
92 changes: 9 additions & 83 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,39 +6,32 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.3.3</version>
<relativePath/> <!-- lookup parent from repository -->
<relativePath/>
</parent>
<groupId>mate.academy</groupId>
<artifactId>Books</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>Books</name>
<description>Books</description>
<url/>
<licenses>
<license/>
</licenses>
<developers>
<developer/>
</developers>
<scm>
<connection/>
<developerConnection/>
<tag/>
<url/>
</scm>
<properties>
<java.version>17</java.version>
<maven.checkstyle.plugin.configLocation>checkstyle.xml</maven.checkstyle.plugin.configLocation>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<lombok.mapstruct.binding.version>0.2.0</lombok.mapstruct.binding.version>
<mapstruct.version>1.5.5.Final</mapstruct.version>
<jjwt.version>0.12.6</jjwt.version>
<liquibase.version>4.27.0</liquibase.version>
<lombok.version>1.18.32</lombok.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-docker-compose</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
Expand All @@ -54,11 +47,6 @@
<artifactId>liquibase-core</artifactId>
<version>${liquibase.version}</version>
</dependency>
<dependency>
<groupId>org.liquibase</groupId>
<artifactId>liquibase-maven-plugin</artifactId>
<version>${liquibase.version}</version>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
Expand All @@ -67,6 +55,7 @@
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
Expand Down Expand Up @@ -118,6 +107,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.11.0</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
Expand Down Expand Up @@ -160,70 +150,6 @@
<sourceDirectories>src</sourceDirectories>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
<annotationProcessorPaths>
<path>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
</path>
<path>
<groupId>org.projectlombok</groupId>
<artifactId>lombok-mapstruct-binding</artifactId>
<version>${lombok.mapstruct.binding.version}</version>
</path>
<path>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-processor</artifactId>
<version>${mapstruct.version}</version>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<version>3.3.0</version>
<executions>
<execution>
<phase>compile</phase>
<goals>
<goal>check</goal>
</goals>
</execution>
</executions>
<configuration>
<configLocation>${maven.checkstyle.plugin.configLocation}</configLocation>
<consoleOutput>true</consoleOutput>
<failsOnError>true</failsOnError>
<linkXRef>false</linkXRef>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<version>3.3.0</version>
<executions>
<execution>
<phase>compile</phase>
<goals>
<goal>check</goal>
</goals>
</execution>
</executions>
<configuration>
<configLocation>${maven.checkstyle.plugin.configLocation}</configLocation>
<consoleOutput>true</consoleOutput>
<failsOnError>true</failsOnError>
<linkXRef>false</linkXRef>
<sourceDirectories>src</sourceDirectories>
</configuration>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ public List<OrderResponseDto> getOrders(Pageable pageable, Authentication authen
@PostMapping
public OrderResponseDto createOrder(Authentication authentication,
@RequestBody @Valid OrderRequestDto orderRequestDto) {
Long authenticatedUserId = getAuthenticatedUserId(authentication);
return orderService.createOrderByUserId(authenticatedUserId,

return orderService.createOrderByUser((User) authentication.getPrincipal(),
orderRequestDto);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@
import mate.academy.bookshop.dto.order.OrderResponseDto;
import mate.academy.bookshop.dto.order.OrderUpdateStatusDto;
import mate.academy.bookshop.dto.orderitem.OrderItemResponseDto;
import mate.academy.bookshop.model.User;
import org.springframework.data.domain.Pageable;

public interface OrderService {
OrderResponseDto createOrderByUserId(Long userId, OrderRequestDto orderRequestDto);
OrderResponseDto createOrderByUser(User user, OrderRequestDto orderRequestDto);

List<OrderResponseDto> getOrderByUserId(Pageable pageable, Long userId);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@
import mate.academy.bookshop.model.Order;
import mate.academy.bookshop.model.OrderItem;
import mate.academy.bookshop.model.ShoppingCart;
import mate.academy.bookshop.model.User;
import mate.academy.bookshop.repository.OrderItemRepository;
import mate.academy.bookshop.repository.OrderRepository;
import mate.academy.bookshop.repository.ShoppingCartRepository;
import mate.academy.bookshop.repository.UserRepository;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

Expand All @@ -35,23 +35,20 @@ public class OrderServiceImpl implements OrderService {
private final OrderRepository orderRepository;
private final OrderItemRepository orderItemRepository;
private final ShoppingCartRepository shoppingCartRepository;
private final UserRepository userRepository;

@Override
public OrderResponseDto createOrderByUserId(Long userId, OrderRequestDto orderRequestDto) {
public OrderResponseDto createOrderByUser(User user, OrderRequestDto orderRequestDto) {
ShoppingCart shoppingCart = shoppingCartRepository
.findByUserId(userId)
.findByUserId(user.getId())
.orElseThrow(() -> new EntityNotFoundException("Shopping cart not found"
+ " for user with id: " + userId
+ " for user with id: " + user.getId()
));
if (shoppingCart.getCartItems().isEmpty()) {
throw new OrderProcessingException("Shopping cart is empty "
+ "for user id: " + userId);
+ "for user id: " + user.getId());
}
Order order = orderMapper.toEntity(orderRequestDto);
order.setUser(userRepository.findById(userId)
.orElseThrow(() -> new EntityNotFoundException("User with id "
+ userId + " not found")));
order.setUser(user);
order.setStatus(Order.Status.PENDING);
order.setOrderDate(LocalDateTime.now());

Expand Down