Skip to content

Commit

Permalink
Commit-1 add-docker
Browse files Browse the repository at this point in the history
  • Loading branch information
StopTheClocks4ever committed May 11, 2024
1 parent c987000 commit 8e65dfc
Show file tree
Hide file tree
Showing 19 changed files with 548 additions and 23 deletions.
34 changes: 25 additions & 9 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,28 +1,44 @@
version: '3.8'
services:
gateway:
build:
image:
container_name:
build: gateway
image: shareit-gateway
container_name: shareit-gateway
volumes:
- /etc/timezone:/etc/timezone:ro
- /etc/localtime:/etc/localtime:ro
ports:
- "8080:8080"
depends_on:
- server
environment:
- SHAREIT_SERVER_URL=http://server:9090

server:
build:
image:
container_name:
build: server
image: shareit-server
container_name: shareit-server
volumes:
- /etc/timezone:/etc/timezone:ro
- /etc/localtime:/etc/localtime:ro
ports:
- "9090:9090"
depends_on:
- db
environment:
- SPRING_DATASOURCE_URL=jdbc:postgresql://db:5432/shareit
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=iamroot

db:
image:
container_name:
image: postgres:14-alpine
container_name: shareit-db
volumes:
- /etc/timezone:/etc/timezone:ro
- /etc/localtime:/etc/localtime:ro
ports:
- "6541:5432"
environment:
environment:
- POSTGRES_DB=shareit
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=iamroot
4 changes: 3 additions & 1 deletion gateway/Dockerfile
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
FROM amazoncorretto:11
FROM amazoncorretto:11-alpine-jdk
COPY target/*.jar app.jar
ENTRYPOINT ["java","-jar","/app.jar"]
12 changes: 11 additions & 1 deletion gateway/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@

<name>ShareIt Gateway</name>

<properties>
<java.version>11</java.version>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
Expand Down Expand Up @@ -51,6 +55,12 @@
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>5.3.25</version>
<scope>compile</scope>
</dependency>
</dependencies>

<build>
Expand All @@ -62,4 +72,4 @@
</plugins>
</build>

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

import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice
@Slf4j
public class ErrorHandler {

@ExceptionHandler
@ResponseStatus(HttpStatus.BAD_REQUEST)
public ErrorResponse handleNotValidException(final MethodArgumentNotValidException e) {
log.error(e.getMessage(), e);
return new ErrorResponse(e.getMessage());
}

@ExceptionHandler
@ResponseStatus(HttpStatus.BAD_REQUEST)
public ErrorResponse handleRuntimeException(final RuntimeException e) {
log.error(e.getMessage(), e);
return new ErrorResponse(e.getMessage());
}

@ExceptionHandler
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
public ErrorResponse handleIllegalArgumentException(final IllegalArgumentException e) {
log.error(e.getMessage(), e);
return new ErrorResponse(e.getMessage());
}
}
13 changes: 13 additions & 0 deletions gateway/src/main/java/ru/practicum/shareit/ErrorResponse.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package ru.practicum.shareit;

public class ErrorResponse {
private final String error;

public ErrorResponse(String error) {
this.error = error;
}

public String getError() {
return error;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,17 @@ public ResponseEntity<Object> bookItem(long userId, BookItemRequestDto requestDt
public ResponseEntity<Object> getBooking(long userId, Long bookingId) {
return get("/" + bookingId, userId);
}

public ResponseEntity<Object> getBookingsForOwner(long userId, BookingState state, Integer from, Integer 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);
}

public ResponseEntity<Object> updateBooking(long userId, long bookingId, boolean approved) {
return patch("/" + bookingId + "?approved=" + approved, userId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ public class BookingController {

@GetMapping
public ResponseEntity<Object> getBookings(@RequestHeader("X-Sharer-User-Id") long userId,
@RequestParam(name = "state", defaultValue = "all") String stateParam,
@PositiveOrZero @RequestParam(name = "from", defaultValue = "0") Integer from,
@Positive @RequestParam(name = "size", defaultValue = "10") Integer size) {
@RequestParam(name = "state", defaultValue = "all") String stateParam,
@PositiveOrZero @RequestParam(name = "from", defaultValue = "0") Integer from,
@Positive @RequestParam(name = "size", defaultValue = "10") Integer size) {
BookingState state = BookingState.from(stateParam)
.orElseThrow(() -> new IllegalArgumentException("Unknown state: " + stateParam));
log.info("Get booking with state {}, userId={}, from={}, size={}", stateParam, userId, from, size);
Expand All @@ -35,14 +35,32 @@ public ResponseEntity<Object> getBookings(@RequestHeader("X-Sharer-User-Id") lon

@PostMapping
public ResponseEntity<Object> bookItem(@RequestHeader("X-Sharer-User-Id") long userId,
@RequestBody @Valid BookItemRequestDto requestDto) {
@RequestBody @Valid BookItemRequestDto requestDto) {
log.info("Creating booking {}, userId={}", requestDto, userId);
return bookingClient.bookItem(userId, requestDto);
}

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

@PatchMapping("/{bookingId}")
public ResponseEntity<Object> updateBooking(@RequestHeader("X-Sharer-User-Id") int ownerId, @PathVariable int bookingId, @RequestParam (name = "approved") boolean isApproved) {
log.info("Patch booking {}, userId={}", bookingId, ownerId);
return bookingClient.updateBooking(ownerId, bookingId, isApproved);
}

@GetMapping("/owner")
public ResponseEntity<Object> getAllItemOwnerBookings(@RequestHeader("X-Sharer-User-Id") long userId,
@RequestParam(name = "state", defaultValue = "all") String stateParam,
@PositiveOrZero @RequestParam(name = "from", defaultValue = "0") Integer from,
@Positive @RequestParam(name = "size", defaultValue = "10") Integer size) {
BookingState state = BookingState.from(stateParam)
.orElseThrow(() -> new IllegalArgumentException("Unknown state: " + stateParam));
log.info("Get booking with state {}, userId={}, from={}, size={}", stateParam, userId, from, size);
return bookingClient.getBookingsForOwner(userId, state, from, size);
}
}
67 changes: 67 additions & 0 deletions gateway/src/main/java/ru/practicum/shareit/item/ItemClient.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package ru.practicum.shareit.item;

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.client.BaseClient;
import ru.practicum.shareit.item.dto.CommentDto;
import ru.practicum.shareit.item.dto.ItemDto;

import java.util.Map;

@Service
public class ItemClient extends BaseClient {

private static final String API_PREFIX = "/items";

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

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

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

public ResponseEntity<Object> updateItem(long userId, ItemDto item, long itemId) {
return patch("/" + itemId, userId, item);
}

public ResponseEntity<Object> addItem(long userId, ItemDto item) {
return post("", userId, item);
}

public ResponseEntity<Object> delete(long userId, long itemId) {
return delete("/" + itemId, userId);
}

public ResponseEntity<Object> search(long userId, int from, int size, String text) {
Map<String, Object> parameters = Map.of(
"from", from,
"size", size,
"text", text
);
return get("/search?text={text}&from={from}&size={size}", userId, parameters);
}

public ResponseEntity<Object> addComment(long userId, long itemId, CommentDto comment) {
return post("/" + itemId + "/comment", userId, comment);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package ru.practicum.shareit.item;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
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.item.dto.CommentDto;
import ru.practicum.shareit.item.dto.ItemDto;

import javax.validation.Valid;
import javax.validation.constraints.Positive;
import javax.validation.constraints.PositiveOrZero;

@Controller
@RequestMapping(path = "/items")
@RequiredArgsConstructor
@Slf4j
@Validated
public class ItemController {
private final ItemClient itemClient;

@PostMapping
public ResponseEntity<Object> addItem(@RequestBody @Valid ItemDto itemDto, @RequestHeader("X-Sharer-User-Id") int ownerId) {
log.info("Create item {}, userId = {}", itemDto, ownerId);
return itemClient.addItem(ownerId, itemDto);
}

@PatchMapping("/{itemId}")
public ResponseEntity<Object> patchItem(@RequestBody ItemDto itemDto, @PathVariable int itemId, @RequestHeader("X-Sharer-User-Id") int ownerId) {
log.info("Update item {}, userId = {}", itemId, ownerId);
return itemClient.updateItem(ownerId, itemDto, itemId);
}

@GetMapping("/{itemId}")
public ResponseEntity<Object> getItemById(@RequestHeader("X-Sharer-User-Id") int ownerId, @PathVariable int itemId) {
log.info("Get item {}, userId = {}", itemId, ownerId);
return itemClient.getItem(ownerId, itemId);
}

@GetMapping
public ResponseEntity<Object> getUsersItems(@RequestHeader("X-Sharer-User-Id") int ownerId,
@PositiveOrZero @RequestParam (value = "from", defaultValue = "0", required = false) Integer from,
@Positive @RequestParam (value = "size", defaultValue = "10",required = false) Integer size) {
log.info("Get items with ownerId = {}, from = {}, size = {}", ownerId, from, size);
return itemClient.getItemsPagination(ownerId, from, size);
}

@GetMapping("/search")
public ResponseEntity<Object> getSearch(@RequestParam(value = "text") String text,
@RequestHeader("X-Sharer-User-Id") int ownerId,
@PositiveOrZero @RequestParam (value = "from", defaultValue = "0", required = false) Integer from,
@Positive @RequestParam (value = "size", defaultValue = "10",required = false) Integer size) {
log.info("Search items with text = {}, from = {}, size = {}", text, from, size);
return itemClient.search(ownerId, from, size, text);
}

@PostMapping("/{itemId}/comment")
public ResponseEntity<Object> addComment(@RequestBody @Valid CommentDto commentDto, @PathVariable int itemId, @RequestHeader("X-Sharer-User-Id") int authorId) {
log.info("Create comment {}, userId = {}, for item {}", commentDto, authorId, itemId);
return itemClient.addComment(authorId, itemId, commentDto);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package ru.practicum.shareit.item.dto;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import javax.validation.constraints.NotBlank;

@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
public class CommentDto {

@NotBlank
private String text;
}
28 changes: 28 additions & 0 deletions gateway/src/main/java/ru/practicum/shareit/item/dto/ItemDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package ru.practicum.shareit.item.dto;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Positive;

@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
public class ItemDto {

@NotBlank
private String name;
@NotBlank
private String description;
@NotNull
private Boolean available;
@Positive
private Integer ownerId;
@Positive
private Integer requestId;
}
Loading

0 comments on commit 8e65dfc

Please sign in to comment.