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
15 changes: 15 additions & 0 deletions .run/ShareItGateway.run.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="ShareItGateway" type="Application" factoryName="Application" nameIsGenerated="true">
<option name="MAIN_CLASS_NAME" value="ru.practicum.shareit.ShareItGateway" />
<module name="shareit-gateway" />
<extension name="coverage">
<pattern>
<option name="PATTERN" value="ru.practicum.shareit.*" />
<option name="ENABLED" value="true" />
</pattern>
</extension>
<method v="2">
<option name="Make" enabled="true" />
</method>
</configuration>
</component>
15 changes: 15 additions & 0 deletions .run/ShareItServer.run.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="ShareItServer" type="Application" factoryName="Application" nameIsGenerated="true">
<option name="MAIN_CLASS_NAME" value="ru.practicum.shareit.ShareItServer" />
<module name="shareit-server" />
<extension name="coverage">
<pattern>
<option name="PATTERN" value="ru.practicum.shareit.*" />
<option name="ENABLED" value="true" />
</pattern>
</extension>
<method v="2">
<option name="Make" enabled="true" />
</method>
</configuration>
</component>
57 changes: 57 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
version: '3.8'

services:
gateway:
build: ./gateway
container_name: shareit-gateway
ports:
- "8080:8080"
depends_on:
server:
condition: service_healthy
environment:
- SHAREIT_SERVER_URL=http://server:9090
healthcheck:
test: ["CMD", "curl", "-f", "http://localhost:8080/actuator/health"]
interval: 10s
timeout: 5s
retries: 10

server:
build: ./server
container_name: shareit-server
ports:
- "9090:9090"
depends_on:
db:
condition: service_healthy
environment:
- SPRING_DATASOURCE_URL=jdbc:postgresql://db:5432/shareit
- SPRING_DATASOURCE_USERNAME=postgres
- SPRING_DATASOURCE_PASSWORD=postgres
healthcheck:
test: ["CMD", "curl", "-f", "http://localhost:9090/actuator/health"]
interval: 10s
timeout: 5s
retries: 10
command: ["./wait-for-it.sh", "db:5432", "--timeout=120", "--", "java", "-jar", "app.jar"]

db:
image: postgres:13.7-alpine
container_name: postgres
ports:
- "5432:5432"
environment:
- POSTGRES_PASSWORD=postgres
- POSTGRES_USER=postgres
- POSTGRES_DB=shareit
volumes:
- postgres_data:/var/lib/postgresql/data
healthcheck:
test: ["CMD-SHELL", "pg_isready -U postgres"]
interval: 5s
timeout: 5s
retries: 10

volumes:
postgres_data:
5 changes: 5 additions & 0 deletions gateway/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
FROM eclipse-temurin:21-jre-jammy
VOLUME /tmp
ARG JAR_FILE=target/*.jar
COPY ${JAR_FILE} app.jar
ENTRYPOINT ["sh", "-c", "java ${JAVA_OPTS} -jar /app.jar"]
83 changes: 83 additions & 0 deletions gateway/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?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">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>ru.practicum</groupId>
<artifactId>shareit</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>

<artifactId>shareit-gateway</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>ShareIt Gateway</name>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.14</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents.client5</groupId>
<artifactId>httpclient5</artifactId>
<version>5.2.1</version>
</dependency>
<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct</artifactId>
<version>${mapstruct.version}</version>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</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.mapstruct</groupId>
<artifactId>mapstruct-processor</artifactId>
<version>${mapstruct.version}</version>
</path>
<path>
<groupId>org.projectlombok</groupId>
<artifactId>lombok-mapstruct-binding</artifactId>
<version>0.2.0</version>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>
</plugins>
</build>
</project>
11 changes: 11 additions & 0 deletions gateway/src/main/java/ru/practicum/shareit/ShareItGatewayApp.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package ru.practicum.shareit;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class ShareItGatewayApp {
public static void main(String[] args) {
SpringApplication.run(ShareItGatewayApp.class, args);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package ru.practicum.shareit.booking;

public enum BookingStatus {
WAITING,
APPROVED,
REJECTED,
CANCELED
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package ru.practicum.shareit.booking.client;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.http.ResponseEntity;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.stereotype.Service;
import org.springframework.web.util.DefaultUriBuilderFactory;
import ru.practicum.shareit.booking.dto.BookingDto;
import ru.practicum.shareit.booking.dto.BookingState;
import ru.practicum.shareit.client.BaseClient;

import java.util.Map;

@Service
public class BookingClient extends BaseClient {
private static final String API_PREFIX = "/bookings";

@Autowired
public BookingClient(@Value("${shareit-server.url}") String serverUrl, RestTemplateBuilder builder) {
super(
builder
.uriTemplateHandler(new DefaultUriBuilderFactory(serverUrl + API_PREFIX))
.requestFactory(() -> new HttpComponentsClientHttpRequestFactory())
.build()
);
}

public ResponseEntity<Object> createBooking(BookingDto bookingDto, long userId) {
return post("", userId, bookingDto);
}

public ResponseEntity<Object> approve(long bookingId, long userId, Boolean approved) {
Map<String, Object> parameters = Map.of("approved", approved);
return patch("/" + bookingId + "?approved={approved}", userId, parameters, null);
}

public ResponseEntity<Object> getById(long bookingId, long userId) {
return get("/" + bookingId, userId);
}

public ResponseEntity<Object> getAllByBooker(long userId, BookingState state, int from, int size) {
Map<String, Object> parameters = Map.of(
"state", state.name(),
"from", from,
"size", size
);
return get("?state={state}&from={from}&size={size}", userId, parameters);
}

public ResponseEntity<Object> getAllByOwner(long userId, BookingState state, int from, int size) {
Map<String, Object> parameters = Map.of(
"state", state.name(),
"from", from,
"size", size
);
return get("/owner?state={state}&from={from}&size={size}", userId, parameters);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package ru.practicum.shareit.booking.controller;

import jakarta.validation.Valid;
import jakarta.validation.constraints.Positive;
import jakarta.validation.constraints.PositiveOrZero;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PatchMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseStatus;
import ru.practicum.shareit.booking.client.BookingClient;
import ru.practicum.shareit.booking.dto.BookingDto;
import ru.practicum.shareit.booking.dto.BookingState;
import ru.practicum.shareit.util.Constants;

@Controller
@RequestMapping(path = "/bookings")
@RequiredArgsConstructor
@Slf4j
@Validated
public class BookingController {
private final BookingClient bookingClient;

@PostMapping
@ResponseStatus(HttpStatus.CREATED)
public ResponseEntity<Object> createBooking(@Valid @RequestBody BookingDto bookingDto,
@RequestHeader(Constants.USER_ID_HEADER) Long userId) {
log.info("Creating booking {}, userId={}", bookingDto, userId);
return bookingClient.createBooking(bookingDto, userId);
}

@PatchMapping("/{bookingId}")
public ResponseEntity<Object> approve(@PathVariable Long bookingId,
@RequestHeader(Constants.USER_ID_HEADER) Long userId,
@RequestParam Boolean approved) {
log.info("Patch booking {}, userId={}, approved={}", bookingId, userId, approved);
return bookingClient.approve(bookingId, userId, approved);
}

@GetMapping("/{bookingId}")
public ResponseEntity<Object> getById(@PathVariable Long bookingId,
@RequestHeader(Constants.USER_ID_HEADER) Long userId) {
log.info("Get booking {}, userId={}", bookingId, userId);
return bookingClient.getById(bookingId, userId);
}

@GetMapping
public ResponseEntity<Object> getAllByBooker(
@RequestHeader(Constants.USER_ID_HEADER) Long userId,
@RequestParam(name = "state", defaultValue = "all") String stateParam,
@RequestParam(defaultValue = "0") @PositiveOrZero int from,
@RequestParam(defaultValue = "10") @Positive int size) {
BookingState state = BookingState.from(stateParam)
.orElseThrow(() -> new IllegalArgumentException("Unknown state: " + stateParam));
log.info("Get bookings by booker userId={}, state={}, from={}, size={}", userId, state, from, size);
return bookingClient.getAllByBooker(userId, state, from, size);
}

@GetMapping("/owner")
public ResponseEntity<Object> getAllByOwner(
@RequestHeader(Constants.USER_ID_HEADER) Long userId,
@RequestParam(name = "state", defaultValue = "all") String stateParam,
@RequestParam(defaultValue = "0") @PositiveOrZero int from,
@RequestParam(defaultValue = "10") @Positive int size) {
BookingState state = BookingState.from(stateParam)
.orElseThrow(() -> new IllegalArgumentException("Unknown state: " + stateParam));
log.info("Get bookings by owner userId={}, state={}, from={}, size={}", userId, state, from, size);
return bookingClient.getAllByOwner(userId, state, from, size);
}
}
Original file line number Diff line number Diff line change
@@ -1,35 +1,30 @@
package ru.practicum.shareit.booking.dto;

import jakarta.validation.constraints.Future;
import jakarta.validation.constraints.NotNull;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.Setter;
import lombok.Data;
import lombok.NoArgsConstructor;
import ru.practicum.shareit.booking.BookingStatus;
import ru.practicum.shareit.item.dto.ItemDto;
import ru.practicum.shareit.user.dto.UserDto;

import java.time.LocalDateTime;

@Getter
@Setter
@Builder(toBuilder = true)
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class BookingDto {
private Long id;

@NotNull(message = "Дата начала бронирования не может быть пустой")
@Future(message = "Дата начала бронирования должна быть в будущем")
private LocalDateTime start;

@NotNull(message = "Дата окончания бронирования не может быть пустой")
@Future(message = "Дата окончания бронирования должна быть в будущем")
private LocalDateTime end;

private ItemDto item;

private UserDto booker;
@NotNull(message = "ID вещи должен быть указан")
private Long itemId;

private Long bookerId;
private BookingStatus status;

private Long itemId;
}
Loading