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>
39 changes: 39 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
services:
gateway:
build: gateway
image: shareit-gateway
container_name: shareit-gateway
ports:
- "8080:8080"
depends_on:
- server
environment:
- SHAREIT_SERVER_URL=http://server:9090

server:
build: server
image: shareit-server
container_name: shareit-server
ports:
- "9090:9090"
depends_on:
- db
environment:
- SPRING_DATASOURCE_URL=jdbc:postgresql://db:5432/shareit
- SPRING_DATASOURCE_USERNAME=dbuser
- SPRING_DATASOURCE_PASSWORD=12345

db:
image: postgres:16.1
container_name: postgres
ports:
- "6541:5432"
environment:
- POSTGRES_PASSWORD=12345
- POSTGRES_USER=dbuser
- POSTGRES_DB=shareit
healthcheck:
test: pg_isready -q -d $$POSTGRES_DB -U $$POSTGRES_USER
timeout: 5s
interval: 5s
retries: 10
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"]
70 changes: 70 additions & 0 deletions gateway/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?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.hibernate.validator</groupId>
<artifactId>hibernate-validator</artifactId>
</dependency>

<dependency>
<groupId>org.apache.httpcomponents.client5</groupId>
<artifactId>httpclient5</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>

<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

</project>
12 changes: 12 additions & 0 deletions gateway/src/main/java/ru/practicum/shareit/ShareItGateway.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package ru.practicum.shareit;

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

@SpringBootApplication
public class ShareItGateway {
public static void main(String[] args) {
SpringApplication.run(ShareItGateway.class, args);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package ru.practicum.shareit.booking;

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.BookingDtoRequestCreate;
import ru.practicum.shareit.client.BaseClient;
import ru.practicum.shareit.booking.dto.BookingState;

import java.util.Map;

@Service
public class BookingClient extends BaseClient {
@Autowired
public BookingClient(@Value("http://shareit-server:9090") String serverUrl, RestTemplateBuilder builder) {
super(
builder
.uriTemplateHandler(new DefaultUriBuilderFactory(serverUrl + "/bookings"))
.requestFactory(() -> new HttpComponentsClientHttpRequestFactory())
.build()
);
}

public ResponseEntity<Object> create(Long userId, BookingDtoRequestCreate booking) {
return post("", userId, booking);
}

public ResponseEntity<Object> approve(Long userId, Long bookingId, Boolean approved) {
return patch("/" + bookingId + "?approved=" + approved, userId);
}

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

public ResponseEntity<Object> getAllBookingCurrentUser(Long userId, BookingState state) {
Map<String, Object> parameters = Map.of(
"state", state.name()
);
return get("?state={state}", userId, parameters);
}

public ResponseEntity<Object> getAllBookingAllItemCurrentUser(Long userId, BookingState state) {
Map<String, Object> parameters = Map.of(
"state", state.name()
);
return get("/owner?state={state}", userId, parameters);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package ru.practicum.shareit.booking;

import jakarta.validation.Valid;
import jakarta.validation.constraints.Positive;
import lombok.RequiredArgsConstructor;
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.*;
import ru.practicum.shareit.booking.dto.BookingDtoRequestCreate;
import ru.practicum.shareit.booking.dto.BookingState;

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

@ResponseStatus(HttpStatus.CREATED)
@PostMapping
public ResponseEntity<Object> create(
@RequestHeader(value = "X-Sharer-User-Id") @Positive Long userId,
@RequestBody @Valid BookingDtoRequestCreate booking
) {
return bookingClient.create(userId, booking);
}

@ResponseStatus(HttpStatus.OK)
@PatchMapping("/{bookingId}")
public ResponseEntity<Object> approve(
@RequestHeader(value = "X-Sharer-User-Id") @Positive Long userId,
@PathVariable @Positive Long bookingId,
@RequestParam("approved") Boolean approved
) {
return bookingClient.approve(userId, bookingId, approved);
}

@ResponseStatus(HttpStatus.OK)
@GetMapping("/{bookingId}")
public ResponseEntity<Object> get(
@RequestHeader(value = "X-Sharer-User-Id") @Positive Long userId,
@PathVariable @Positive Long bookingId
) {
return bookingClient.get(userId, bookingId);
}

@ResponseStatus(HttpStatus.OK)
@GetMapping
public ResponseEntity<Object> getAllBookingCurrentUser(
@RequestHeader(value = "X-Sharer-User-Id") @Positive Long userId,
@RequestParam(name = "state", defaultValue = "ALL") BookingState state
) {
return bookingClient.getAllBookingCurrentUser(userId, state);
}

@ResponseStatus(HttpStatus.OK)
@GetMapping("/owner")
public ResponseEntity<Object> getAllBookingAllItemCurrentUser(
@RequestHeader(value = "X-Sharer-User-Id") @Positive Long userId,
@RequestParam(name = "state", defaultValue = "ALL") BookingState state
) {
return bookingClient.getAllBookingAllItemCurrentUser(userId, state);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package ru.practicum.shareit.booking.dto;

import java.time.LocalDateTime;

import jakarta.validation.constraints.Future;
import jakarta.validation.constraints.FutureOrPresent;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Getter
@NoArgsConstructor
@AllArgsConstructor
public class BookItemRequestDto {
private long itemId;
@FutureOrPresent
private LocalDateTime start;
@Future
private LocalDateTime end;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package ru.practicum.shareit.booking.dto;

import jakarta.validation.constraints.FutureOrPresent;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Positive;

import java.time.LocalDateTime;

public record BookingDtoRequestCreate(
@NotNull(message = "Необходимо указать идентификатор вещи")
@Positive(message = "Идентификатор вещи должен быть больше нуля")
Long itemId,
@NotNull(message = "Необходимо указать дату и время начала аренды")
@FutureOrPresent(message = "Нельзя указывать прошедшую дату и время начала аренды")
LocalDateTime start,
@FutureOrPresent(message = "Нельзя указывать прошедшую дату и время окончания аренды")
@NotNull(message = "Необходимо указать дату и время окончания аренды")
LocalDateTime end
) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package ru.practicum.shareit.booking.dto;

import ru.practicum.shareit.item.dto.ItemDtoResponse;
import ru.practicum.shareit.user.dto.UserDtoResponse;

import java.time.LocalDateTime;

public record BookingDtoResponse(
Long id,
LocalDateTime start,
LocalDateTime end,
Status status,
ItemDtoResponse item,
UserDtoResponse booker
) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package ru.practicum.shareit.booking.dto;

import java.util.Optional;

public enum BookingState {
ALL,
CURRENT,
FUTURE,
PAST,
REJECTED,
WAITING;

public static Optional<BookingState> from(String stringState) {
for (BookingState state : values()) {
if (state.name().equalsIgnoreCase(stringState)) {
return Optional.of(state);
}
}
return Optional.empty();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package ru.practicum.shareit.booking.dto;

public enum Status {
WAITING,
APPROVED,
REJECTED,
CANCELED
}
Loading