generated from yandex-praktikum/java-shareit
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from Nikolaev-Java/add-controllers
feat: реализовал тз 14 спринта
- Loading branch information
Showing
33 changed files
with
1,897 additions
and
8 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
7 changes: 7 additions & 0 deletions
7
src/main/java/ru/practicum/shareit/exception/AccessException.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,7 @@ | ||
package ru.practicum.shareit.exception; | ||
|
||
public class AccessException extends RuntimeException { | ||
public AccessException(String message) { | ||
super(message); | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
src/main/java/ru/practicum/shareit/exception/DuplicatedDataException.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,7 @@ | ||
package ru.practicum.shareit.exception; | ||
|
||
public class DuplicatedDataException extends RuntimeException { | ||
public DuplicatedDataException(String message) { | ||
super(message); | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
src/main/java/ru/practicum/shareit/exception/ErrorHandlingControllerAdvice.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,41 @@ | ||
package ru.practicum.shareit.exception; | ||
|
||
import jakarta.validation.ConstraintViolationException; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.web.bind.annotation.ExceptionHandler; | ||
import org.springframework.web.bind.annotation.ResponseStatus; | ||
import org.springframework.web.bind.annotation.RestControllerAdvice; | ||
|
||
import java.util.List; | ||
|
||
@RestControllerAdvice | ||
@Slf4j | ||
public class ErrorHandlingControllerAdvice { | ||
@ExceptionHandler(ConstraintViolationException.class) | ||
@ResponseStatus(HttpStatus.BAD_REQUEST) | ||
public List<ErrorResponse> handleMethodArgumentNotValidException(final ConstraintViolationException ex) { | ||
return ex.getConstraintViolations().stream() | ||
.map(constraintViolation -> { | ||
log.warn("{} - {}", constraintViolation.getPropertyPath(), constraintViolation.getMessage()); | ||
return new ErrorResponse(String.format("Invalid value of the %s parameter: %s", | ||
constraintViolation.getPropertyPath().toString(), | ||
constraintViolation.getMessage())); | ||
}) | ||
.toList(); | ||
} | ||
|
||
@ExceptionHandler({NotFoundException.class, AccessException.class}) | ||
@ResponseStatus(HttpStatus.NOT_FOUND) | ||
public ErrorResponse handleNotFoundException(final RuntimeException ex) { | ||
log.warn(ex.getMessage()); | ||
return new ErrorResponse(ex.getMessage()); | ||
} | ||
|
||
@ExceptionHandler(DuplicatedDataException.class) | ||
@ResponseStatus(HttpStatus.CONFLICT) | ||
public ErrorResponse handleDuplicatedDataException(final DuplicatedDataException ex) { | ||
log.warn(ex.getMessage()); | ||
return new ErrorResponse(ex.getMessage()); | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
src/main/java/ru/practicum/shareit/exception/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,12 @@ | ||
package ru.practicum.shareit.exception; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Data; | ||
import lombok.NoArgsConstructor; | ||
|
||
@Data | ||
@AllArgsConstructor | ||
@NoArgsConstructor | ||
public class ErrorResponse { | ||
private String error; | ||
} |
7 changes: 7 additions & 0 deletions
7
src/main/java/ru/practicum/shareit/exception/NotFoundException.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,7 @@ | ||
package ru.practicum.shareit.exception; | ||
|
||
public class NotFoundException extends RuntimeException { | ||
public NotFoundException(String message) { | ||
super(message); | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
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 |
---|---|---|
@@ -1,12 +1,60 @@ | ||
package ru.practicum.shareit.item; | ||
|
||
import jakarta.validation.Valid; | ||
import lombok.RequiredArgsConstructor; | ||
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.RestController; | ||
import ru.practicum.shareit.item.dto.ItemDto; | ||
import ru.practicum.shareit.item.service.ItemService; | ||
import ru.practicum.shareit.validationMarker.Marker; | ||
|
||
import java.util.List; | ||
|
||
/** | ||
* TODO Sprint add-controllers. | ||
*/ | ||
@RestController | ||
@RequestMapping("/items") | ||
@RequiredArgsConstructor | ||
@Validated | ||
public class ItemController { | ||
private final ItemService itemService; | ||
private static final String USER_ID = "X-Sharer-User-Id"; | ||
|
||
@PostMapping | ||
@Validated(Marker.OnCreate.class) | ||
public ItemDto create(@RequestBody @Valid ItemDto itemDto, @RequestHeader(USER_ID) long userId) { | ||
return itemService.create(itemDto, userId); | ||
} | ||
|
||
@PatchMapping("/{id}") | ||
@Validated(Marker.OnUpdate.class) | ||
public ItemDto update(@PathVariable Long id, @RequestBody @Valid ItemDto itemDto, | ||
@RequestHeader(USER_ID) long userId) { | ||
itemDto.setId(id); | ||
return itemService.update(itemDto, userId); | ||
} | ||
|
||
@GetMapping("/{id}") | ||
public ItemDto getById(@PathVariable Long id) { | ||
return itemService.getById(id); | ||
} | ||
|
||
@GetMapping | ||
public List<ItemDto> getAllOfOwner(@RequestHeader(USER_ID) long userId) { | ||
return itemService.getAllOfOwner(userId); | ||
} | ||
|
||
@GetMapping("/search") | ||
public List<ItemDto> findByNameByDescription(@RequestParam String text) { | ||
return itemService.findByNameByDescription(text); | ||
} | ||
} |
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,7 +1,24 @@ | ||
package ru.practicum.shareit.item.dto; | ||
|
||
import jakarta.validation.constraints.NotBlank; | ||
import jakarta.validation.constraints.NotNull; | ||
import jakarta.validation.constraints.Null; | ||
import lombok.Data; | ||
import lombok.EqualsAndHashCode; | ||
import ru.practicum.shareit.validationMarker.Marker; | ||
|
||
/** | ||
* TODO Sprint add-controllers. | ||
*/ | ||
@Data | ||
@EqualsAndHashCode(of = "id") | ||
public class ItemDto { | ||
@Null(message = "The ID must be null", groups = Marker.OnCreate.class) | ||
private Long id; | ||
@NotBlank(message = "The name should not be blank", groups = Marker.OnCreate.class) | ||
private String name; | ||
@NotNull(message = "The description should not be null", groups = Marker.OnCreate.class) | ||
private String description; | ||
@NotNull(message = "The available should not be null", groups = Marker.OnCreate.class) | ||
private Boolean available; | ||
} |
36 changes: 36 additions & 0 deletions
36
src/main/java/ru/practicum/shareit/item/mappers/ItemMapper.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,36 @@ | ||
package ru.practicum.shareit.item.mappers; | ||
|
||
import org.springframework.stereotype.Component; | ||
import ru.practicum.shareit.item.dto.ItemDto; | ||
import ru.practicum.shareit.item.model.Item; | ||
import ru.practicum.shareit.user.User; | ||
|
||
import java.util.List; | ||
|
||
@Component | ||
public class ItemMapper { | ||
public ItemDto toDto(Item item) { | ||
if (item == null) return null; | ||
ItemDto dto = new ItemDto(); | ||
dto.setId(item.getId()); | ||
dto.setName(item.getName()); | ||
dto.setDescription(item.getDescription()); | ||
dto.setAvailable(item.isAvailable()); | ||
return dto; | ||
} | ||
|
||
public Item fromDto(ItemDto dto, User user) { | ||
if (dto == null) return null; | ||
Item item = new Item(); | ||
item.setName(dto.getName()); | ||
item.setDescription(dto.getDescription()); | ||
item.setOwner(user); | ||
item.setAvailable(dto.getAvailable()); | ||
return item; | ||
} | ||
|
||
public List<ItemDto> toDtoList(List<Item> items) { | ||
if (items == null) return null; | ||
return items.stream().map(this::toDto).toList(); | ||
} | ||
} |
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,7 +1,20 @@ | ||
package ru.practicum.shareit.item.model; | ||
|
||
import lombok.Data; | ||
import lombok.EqualsAndHashCode; | ||
import ru.practicum.shareit.request.ItemRequest; | ||
import ru.practicum.shareit.user.User; | ||
|
||
/** | ||
* TODO Sprint add-controllers. | ||
*/ | ||
@Data | ||
@EqualsAndHashCode(of = "id") | ||
public class Item { | ||
private Long id; | ||
private String name; | ||
private String description; | ||
private boolean available; | ||
private User owner; | ||
private ItemRequest request; | ||
} |
56 changes: 56 additions & 0 deletions
56
src/main/java/ru/practicum/shareit/item/repository/InMemoryItemRepository.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,56 @@ | ||
package ru.practicum.shareit.item.repository; | ||
|
||
import org.springframework.stereotype.Repository; | ||
import ru.practicum.shareit.item.model.Item; | ||
|
||
import java.util.ArrayList; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Optional; | ||
|
||
@Repository | ||
public class InMemoryItemRepository implements ItemRepository { | ||
private final Map<Long, Item> items = new HashMap<>(); | ||
private final Map<Long, List<Item>> itemsByUser = new HashMap<>(); | ||
private long id = 0; | ||
|
||
@Override | ||
public Item create(Item item) { | ||
item.setId(generateId()); | ||
items.put(item.getId(), item); | ||
itemsByUser.computeIfAbsent(item.getOwner().getId(), k -> new ArrayList<>()).add(item); | ||
return item; | ||
} | ||
|
||
|
||
@Override | ||
public Item update(Item item) { | ||
items.put(item.getId(), item); | ||
itemsByUser.get(item.getOwner().getId()).remove(item); | ||
itemsByUser.get(item.getOwner().getId()).add(item); | ||
return item; | ||
} | ||
|
||
@Override | ||
public Optional<Item> getById(Long id) { | ||
return Optional.ofNullable(items.get(id)); | ||
} | ||
|
||
@Override | ||
public List<Item> getAllOfOwner(Long userId) { | ||
return new ArrayList<>(itemsByUser.get(userId)); | ||
} | ||
|
||
@Override | ||
public List<Item> findByNameByDescription(String text) { | ||
return items.values().stream() | ||
.filter(item -> (item.getDescription().toLowerCase().contains(text.toLowerCase()) | ||
|| item.getName().toLowerCase().contains(text.toLowerCase())) && item.isAvailable()) | ||
.toList(); | ||
} | ||
|
||
private Long generateId() { | ||
return ++id; | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
src/main/java/ru/practicum/shareit/item/repository/ItemRepository.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.repository; | ||
|
||
import ru.practicum.shareit.item.model.Item; | ||
|
||
import java.util.List; | ||
import java.util.Optional; | ||
|
||
public interface ItemRepository { | ||
Item create(Item item); | ||
|
||
Item update(Item item); | ||
|
||
Optional<Item> getById(Long id); | ||
|
||
List<Item> getAllOfOwner(Long userId); | ||
|
||
List<Item> findByNameByDescription(String text); | ||
} |
20 changes: 20 additions & 0 deletions
20
src/main/java/ru/practicum/shareit/item/service/ItemService.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,20 @@ | ||
package ru.practicum.shareit.item.service; | ||
|
||
import org.springframework.validation.annotation.Validated; | ||
import ru.practicum.shareit.item.dto.ItemDto; | ||
|
||
import java.util.List; | ||
|
||
@Validated | ||
public interface ItemService { | ||
|
||
ItemDto create(ItemDto itemDto, Long userId); | ||
|
||
ItemDto update(ItemDto itemDto, Long userId); | ||
|
||
ItemDto getById(Long id); | ||
|
||
List<ItemDto> getAllOfOwner(Long userId); | ||
|
||
List<ItemDto> findByNameByDescription(String text); | ||
} |
Oops, something went wrong.