Skip to content
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

Add item requests #46

Closed
wants to merge 2 commits into from
Closed
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
526 changes: 267 additions & 259 deletions pom.xml

Large diffs are not rendered by default.

114 changes: 0 additions & 114 deletions src/main/java/ru/practicum/shareit/aspects/LoggingAspect.java

This file was deleted.

55 changes: 3 additions & 52 deletions src/main/java/ru/practicum/shareit/booking/Booking.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,19 @@
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.ToString;
import lombok.experimental.Accessors;
import ru.practicum.shareit.item.model.Item;
import ru.practicum.shareit.user.User;

import javax.persistence.*;
import java.time.LocalDateTime;
import java.util.Objects;

@Entity
@Getter
@Setter
@NoArgsConstructor
@ToString

@Accessors(chain = true)
@Table(name = "bookings")
public class Booking {

Expand All @@ -39,53 +39,4 @@ public class Booking {
private User booker;
@Transient
int bookerId;

@Override
public boolean equals(Object object) {
if (this == object) return true;
if (object == null) return false;
if (this.getClass() != object.getClass()) return false;
Booking booking = (Booking) object;
return this.getId() == booking.getId() && Objects.equals(this.start, booking.start)
&& Objects.equals(this.end, booking.end) && this.getItemId() == booking.getItemId()
&& Objects.equals(this.status, booking.status) && Objects.equals(this.item, booking.item)
&& Objects.equals(this.booker, booking.booker) && this.booker == booking.booker;
}

@Override
public int hashCode() {
int hash = 17;
if (id == 0) {
hash = hash + id;
}
hash = hash * 31;
if (start != null) {
hash = hash + start.hashCode();
}
hash = hash * 31;
if (end != null) {
hash = hash + end.hashCode();
}
hash = hash * 31;
if (itemId != 0) {
hash = hash + itemId;
}
hash = hash * 31;
if (status != null) {
hash = hash + status.hashCode();
}
hash = hash * 31;
if (item != null) {
hash = hash + item.hashCode();
}
hash = hash * 31;
if (booker != null) {
hash = hash + booker.hashCode();
}
hash = hash * 31;
if (bookerId != 0) {
hash = hash + bookerId;
}
return hash;
}
}
22 changes: 16 additions & 6 deletions src/main/java/ru/practicum/shareit/booking/BookingController.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import ru.practicum.shareit.user.service.UserService;

import java.util.List;
import java.util.Optional;

@RestController
@RequestMapping(path = "/bookings")
Expand All @@ -19,7 +20,6 @@ public class BookingController {

@PostMapping
public BookingDto addBooking(@RequestBody Booking booking, @RequestHeader("X-Sharer-User-Id") int userId) {
// userService.getUserById(userId);
return bookingService.addBooking(booking, userId);
}

Expand All @@ -37,14 +37,24 @@ public BookingDto getBookingById(@RequestHeader("X-Sharer-User-Id") int userId,

@GetMapping()
public List<BookingDto> getAllBookingCurrentUser(@RequestHeader("X-Sharer-User-Id") int userId,
@RequestParam(name = "state", defaultValue = "ALL") String state) {
return bookingService.getAllBookingCurrentUser(userId, state);
@RequestParam(name = "state", required = false,
defaultValue = "ALL") String state,
@RequestParam(name = "from", required = false)
Optional<Integer> from,
@RequestParam(name = "size", required = false)
Optional<Integer> size) {

return bookingService.getAllBookingCurrentUser(userId, state, from, size);
}


@GetMapping("/owner")
public List<BookingDto> getAllBookingCurrentOwner(@RequestHeader("X-Sharer-User-Id") int userId,
@RequestParam(name = "state", defaultValue = "ALL") String state) {
return bookingService.getAllBookingCurrentOwner(userId, state);
@RequestParam(name = "state", defaultValue = "ALL") String state,
@RequestParam(name = "from", required = false)
Optional<Integer> from,
@RequestParam(name = "size", required = false)
Optional<Integer> size) {
return bookingService.getAllBookingCurrentOwner(userId, state, from, size);
}

}
4 changes: 4 additions & 0 deletions src/main/java/ru/practicum/shareit/booking/BookingMapper.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
package ru.practicum.shareit.booking;

import lombok.Getter;
import lombok.Setter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import ru.practicum.shareit.booking.dto.BookingDto;
import ru.practicum.shareit.item.ItemMapper;
import ru.practicum.shareit.user.UserMapper;

@Component
@Getter
@Setter
public class BookingMapper {
@Autowired
private ItemMapper itemMapper;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package ru.practicum.shareit.booking.dao;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
import ru.practicum.shareit.booking.Booking;
import ru.practicum.shareit.item.model.Item;
Expand All @@ -13,5 +16,12 @@ public interface BookingRepository extends JpaRepository<Booking, Integer> {
List<Booking> findByBookerEquals(User user);

List<Booking> findByItemEquals(Item item);

@Query(value = "SELECT * FROM bookings WHERE user_id = ?1 ORDER BY id DESC", nativeQuery = true)
Page<Booking> findByBookerIdWithPagination(int userId, Pageable pageable);

Page<Booking> findByItemOwnerId(int userId, Pageable pageable);


}

Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,18 @@
import ru.practicum.shareit.booking.dto.BookingDto;

import java.util.List;
import java.util.Optional;

public interface BookingService {
BookingDto addBooking(Booking booking, int bookerId);

BookingDto getBookingById(int bookingId, int userId);

List<BookingDto> getAllBookingCurrentUser(int userId, String state);
List<BookingDto> getAllBookingCurrentUser(int userId, String state, Optional<Integer> from, Optional<Integer> size);

List<BookingDto> getAllBookingCurrentOwner(int userId, String state);
List<BookingDto> getAllBookingCurrentOwner(int userId, String state, Optional<Integer> from, Optional<Integer> size);

BookingDto confirmationOrRejectionBooking(int bookingId, int userId, String bookingStatus);

Booking getBookingByOwner(int bookingId, int userId);
}
Loading
Loading