Repository navigation
add-bookings #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
add-bookings #3
Changes from 10 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
6f5595f
Подключение к БД и настройка таблиц
AleksOBM f9aad3f
Тесты пройдены
AleksOBM ed7dc1b
Первое ревью 15-го спринта
AleksOBM c87772b
Первое ревью 15-го спринта
AleksOBM 5931b0f
Первое ревью 15-го спринта
AleksOBM 73acfdd
Первое ревью 15-го спринта
AleksOBM cdc5aba
Первое ревью 15-го спринта
AleksOBM 46ab38e
Первое ревью 15-го спринта
AleksOBM 95609be
Первое ревью 15-го спринта
AleksOBM 05c5a06
Первое ревью 15-го спринта
AleksOBM 6bdaab3
Второе ревью 15-го спринта
AleksOBM ce4fb11
Третье ревью 15-го спринта
AleksOBM 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| services: | ||
| db: | ||
| image: postgres:16.1 | ||
| ports: | ||
| - "5432:5432" | ||
| volumes: | ||
| - ./volumes/postgres:/var/lib/postgresql/data/ | ||
| environment: | ||
| - POSTGRES_DB=shareit | ||
| - POSTGRES_USER=dbuser | ||
| - POSTGRES_PASSWORD=12345 | ||
| healthcheck: | ||
| test: pg_isready -q -d $$POSTGRES_DB -U $$POSTGRES_USER | ||
| timeout: 5s | ||
| interval: 5s | ||
| retries: 10 |
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.
56 changes: 54 additions & 2 deletions
56
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,64 @@ | ||
| 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.RequiredArgsConstructor; | ||
| import org.springframework.validation.annotation.Validated; | ||
| import org.springframework.web.bind.annotation.*; | ||
| import ru.practicum.shareit.booking.dto.BookingDto; | ||
| import ru.practicum.shareit.booking.service.BookingService; | ||
| import ru.practicum.shareit.util.Marker; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| /** | ||
| * TODO Sprint add-bookings. | ||
| */ | ||
| @RestController | ||
| @Validated | ||
| @RequestMapping(path = "/bookings") | ||
| @RequiredArgsConstructor | ||
| public class BookingController { | ||
| private final BookingService bookingService; | ||
|
|
||
| @GetMapping("/{bookingId}") | ||
| public BookingDto getBooking( | ||
| @RequestHeader("X-Sharer-User-Id") Long userId, | ||
| @PathVariable long bookingId | ||
| ) { | ||
| return bookingService.getBooking(userId, bookingId); | ||
| } | ||
|
|
||
| @GetMapping | ||
| public List<BookingDto> getAllBookingsByBooker( | ||
| @RequestHeader("X-Sharer-User-Id") long userId, | ||
| @RequestParam(required = false) String state | ||
| ) { | ||
| return bookingService.getAllBookingsByBooker(userId, state); | ||
| } | ||
|
|
||
| @GetMapping("/owner") | ||
| public List<BookingDto> getAllBookingsByOwner( | ||
| @RequestHeader("X-Sharer-User-Id") long userId, | ||
| @RequestParam(required = false) String state | ||
| ) { | ||
| return bookingService.getAllBookingsByOwner(userId, state); | ||
| } | ||
|
|
||
| @PostMapping | ||
| @Validated(Marker.OnCreate.class) | ||
| public BookingDto addNewBooking( | ||
| @RequestHeader("X-Sharer-User-Id") Long userId, | ||
| @Valid @RequestBody BookingDto bookingDto | ||
| ) { | ||
| return bookingService.addNewBooking(userId, bookingDto); | ||
| } | ||
|
|
||
| @PatchMapping("/{bookingId}") | ||
| public BookingDto approveBooking( | ||
| @RequestHeader("X-Sharer-User-Id") long userId, | ||
| @PathVariable long bookingId, | ||
| @RequestParam boolean approved | ||
| ) { | ||
| return bookingService.approveBooking(userId, bookingId, approved); | ||
| } | ||
| } |
8 changes: 0 additions & 8 deletions
8
src/main/java/ru/practicum/shareit/booking/BookingStatus.java
This file was deleted.
Oops, something went wrong.
54 changes: 54 additions & 0 deletions
54
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 |
|---|---|---|
| @@ -1,7 +1,61 @@ | ||
| package ru.practicum.shareit.booking.dto; | ||
|
|
||
| import jakarta.validation.constraints.Future; | ||
| import jakarta.validation.constraints.NotNull; | ||
| import lombok.AccessLevel; | ||
| import lombok.Data; | ||
| import lombok.experimental.FieldDefaults; | ||
| import ru.practicum.shareit.booking.model.BookingStatus; | ||
| import ru.practicum.shareit.item.dto.ItemDto; | ||
| import ru.practicum.shareit.user.dto.UserDto; | ||
| import ru.practicum.shareit.util.Marker; | ||
| import ru.practicum.shareit.util.datetime.FutureOrPresentWithTolerance; | ||
| import ru.practicum.shareit.util.datetime.TimeRoundingStrategy; | ||
|
|
||
| import java.time.LocalDateTime; | ||
|
|
||
| /** | ||
| * TODO Sprint add-bookings. | ||
| */ | ||
| @Data | ||
| @NotNull | ||
| @FieldDefaults(level = AccessLevel.PRIVATE) | ||
| public class BookingDto { | ||
|
|
||
| Long id; | ||
|
|
||
| @NotNull(groups = Marker.OnCreate.class, message = "Дата начала бронирования - обязательна") | ||
| @FutureOrPresentWithTolerance( | ||
| rounding = TimeRoundingStrategy.TRUNCATE_TO_SECONDS, | ||
| message = "Дата начала не может быть в прошлом" | ||
| ) | ||
| LocalDateTime start; | ||
|
|
||
| @NotNull(groups = Marker.OnCreate.class, message = "Дата окончания бронирования - обязательна") | ||
| @Future(message = "Дата окончания должна быть в будущем") | ||
| LocalDateTime end; | ||
|
|
||
| ItemDto item; | ||
|
|
||
| UserDto booker; | ||
|
|
||
| BookingStatus status; | ||
|
|
||
| public BookingDto( | ||
| Long id, | ||
| LocalDateTime start, | ||
| LocalDateTime end, | ||
| Long itemId, | ||
| Long bookerId, | ||
| BookingStatus status | ||
| ) { | ||
| this.id = id != null && id <= 0 ? null : id; | ||
| this.start = start; | ||
| this.end = end; | ||
| this.item = itemId != null && itemId <= 0 ? null : | ||
| new ItemDto(itemId, null, null, null, null); | ||
| this.booker = bookerId != null && bookerId <= 0 ? null : | ||
| new UserDto(bookerId, null, null); | ||
| this.status = status; | ||
| } | ||
| } | ||
40 changes: 40 additions & 0 deletions
40
src/main/java/ru/practicum/shareit/booking/dto/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,40 @@ | ||
| package ru.practicum.shareit.booking.dto; | ||
|
|
||
| import org.springframework.lang.NonNull; | ||
| import ru.practicum.shareit.booking.model.Booking; | ||
| import ru.practicum.shareit.item.dto.ItemMapper; | ||
| import ru.practicum.shareit.item.model.Item; | ||
| import ru.practicum.shareit.user.dto.UserMapper; | ||
| import ru.practicum.shareit.user.model.User; | ||
|
|
||
| import java.time.ZoneId; | ||
|
|
||
| public class BookingMapper { | ||
|
|
||
| @NonNull | ||
| public static BookingDto toBookingDto(@NonNull Booking booking) { | ||
| BookingDto bookingDto = new BookingDto( | ||
| booking.getId(), | ||
| booking.getStart().atZone(ZoneId.systemDefault()).toLocalDateTime(), | ||
| booking.getEnd().atZone(ZoneId.systemDefault()).toLocalDateTime(), | ||
| null, | ||
| null, | ||
| booking.getStatus() | ||
| ); | ||
| bookingDto.setItem(ItemMapper.toItemDto(booking.getItem())); | ||
| bookingDto.setBooker(UserMapper.toUserDto(booking.getBooker())); | ||
| return bookingDto; | ||
| } | ||
|
|
||
| @NonNull | ||
| public static Booking toBooking(@NonNull BookingDto bookingDto, Item item, User booker) { | ||
| return new Booking( | ||
| bookingDto.getId(), | ||
| bookingDto.getStart().atZone(ZoneId.systemDefault()).toLocalDateTime(), | ||
| bookingDto.getEnd().atZone(ZoneId.systemDefault()).toLocalDateTime(), | ||
| item, | ||
| booker, | ||
| bookingDto.getStatus() | ||
| ); | ||
| } | ||
| } |
45 changes: 45 additions & 0 deletions
45
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,45 @@ | ||
| package ru.practicum.shareit.booking.model; | ||
|
|
||
| import jakarta.persistence.*; | ||
| import lombok.AccessLevel; | ||
| import lombok.AllArgsConstructor; | ||
| import lombok.Data; | ||
| import lombok.NoArgsConstructor; | ||
| import lombok.experimental.FieldDefaults; | ||
| import ru.practicum.shareit.item.model.Item; | ||
| import ru.practicum.shareit.user.model.User; | ||
|
|
||
| import java.time.LocalDateTime; | ||
|
|
||
| /** | ||
| * TODO Sprint add-bookings. | ||
| */ | ||
| @Entity | ||
| @Table(name = "booking") | ||
| @Data | ||
| @AllArgsConstructor | ||
| @NoArgsConstructor | ||
| @FieldDefaults(level = AccessLevel.PRIVATE) | ||
| public class Booking { | ||
|
|
||
| @Id | ||
| @GeneratedValue(strategy = GenerationType.IDENTITY) | ||
| Long id; | ||
|
|
||
| @Column(name = "start_date", nullable = false) | ||
| LocalDateTime start; | ||
|
|
||
| @Column(name = "end_date", nullable = false) | ||
| LocalDateTime end; | ||
|
|
||
| @ManyToOne(fetch = FetchType.LAZY) | ||
| @JoinColumn(name = "item_id", nullable = false) | ||
| Item item; | ||
|
|
||
| @ManyToOne(fetch = FetchType.LAZY) | ||
| @JoinColumn(name = "booker_id", nullable = false) | ||
| User booker; | ||
|
|
||
| @Enumerated(EnumType.STRING) | ||
| BookingStatus status; | ||
| } |
17 changes: 17 additions & 0 deletions
17
src/main/java/ru/practicum/shareit/booking/model/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,17 @@ | ||
| package ru.practicum.shareit.booking.model; | ||
|
|
||
| /// Статус бронирования вещи | ||
| public enum BookingStatus { | ||
|
|
||
| /// новое бронирование, ожидает одобрения | ||
| WAITING, | ||
|
|
||
| /// бронирование подтверждено владельцем | ||
| APPROVED, | ||
|
|
||
| /// бронирование отклонено владельцем | ||
| REJECTED, | ||
|
|
||
| /// бронирование отменено создателем | ||
| CANCELED | ||
| } |
39 changes: 39 additions & 0 deletions
39
src/main/java/ru/practicum/shareit/booking/model/StateOfBooking.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,39 @@ | ||
| package ru.practicum.shareit.booking.model; | ||
|
|
||
| import lombok.Getter; | ||
| import org.springframework.lang.NonNull; | ||
|
|
||
| import java.util.Arrays; | ||
| import java.util.List; | ||
|
|
||
| /// Параметр запроса списка бронирований | ||
| public enum StateOfBooking { | ||
| UNDEFINED, ALL, CURRENT, PAST, FUTURE, WAITING, REJECTED; | ||
|
|
||
| @Getter | ||
| private String invalidValue; | ||
|
|
||
| public static StateOfBooking of(String text) { | ||
| if (text == null) { | ||
| return ALL; | ||
| } | ||
| String state = Arrays.stream(StateOfBooking.values()) | ||
| .map(StateOfBooking::name) | ||
| .filter(value -> text.toUpperCase().equals(value)) | ||
| .findAny().orElse(null); | ||
|
|
||
| if (state == null) { | ||
| StateOfBooking so = UNDEFINED; | ||
| so.invalidValue = text; | ||
| return so; | ||
| } | ||
| return StateOfBooking.valueOf(state); | ||
| } | ||
|
|
||
| @NonNull | ||
| public static List<String> getValidValues() { | ||
| return Arrays.stream(values()).filter(v -> v.ordinal() > 0) | ||
| .map(StateOfBooking::name) | ||
| .toList(); | ||
| } | ||
| } |
Oops, something went wrong.
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
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.
С дефолтным ложные срабатывания из-за микросекунд