Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added db/shareit.mv.db
Binary file not shown.
346 changes: 346 additions & 0 deletions db/shareit.trace.db

Large diffs are not rendered by default.

8 changes: 6 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>42.5.0</version>
<scope>runtime</scope>
</dependency>
<dependency>
Expand All @@ -55,6 +54,11 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>

</dependencies>

<build>
Expand Down Expand Up @@ -239,4 +243,4 @@
</profile>
</profiles>

</project>
</project>
39 changes: 24 additions & 15 deletions src/main/java/ru/practicum/shareit/booking/Booking.java
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 src/main/java/ru/practicum/shareit/booking/BookingController.java
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);
Comment thread
anzerdev marked this conversation as resolved.
}

@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 src/main/java/ru/practicum/shareit/booking/BookingMapper.java
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 src/main/java/ru/practicum/shareit/booking/BookingRepository.java
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 src/main/java/ru/practicum/shareit/booking/BookingRequestDto.java
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 src/main/java/ru/practicum/shareit/booking/BookingResponseDto.java
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 src/main/java/ru/practicum/shareit/booking/BookingService.java
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);
}
Loading