Skip to content

Commit 5e25fa8

Browse files
committed
add-controllers. Добавить апи для пользователей и вещей.
1 parent b0947b4 commit 5e25fa8

24 files changed

+617
-23
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,28 @@
11
package ru.practicum.shareit.booking;
22

3+
import lombok.Data;
4+
import ru.practicum.shareit.item.model.Item;
5+
import ru.practicum.shareit.user.model.User;
6+
7+
import java.time.LocalDateTime;
8+
39
/**
410
* TODO Sprint add-bookings.
511
*/
12+
@Data
613
public class Booking {
14+
private Long id;
15+
private LocalDateTime start;
16+
private LocalDateTime end;
17+
private Item item;
18+
private User booker;
19+
private Status status;
20+
21+
enum Status {
22+
WAITING,
23+
APPROVED,
24+
REJECTED,
25+
CANCELED
26+
}
27+
728
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package ru.practicum.shareit.exception;
2+
3+
public class AlreadyExistException extends RuntimeException {
4+
public AlreadyExistException(String message) {
5+
super(message);
6+
}
7+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package ru.practicum.shareit.exception;
2+
3+
import lombok.extern.slf4j.Slf4j;
4+
import org.springframework.http.HttpStatus;
5+
import org.springframework.web.bind.MethodArgumentNotValidException;
6+
import org.springframework.web.bind.annotation.ExceptionHandler;
7+
import org.springframework.web.bind.annotation.ResponseStatus;
8+
import org.springframework.web.bind.annotation.RestControllerAdvice;
9+
10+
@Slf4j
11+
@RestControllerAdvice
12+
public class ErrorHandler {
13+
14+
@ExceptionHandler
15+
@ResponseStatus(HttpStatus.BAD_REQUEST)
16+
public ErrorResponse handleValidationException(MethodArgumentNotValidException e) {
17+
log.debug(e.getBindingResult().getAllErrors().getFirst().getDefaultMessage(), e);
18+
return new ErrorResponse(HttpStatus.BAD_REQUEST.value(), e.getBindingResult().getAllErrors().getFirst().getDefaultMessage());
19+
}
20+
21+
@ExceptionHandler
22+
@ResponseStatus(HttpStatus.NOT_FOUND)
23+
public ErrorResponse handleValidationException(NotFoundException e) {
24+
log.debug(e.getMessage(), e);
25+
return new ErrorResponse(HttpStatus.BAD_REQUEST.value(), e.getMessage());
26+
}
27+
28+
@ExceptionHandler
29+
@ResponseStatus(HttpStatus.CONFLICT)
30+
public ErrorResponse handleValidationException(AlreadyExistException e) {
31+
log.debug(e.getMessage(), e);
32+
return new ErrorResponse(HttpStatus.BAD_REQUEST.value(), e.getMessage());
33+
}
34+
35+
@ExceptionHandler
36+
@ResponseStatus(HttpStatus.FORBIDDEN)
37+
public ErrorResponse handleValidationException(NotAccessException e) {
38+
log.debug(e.getMessage(), e);
39+
return new ErrorResponse(HttpStatus.FORBIDDEN.value(), e.getMessage());
40+
}
41+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package ru.practicum.shareit.exception;
2+
3+
import lombok.AllArgsConstructor;
4+
import lombok.Data;
5+
6+
@Data
7+
@AllArgsConstructor
8+
public class ErrorResponse {
9+
private Integer statusCode;
10+
private String error;
11+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package ru.practicum.shareit.exception;
2+
3+
public class NotAccessException extends RuntimeException {
4+
public NotAccessException(String message) {
5+
super(message);
6+
}
7+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package ru.practicum.shareit.exception;
2+
3+
public class NotFoundException extends RuntimeException {
4+
public NotFoundException(String message) {
5+
super(message);
6+
}
7+
}
Lines changed: 40 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,47 @@
11
package ru.practicum.shareit.item;
22

3-
import org.springframework.web.bind.annotation.RequestMapping;
4-
import org.springframework.web.bind.annotation.RestController;
3+
import jakarta.validation.Valid;
4+
import lombok.AllArgsConstructor;
5+
import org.springframework.web.bind.annotation.*;
6+
import ru.practicum.shareit.item.dto.ItemDto;
7+
import ru.practicum.shareit.item.service.ItemService;
8+
9+
import java.util.List;
510

6-
/**
7-
* TODO Sprint add-controllers.
8-
*/
911
@RestController
12+
@AllArgsConstructor
1013
@RequestMapping("/items")
1114
public class ItemController {
15+
16+
private final ItemService itemService;
17+
18+
@GetMapping
19+
public List<ItemDto> getItemsByUserId(@RequestHeader("X-Sharer-User-Id") Long userId) {
20+
return itemService.getItemsByUserId(userId);
21+
}
22+
23+
@GetMapping("/{itemId}")
24+
public ItemDto getItemById(@PathVariable Long itemId) {
25+
return itemService.getItemById(itemId);
26+
}
27+
28+
@PostMapping
29+
public ItemDto createItem(
30+
@Valid @RequestBody ItemDto itemDto,
31+
@RequestHeader("X-Sharer-User-Id") Long userId) {
32+
return itemService.createItem(itemDto, userId);
33+
}
34+
35+
@PatchMapping("/{itemId}")
36+
public ItemDto updateItem(
37+
@RequestBody ItemDto itemDto,
38+
@PathVariable Long itemId,
39+
@RequestHeader("X-Sharer-User-Id") Long userId) {
40+
return itemService.updateItem(itemDto, itemId, userId);
41+
}
42+
43+
@GetMapping("/search")
44+
public List<ItemDto> getItemsSearchByText(@RequestParam String text) {
45+
return itemService.searchItemsByText(text);
46+
}
1247
}
Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,19 @@
11
package ru.practicum.shareit.item.dto;
22

3-
/**
4-
* TODO Sprint add-controllers.
5-
*/
3+
import jakarta.validation.constraints.NotBlank;
4+
import jakarta.validation.constraints.NotNull;
5+
import lombok.AllArgsConstructor;
6+
import lombok.Data;
7+
8+
@Data
9+
@AllArgsConstructor
610
public class ItemDto {
11+
private Long id;
12+
@NotBlank(message = "Название не может быть пустым")
13+
private String name;
14+
@NotBlank(message = "Описание не может быть пустым")
15+
private String description;
16+
@NotNull(message = "Статус о доступности не может быть пустым")
17+
private Boolean available;
18+
private Long requestId;
719
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package ru.practicum.shareit.item.mapper;
2+
3+
import ru.practicum.shareit.item.dto.ItemDto;
4+
import ru.practicum.shareit.item.model.Item;
5+
import ru.practicum.shareit.request.ItemRequest;
6+
import ru.practicum.shareit.user.model.User;
7+
8+
public class ItemMapper {
9+
10+
public static ItemDto toItemDto(Item item) {
11+
return new ItemDto(
12+
item.getId(),
13+
item.getName(),
14+
item.getDescription(),
15+
item.getAvailable(),
16+
item.getRequest() != null ? item.getRequest().getId() : null
17+
);
18+
}
19+
20+
public static Item toItem(Long itemId, ItemDto itemDto, User owner, ItemRequest request) {
21+
return new Item(
22+
itemId,
23+
itemDto.getName(),
24+
itemDto.getDescription(),
25+
itemDto.getAvailable(),
26+
owner,
27+
request
28+
);
29+
}
30+
31+
}
Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,18 @@
11
package ru.practicum.shareit.item.model;
22

3-
/**
4-
* TODO Sprint add-controllers.
5-
*/
3+
4+
import lombok.AllArgsConstructor;
5+
import lombok.Data;
6+
import ru.practicum.shareit.request.ItemRequest;
7+
import ru.practicum.shareit.user.model.User;
8+
9+
@Data
10+
@AllArgsConstructor
611
public class Item {
12+
private Long id;
13+
private String name;
14+
private String description;
15+
private Boolean available;
16+
private User owner;
17+
private ItemRequest request;
718
}

0 commit comments

Comments
 (0)