Repository navigation
feat: реализовать 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
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
131349a
feat: реализовать add-bookings
724c518
style: исправить code-style
a42fb9d
fix: исправить annotations to Booking
063c65d
style: исправить code-style
5bf40a5
fix: исправить проблемы с тестами постман
3fb4f69
fix: исправить замечания после ревью
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
Binary file not shown.
Large diffs are not rendered by default.
Oops, something went wrong.
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,26 +1,35 @@ | ||
| package ru.practicum.shareit.booking; | ||
|
|
||
| import lombok.AllArgsConstructor; | ||
| import lombok.Data; | ||
| import ru.practicum.shareit.enums.Status; | ||
| import lombok.*; | ||
| import ru.practicum.shareit.item.Item; | ||
| import ru.practicum.shareit.user.User; | ||
|
|
||
| import javax.validation.constraints.FutureOrPresent; | ||
| import javax.validation.constraints.NotNull; | ||
| import javax.validation.constraints.Positive; | ||
| import javax.persistence.*; | ||
| import java.time.LocalDateTime; | ||
|
|
||
| @Data | ||
| @Entity | ||
| @Table(name = "bookings", schema = "public") | ||
| @Getter | ||
| @Setter | ||
| @ToString | ||
| @AllArgsConstructor | ||
| @NoArgsConstructor | ||
| public class Booking { | ||
| private final long id; | ||
| @FutureOrPresent | ||
| @Id | ||
| @GeneratedValue(strategy = GenerationType.IDENTITY) | ||
| @Column(name = "id") | ||
| private Long id; | ||
| @Column(name = "start_date", nullable = false) | ||
| private LocalDateTime start; | ||
| @FutureOrPresent | ||
| @Column(name = "end_date", nullable = false) | ||
| private LocalDateTime end; | ||
| @Positive | ||
| private long item; | ||
| @Positive | ||
| private long booker; | ||
| @NotNull | ||
| @ManyToOne | ||
| @JoinColumn(name = "item_id", referencedColumnName = "id") | ||
| private Item item; | ||
| @ManyToOne | ||
| @JoinColumn(name = "booker_id", referencedColumnName = "id") | ||
| private User booker; | ||
| @Column(name = "status", nullable = false) | ||
| @Enumerated(EnumType.STRING) | ||
| private Status status; | ||
| } |
45 changes: 40 additions & 5 deletions
45
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,47 @@ | ||
| package ru.practicum.shareit.booking; | ||
|
|
||
| import org.springframework.web.bind.annotation.RequestMapping; | ||
| import org.springframework.web.bind.annotation.RestController; | ||
| import lombok.RequiredArgsConstructor; | ||
| import org.springframework.web.bind.annotation.*; | ||
|
|
||
| import javax.validation.Valid; | ||
| import java.util.List; | ||
|
|
||
| /** | ||
| * TODO Sprint add-bookings. | ||
| */ | ||
| @RestController | ||
| @RequiredArgsConstructor | ||
| @RequestMapping(path = "/bookings") | ||
| public class BookingController { | ||
| private final BookingService bookingService; | ||
|
|
||
| @PostMapping | ||
| public BookingResponseDto saveNewBooking(@RequestHeader("X-Sharer-User-Id") Long userId, | ||
| @Valid @RequestBody BookingRequestDto bookingDto) { | ||
| return bookingService.saveNewBooking(userId, bookingDto); | ||
| } | ||
|
|
||
| @PatchMapping("/{bookingId}") | ||
| public BookingResponseDto approveBooking(@RequestHeader("X-Sharer-User-Id") Long userId, | ||
| @PathVariable Long bookingId, | ||
| @RequestParam Boolean approved) { | ||
| return bookingService.approveBooking(userId, bookingId, approved); | ||
| } | ||
|
|
||
| @GetMapping("/{bookingId}") | ||
| public BookingResponseDto getBooking(@RequestHeader("X-Sharer-User-Id") Long userId, | ||
| @PathVariable Long bookingId) { | ||
| return bookingService.getById(userId, bookingId); | ||
| } | ||
|
|
||
| @GetMapping | ||
| public List<BookingResponseDto> getAllUserBookings(@RequestHeader("X-Sharer-User-Id") Long userId, | ||
| @RequestParam(name = "state", | ||
| defaultValue = "ALL") String state) { | ||
| return bookingService.getAllUserBookings(userId, state); | ||
| } | ||
|
|
||
| @GetMapping("/owner") | ||
| public List<BookingResponseDto> getAllUserItemsBookings(@RequestHeader("X-Sharer-User-Id") Long userId, | ||
| @RequestParam(name = "state", | ||
| defaultValue = "ALL") String state) { | ||
| return bookingService.getAllUserItemsBookings(userId, state); | ||
| } | ||
| } | ||
30 changes: 30 additions & 0 deletions
30
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,30 @@ | ||
| 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 BookingResponseDto toBookingResponseDto(Booking booking) { | ||
| return new BookingResponseDto( | ||
| booking.getId(), | ||
| booking.getStart(), | ||
| booking.getEnd(), | ||
| ItemMapper.toItemDto(booking.getItem()), | ||
| booking.getStatus(), | ||
| UserMapper.toUserDto(booking.getBooker()) | ||
| ); | ||
| } | ||
|
|
||
| public static Booking toBooking(BookingRequestDto bookingRequestDto, User booker, Item item, Status status) { | ||
| return new Booking( | ||
| bookingRequestDto.getId(), | ||
| bookingRequestDto.getStart(), | ||
| bookingRequestDto.getEnd(), | ||
| item, | ||
| booker, | ||
| status | ||
| ); | ||
| } | ||
| } |
49 changes: 49 additions & 0 deletions
49
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,49 @@ | ||
| package ru.practicum.shareit.booking; | ||
|
|
||
| import org.springframework.data.jpa.repository.JpaRepository; | ||
| import org.springframework.data.jpa.repository.Query; | ||
|
|
||
| import java.time.LocalDateTime; | ||
| import java.util.List; | ||
|
|
||
| public interface BookingRepository extends JpaRepository<Booking, Long> { | ||
| List<Booking> findAllByBookerIdOrderByStartDesc(Long userId); | ||
|
|
||
| List<Booking> findAllByBookerIdAndStartIsBeforeAndEndIsAfterOrderByStartDesc(Long userId, LocalDateTime startTime, | ||
| LocalDateTime endTime); | ||
|
|
||
| List<Booking> findAllByBookerIdAndEndIsBeforeOrderByStartDesc(Long userId, LocalDateTime endTime); | ||
|
|
||
| List<Booking> findAllByBookerIdAndStartIsAfterOrderByStartDesc(Long userId, LocalDateTime startTime); | ||
|
|
||
| List<Booking> findAllByBookerIdAndStatusOrderByStartDesc(Long userId, Status status); | ||
|
|
||
| @Query("select b from Booking b " + | ||
| "where b.item.owner.id = ?1 order by b.start desc") | ||
| List<Booking> findAllItemOwnerBookings(Long userId); | ||
|
|
||
| @Query("select b from Booking b " + | ||
| "where b.item.owner.id = ?1 and b.start< ?2 and b.end> ?3 order by b.start desc") | ||
| List<Booking> findAllItemOwnerCurrentBookings(Long userId, LocalDateTime startTime, | ||
| LocalDateTime endTime); | ||
|
|
||
| @Query("select b from Booking b " + | ||
| "where b.item.owner.id = ?1 and b.end< ?2 order by b.start desc") | ||
| List<Booking> findAllItemOwnerPastBookings(Long userId, LocalDateTime endTime); | ||
|
|
||
| @Query("select b from Booking b " + | ||
| "where b.item.owner.id = ?1 and b.start> ?2 order by b.start desc") | ||
| List<Booking> findAllItemOwnerFutureBookings(Long userId, LocalDateTime startTime); | ||
|
|
||
| @Query("select b from Booking b " + | ||
| "where b.item.owner.id = ?1 and b.status = ?2 order by b.start desc") | ||
| List<Booking> findAllItemOwnerBookingsByStatus(Long userId, Status status); | ||
|
|
||
| @Query("select b from Booking b " + | ||
| "where b.item.id = ?1") | ||
| List<Booking> findAllByItemId(Long itemId); | ||
|
|
||
| @Query("select b from Booking b " + | ||
| "where b.booker.id = ?1 and b.item.id = ?2 and b.end <= ?3") | ||
| List<Booking> findAllUserBookings(Long userId, Long itemId, LocalDateTime now); | ||
| } |
22 changes: 22 additions & 0 deletions
22
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,22 @@ | ||
| package ru.practicum.shareit.booking; | ||
|
|
||
| import lombok.AllArgsConstructor; | ||
| import lombok.Data; | ||
| import lombok.NoArgsConstructor; | ||
|
|
||
| import javax.validation.constraints.FutureOrPresent; | ||
| import javax.validation.constraints.NotNull; | ||
| import java.time.LocalDateTime; | ||
|
|
||
| @Data | ||
| @AllArgsConstructor | ||
| @NoArgsConstructor | ||
| public class BookingRequestDto { | ||
| private Long id; | ||
| @FutureOrPresent | ||
| private LocalDateTime start; | ||
| @FutureOrPresent | ||
| private LocalDateTime end; | ||
| @NotNull | ||
| private Long itemId; | ||
| } |
21 changes: 21 additions & 0 deletions
21
src/main/java/ru/practicum/shareit/booking/BookingResponseDto.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; | ||
|
|
||
| import lombok.AllArgsConstructor; | ||
| import lombok.Data; | ||
| import lombok.NoArgsConstructor; | ||
| import ru.practicum.shareit.item.ItemDto; | ||
| import ru.practicum.shareit.user.UserDto; | ||
|
|
||
| import java.time.LocalDateTime; | ||
|
|
||
| @Data | ||
| @AllArgsConstructor | ||
| @NoArgsConstructor | ||
| public class BookingResponseDto { | ||
| private Long id; | ||
| private LocalDateTime start; | ||
| private LocalDateTime end; | ||
| private ItemDto item; | ||
| private Status status; | ||
| private UserDto booker; | ||
| } |
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 { | ||
| BookingResponseDto saveNewBooking(Long userId, BookingRequestDto bookingDto); | ||
|
|
||
| BookingResponseDto approveBooking(Long userId, Long bookingId, Boolean approved); | ||
|
|
||
| BookingResponseDto getById(Long userId, Long bookingId); | ||
|
|
||
| List<BookingResponseDto> getAllUserBookings(Long userId, String state); | ||
|
|
||
| List<BookingResponseDto> getAllUserItemsBookings(Long userId, String 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.