Добавлены условия запроса

This commit is contained in:
2025-04-20 16:24:51 +04:00
parent 5743d1ad47
commit fa20efb1b4
11 changed files with 48 additions and 3 deletions

View File

@@ -10,6 +10,7 @@ import java.util.UUID;
@Entity
public class Car {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(nullable = false)
@@ -21,6 +22,9 @@ public class Car {
@Column(nullable = false)
private Integer year;
@OneToOne(fetch = FetchType.EAGER)
private User user;
@ManyToMany(fetch = FetchType.EAGER)
private List<Configuration> configurations;

View File

@@ -1,8 +1,10 @@
package org.example.domain.entity;
import jakarta.persistence.*;
import jdk.jfr.Timestamp;
import lombok.Data;
import java.time.LocalDateTime;
import java.util.List;
import java.util.UUID;
@@ -10,6 +12,7 @@ import java.util.UUID;
@Entity
public class Configuration {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(nullable = false)
@@ -18,9 +21,16 @@ public class Configuration {
@Column(nullable = false)
private String description;
@Timestamp
@Column(nullable = false)
private LocalDateTime createdAt;
@OneToOne(fetch = FetchType.EAGER)
private User user;
@ManyToMany(fetch = FetchType.LAZY, mappedBy = "configurations")
private List<Car> cars;
@ManyToMany(fetch = FetchType.LAZY, mappedBy = "configurations")
private List<PreparationWork> preparationWorks;
}

View File

@@ -9,6 +9,7 @@ import java.util.UUID;
@Data
public class Feature {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(nullable = false)

View File

@@ -10,6 +10,7 @@ import java.util.UUID;
@Data
public class PreparationWork {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(nullable = false)

View File

@@ -13,6 +13,7 @@ import java.util.UUID;
@Data
public class Request {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Timestamp

View File

@@ -12,6 +12,7 @@ import java.util.UUID;
@Table(name = "users")
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(nullable = false)

View File

@@ -11,6 +11,7 @@ import java.util.UUID;
@Data
public class Wish {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(nullable = false)

View File

@@ -1,7 +1,20 @@
package org.example.domain.repositories;
import org.example.domain.entity.Configuration;
import org.example.domain.entity.Request;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import java.time.LocalDate;
import java.util.List;
public interface ConfigurationRepository extends JpaRepository<Configuration, Long> {
@Query("select c from Configuration c " +
"join c.cars car " +
"where (:userId is null or c.user.id = :userid)" +
"and c.user.id = car.user.id " +
"and c.createdAt > :dateFrom " +
"and c.createdAt < :dateFrom")
List<Configuration> findByCreatedAtBetween(LocalDate dateFrom, LocalDate dateTo, Long userId);
}

View File

@@ -2,10 +2,17 @@ package org.example.domain.repositories;
import org.example.domain.entity.Request;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import java.time.LocalDate;
import java.util.List;
public interface RequestRepository extends JpaRepository<Request, Long> {
List<Request> findByCreatedAtBetween(LocalDate dateFrom, LocalDate dateTo);
@Query("select r from Request r " +
"join r.preparationWorks p " +
"where (:userId is null or r.user.id = :userid)" +
"and r.user.id = p.user.id " +
"and r.createdAt > :dateFrom " +
"and r.createdAt < :dateFrom")
List<Request> findByCreatedAtBetween(LocalDate dateFrom, LocalDate dateTo, Long userId);
}

View File

@@ -6,6 +6,8 @@ import org.example.domain.repositories.ConfigurationRepository;
import org.example.exceptions.ObjectNotExistsException;
import org.springframework.stereotype.Component;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
@Component
@@ -29,4 +31,8 @@ public class ConfigurationManager {
public List<Configuration> getAll(){
return configurationRepository.findAll();
}
public List<Configuration> getAll(LocalDate dateFrom, LocalDate dateTo, Long userId){
return configurationRepository.findByCreatedAtBetween(dateFrom, dateTo, userId);
}
}

View File

@@ -31,7 +31,7 @@ public class RequestManager {
return requestRepository.findAll();
}
public List<Request> getAll(LocalDate dateFrom, LocalDate dateTo){
return requestRepository.findByCreatedAtBetween(dateFrom, dateTo);
public List<Request> getAll(LocalDate dateFrom, LocalDate dateTo, Long userId){
return requestRepository.findByCreatedAtBetween(dateFrom, dateTo, userId);
}
}