generated from yandex-praktikum/java-shareit
-
Notifications
You must be signed in to change notification settings - Fork 0
add all files #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
Open
LetaTreiden
wants to merge
11
commits into
main
Choose a base branch
from
add-controllers
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
718545b
add all files
LetaTreiden c511c93
userRepoImpl update
LetaTreiden f67076c
exceptions bugs fixed
LetaTreiden d538dc0
Item id s fixwd
LetaTreiden a9f556b
create Item with empty param bugs fixed
LetaTreiden ef61b84
unused imports
LetaTreiden 640b78b
review
LetaTreiden bf27a64
review 2
LetaTreiden 1ddee6a
checkstyle
LetaTreiden 832cd8f
review 3
LetaTreiden 2e19e0f
add all items fixed
LetaTreiden 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
57 changes: 51 additions & 6 deletions
57
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 lombok.extern.slf4j.Slf4j; | ||
| import org.springframework.beans.factory.annotation.Autowired; | ||
| import org.springframework.web.bind.annotation.*; | ||
| import ru.practicum.shareit.booking.dto.BookingDTO; | ||
| import ru.practicum.shareit.booking.model.Booking; | ||
| import ru.practicum.shareit.booking.service.BookingServiceImpl; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| /** | ||
| * TODO Sprint add-bookings. | ||
| */ | ||
| @RestController | ||
| @RequestMapping(path = "/bookings") | ||
| @Slf4j | ||
| @RequestMapping("/bookings") | ||
| public class BookingController { | ||
|
|
||
| private final BookingServiceImpl bookingService; | ||
|
|
||
| @Autowired | ||
| public BookingController(BookingServiceImpl bookingService) { | ||
| this.bookingService = bookingService; | ||
| } | ||
|
|
||
| @PostMapping | ||
| public BookingDTO saveBooking(@RequestHeader("X-Sharer-User-Id") Long id, | ||
| @RequestBody BookingDTO bookingDto) { | ||
| return bookingService.create(id, bookingDto); | ||
| } | ||
|
|
||
| @GetMapping("/{bookingId}") | ||
| public Booking findBookingById(@RequestHeader("X-Sharer-User-Id") Long id, | ||
| @PathVariable Long bookingId) { | ||
| return bookingService.findBookingById(id, bookingId); | ||
| } | ||
|
|
||
| @GetMapping | ||
| public List<Booking> findAllBookingsById(@RequestParam(defaultValue = "ALL") String state, | ||
| @RequestHeader("X-Sharer-User-Id") Long id) { | ||
|
|
||
| return bookingService.findBookingByIdAndStatus(state, id); | ||
| } | ||
|
|
||
| @PatchMapping("/{bookingId}") | ||
| public BookingDTO confirmOrRejectBooking(@RequestHeader("X-Sharer-User-Id") Long id, | ||
| @PathVariable Long bookingId, | ||
| @RequestParam Boolean approved) { | ||
| return bookingService.confirmOrRejectBooking(id, bookingId, approved); | ||
| } | ||
|
|
||
| @GetMapping("/owner") | ||
| public List<Booking> findAllOwnersBookings(@RequestParam(defaultValue = "ALL") String state, | ||
| @RequestHeader("X-Sharer-User-Id") Long id) { | ||
|
|
||
| return bookingService.findAllOwnersBookings(state, id); | ||
| } | ||
|
|
||
| } |
20 changes: 20 additions & 0 deletions
20
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,20 @@ | ||
| package ru.practicum.shareit.booking; | ||
|
|
||
| import org.springframework.stereotype.Component; | ||
| import ru.practicum.shareit.booking.model.Booking; | ||
| import ru.practicum.shareit.booking.dto.BookingDTO; | ||
|
|
||
| @Component | ||
| public class BookingMapper { | ||
|
|
||
| public static BookingDTO toBookingDto(Booking booking) { | ||
| return BookingDTO.builder() | ||
| .id(booking.getId()) | ||
| .start(booking.getStart()) | ||
| .end(booking.getEnd()) | ||
| .item(booking.getItem()) | ||
| .booker(booking.getBooker()) | ||
| .bookingStatus(booking.getStatus()) | ||
| .build(); | ||
| } | ||
| } |
13 changes: 13 additions & 0 deletions
13
src/main/java/ru/practicum/shareit/booking/BookingStatus.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,13 @@ | ||
| package ru.practicum.shareit.booking; | ||
|
|
||
|
|
||
| public enum BookingStatus { | ||
| WAITING, | ||
| APPROVED, | ||
| REJECTED, | ||
| CANCELED, | ||
| ALL, | ||
| CURRENT, | ||
| PAST, | ||
| FUTURE | ||
| } |
45 changes: 45 additions & 0 deletions
45
src/main/java/ru/practicum/shareit/booking/dto/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,45 @@ | ||
| package ru.practicum.shareit.booking.dto; | ||
|
|
||
| import lombok.Builder; | ||
| import lombok.Data; | ||
| import lombok.Getter; | ||
| import lombok.Setter; | ||
| import ru.practicum.shareit.booking.BookingStatus; | ||
|
|
||
| import java.time.LocalDateTime; | ||
|
|
||
| @Data | ||
| @Builder | ||
| @Getter | ||
| @Setter | ||
| public class BookingDTO { | ||
| private Long id; | ||
| private LocalDateTime start; | ||
| private LocalDateTime end; | ||
| private ru.practicum.shareit.item.model.Item item; | ||
| private ru.practicum.shareit.user.model.User booker; | ||
| private User owner; | ||
| private BookingStatus bookingStatus; | ||
|
|
||
| @Data | ||
| @Builder | ||
| @Getter | ||
| @Setter | ||
| public static class Item { | ||
| private Long id; | ||
| private String name; | ||
| private String description; | ||
| private boolean available; | ||
| private Long requestId; | ||
| } | ||
|
|
||
| @Data | ||
| @Builder | ||
| @Getter | ||
| @Setter | ||
| public static class User { | ||
| private Long id; | ||
| private String name; | ||
| private String email; | ||
| } | ||
| } | ||
56 changes: 56 additions & 0 deletions
56
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,56 @@ | ||
| package ru.practicum.shareit.booking.model; | ||
|
|
||
| import lombok.*; | ||
| import org.hibernate.Hibernate; | ||
| import ru.practicum.shareit.booking.BookingStatus; | ||
| import ru.practicum.shareit.item.model.Item; | ||
| import ru.practicum.shareit.user.model.User; | ||
|
|
||
| import javax.persistence.*; | ||
| import java.time.LocalDateTime; | ||
| import java.util.Objects; | ||
|
|
||
| @Entity | ||
| @Table(name = "bookings", schema = "PUBLIC") | ||
| @Getter | ||
| @Setter | ||
| @ToString | ||
| @RequiredArgsConstructor | ||
| public class Booking { | ||
|
|
||
| @Id | ||
| @GeneratedValue(strategy = GenerationType.IDENTITY) | ||
| @Column(name = "booking_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", nullable = false) | ||
| private Item item; | ||
|
|
||
| @ManyToOne | ||
| @JoinColumn(name = "booker_id", nullable = false) | ||
| private User booker; | ||
|
|
||
| @Column(name = "status", nullable = false) | ||
| @Enumerated(EnumType.STRING) | ||
| private BookingStatus status = BookingStatus.WAITING; | ||
|
|
||
| @Override | ||
| public boolean equals(Object o) { | ||
| if (this == o) return true; | ||
| if (o == null || Hibernate.getClass(this) != Hibernate.getClass(o)) return false; | ||
| Booking booking = (Booking) o; | ||
| return id != null && Objects.equals(id, booking.id); | ||
| } | ||
|
|
||
| @Override | ||
| public int hashCode() { | ||
| return getClass().hashCode(); | ||
| } | ||
| } |
56 changes: 56 additions & 0 deletions
56
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,56 @@ | ||
| package ru.practicum.shareit.booking.repository; | ||
|
|
||
| import org.springframework.data.jpa.repository.JpaRepository; | ||
| import org.springframework.data.jpa.repository.Query; | ||
| import ru.practicum.shareit.booking.BookingStatus; | ||
| import ru.practicum.shareit.booking.model.Booking; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| public interface BookingRepository extends JpaRepository<Booking, Long> { | ||
|
|
||
| @Query("SELECT b FROM Booking b WHERE b.booker.id = ?1") | ||
| List<Booking> findBookingsByBookerId(Long id); | ||
|
|
||
| @Query("SELECT b FROM Booking b WHERE b.booker.id = ?1 AND" + | ||
| " b.start < CURRENT_TIMESTAMP AND b.end > CURRENT_TIMESTAMP") | ||
| List<Booking> findBookingsByBookerIdWithCurrentStatus(Long id); | ||
|
|
||
| @Query("SELECT b FROM Booking b WHERE b.booker.id = ?1 AND" + | ||
| " b.start < CURRENT_TIMESTAMP AND b.end < CURRENT_TIMESTAMP") | ||
| List<Booking> findBookingsByBookerIdWithPastStatus(Long id); | ||
|
|
||
| @Query("SELECT b FROM Booking b WHERE b.booker.id = ?1 AND" + " b.start > CURRENT_TIMESTAMP ") | ||
| List<Booking> findBookingsByBookerIdWithFutureStatus(Long id); | ||
|
|
||
| @Query("SELECT b FROM Booking b WHERE b.booker.id = ?1 AND b.status = ?2") | ||
| List<Booking> findBookingsByBookerIdWithWaitingOrRejectStatus(Long id, BookingStatus status); | ||
|
|
||
| @Query(value = "SELECT b FROM Booking AS b WHERE b.item.id IN (SELECT it FROM Item AS it WHERE it.owner.id = ?1) " | ||
| + "AND b.status = ?2 ") | ||
| List<Booking> findAllOwnersBookingsWithStatus(Long id, BookingStatus status); | ||
|
|
||
| @Query(value = "SELECT b FROM Booking AS b WHERE b.item.id IN (SELECT it FROM Item AS it WHERE it.owner.id = ?1) ") | ||
| List<Booking> findAllOwnersBookings(Long id); | ||
|
|
||
| @Query(value = "SELECT b FROM Booking AS b WHERE b.item.id IN (SELECT it FROM Item AS it WHERE it.owner.id = ?1) " | ||
| + "AND b.start > CURRENT_TIMESTAMP ") | ||
| List<Booking> findAllOwnersBookingsWithFutureStatus(Long id); | ||
|
|
||
| @Query(value = "SELECT b FROM Booking AS b WHERE b.item.id IN (SELECT it FROM Item AS it WHERE it.owner.id = ?1) " | ||
| + "AND b.start < CURRENT_TIMESTAMP AND b.end > CURRENT_TIMESTAMP ") | ||
| List<Booking> findAllOwnersBookingsWithCurrentStatus(Long id); | ||
|
|
||
| @Query(value = "SELECT b FROM Booking AS b WHERE b.item.id IN (SELECT it FROM Item AS it WHERE it.owner.id = ?1) " | ||
| + "AND b.end < CURRENT_TIMESTAMP ") | ||
| List<Booking> findAllOwnersBookingsWithPastStatus(Long id); | ||
|
|
||
| @Query(value = "SELECT b FROM Booking AS b WHERE b.item.id = ?1") | ||
| List<Booking> findAllItemBookings(Long id); | ||
|
|
||
| @Query(value = "SELECT b FROM Booking AS b WHERE b.item.id = ?1 AND b.start < CURRENT_TIMESTAMP ") | ||
| List<Booking> findAllItemBookingsPast(Long id); | ||
|
|
||
| @Query(value = "SELECT b FROM Booking AS b WHERE b.item.id = ?1 AND b.start > CURRENT_TIMESTAMP ") | ||
| List<Booking> findAllItemBookingsFuture(Long id); | ||
| } |
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.BookingDTO; | ||
| import ru.practicum.shareit.booking.model.Booking; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| public interface BookingService { | ||
|
|
||
| BookingDTO create(Long id, BookingDTO dto); | ||
|
|
||
| Booking findBookingById(Long id, Long bId); | ||
|
|
||
| BookingDTO confirmOrRejectBooking(Long id, Long bId, Boolean approved); | ||
|
|
||
| List<Booking> findBookingByIdAndStatus(String state, Long id); | ||
|
|
||
| List<Booking> findAllOwnersBookings(String state, Long id); | ||
|
|
||
| } |
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.