new commit

This commit is contained in:
revengel66 2024-04-19 23:55:08 +04:00
parent 6b9a2025ed
commit cd388cff5c
5 changed files with 34 additions and 72 deletions

View File

@ -15,7 +15,6 @@ import org.springframework.web.bind.annotation.RestController;
import com.example.demo.core.configuration.Constants; import com.example.demo.core.configuration.Constants;
import com.example.demo.itemOrders.model.OrdersEntity; import com.example.demo.itemOrders.model.OrdersEntity;
import com.example.demo.itemOrders.model.OrdersGrouped;
import com.example.demo.itemOrders.service.OrdersService; import com.example.demo.itemOrders.service.OrdersService;
import com.example.demo.itemProducts.model.ProductsEntity; import com.example.demo.itemProducts.model.ProductsEntity;
import com.example.demo.itemProducts.service.ProductsService; import com.example.demo.itemProducts.service.ProductsService;
@ -38,9 +37,6 @@ public class OrdersController {
private OrdersDto toDto(OrdersEntity entity) { private OrdersDto toDto(OrdersEntity entity) {
return modelMapper.map(entity, OrdersDto.class); return modelMapper.map(entity, OrdersDto.class);
} }
private OrdersGroupedDto toGroupedDto(OrdersGrouped entity) {
return modelMapper.map(entity, OrdersGroupedDto.class);
}
private OrdersEntity toEntity(OrdersDto dto) { private OrdersEntity toEntity(OrdersDto dto) {
final OrdersEntity entity = modelMapper.map(dto, OrdersEntity.class); final OrdersEntity entity = modelMapper.map(dto, OrdersEntity.class);
entity.setProduct(productsService.get(dto.getProductId())); entity.setProduct(productsService.get(dto.getProductId()));
@ -48,8 +44,8 @@ public class OrdersController {
} }
@GetMapping @GetMapping
public List<OrdersDto> getAll(@PathVariable(name = "user") Long userId, @RequestParam(name = "typeId", defaultValue = "0") ProductsEntity product) { public List<OrdersDto> getAll(@PathVariable(name = "user") Long userId, @RequestParam(name = "productId", defaultValue = "0") Long productId) {
return ordersService.getAll(userId, categoryId).stream().map(this::toDto).toList(); return ordersService.getAll(userId, productId).stream().map(this::toDto).toList();
} }
@GetMapping("/{id}") @GetMapping("/{id}")
@ -71,9 +67,4 @@ public class OrdersController {
public OrdersDto delete(@PathVariable(name = "user") Long userId, @PathVariable(name = "id") Long id) { public OrdersDto delete(@PathVariable(name = "user") Long userId, @PathVariable(name = "id") Long id) {
return toDto(ordersService.delete(userId, id)); return toDto(ordersService.delete(userId, id));
} }
@GetMapping("/total")
public List<OrdersGroupedDto> getMethodName(@PathVariable(name = "user") Long userId) {
return ordersService.getTotal(userId).stream().map(this::toGroupedDto).toList();
}
} }

View File

@ -1,32 +0,0 @@
package com.example.demo.itemOrders.api;
public class OrdersGroupedDto {
private Long categoryId;
private Long totalSum;
private Integer totalCount;
public Long getCategoryId() {
return categoryId;
}
public void setCategoryId(Long categoryId) {
this.categoryId = categoryId;
}
public Long getTotalSum() {
return totalSum;
}
public void setTotalSum(Long totalSum) {
this.totalSum = totalSum;
}
public Integer getTotalCount() {
return totalCount;
}
public void setTotalCount(Integer totalCount) {
this.totalCount = totalCount;
}
}

View File

@ -1,11 +0,0 @@
package com.example.demo.itemOrders.model;
import com.example.demo.itemCategories.model.CategoriesEntity;
public interface OrdersGrouped {
CategoriesEntity getCategory();
double getTotalSum();
int getTotalCount();
}

View File

@ -10,4 +10,6 @@ public interface OrdersRepository extends CrudRepository<OrdersEntity, Long> {
List<OrdersEntity> findByUserId(long userId); List<OrdersEntity> findByUserId(long userId);
List<OrdersEntity> findByUserIdAndProductId(long userId, long productId);
} }

View File

@ -1,14 +1,14 @@
package com.example.demo.itemOrders.service; package com.example.demo.itemOrders.service;
import java.util.List; import java.util.List;
import java.util.Objects;
import java.util.Optional;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.example.demo.core.error.NotFoundException; import com.example.demo.core.error.NotFoundException;
import com.example.demo.itemOrders.model.OrdersEntity; import com.example.demo.itemOrders.model.OrdersEntity;
import com.example.demo.itemOrders.repository.OrdersRepository; import com.example.demo.itemOrders.repository.OrdersRepository;
import com.example.demo.itemUsers.model.UsersEntity;
import com.example.demo.itemUsers.service.UsersService; import com.example.demo.itemUsers.service.UsersService;
@Service @Service
@ -21,31 +21,43 @@ public class OrdersService {
this.userService = userService; this.userService = userService;
} }
public List<OrdersEntity> getAll(Long productId) { @Transactional(readOnly = true)
if (Objects.equals(productId, 0L)) { public List<OrdersEntity> getAll(long userId, Long productId) {
return repository.getAll(); userService.get(userId);
if (productId <= 0L) {
return repository.findByUserId(userId);
} else {
return repository.findByUserIdAndProductId(userId, productId);
} }
return repository.getAll().stream().filter(item -> item.getProduct().getId().equals(productId)).toList(); }
@Transactional(readOnly = true)
public OrdersEntity get(long userId, Long id) {
userService.get(userId);
return repository.findOneByUserIdAndId(userId, id).orElseThrow(() -> new NotFoundException(OrdersEntity.class, id));
} }
public OrdersEntity get(Long id) { public OrdersEntity create(long userId, OrdersEntity entity) {
return Optional.ofNullable(repository.get(id)).orElseThrow(() -> new NotFoundException(id)); if (entity == null) {
throw new IllegalArgumentException("Entity is null");
}
final UsersEntity existsUser = userService.get(userId);
entity.setUser(existsUser);
return repository.save(entity);
} }
public OrdersEntity create(OrdersEntity entity) { public OrdersEntity update(long userId, Long id, OrdersEntity entity) {
return repository.create(entity); userService.get(userId);
} final OrdersEntity existsEntity = get(userId, id);
public OrdersEntity update(Long id, OrdersEntity entity) {
final OrdersEntity existsEntity = get(id);
existsEntity.setProduct(entity.getProduct()); existsEntity.setProduct(entity.getProduct());
existsEntity.setCount(entity.getCount()); existsEntity.setCount(entity.getCount());
existsEntity.setDate(entity.getDate()); existsEntity.setDate(entity.getDate());
return repository.update(existsEntity); return repository.save(existsEntity);
} }
public OrdersEntity delete(Long id) { public OrdersEntity delete(long userId, Long id) {
final OrdersEntity existsEntity = get(id); userService.get(userId);
return repository.delete(existsEntity); final OrdersEntity existsEntity = get(userId, id);
repository.delete(existsEntity);
return existsEntity;
} }
} }