Skip to content
Open
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
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,9 @@ build/

### VS Code ###
.vscode/

*.DS_Store

*.class
*.jar
.DS_Store
45 changes: 45 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
version: '3.8'
services:
gateway:
build:
context: .
dockerfile: gateway/Dockerfile
image: shared-gateway
container_name: shareit-gateway
ports:
- "8080:8080"
depends_on:
- server
environment:
- SHAREIT_SERVER_URL=http://server:9090

server:
build:
context: .
dockerfile: server/Dockerfile
image: shared-server
container_name: shareit-server
expose:
- 9090
ports:
- "9090:9090"
depends_on:
- db
environment:
- SPRING_DATASOURCE_URL=jdbc:postgresql://db:5432/shareit
- SPRING_DATASOURCE_USERNAME=root
- SPRING_DATASOURCE_PASSWORD=root
- SPRING_JPA_HIBERNATE_DDL_AUTO=update
command: -p 9090

db:

Choose a reason for hiding this comment

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

Здорово выполнили с docker-compose, но название сервиса базы я бы переименовал в postgres, т.к. у вас может быть ещё один сервис базы mongo и так далее

image: postgres:13.7-alpine
container_name: database
ports:
- "6541:5432"
volumes:
- ./server/resources:/schema.sql
environment:
- POSTGRES_PASSWORD=root
- POSTGRES_USER=root
- POSTGRES_DB=shareit
3 changes: 3 additions & 0 deletions gateway/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
FROM amazoncorretto:11
COPY gateway/target/*.jar app.jar
ENTRYPOINT ["java","-jar","/app.jar"]
71 changes: 71 additions & 0 deletions gateway/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?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>

<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<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>
</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,66 @@
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.bind.annotation.*;
import org.springframework.web.util.DefaultUriBuilderFactory;
import ru.practicum.shareit.booking.dto.BookingShortDto;
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(HttpComponentsClientHttpRequestFactory::new)
.build()
);
}

public ResponseEntity<Object> createBooking(BookingShortDto bookingShortDto, Long userId) {
return post("", userId, bookingShortDto);
}

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

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

@GetMapping
public ResponseEntity<Object> getAllBookings(Long userId, BookingState state, Long from, Long size) {
Map<String, Object> parameters = Map.of(
"state", state,
"from", from,
"size", size
);
return get("?state={state}&from={from}&size={size}", userId, parameters);
}

@GetMapping("/owner")
public ResponseEntity<Object> getOwnerBookings(Long userId, BookingState state, Long from, Long size) {

Map<String, Object> parameters = Map.of(
"state", state,
"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,68 @@
package ru.practicum.shareit.booking;

import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.MethodParameter;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.BeanPropertyBindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.*;
import ru.practicum.shareit.booking.dto.BookingShortDto;
import ru.practicum.shareit.booking.dto.BookingState;

@RestController
@RequestMapping("/bookings")
@Validated
@Slf4j
public class BookingController {
private final BookingClient bookingClient;

@Autowired
public BookingController(BookingClient bookingClient) {
this.bookingClient = bookingClient;
}

@SneakyThrows
@PostMapping
public ResponseEntity<Object> createBooking(@Validated @RequestBody BookingShortDto bookingShortDto,
@RequestHeader("X-Sharer-User-Id") Long userId) {
if (bookingShortDto.getStart().equals(bookingShortDto.getEnd())
|| bookingShortDto.getStart().isAfter(bookingShortDto.getEnd())) {
throw new MethodArgumentNotValidException(new MethodParameter(
this.getClass().getDeclaredMethod("createBooking", BookingShortDto.class, Long.class), 0), new BeanPropertyBindingResult(bookingShortDto, "bookingShortDto"));
}

return bookingClient.createBooking(bookingShortDto, userId);
}

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

@GetMapping("/{bookingId}")
public ResponseEntity<Object> getBooking(@PathVariable Long bookingId,
@RequestHeader("X-Sharer-User-Id") Long userId) {
return bookingClient.getBooking(bookingId, userId);
}

@GetMapping
public ResponseEntity<Object> getAllBookings(@RequestHeader("X-Sharer-User-Id") Long userId,
@RequestParam(name = "state", defaultValue = "ALL") BookingState state,
@RequestParam(name = "from", defaultValue = "0") Long from,
@RequestParam(name = "size", defaultValue = "20") Long size) {
return bookingClient.getAllBookings(userId, state, from, size);
}

@GetMapping("/owner")
public ResponseEntity<Object> getOwnerBookings(@RequestHeader("X-Sharer-User-Id") Long userId,
@RequestParam(name = "state", defaultValue = "ALL") BookingState state,
@RequestParam(name = "from", defaultValue = "0") Long from,
@RequestParam(name = "size", defaultValue = "20") Long size) {
return bookingClient.getOwnerBookings(userId, state, from, size);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import ru.practicum.shareit.booking.BookingStatus;

import javax.validation.constraints.FutureOrPresent;
import javax.validation.constraints.NotNull;
Expand All @@ -26,8 +25,6 @@ public class BookingDto {
@JsonFormat(pattern = "yyyy-MM-dd'T'HH:mm:ss")
private final LocalDateTime end;

private final BookingStatus status;

private final Item item;
private final User booker;

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

public enum BookingState {
CURRENT,
PAST,
FUTURE,
ALL,
REJECTED,
WAITING
}
Loading