-
Notifications
You must be signed in to change notification settings - Fork 130
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c987000
commit 8e65dfc
Showing
19 changed files
with
548 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
gateway/src/main/java/ru/practicum/shareit/ErrorHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
13
gateway/src/main/java/ru/practicum/shareit/ErrorResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
gateway/src/main/java/ru/practicum/shareit/item/ItemClient.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
64 changes: 64 additions & 0 deletions
64
gateway/src/main/java/ru/practicum/shareit/item/ItemController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
gateway/src/main/java/ru/practicum/shareit/item/dto/CommentDto.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
28
gateway/src/main/java/ru/practicum/shareit/item/dto/ItemDto.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
Oops, something went wrong.