-
Notifications
You must be signed in to change notification settings - Fork 0
ТЗ 15 Бронирование вещей #3
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
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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 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,22 +1,60 @@ | ||
| package ru.practicum.shareit.booking; | ||
|
|
||
| import lombok.Data; | ||
| import jakarta.persistence.*; | ||
| import lombok.*; | ||
| import ru.practicum.shareit.item.Item; | ||
| import ru.practicum.shareit.user.User; | ||
|
|
||
| import java.time.LocalDateTime; | ||
|
|
||
| @Data | ||
| @Getter | ||
| @Setter | ||
| @Entity | ||
| @NoArgsConstructor | ||
| @AllArgsConstructor | ||
| @Table(name = "bookings") | ||
| @Builder | ||
| public class Booking { | ||
|
|
||
| public enum Status { | ||
| public enum BookingStatus { | ||
| WAITING, | ||
| APPROVED, | ||
| REJECTED, | ||
| CANCELED | ||
| } | ||
|
|
||
| @Id | ||
| @GeneratedValue(strategy = GenerationType.IDENTITY) | ||
| private Long id; | ||
|
|
||
| @Column(name = "start_date", nullable = false) | ||
| private LocalDateTime start; | ||
|
|
||
| @Column(name = "end_date", nullable = false) | ||
| private LocalDateTime end; | ||
| private Long itemId; | ||
| private Long bookerId; | ||
| private Status status; | ||
|
|
||
| @ManyToOne(fetch = FetchType.LAZY) | ||
| @JoinColumn(name = "item_id", nullable = false) | ||
| private Item item; | ||
|
|
||
| @ManyToOne(fetch = FetchType.LAZY) | ||
| @JoinColumn(name = "booker_id", nullable = false) | ||
| private User booker; | ||
|
|
||
| @Enumerated(EnumType.STRING) | ||
| @Column(nullable = false) | ||
| private BookingStatus status; | ||
|
|
||
| @Override | ||
| public boolean equals(Object o) { | ||
| if (this == o) return true; | ||
| if (o == null || getClass() != o.getClass()) return false; | ||
| Booking booking = (Booking) o; | ||
| return id != null && id.equals(booking.id); | ||
| } | ||
|
|
||
| @Override | ||
| public int hashCode() { | ||
| return 13; | ||
| } | ||
| } | ||
52 changes: 45 additions & 7 deletions
52
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,50 @@ | ||
| package ru.practicum.shareit.booking; | ||
|
|
||
| import org.springframework.web.bind.annotation.RequestMapping; | ||
| import org.springframework.web.bind.annotation.RestController; | ||
| import lombok.RequiredArgsConstructor; | ||
| import lombok.extern.slf4j.Slf4j; | ||
| import org.springframework.web.bind.annotation.*; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| /** | ||
| * TODO Sprint add-bookings. | ||
| */ | ||
| @RestController | ||
| @RequestMapping(path = "/bookings") | ||
| @RequiredArgsConstructor | ||
| @RequestMapping("/bookings") | ||
| @Slf4j | ||
| public class BookingController { | ||
| } | ||
| private final BookingService bookingService; | ||
|
|
||
| @PostMapping | ||
| public BookingDto createBooking(@RequestHeader("X-Sharer-User-Id") Long userId, | ||
| @RequestBody BookingRequestDto request) { | ||
| return BookingMapper.toDto(bookingService.createBooking(userId, request)); | ||
| } | ||
|
|
||
| @PatchMapping("/{bookingId}") | ||
| public BookingDto approveBooking(@RequestHeader("X-Sharer-User-Id") Long ownerId, | ||
| @PathVariable Long bookingId, | ||
| @RequestParam boolean approved) { | ||
| return BookingMapper.toDto(bookingService.approveBooking(ownerId, bookingId, approved)); | ||
| } | ||
|
|
||
| @GetMapping("/{bookingId}") | ||
| public BookingDto getBookingById(@RequestHeader("X-Sharer-User-Id") Long userId, | ||
| @PathVariable Long bookingId) { | ||
| return BookingMapper.toDto(bookingService.getBookingById(userId, bookingId)); | ||
| } | ||
|
|
||
| @GetMapping | ||
| public List<BookingDto> getBookingsByUser(@RequestHeader("X-Sharer-User-Id") Long userId, | ||
| @RequestParam(defaultValue = "ALL") String state) { | ||
| return bookingService.getBookingsByUser(userId, BookingState.from(state)).stream() | ||
| .map(BookingMapper::toDto) | ||
| .toList(); | ||
| } | ||
|
|
||
| @GetMapping("/owner") | ||
| public List<BookingDto> getBookingsByOwner(@RequestHeader("X-Sharer-User-Id") Long userId, | ||
| @RequestParam(defaultValue = "ALL") String state) { | ||
| return bookingService.getBookingsByOwner(userId, BookingState.from(state)).stream() | ||
| .map(BookingMapper::toDto) | ||
| .toList(); | ||
| } | ||
| } |
23 changes: 23 additions & 0 deletions
23
src/main/java/ru/practicum/shareit/booking/BookingDto.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,23 @@ | ||
| package ru.practicum.shareit.booking; | ||
|
|
||
| import lombok.AllArgsConstructor; | ||
| import lombok.Builder; | ||
| import lombok.Data; | ||
| import lombok.NoArgsConstructor; | ||
| import ru.practicum.shareit.item.ItemDto; | ||
| import ru.practicum.shareit.user.UserDto; | ||
|
|
||
| import java.time.LocalDateTime; | ||
|
|
||
| @Data | ||
| @Builder | ||
| @NoArgsConstructor | ||
| @AllArgsConstructor | ||
| public class BookingDto { | ||
| private Long id; | ||
| private ItemDto item; | ||
| private UserDto booker; | ||
| private LocalDateTime start; | ||
| private LocalDateTime end; | ||
| private Booking.BookingStatus status; | ||
| } |
31 changes: 31 additions & 0 deletions
31
src/main/java/ru/practicum/shareit/booking/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,31 @@ | ||
| package ru.practicum.shareit.booking; | ||
|
|
||
| import ru.practicum.shareit.item.Item; | ||
| import ru.practicum.shareit.item.ItemMapper; | ||
| import ru.practicum.shareit.user.User; | ||
| import ru.practicum.shareit.user.UserMapper; | ||
|
|
||
| public class BookingMapper { | ||
| public static Booking toBooking(BookingRequestDto dto, Item item, User booker) { | ||
| return Booking.builder() | ||
| .start(dto.getStart()) | ||
| .end(dto.getEnd()) | ||
| .item(item) | ||
| .booker(booker) | ||
| .status(Booking.BookingStatus.WAITING) | ||
| .build(); | ||
| } | ||
|
|
||
| public static BookingDto toDto(Booking booking) { | ||
| if (booking == null) return null; | ||
|
|
||
| return BookingDto.builder() | ||
| .id(booking.getId()) | ||
| .start(booking.getStart()) | ||
| .end(booking.getEnd()) | ||
| .status(booking.getStatus()) | ||
| .booker(UserMapper.toUserDto(booking.getBooker())) | ||
| .item(ItemMapper.toItemDto(booking.getItem())) | ||
| .build(); | ||
| } | ||
| } |
57 changes: 57 additions & 0 deletions
57
src/main/java/ru/practicum/shareit/booking/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,57 @@ | ||
| package ru.practicum.shareit.booking; | ||
|
|
||
| import org.springframework.data.jpa.repository.JpaRepository; | ||
| import org.springframework.data.jpa.repository.Query; | ||
| import org.springframework.data.repository.query.Param; | ||
|
|
||
| import java.time.LocalDateTime; | ||
| import java.util.List; | ||
|
|
||
| public interface BookingRepository extends JpaRepository<Booking, Long> { | ||
| @Query(""" | ||
| SELECT b FROM Booking b | ||
| WHERE b.item.id = :itemId | ||
| AND b.status IN ('APPROVED', 'WAITING') | ||
| AND b.end > :start AND b.start < :end | ||
| """) | ||
| List<Booking> findOverlappingBookings(@Param("itemId") Long itemId, | ||
| @Param("start") LocalDateTime start, | ||
| @Param("end") LocalDateTime end); | ||
|
|
||
| @Query(""" | ||
| SELECT b FROM Booking b | ||
| WHERE b.item.id = :itemId | ||
| AND b.start < :now | ||
| AND b.status = 'APPROVED' | ||
| ORDER BY b.start DESC LIMIT 1 | ||
| """) | ||
| Booking findLastBooking(@Param("itemId") Long itemId, @Param("now") LocalDateTime now); | ||
|
|
||
| @Query(""" | ||
| SELECT b FROM Booking b | ||
| WHERE b.item.id = :itemId | ||
| AND b.start > :now | ||
| AND b.status = 'APPROVED' | ||
| ORDER BY b.start ASC LIMIT 1 | ||
| """) | ||
| Booking findNextBooking(@Param("itemId") Long itemId, @Param("now") LocalDateTime now); | ||
|
|
||
| List<Booking> findByBookerIdOrderByStartDesc(Long bookerId); | ||
|
|
||
| List<Booking> findByItemOwnerIdOrderByStartDesc(Long ownerId); | ||
|
|
||
| List<Booking> findByBookerIdAndStatusOrderByStartDesc(Long bookerId, Booking.BookingStatus status); | ||
|
|
||
| List<Booking> findByBookerIdAndStartAfterOrderByStartDesc(Long bookerId, LocalDateTime now); | ||
|
|
||
| List<Booking> findByBookerIdAndEndBeforeOrderByStartDesc(Long bookerId, LocalDateTime now); | ||
|
|
||
| List<Booking> findByBookerIdAndStartBeforeAndEndAfterOrderByStartDesc( | ||
| Long bookerId, LocalDateTime now1, LocalDateTime now2); | ||
|
|
||
| List<Booking> findByItemOwnerIdAndStartBeforeAndEndAfterOrderByStartDesc( | ||
| Long ownerId, LocalDateTime now1, LocalDateTime now2); | ||
|
|
||
| boolean existsByItemIdAndBookerIdAndEndBeforeAndStatus( | ||
| Long itemId, Long userId, LocalDateTime now, Booking.BookingStatus status); | ||
| } |
12 changes: 12 additions & 0 deletions
12
src/main/java/ru/practicum/shareit/booking/BookingRequestDto.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,12 @@ | ||
| package ru.practicum.shareit.booking; | ||
|
|
||
| import lombok.Data; | ||
|
|
||
| import java.time.LocalDateTime; | ||
|
|
||
| @Data | ||
| public class BookingRequestDto { | ||
| private Long itemId; | ||
| private LocalDateTime start; | ||
| private LocalDateTime end; | ||
| } |
15 changes: 15 additions & 0 deletions
15
src/main/java/ru/practicum/shareit/booking/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,15 @@ | ||
| package ru.practicum.shareit.booking; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| public interface BookingService { | ||
| Booking createBooking(Long userId, BookingRequestDto dto); | ||
|
|
||
| Booking approveBooking(Long ownerId, Long bookingId, boolean approved); | ||
|
|
||
| Booking getBookingById(Long userId, Long bookingId); | ||
|
|
||
| List<Booking> getBookingsByUser(Long userId, BookingState state); | ||
|
|
||
| List<Booking> getBookingsByOwner(Long ownerId, BookingState state); | ||
| } |
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.
Uh oh!
There was an error while loading. Please reload this page.