This commit is contained in:
revengel66 2024-04-09 11:28:29 +04:00
parent e92ced5360
commit e79da32126
11 changed files with 126 additions and 246 deletions

Binary file not shown.

View File

@ -1,23 +0,0 @@
#
# Copyright 2012-2024 the original author or authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.6-bin.zip
networkTimeout=10000
validateDistributionUrl=true
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists

View File

@ -1,21 +1,19 @@
package com.example.demo.itemCategories.model;
import java.util.Objects;
import com.example.demo.core.model.BaseEntity;
import com.example.demo.types.model.TypeEntity;
public class ItemEntity extends BaseEntity {
public class CategoriesEntity extends BaseEntity {
private TypeEntity type;
// private Double price;
// private Integer count;
private String name;
public ItemEntity() {
public CategoriesEntity() {
super();
}
public ItemEntity(Long id, TypeEntity type, String name) {
public CategoriesEntity(Long id, TypeEntity type, String name) {
super(id);
this.type = type;
this.name = name;
@ -48,7 +46,7 @@ public class ItemEntity extends BaseEntity {
return true;
if (obj == null || getClass() != obj.getClass())
return false;
final ItemEntity other = (ItemEntity) obj;
final CategoriesEntity other = (CategoriesEntity) obj;
return Objects.equals(other.getId(), id)
&& Objects.equals(other.getType(), type)
&& Objects.equals(other.getName(), name);

View File

@ -1,66 +0,0 @@
package com.example.demo.itemOrders.model;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import com.example.demo.core.model.BaseEntity;
import com.example.demo.itemProducts.model.ProductsEntity;
import com.example.demo.itemUsers.model.UserEntity;
import com.example.demo.types.model.TypeEntity;
public class OrderEntity extends BaseEntity {
private TypeEntity type;
private UserEntity user;
private List<ProductsEntity> products = new ArrayList<>();
public OrderEntity() {
super();
}
public OrderEntity(Long id, TypeEntity type, UserEntity user, List<ProductsEntity> products) {
super(id);
this.type = type;
this.user = user;
this.products = products;
}
public TypeEntity getType() {
return type;
}
public void setType(TypeEntity type) {
this.type = type;
}
public UserEntity getUser() {
return user;
}
public void setUser(UserEntity user) {
this.user = user;
}
// добавить методы добавить и удалить продукт, и получить продукт
public List<ProductsEntity> getProducts() {
return products;
}
@Override
public int hashCode() {
return Objects.hash(id, type, login, password);
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null || getClass() != obj.getClass())
return false;
final OrderEntity other = (OrderEntity) obj;
return Objects.equals(other.getId(), id)
&& Objects.equals(other.getType(), type)
&& Objects.equals(other.getLogin(), login)
&& Objects.equals(other.getPassword(), password);
}
}

View File

@ -0,0 +1,82 @@
package com.example.demo.itemOrders.model;
import java.sql.Date;
import java.util.Objects;
import com.example.demo.core.model.BaseEntity;
import com.example.demo.types.model.TypeEntity;
public class OrdersEntity extends BaseEntity {
private TypeEntity type;
private Integer categoriesId;
private Double price;
private Integer count;
private Date date;
public OrdersEntity() {
super();
}
public OrdersEntity(Long id, TypeEntity type, Integer categoriesId, Double price, Integer count, Date date) {
super(id);
this.type = type;
this.categoriesId = categoriesId;
this.price = price;
this.count = count;
this.date = date;
}
public TypeEntity getType() {
return type;
}
public void setType(TypeEntity type) {
this.type = type;
}
public Integer getCategoriesId() {
return categoriesId;
}
public void setCategoriesId(Integer categoriesId) {
this.categoriesId = categoriesId;
}
public Double getPrice() {
return price;
}
public void setPrice(Double price) {
this.price = price;
}
public Integer getCount() {
return count;
}
public void setCount(Integer count) {
this.count = count;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
@Override
public int hashCode() {
return Objects.hash(id, type, categoriesId, price, count);
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null || getClass() != obj.getClass())
return false;
final OrdersEntity other = (OrdersEntity) obj;
return Objects.equals(other.getId(), id)
&& Objects.equals(other.getType(), type)
&& Objects.equals(other.getCategoriesId(), categoriesId)
&& Objects.equals(other.getPrice(), price)
&& Objects.equals(other.getCount(), count)
&& Objects.equals(other.getDate(), date);
}
}

View File

@ -7,18 +7,22 @@ import com.example.demo.types.model.TypeEntity;
public class ProductsEntity extends BaseEntity{
private TypeEntity type;
private Integer categoriesId;
private Double price;
private Integer count;
private Double sum;
public ProductsEntity() {
super();
}
public ProductsEntity(Long id, TypeEntity type, Double price, Integer count) {
public ProductsEntity(Long id, TypeEntity type, Integer categoriesId, Double price, Integer count, Double sum) {
super(id);
this.type = type;
this.categoriesId = categoriesId;
this.price = price;
this.count = count;
this.sum = sum;
}
public TypeEntity getType() {
@ -28,7 +32,13 @@ public class ProductsEntity extends BaseEntity {
public void setType(TypeEntity type) {
this.type = type;
}
public Integer getCategoriesId() {
return categoriesId;
}
public void setCategoriesId(Integer categoriesId) {
this.categoriesId = categoriesId;
}
public Double getPrice() {
return price;
}
@ -44,10 +54,16 @@ public class ProductsEntity extends BaseEntity {
public void setCount(Integer count) {
this.count = count;
}
public Double getSum() {
return sum;
}
public void setSum(Double sum) {
this.sum = sum;
}
@Override
public int hashCode() {
return Objects.hash(id, type, price, count);
return Objects.hash(id, type, categoriesId, price, count, sum);
}
@Override
@ -59,7 +75,9 @@ public class ProductsEntity extends BaseEntity {
final ProductsEntity other = (ProductsEntity) obj;
return Objects.equals(other.getId(), id)
&& Objects.equals(other.getType(), type)
&& Objects.equals(other.getCategoriesId(), categoriesId)
&& Objects.equals(other.getPrice(), price)
&& Objects.equals(other.getCount(), count);
&& Objects.equals(other.getCount(), count)
&& Objects.equals(other.getSum(), sum);
}
}

View File

@ -1,70 +0,0 @@
package com.example.demo.itemUsers.api;
import java.util.List;
import org.modelmapper.ModelMapper;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.example.demo.core.configuration.Constants;
import com.example.demo.itemUsers.model.UserEntity;
import com.example.demo.itemUsers.service.UserService;
import com.example.demo.types.service.TypeService;
import jakarta.validation.Valid;
@RestController
@RequestMapping(Constants.API_URL + "/item")
public class UserController {
private final UserService itemService;
private final TypeService typeService;
private final ModelMapper modelMapper;
public UserController(UserService itemService, TypeService typeService, ModelMapper modelMapper) {
this.itemService = itemService;
this.typeService = typeService;
this.modelMapper = modelMapper;
}
private UserDto toDto(OrderEntity entity) {
return modelMapper.map(entity, UserDto.class);
}
private OrderEntity toEntity(UserDto dto) {
final OrderEntity entity = modelMapper.map(dto, OrderEntity.class);
entity.setType(typeService.get(dto.getTypeId()));
return entity;
}
@GetMapping
public List<UserDto> getAll(@RequestParam(name = "typeId", defaultValue = "0") Long typeId) {
return itemService.getAll(typeId).stream().map(this::toDto).toList();
}
@GetMapping("/{id}")
public UserDto get(@PathVariable(name = "id") Long id) {
return toDto(itemService.get(id));
}
@PostMapping
public UserDto create(@RequestBody @Valid UserDto dto) {
return toDto(itemService.create(toEntity(dto)));
}
@PutMapping("/{id}")
public UserDto update(@PathVariable(name = "id") Long id, @RequestBody UserDto dto) {
return toDto(itemService.update(id, toEntity(dto)));
}
@DeleteMapping("/{id}")
public UserDto delete(@PathVariable(name = "id") Long id) {
return toDto(itemService.delete(id));
}
}

View File

@ -1,57 +0,0 @@
package com.example.demo.itemUsers.api;
import com.fasterxml.jackson.annotation.JsonProperty;
import jakarta.validation.constraints.Min;
import jakarta.validation.constraints.NotNull;
public class UserDto {
@JsonProperty(access = JsonProperty.Access.READ_ONLY)
private Long id;
@NotNull
@Min(1)
private Long typeId;
@NotNull
@Min(1)
private Double price;
@NotNull
@Min(1)
private Integer count;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public Long getTypeId() {
return typeId;
}
public void setTypeId(Long typeId) {
this.typeId = typeId;
}
public Double getPrice() {
return price;
}
public void setPrice(Double price) {
this.price = price;
}
public Integer getCount() {
return count;
}
public void setCount(Integer count) {
this.count = count;
}
@JsonProperty(access = JsonProperty.Access.READ_ONLY)
public Double getSum() {
return price * count;
}
}

View File

@ -1,20 +1,18 @@
package com.example.demo.itemUsers.model;
import java.util.Objects;
import com.example.demo.core.model.BaseEntity;
import com.example.demo.types.model.TypeEntity;
public class UserEntity extends BaseEntity {
public class UsersEntity extends BaseEntity {
private TypeEntity type;
private String login;
private String password;
public UserEntity() {
public UsersEntity() {
super();
}
public UserEntity(Long id, TypeEntity type, String login, String password) {
public UsersEntity(Long id, TypeEntity type, String login, String password) {
super(id);
this.type = type;
this.login = login;
@ -56,7 +54,7 @@ public class UserEntity extends BaseEntity {
return true;
if (obj == null || getClass() != obj.getClass())
return false;
final UserEntity other = (UserEntity) obj;
final UsersEntity other = (UsersEntity) obj;
return Objects.equals(other.getId(), id)
&& Objects.equals(other.getType(), type)
&& Objects.equals(other.getLogin(), login)

View File

@ -6,5 +6,5 @@ import com.example.demo.core.repository.MapRepository;
import com.example.demo.itemUsers.model.ItemEntity;
@Repository
public class UserRepository extends MapRepository<OrderEntity> {
public class UsersRepository extends MapRepository<ProductsEntity> {
}

View File

@ -7,18 +7,18 @@ import java.util.Optional;
import org.springframework.stereotype.Service;
import com.example.demo.core.error.NotFoundException;
import com.example.demo.itemUsers.model.UserEntity;
import com.example.demo.itemUsers.repository.UserRepository;
import com.example.demo.itemUsers.model.UsersEntity;
import com.example.demo.itemUsers.repository.UsersRepository;
@Service
public class UserService {
private final UserRepository repository;
private final UsersRepository repository;
public UserService(UserRepository repository) {
public UserService(UsersRepository repository) {
this.repository = repository;
}
public List<UserEntity> getAll(Long typeId) {
public List<UsersEntity> getAll(Long typeId) {
if (Objects.equals(typeId, 0L)) {
return repository.getAll();
}
@ -27,25 +27,25 @@ public class UserService {
.toList();
}
public UserEntity get(Long id) {
public UsersEntity get(Long id) {
return Optional.ofNullable(repository.get(id))
.orElseThrow(() -> new NotFoundException(id));
}
public UserEntity create(UserEntity entity) {
public UsersEntity create(UsersEntity entity) {
return repository.create(entity);
}
public UserEntity update(Long id, UserEntity entity) {
final UserEntity existsEntity = get(id);
public UsersEntity update(Long id, UsersEntity entity) {
final UsersEntity existsEntity = get(id);
existsEntity.setType(entity.getType());
existsEntity.setPrice(entity.getPrice());
existsEntity.setCount(entity.getCount());
return repository.update(existsEntity);
}
public UserEntity delete(Long id) {
final UserEntity existsEntity = get(id);
public UsersEntity delete(Long id) {
final UsersEntity existsEntity = get(id);
return repository.delete(existsEntity);
}
}