generated from yandex-praktikum/java-shareit
-
Notifications
You must be signed in to change notification settings - Fork 0
add-bookings. Изменить сохранение данных в БД. Добавить функционал "Бронирование" и "Комментарии" #2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
add-bookings. Изменить сохранение данных в БД. Добавить функционал "Бронирование" и "Комментарии" #2
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
b75963f
add-bookings. Изменить сохранение данных в БД. Добавить функционал "Б…
walder86 3bed9a2
add-bookings. Ревью. Изменить проверки времени на аннотации. Добавить…
walder86 ac52d28
add-bookings. Ревью. Изменить в получении всех вещей пользователя мас…
walder86 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 was deleted.
Oops, something went wrong.
55 changes: 50 additions & 5 deletions
55
src/main/java/ru/practicum/shareit/booking/BookingController.java
This file contains hidden or 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,57 @@ | ||
| package ru.practicum.shareit.booking; | ||
|
|
||
| import org.springframework.web.bind.annotation.RequestMapping; | ||
| import org.springframework.web.bind.annotation.RestController; | ||
| import jakarta.validation.Valid; | ||
| import lombok.AllArgsConstructor; | ||
| import org.springframework.web.bind.annotation.*; | ||
| import ru.practicum.shareit.booking.dto.BookingDtoRequest; | ||
| import ru.practicum.shareit.booking.dto.BookingDtoResponse; | ||
| import ru.practicum.shareit.booking.enumerated.State; | ||
| import ru.practicum.shareit.booking.service.BookingService; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| /** | ||
| * TODO Sprint add-bookings. | ||
| */ | ||
| @RestController | ||
| @AllArgsConstructor | ||
| @RequestMapping(path = "/bookings") | ||
| public class BookingController { | ||
|
|
||
| private final BookingService bookingService; | ||
| private final String userIdHeader = "X-Sharer-User-Id"; | ||
|
|
||
| @PostMapping | ||
| public BookingDtoResponse createBooking( | ||
| @RequestHeader(userIdHeader) Long bookerId, | ||
| @Valid @RequestBody BookingDtoRequest bookingDtoRequest) { | ||
| return bookingService.createBooking(bookerId, bookingDtoRequest); | ||
| } | ||
|
|
||
| @PatchMapping("/{bookingId}") | ||
| public BookingDtoResponse updateBooking( | ||
| @RequestHeader(userIdHeader) Long bookerId, | ||
| @RequestParam Boolean approved, | ||
| @PathVariable Long bookingId) { | ||
| return bookingService.updateBooking(bookerId, bookingId, approved); | ||
| } | ||
|
|
||
| @GetMapping("/{bookingId}") | ||
| public BookingDtoResponse getBookingById( | ||
| @RequestHeader(userIdHeader) Long bookerId, | ||
| @PathVariable Long bookingId) { | ||
| return bookingService.getBookingById(bookerId, bookingId); | ||
| } | ||
|
|
||
| @GetMapping | ||
| public List<BookingDtoResponse> getBookingByUser( | ||
| @RequestHeader(userIdHeader) Long bookerId, | ||
| @RequestParam(defaultValue = "ALL") State state) { | ||
| return bookingService.getBookingByUser(bookerId, state); | ||
| } | ||
|
|
||
| @GetMapping("/owner") | ||
| public List<BookingDtoResponse> getBookingByItemsUser( | ||
| @RequestHeader(userIdHeader) Long userOwnerItemId, | ||
| @RequestParam(defaultValue = "ALL") State state) { | ||
| return bookingService.getBookingByItemsUser(userOwnerItemId, state); | ||
| } | ||
|
|
||
| } |
7 changes: 0 additions & 7 deletions
7
src/main/java/ru/practicum/shareit/booking/dto/BookingDto.java
This file was deleted.
Oops, something went wrong.
25 changes: 25 additions & 0 deletions
25
src/main/java/ru/practicum/shareit/booking/dto/BookingDtoRequest.java
This file contains hidden or 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,25 @@ | ||
| package ru.practicum.shareit.booking.dto; | ||
|
|
||
| import jakarta.validation.constraints.Future; | ||
| import jakarta.validation.constraints.FutureOrPresent; | ||
| import jakarta.validation.constraints.NotNull; | ||
| import lombok.AllArgsConstructor; | ||
| import lombok.Data; | ||
| import ru.practicum.shareit.booking.enumerated.Status; | ||
|
|
||
| import java.time.LocalDateTime; | ||
|
|
||
| @Data | ||
| @AllArgsConstructor | ||
| public class BookingDtoRequest { | ||
|
|
||
| @FutureOrPresent(message = "Дата начала бронирования не может быть раньше текущего времени") | ||
| @NotNull(message = "Дата начала бронирования не может быть пустой") | ||
| private LocalDateTime start; | ||
| @Future(message = "Дата окончания бронирования не может быть раньше текущего времени") | ||
| @NotNull(message = "Дата окончания бронирования не может быть пустой") | ||
| private LocalDateTime end; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. а сюда @future |
||
| @NotNull(message = "ID вещи не может быть пустым") | ||
| private Long itemId; | ||
| private Status status; | ||
| } | ||
21 changes: 21 additions & 0 deletions
21
src/main/java/ru/practicum/shareit/booking/dto/BookingDtoResponse.java
This file contains hidden or 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,21 @@ | ||
| package ru.practicum.shareit.booking.dto; | ||
|
|
||
| import lombok.AllArgsConstructor; | ||
| import lombok.Data; | ||
| import ru.practicum.shareit.booking.enumerated.Status; | ||
| import ru.practicum.shareit.item.dto.ItemDto; | ||
| import ru.practicum.shareit.user.dto.UserDto; | ||
|
|
||
| import java.time.LocalDateTime; | ||
|
|
||
| @Data | ||
| @AllArgsConstructor | ||
| public class BookingDtoResponse { | ||
|
|
||
| private Long id; | ||
| private LocalDateTime start; | ||
| private LocalDateTime end; | ||
| private ItemDto item; | ||
| private UserDto booker; | ||
| private Status status; | ||
| } |
5 changes: 5 additions & 0 deletions
5
src/main/java/ru/practicum/shareit/booking/enumerated/State.java
This file contains hidden or 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,5 @@ | ||
| package ru.practicum.shareit.booking.enumerated; | ||
|
|
||
| public enum State { | ||
| ALL, CURRENT, PAST, FUTURE, WAITING, REJECTED, UNSUPPORTED_STATUS | ||
| } |
5 changes: 5 additions & 0 deletions
5
src/main/java/ru/practicum/shareit/booking/enumerated/Status.java
This file contains hidden or 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,5 @@ | ||
| package ru.practicum.shareit.booking.enumerated; | ||
|
|
||
| public enum Status { | ||
| WAITING, APPROVED, REJECTED, CANCELED | ||
| } |
33 changes: 33 additions & 0 deletions
33
src/main/java/ru/practicum/shareit/booking/mapper/BookingMapper.java
This file contains hidden or 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,33 @@ | ||
| package ru.practicum.shareit.booking.mapper; | ||
|
|
||
| import ru.practicum.shareit.booking.dto.BookingDtoRequest; | ||
| import ru.practicum.shareit.booking.dto.BookingDtoResponse; | ||
| import ru.practicum.shareit.booking.model.Booking; | ||
| import ru.practicum.shareit.item.dto.ItemDto; | ||
| import ru.practicum.shareit.item.model.Item; | ||
| import ru.practicum.shareit.user.dto.UserDto; | ||
| import ru.practicum.shareit.user.model.User; | ||
|
|
||
| public class BookingMapper { | ||
|
|
||
| public static BookingDtoResponse toBookingDtoResponse(Booking booking, UserDto userDto, ItemDto itemDto) { | ||
| return new BookingDtoResponse( | ||
| booking.getId(), | ||
| booking.getStart(), | ||
| booking.getEnd(), | ||
| itemDto, | ||
| userDto, | ||
| booking.getStatus() | ||
| ); | ||
| } | ||
|
|
||
| public static Booking toBooking(BookingDtoRequest bookingDtoRequest, User booker, Item item) { | ||
| return new Booking( | ||
| bookingDtoRequest.getStart(), | ||
| bookingDtoRequest.getEnd(), | ||
| item, | ||
| booker, | ||
| bookingDtoRequest.getStatus() | ||
| ); | ||
| } | ||
| } |
44 changes: 44 additions & 0 deletions
44
src/main/java/ru/practicum/shareit/booking/model/Booking.java
This file contains hidden or 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,44 @@ | ||
| package ru.practicum.shareit.booking.model; | ||
|
|
||
| import jakarta.persistence.*; | ||
| import lombok.AllArgsConstructor; | ||
| import lombok.Data; | ||
| import lombok.NoArgsConstructor; | ||
| import ru.practicum.shareit.booking.enumerated.Status; | ||
| import ru.practicum.shareit.item.model.Item; | ||
| import ru.practicum.shareit.user.model.User; | ||
|
|
||
| import java.time.LocalDateTime; | ||
|
|
||
| @Data | ||
| @Entity | ||
| @NoArgsConstructor | ||
| @AllArgsConstructor | ||
| @Table(name = "bookings") | ||
| public class Booking { | ||
|
|
||
| @Id | ||
| @GeneratedValue(strategy = GenerationType.IDENTITY) | ||
| @Column(name = "id") | ||
| private Long id; | ||
| @Column(name = "start_date", nullable = false) | ||
| private LocalDateTime start; | ||
| @Column(name = "end_date", nullable = false) | ||
| private LocalDateTime end; | ||
| @ManyToOne | ||
| @JoinColumn(name = "item_id") | ||
| private Item item; | ||
| @ManyToOne | ||
| @JoinColumn(name = "booker_id") | ||
| private User booker; | ||
| @Enumerated(EnumType.STRING) | ||
| private Status status; | ||
|
|
||
| public Booking(LocalDateTime start, LocalDateTime end, Item item, User booker, Status status) { | ||
| this.start = start; | ||
| this.end = end; | ||
| this.item = item; | ||
| this.booker = booker; | ||
| this.status = status; | ||
| } | ||
| } |
38 changes: 38 additions & 0 deletions
38
src/main/java/ru/practicum/shareit/booking/repository/BookingRepository.java
This file contains hidden or 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,38 @@ | ||
| package ru.practicum.shareit.booking.repository; | ||
|
|
||
| import org.springframework.data.jpa.repository.JpaRepository; | ||
| import ru.practicum.shareit.booking.enumerated.Status; | ||
| import ru.practicum.shareit.booking.model.Booking; | ||
|
|
||
| import java.time.LocalDateTime; | ||
| import java.util.Collection; | ||
| import java.util.List; | ||
| import java.util.Optional; | ||
|
|
||
| public interface BookingRepository extends JpaRepository<Booking, Long> { | ||
| List<Booking> findAllByBookerIdOrderByStartDesc(Long bookerId); | ||
|
|
||
| Collection<Booking> findAllByBookerIdAndStartIsBeforeAndEndIsAfterOrderByStartDesc(long bookerId, LocalDateTime startBefore, LocalDateTime endAfter); | ||
|
|
||
| Collection<Booking> findAllByBookerIdAndEndIsBeforeOrderByStartDesc(long bookerId, LocalDateTime endAfter); | ||
|
|
||
| Collection<Booking> findAllByBookerIdAndStartIsAfterOrderByStartDesc(long bookerId, LocalDateTime endAfter); | ||
|
|
||
| Collection<Booking> findAllByBookerIdAndStatusOrderByStartDesc(long bookerId, Status status); | ||
|
|
||
| List<Booking> findAllByItemIdInOrderByStartDesc(Collection<Long> itemId); | ||
|
|
||
| Collection<Booking> findAllByItemIdInAndStartIsBeforeAndEndIsAfterOrderByStartDesc(Collection<Long> itemId, LocalDateTime startBefore, LocalDateTime endAfter); | ||
|
|
||
| Collection<Booking> findAllByItemIdInAndEndIsBeforeOrderByStartDesc(Collection<Long> itemId, LocalDateTime endAfter); | ||
|
|
||
| Collection<Booking> findAllByItemIdInAndStartIsAfterOrderByStartDesc(Collection<Long> itemId, LocalDateTime endAfter); | ||
|
|
||
| Collection<Booking> findAllByItemIdInAndStatusOrderByStartDesc(Collection<Long> itemId, Status status); | ||
|
|
||
| boolean existsBookingByItemIdAndBookerIdAndStatusAndEndIsBefore(Long itemId, long bookerId, Status status, LocalDateTime endBefore); | ||
|
|
||
| Optional<Booking> findFirstByItemIdAndStartBeforeAndStatusOrderByEndDesc(Long itemId, LocalDateTime startBefore, Status status); | ||
|
|
||
| Optional<Booking> findFirstByItemIdAndStartAfterAndStatusOrderByStartAsc(Long itemId, LocalDateTime startAfter, Status status); | ||
| } |
20 changes: 20 additions & 0 deletions
20
src/main/java/ru/practicum/shareit/booking/service/BookingService.java
This file contains hidden or 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.booking.service; | ||
|
|
||
| import ru.practicum.shareit.booking.dto.BookingDtoRequest; | ||
| import ru.practicum.shareit.booking.dto.BookingDtoResponse; | ||
| import ru.practicum.shareit.booking.enumerated.State; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| public interface BookingService { | ||
|
|
||
| BookingDtoResponse getBookingById(Long userId, Long bookingId); | ||
|
|
||
| List<BookingDtoResponse> getBookingByUser(Long bookerId, State state); | ||
|
|
||
| List<BookingDtoResponse> getBookingByItemsUser(Long userOwnerItemId, State state); | ||
|
|
||
| BookingDtoResponse createBooking(Long userId, BookingDtoRequest bookingDtoRequest); | ||
|
|
||
| BookingDtoResponse updateBooking(Long userId, Long bookingId, Boolean approved); | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
добавь еще аннотацию @FutureOrPresent