начало конца
This commit is contained in:
parent
325faa8791
commit
8a89da8daf
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@ -17,10 +17,12 @@ repositories {
|
||||
|
||||
dependencies {
|
||||
implementation 'org.springframework.boot:spring-boot-starter-web'
|
||||
implementation 'org.springframework.boot:spring-boot-starter-validation'
|
||||
implementation 'org.springdoc:springdoc-openapi-starter-webmvc-ui:2.3.0'
|
||||
implementation 'org.modelmapper:modelmapper:3.2.0'
|
||||
implementation 'javax.persistence:javax.persistence-api:2.2'
|
||||
implementation 'javax.persistence:javax.persistence-api:2.2'
|
||||
|
||||
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
|
||||
implementation 'com.h2database:h2:2.2.224'
|
||||
|
||||
|
||||
testImplementation 'org.springframework.boot:spring-boot-starter-test'
|
||||
|
@ -1 +1,20 @@
|
||||
# Server
|
||||
spring.main.banner-mode=off
|
||||
server.port=8080
|
||||
|
||||
# Logger settings
|
||||
# Available levels are: TRACE, DEBUG, INFO, WARN, ERROR, FATAL, OFF
|
||||
logging.level.com.example.demo=DEBUG
|
||||
|
||||
# JPA Settings
|
||||
spring.datasource.url=jdbc:h2:file:./data
|
||||
spring.datasource.username=sa
|
||||
spring.datasource.password=password
|
||||
spring.datasource.driver-class-name=org.h2.Driver
|
||||
spring.jpa.hibernate.ddl-auto=create
|
||||
spring.jpa.open-in-view=false
|
||||
# spring.jpa.show-sql=true
|
||||
# spring.jpa.properties.hibernate.format_sql=true
|
||||
|
||||
# H2 console
|
||||
spring.h2.console.enabled=true
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
BIN
data.mv.db
Normal file
BIN
data.mv.db
Normal file
Binary file not shown.
@ -45,30 +45,30 @@ public class DemoApplication implements CommandLineRunner {
|
||||
public void run(String... args) throws Exception {
|
||||
if (args.length > 0 && Objects.equals("--populate", args[0])) {
|
||||
log.info("Create default types values");
|
||||
final var type1 = typeService.create(new TypeEntity(null, "Пицца"));
|
||||
final var type2 = typeService.create(new TypeEntity(null, "Напиток"));
|
||||
final var type3 = typeService.create(new TypeEntity(null, "Закуска"));
|
||||
final var type1 = typeService.create(new TypeEntity("Пицца"));
|
||||
final var type2 = typeService.create(new TypeEntity("Напиток"));
|
||||
final var type3 = typeService.create(new TypeEntity("Закуска"));
|
||||
|
||||
log.info("Create default products values");
|
||||
productService.create(new ProductEntity(null, "Маргарита", type1, 499.00));
|
||||
productService.create(new ProductEntity(null, "Эль Дьябло", type1, 699.00));
|
||||
productService.create(new ProductEntity(null, "Гавайская", type1, 399.00));
|
||||
productService.create(new ProductEntity(null, "Лимонад", type2, 99.00));
|
||||
productService.create(new ProductEntity(null, "Сок", type2, 99.00));
|
||||
productService.create(new ProductEntity(null, "Чай", type2, 49.00));
|
||||
productService.create(new ProductEntity(null, "Картошка фри", type3, 199.00));
|
||||
productService.create(new ProductEntity(null, "Нагетсы", type3, 199.00));
|
||||
productService.create(new ProductEntity("Маргарита", type1, 499.00));
|
||||
productService.create(new ProductEntity("Эль Дьябло", type1, 699.00));
|
||||
productService.create(new ProductEntity("Гавайская", type1, 399.00));
|
||||
productService.create(new ProductEntity("Лимонад", type2, 99.00));
|
||||
productService.create(new ProductEntity("Сок", type2, 99.00));
|
||||
productService.create(new ProductEntity("Чай", type2, 49.00));
|
||||
productService.create(new ProductEntity("Картошка фри", type3, 199.00));
|
||||
productService.create(new ProductEntity("Нагетсы", type3, 199.00));
|
||||
|
||||
log.info("Create default users values");
|
||||
userService.create(new UserEntity(null, "Alex", "Kryukov", "akryu@mail.ru", "password123"));
|
||||
userService.create(new UserEntity(null, "Oleg", "Zyngin", "@mail.ru", "password"));
|
||||
userService.create(new UserEntity("Alex", "Kryukov", "akryu@mail.ru", "password123"));
|
||||
userService.create(new UserEntity("Oleg", "Zyngin", "@mail.ru", "password"));
|
||||
|
||||
log.info("Create default orders values");
|
||||
@SuppressWarnings({ "rawtypes", "unchecked" })
|
||||
List<OrderLineEntity> lines = new ArrayList();
|
||||
lines.add(new OrderLineEntity(null, 3));
|
||||
final var user1 = userService.create(new UserEntity(null, "Alex", "Kryukov", "akryu@mail.ru", "password"));
|
||||
orderService.create(new OrderEntity(null, user1, null));
|
||||
final var user1 = userService.create(new UserEntity("Alex", "Kryukov", "akryu@mail.ru", "password"));
|
||||
orderService.create(new OrderEntity(user1, null));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,6 +1,8 @@
|
||||
package com.example.demo.core.configuration;
|
||||
|
||||
public class Constants {
|
||||
public static final String SEQUENCE_NAME = "hibernate_sequence";
|
||||
|
||||
public static final String API_URL = "/api/1.0";
|
||||
|
||||
private Constants() {
|
||||
|
@ -1,7 +1,7 @@
|
||||
package com.example.demo.core.error;
|
||||
|
||||
public class NotFoundException extends RuntimeException {
|
||||
public NotFoundException(Long id) {
|
||||
super(String.format("Entity with id [%s] is not found or not exists", id));
|
||||
public <T> NotFoundException(Class<T> clazz, Long id) {
|
||||
super(String.format("%s with id [%s] is not found or not exists", clazz.getSimpleName(), id));
|
||||
}
|
||||
}
|
||||
|
@ -1,15 +1,23 @@
|
||||
package com.example.demo.core.model;
|
||||
|
||||
import com.example.demo.core.configuration.Constants;
|
||||
|
||||
import jakarta.persistence.GeneratedValue;
|
||||
import jakarta.persistence.GenerationType;
|
||||
import jakarta.persistence.Id;
|
||||
import jakarta.persistence.MappedSuperclass;
|
||||
import jakarta.persistence.SequenceGenerator;
|
||||
|
||||
@MappedSuperclass
|
||||
public abstract class BaseEntity {
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = Constants.SEQUENCE_NAME)
|
||||
@SequenceGenerator(name = Constants.SEQUENCE_NAME, sequenceName = Constants.SEQUENCE_NAME, allocationSize = 1)
|
||||
protected Long id;
|
||||
|
||||
protected BaseEntity() {
|
||||
}
|
||||
|
||||
protected BaseEntity(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
@ -1,17 +0,0 @@
|
||||
package com.example.demo.core.repository;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface CommonRepository<E, T> {
|
||||
List<E> getAll();
|
||||
|
||||
E get(T id);
|
||||
|
||||
E create(E entity);
|
||||
|
||||
E update(E entity);
|
||||
|
||||
E delete(E entity);
|
||||
|
||||
void deleteAll();
|
||||
}
|
@ -1,57 +0,0 @@
|
||||
package com.example.demo.core.repository;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.TreeMap;
|
||||
|
||||
import com.example.demo.core.model.BaseEntity;
|
||||
|
||||
public abstract class MapRepository<E extends BaseEntity> implements CommonRepository<E, Long> {
|
||||
private final Map<Long, E> entities = new TreeMap<>();
|
||||
private Long lastId = 0L;
|
||||
|
||||
protected MapRepository() {
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<E> getAll() {
|
||||
return entities.values().stream().toList();
|
||||
}
|
||||
|
||||
@Override
|
||||
public E get(Long id) {
|
||||
return entities.get(id);
|
||||
}
|
||||
|
||||
@Override
|
||||
public E create(E entity) {
|
||||
lastId++;
|
||||
entity.setId(lastId);
|
||||
entities.put(lastId, entity);
|
||||
return entity;
|
||||
}
|
||||
|
||||
@Override
|
||||
public E update(E entity) {
|
||||
if (get(entity.getId()) == null) {
|
||||
return null;
|
||||
}
|
||||
entities.put(entity.getId(), entity);
|
||||
return entity;
|
||||
}
|
||||
|
||||
@Override
|
||||
public E delete(E entity) {
|
||||
if (get(entity.getId()) == null) {
|
||||
return null;
|
||||
}
|
||||
entities.remove(entity.getId());
|
||||
return entity;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void deleteAll() {
|
||||
lastId = 0L;
|
||||
entities.clear();
|
||||
}
|
||||
}
|
@ -1,10 +1,8 @@
|
||||
package com.example.demo.order_lines.repository;
|
||||
|
||||
import org.springframework.stereotype.Repository;
|
||||
import org.springframework.data.repository.CrudRepository;
|
||||
|
||||
import com.example.demo.core.repository.MapRepository;
|
||||
import com.example.demo.order_lines.model.OrderLineEntity;
|
||||
|
||||
@Repository
|
||||
public class OrderLineRepository extends MapRepository<OrderLineEntity> {
|
||||
public interface OrderLineRepository extends CrudRepository<OrderLineEntity, Long> {
|
||||
}
|
||||
|
@ -5,13 +5,25 @@ import com.example.demo.order_lines.model.OrderLineEntity;
|
||||
import com.example.demo.users.model.UserEntity;
|
||||
import com.fasterxml.jackson.annotation.JsonManagedReference;
|
||||
|
||||
import jakarta.persistence.Entity;
|
||||
import jakarta.persistence.FetchType;
|
||||
import jakarta.persistence.JoinColumn;
|
||||
import jakarta.persistence.ManyToOne;
|
||||
import jakarta.persistence.OneToMany;
|
||||
import jakarta.persistence.Table;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
|
||||
@Entity
|
||||
@Table(name = "orders")
|
||||
public class OrderEntity extends BaseEntity {
|
||||
@ManyToOne
|
||||
@JoinColumn()
|
||||
private UserEntity user;
|
||||
@JsonManagedReference
|
||||
@OneToMany(fetch = FetchType.EAGER)
|
||||
private final List<OrderLineEntity> lines = new ArrayList<>();
|
||||
private Double totalPrice;
|
||||
|
||||
@ -19,8 +31,7 @@ public class OrderEntity extends BaseEntity {
|
||||
super();
|
||||
}
|
||||
|
||||
public OrderEntity(Long id, UserEntity user, Double totalPrice) {
|
||||
super(id);
|
||||
public OrderEntity(UserEntity user, Double totalPrice) {
|
||||
this.user = user;
|
||||
this.totalPrice = totalPrice;
|
||||
}
|
||||
|
@ -0,0 +1,7 @@
|
||||
package com.example.demo.orders.model;
|
||||
|
||||
public interface OrderGrouped {
|
||||
double getTotalPrice();
|
||||
|
||||
int getTotalCount();
|
||||
}
|
@ -1,10 +1,28 @@
|
||||
package com.example.demo.orders.repository;
|
||||
|
||||
import org.springframework.stereotype.Repository;
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.data.jpa.repository.Query;
|
||||
import org.springframework.data.repository.CrudRepository;
|
||||
|
||||
import com.example.demo.core.repository.MapRepository;
|
||||
import com.example.demo.orders.model.OrderEntity;
|
||||
import com.example.demo.orders.model.OrderGrouped;
|
||||
|
||||
@Repository
|
||||
public class OrderRepository extends MapRepository<OrderEntity> {
|
||||
public interface OrderRepository extends CrudRepository<OrderEntity, Long> {
|
||||
@SuppressWarnings("null")
|
||||
List<OrderEntity> findAll();
|
||||
|
||||
List<OrderEntity> findByUserIdAndLinesProductIds(long userId, List<Long> lines);
|
||||
|
||||
List<OrderEntity> findByLinesProductIds(List<Long> lines);
|
||||
|
||||
List<OrderEntity> findByUserId(long userId);
|
||||
|
||||
@Query("select "
|
||||
+ "t as type, "
|
||||
+ "coalesce(sum(o.price), 0) as totalPrice, "
|
||||
+ "coalesce(sum(o.count), 0) as totalCount "
|
||||
+ "from TypeEntity t left join OrderEntity o on o.type = t and o.user.id = ?1 "
|
||||
+ "group by t order by t.id")
|
||||
List<OrderGrouped> getOrdersTotalByType(long userId);
|
||||
}
|
||||
|
@ -1,9 +1,9 @@
|
||||
package com.example.demo.orders.service;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import com.example.demo.core.error.NotFoundException;
|
||||
import com.example.demo.order_lines.model.OrderLineEntity;
|
||||
@ -18,30 +18,27 @@ public class OrderService {
|
||||
this.repository = repository;
|
||||
}
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
public List<OrderEntity> getAll(Long userId, List<Long> lines) {
|
||||
if (userId != 0L && !lines.isEmpty()) {
|
||||
return repository.getAll().stream()
|
||||
.filter(line -> line.getUser().getId().equals(userId) && line.getLines().stream()
|
||||
.allMatch(orderLine -> lines.contains(orderLine.getProduct().getId())))
|
||||
.toList();
|
||||
return repository.findByUserIdAndLinesProductIds(userId, lines);
|
||||
}
|
||||
if (userId == 0L && !lines.isEmpty()) {
|
||||
return repository.getAll().stream().filter(line -> line.getLines().stream()
|
||||
.allMatch(orderLine -> lines.contains(orderLine.getProduct().getId()))).toList();
|
||||
return repository.findByLinesProductIds(lines);
|
||||
}
|
||||
if (userId != 0L && lines.isEmpty()) {
|
||||
return repository.getAll().stream().filter(line -> line.getUser().getId().equals(userId)).toList();
|
||||
return repository.findByUserId(userId);
|
||||
}
|
||||
return repository.getAll();
|
||||
return repository.findAll();
|
||||
}
|
||||
|
||||
public OrderEntity get(Long id) {
|
||||
return Optional.ofNullable(repository.get(id))
|
||||
.orElseThrow(() -> new NotFoundException(id));
|
||||
return repository.findById(id)
|
||||
.orElseThrow(() -> new NotFoundException(OrderEntity.class, id));
|
||||
}
|
||||
|
||||
public OrderEntity create(OrderEntity entity) {
|
||||
return repository.create(entity);
|
||||
return repository.save(entity);
|
||||
}
|
||||
|
||||
public OrderEntity update(Long id, OrderEntity entity) {
|
||||
@ -54,11 +51,12 @@ public class OrderService {
|
||||
existEntity.addOrderLine(newOrderLine);
|
||||
}
|
||||
|
||||
return repository.update(existEntity);
|
||||
return repository.save(existEntity);
|
||||
}
|
||||
|
||||
public OrderEntity delete(Long id) {
|
||||
final OrderEntity existsEntity = get(id);
|
||||
return repository.delete(existsEntity);
|
||||
repository.delete(existsEntity);
|
||||
return existsEntity;
|
||||
}
|
||||
}
|
||||
|
@ -5,17 +5,28 @@ import java.util.Objects;
|
||||
import com.example.demo.core.model.BaseEntity;
|
||||
import com.example.demo.types.model.TypeEntity;
|
||||
|
||||
import jakarta.persistence.Column;
|
||||
import jakarta.persistence.Entity;
|
||||
import jakarta.persistence.JoinColumn;
|
||||
import jakarta.persistence.ManyToOne;
|
||||
import jakarta.persistence.Table;
|
||||
|
||||
@Entity
|
||||
@Table(name = "product")
|
||||
public class ProductEntity extends BaseEntity {
|
||||
@ManyToOne
|
||||
@JoinColumn(name = "typeId", nullable = false)
|
||||
private TypeEntity type;
|
||||
@Column(nullable = false, length = 50)
|
||||
private String name;
|
||||
@Column(nullable = false, length = 50)
|
||||
private Double price;
|
||||
|
||||
public ProductEntity() {
|
||||
super();
|
||||
}
|
||||
|
||||
public ProductEntity(Long id, String name, TypeEntity type, Double price) {
|
||||
super(id);
|
||||
public ProductEntity(String name, TypeEntity type, Double price) {
|
||||
this.type = type;
|
||||
this.name = name;
|
||||
this.price = price;
|
||||
|
@ -1,10 +1,14 @@
|
||||
package com.example.demo.products.repository;
|
||||
|
||||
import org.springframework.stereotype.Repository;
|
||||
import java.util.Optional;
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.data.repository.CrudRepository;
|
||||
|
||||
import com.example.demo.core.repository.MapRepository;
|
||||
import com.example.demo.products.model.ProductEntity;
|
||||
|
||||
@Repository
|
||||
public class ProductRepository extends MapRepository<ProductEntity> {
|
||||
}
|
||||
public interface ProductRepository extends CrudRepository<ProductEntity, Long> {
|
||||
Optional<ProductEntity> findByNameIgnoreCase(String name);
|
||||
|
||||
List<ProductEntity> findByType_Id(Long typeId);
|
||||
}
|
@ -1,15 +1,16 @@
|
||||
package com.example.demo.products.service;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.Optional;
|
||||
|
||||
import org.springframework.stereotype.Service;
|
||||
import java.util.stream.StreamSupport;
|
||||
|
||||
import com.example.demo.core.error.NotFoundException;
|
||||
import com.example.demo.products.model.ProductEntity;
|
||||
import com.example.demo.products.repository.ProductRepository;
|
||||
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
@Service
|
||||
public class ProductService {
|
||||
private final ProductRepository repository;
|
||||
@ -18,35 +19,39 @@ public class ProductService {
|
||||
this.repository = repository;
|
||||
}
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
public List<ProductEntity> getAll(Long typeId) {
|
||||
if (Objects.equals(typeId, 0L)) {
|
||||
return repository.getAll();
|
||||
if (typeId != null && typeId != 0L) {
|
||||
return repository.findByType_Id(typeId);
|
||||
} else {
|
||||
return StreamSupport.stream(repository.findAll().spliterator(), false).toList();
|
||||
}
|
||||
return repository.getAll().stream()
|
||||
.filter(order -> order.getType().getId().equals(typeId))
|
||||
.toList();
|
||||
}
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
public ProductEntity get(Long id) {
|
||||
return Optional.ofNullable(repository.get(id))
|
||||
.orElseThrow(() -> new NotFoundException(id));
|
||||
return repository.findById(id).orElseThrow(() -> new NotFoundException(ProductEntity.class, id));
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public ProductEntity create(ProductEntity entity) {
|
||||
entity.setPrice(entity.getPrice());
|
||||
return repository.create(entity);
|
||||
if (entity == null) {
|
||||
throw new IllegalArgumentException("Entity is null");
|
||||
}
|
||||
return repository.save(entity);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public ProductEntity update(Long id, ProductEntity entity) {
|
||||
final ProductEntity existsEntity = get(id);
|
||||
existsEntity.setName(entity.getName());
|
||||
existsEntity.setType(entity.getType());
|
||||
existsEntity.setPrice(entity.getPrice());
|
||||
return repository.update(existsEntity);
|
||||
final ProductEntity exisEntity = get(id);
|
||||
exisEntity.setName(entity.getName());
|
||||
return repository.save(exisEntity);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public ProductEntity delete(Long id) {
|
||||
final ProductEntity existsEntity = get(id);
|
||||
return repository.delete(existsEntity);
|
||||
final ProductEntity existEntity = get(id);
|
||||
repository.delete(existEntity);
|
||||
return existEntity;
|
||||
}
|
||||
}
|
||||
|
@ -3,10 +3,12 @@ package com.example.demo.types.api;
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
|
||||
import jakarta.validation.constraints.NotBlank;
|
||||
import jakarta.validation.constraints.Size;
|
||||
|
||||
public class TypeDto {
|
||||
private Long id;
|
||||
@NotBlank
|
||||
@Size(min = 5, max = 50)
|
||||
private String name;
|
||||
|
||||
@JsonProperty(access = JsonProperty.Access.READ_ONLY)
|
||||
|
@ -3,16 +3,20 @@ package com.example.demo.types.model;
|
||||
import java.util.Objects;
|
||||
|
||||
import com.example.demo.core.model.BaseEntity;
|
||||
import jakarta.persistence.Column;
|
||||
import jakarta.persistence.Entity;
|
||||
import jakarta.persistence.Table;
|
||||
|
||||
@Entity
|
||||
@Table(name = "types")
|
||||
public class TypeEntity extends BaseEntity {
|
||||
@Column(nullable = false, unique = true, length = 50)
|
||||
private String name;
|
||||
|
||||
public TypeEntity() {
|
||||
super();
|
||||
}
|
||||
|
||||
public TypeEntity(Long id, String name) {
|
||||
super(id);
|
||||
public TypeEntity(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
|
@ -1,10 +1,11 @@
|
||||
package com.example.demo.types.repository;
|
||||
|
||||
import org.springframework.stereotype.Repository;
|
||||
import java.util.Optional;
|
||||
|
||||
import org.springframework.data.repository.CrudRepository;
|
||||
|
||||
import com.example.demo.core.repository.MapRepository;
|
||||
import com.example.demo.types.model.TypeEntity;
|
||||
|
||||
@Repository
|
||||
public class TypeRepository extends MapRepository<TypeEntity> {
|
||||
public interface TypeRepository extends CrudRepository<TypeEntity, Long> {
|
||||
Optional<TypeEntity> findByNameIgnoreCase(String name);
|
||||
}
|
||||
|
@ -1,9 +1,10 @@
|
||||
package com.example.demo.types.service;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.stream.StreamSupport;
|
||||
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import com.example.demo.core.error.NotFoundException;
|
||||
import com.example.demo.types.model.TypeEntity;
|
||||
@ -17,27 +18,45 @@ public class TypeService {
|
||||
this.repository = repository;
|
||||
}
|
||||
|
||||
private void checkName(String name) {
|
||||
if (repository.findByNameIgnoreCase(name).isPresent()) {
|
||||
throw new IllegalArgumentException(
|
||||
String.format("Type with name %s is already exists", name));
|
||||
}
|
||||
}
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
public List<TypeEntity> getAll() {
|
||||
return repository.getAll();
|
||||
return StreamSupport.stream(repository.findAll().spliterator(), false).toList();
|
||||
}
|
||||
|
||||
public TypeEntity get(Long id) {
|
||||
return Optional.ofNullable(repository.get(id))
|
||||
.orElseThrow(() -> new NotFoundException(id));
|
||||
@Transactional(readOnly = true)
|
||||
public TypeEntity get(long id) {
|
||||
return repository.findById(id)
|
||||
.orElseThrow(() -> new NotFoundException(TypeEntity.class, id));
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public TypeEntity create(TypeEntity entity) {
|
||||
return repository.create(entity);
|
||||
if (entity == null) {
|
||||
throw new IllegalArgumentException("Entity is null");
|
||||
}
|
||||
checkName(entity.getName());
|
||||
return repository.save(entity);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public TypeEntity update(Long id, TypeEntity entity) {
|
||||
final TypeEntity existsEntity = get(id);
|
||||
checkName(entity.getName());
|
||||
existsEntity.setName(entity.getName());
|
||||
return repository.update(existsEntity);
|
||||
return repository.save(existsEntity);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public TypeEntity delete(Long id) {
|
||||
final TypeEntity existsEntity = get(id);
|
||||
return repository.delete(existsEntity);
|
||||
repository.delete(existsEntity);
|
||||
return existsEntity;
|
||||
}
|
||||
}
|
||||
|
@ -4,18 +4,27 @@ import java.util.Objects;
|
||||
|
||||
import com.example.demo.core.model.BaseEntity;
|
||||
|
||||
import jakarta.persistence.Table;
|
||||
import jakarta.persistence.Column;
|
||||
import jakarta.persistence.Entity;
|
||||
|
||||
@Entity
|
||||
@Table(name = "users")
|
||||
public class UserEntity extends BaseEntity {
|
||||
@Column(nullable = false, unique = true, length = 50)
|
||||
private String fullname;
|
||||
@Column(nullable = false, unique = true, length = 50)
|
||||
private String surname;
|
||||
@Column(nullable = false, unique = true, length = 50)
|
||||
private String email;
|
||||
@Column(nullable = false, unique = true, length = 50)
|
||||
private String password;
|
||||
|
||||
public UserEntity() {
|
||||
super();
|
||||
}
|
||||
|
||||
public UserEntity(Long id, String fullname, String surname, String email, String password) {
|
||||
super(id);
|
||||
public UserEntity(String fullname, String surname, String email, String password) {
|
||||
this.fullname = fullname;
|
||||
this.surname = surname;
|
||||
this.email = email;
|
||||
|
@ -1,10 +1,12 @@
|
||||
package com.example.demo.users.repository;
|
||||
|
||||
import org.springframework.stereotype.Repository;
|
||||
import java.util.Optional;
|
||||
|
||||
import org.springframework.data.repository.CrudRepository;
|
||||
import org.springframework.data.repository.PagingAndSortingRepository;
|
||||
|
||||
import com.example.demo.core.repository.MapRepository;
|
||||
import com.example.demo.users.model.UserEntity;
|
||||
|
||||
@Repository
|
||||
public class UserRepository extends MapRepository<UserEntity> {
|
||||
}
|
||||
public interface UserRepository extends CrudRepository<UserEntity, Long>, PagingAndSortingRepository<UserEntity, Long> {
|
||||
Optional<UserEntity> findByEmailIgnoreCase(String email);
|
||||
}
|
@ -1,8 +1,11 @@
|
||||
package com.example.demo.users.service;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.stream.StreamSupport;
|
||||
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import com.example.demo.core.error.NotFoundException;
|
||||
import com.example.demo.users.model.UserEntity;
|
||||
import com.example.demo.users.repository.UserRepository;
|
||||
@ -15,30 +18,49 @@ public class UserService {
|
||||
this.repository = repository;
|
||||
}
|
||||
|
||||
private void checkEmail(String email) {
|
||||
if (repository.findByEmailIgnoreCase(email).isPresent()) {
|
||||
throw new IllegalArgumentException(
|
||||
String.format("User with email %s is already exists", email));
|
||||
}
|
||||
}
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
public List<UserEntity> getAll() {
|
||||
return repository.getAll();
|
||||
return StreamSupport.stream(repository.findAll().spliterator(), false).toList();
|
||||
}
|
||||
|
||||
public UserEntity get(Long id) {
|
||||
return Optional.ofNullable(repository.get(id))
|
||||
.orElseThrow(() -> new NotFoundException(id));
|
||||
@Transactional(readOnly = true)
|
||||
public UserEntity get(long id) {
|
||||
return repository.findById(id)
|
||||
.orElseThrow(() -> new NotFoundException(UserEntity.class, id));
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public UserEntity create(UserEntity entity) {
|
||||
return repository.create(entity);
|
||||
if (entity == null) {
|
||||
throw new IllegalArgumentException("Entity is null");
|
||||
}
|
||||
checkEmail(entity.getEmail());
|
||||
return repository.save(entity);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public UserEntity update(Long id, UserEntity entity) {
|
||||
final UserEntity existsEntity = get(id);
|
||||
existsEntity.setFullname(entity.getFullname());
|
||||
existsEntity.setSurname(entity.getSurname());
|
||||
existsEntity.setEmail(entity.getEmail());
|
||||
existsEntity.setPassword(entity.getPassword());
|
||||
return repository.update(existsEntity);
|
||||
checkEmail(entity.getEmail());
|
||||
final UserEntity existEntity = get(id);
|
||||
existEntity.setFullname(entity.getFullname());
|
||||
existEntity.setSurname(entity.getSurname());
|
||||
existEntity.setEmail(entity.getEmail());
|
||||
existEntity.setPassword(entity.getPassword());
|
||||
repository.save(existEntity);
|
||||
return existEntity;
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public UserEntity delete(Long id) {
|
||||
final UserEntity existsEntity = get(id);
|
||||
return repository.delete(existsEntity);
|
||||
final UserEntity existEntity = get(id);
|
||||
repository.delete(existEntity);
|
||||
return existEntity;
|
||||
}
|
||||
}
|
||||
|
@ -1 +1,20 @@
|
||||
# Server
|
||||
spring.main.banner-mode=off
|
||||
server.port=8080
|
||||
|
||||
# Logger settings
|
||||
# Available levels are: TRACE, DEBUG, INFO, WARN, ERROR, FATAL, OFF
|
||||
logging.level.com.example.demo=DEBUG
|
||||
|
||||
# JPA Settings
|
||||
spring.datasource.url=jdbc:h2:file:./data
|
||||
spring.datasource.username=sa
|
||||
spring.datasource.password=password
|
||||
spring.datasource.driver-class-name=org.h2.Driver
|
||||
spring.jpa.hibernate.ddl-auto=create
|
||||
spring.jpa.open-in-view=false
|
||||
# spring.jpa.show-sql=true
|
||||
# spring.jpa.properties.hibernate.format_sql=true
|
||||
|
||||
# H2 console
|
||||
spring.h2.console.enabled=true
|
@ -29,7 +29,7 @@ class OrderServiceTests {
|
||||
@Test
|
||||
@Order(1)
|
||||
void createTest() {
|
||||
orderService.create(new OrderEntity(null, null, null));
|
||||
orderService.create(new OrderEntity(null, null));
|
||||
@SuppressWarnings({ "rawtypes", "unchecked" })
|
||||
List<OrderLineEntity> lines = new ArrayList();
|
||||
lines.add(new OrderLineEntity(null, 4));
|
||||
@ -37,7 +37,7 @@ class OrderServiceTests {
|
||||
lines.add(new OrderLineEntity(null, 6));
|
||||
lines.add(new OrderLineEntity(null, 7));
|
||||
// Создаем тестовую сущность OrderEntity
|
||||
OrderEntity testOrder = new OrderEntity(null, null, null);
|
||||
OrderEntity testOrder = new OrderEntity(null, null);
|
||||
// Вызываем метод create() и сохраняем созданную сущность
|
||||
OrderEntity createdOrder = orderService.create(testOrder);
|
||||
// Проверяем, что метод create() вернул не null
|
||||
|
@ -28,19 +28,19 @@ class ProductServiceTests {
|
||||
@Order(1)
|
||||
void createTest() {
|
||||
|
||||
final var type1 = new TypeEntity(null, "Пицца");
|
||||
productService.create(new ProductEntity(null, "Mocarela", type1, 20.00));
|
||||
productService.create(new ProductEntity(null, "El'Diablo", type1, 20.00));
|
||||
productService.create(new ProductEntity(null, "Маргарита", type1, 499.00));
|
||||
productService.create(new ProductEntity(null, "Эль Дьябло", type1, 699.00));
|
||||
productService.create(new ProductEntity(null, "Гавайская", type1, 399.00));
|
||||
final var type1 = new TypeEntity("Пицца");
|
||||
productService.create(new ProductEntity("Mocarela", type1, 20.00));
|
||||
productService.create(new ProductEntity("El'Diablo", type1, 20.00));
|
||||
productService.create(new ProductEntity("Маргарита", type1, 499.00));
|
||||
productService.create(new ProductEntity("Эль Дьябло", type1, 699.00));
|
||||
productService.create(new ProductEntity("Гавайская", type1, 399.00));
|
||||
Assertions.assertEquals(5, productService.getAll(0L).size());
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(2)
|
||||
void updateTest() {
|
||||
final ProductEntity newProduct = new ProductEntity(null, "El'Diablo", new TypeEntity(null, "Пицца"), 20.00);
|
||||
final ProductEntity newProduct = new ProductEntity("El'Diablo", new TypeEntity("Пицца"), 20.00);
|
||||
final ProductEntity updProduct = productService.update(1L, newProduct);
|
||||
Assertions.assertEquals(5, productService.getAll(0L).size());
|
||||
Assertions.assertEquals(updProduct, productService.get(1L));
|
||||
|
@ -26,9 +26,9 @@ class TypeServiceTests {
|
||||
@Test
|
||||
@Order(1)
|
||||
void createTest() {
|
||||
typeService.create(new TypeEntity(null, "Пицца"));
|
||||
typeService.create(new TypeEntity(null, "Напиток"));
|
||||
final TypeEntity last = typeService.create(new TypeEntity(null, "Закуска"));
|
||||
typeService.create(new TypeEntity("Пицца"));
|
||||
typeService.create(new TypeEntity("Напиток"));
|
||||
final TypeEntity last = typeService.create(new TypeEntity("Закуска"));
|
||||
Assertions.assertEquals(3, typeService.getAll().size());
|
||||
Assertions.assertEquals(last, typeService.get(3L));
|
||||
}
|
||||
@ -39,7 +39,7 @@ class TypeServiceTests {
|
||||
final String test = "TEST";
|
||||
final TypeEntity entity = typeService.get(3L);
|
||||
final String oldName = entity.getName();
|
||||
final TypeEntity newEntity = typeService.update(3L, new TypeEntity(1L, test));
|
||||
final TypeEntity newEntity = typeService.update(3L, new TypeEntity(test));
|
||||
Assertions.assertEquals(3, typeService.getAll().size());
|
||||
Assertions.assertEquals(newEntity, typeService.get(3L));
|
||||
Assertions.assertEquals(test, newEntity.getName());
|
||||
@ -54,7 +54,7 @@ class TypeServiceTests {
|
||||
final TypeEntity last = typeService.get(2L);
|
||||
Assertions.assertEquals(2L, last.getId());
|
||||
|
||||
final TypeEntity newEntity = typeService.create(new TypeEntity(null, "Закуска"));
|
||||
final TypeEntity newEntity = typeService.create(new TypeEntity("Закуска"));
|
||||
Assertions.assertEquals(3, typeService.getAll().size());
|
||||
Assertions.assertEquals(4L, newEntity.getId());
|
||||
}
|
||||
|
@ -26,9 +26,9 @@ class UserServiceTests {
|
||||
@Test
|
||||
@Order(1)
|
||||
void createTest() {
|
||||
userService.create(new UserEntity(null, "John", "Doe", "gge@fkgjfdj", "password"));
|
||||
userService.create(new UserEntity(null, "Alex", "Kryukov", "fhegehr@ghsjg.com", "password"));
|
||||
final UserEntity last = userService.create(new UserEntity(null, "Alex", "selivanov", "fhegehr@ghsjg.com",
|
||||
userService.create(new UserEntity("John", "Doe", "gge@fkgjfdj", "password"));
|
||||
userService.create(new UserEntity("Alex", "Kryukov", "fhegehr@ghsjg.com", "password"));
|
||||
final UserEntity last = userService.create(new UserEntity("Alex", "selivanov", "fhegehr@ghsjg.com",
|
||||
"password"));
|
||||
Assertions.assertEquals(3, userService.getAll().size());
|
||||
Assertions.assertEquals(last, userService.get(3L));
|
||||
@ -39,7 +39,7 @@ class UserServiceTests {
|
||||
void updateTest() {
|
||||
final UserEntity entity = userService.get(3L);
|
||||
final String oldName = entity.getFullname();
|
||||
final UserEntity newEntity = userService.update(3L, new UserEntity(1L, "test", "test", "test", "test"));
|
||||
final UserEntity newEntity = userService.update(3L, new UserEntity("test", "test", "test", "test"));
|
||||
Assertions.assertEquals(3, userService.getAll().size());
|
||||
Assertions.assertEquals(newEntity, userService.get(3L));
|
||||
Assertions.assertEquals("test", newEntity.getFullname());
|
||||
|
Loading…
Reference in New Issue
Block a user