new commit
This commit is contained in:
parent
6b9a2025ed
commit
cd388cff5c
@ -15,7 +15,6 @@ import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import com.example.demo.core.configuration.Constants;
|
||||
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.itemProducts.model.ProductsEntity;
|
||||
import com.example.demo.itemProducts.service.ProductsService;
|
||||
@ -38,9 +37,6 @@ public class OrdersController {
|
||||
private OrdersDto toDto(OrdersEntity entity) {
|
||||
return modelMapper.map(entity, OrdersDto.class);
|
||||
}
|
||||
private OrdersGroupedDto toGroupedDto(OrdersGrouped entity) {
|
||||
return modelMapper.map(entity, OrdersGroupedDto.class);
|
||||
}
|
||||
private OrdersEntity toEntity(OrdersDto dto) {
|
||||
final OrdersEntity entity = modelMapper.map(dto, OrdersEntity.class);
|
||||
entity.setProduct(productsService.get(dto.getProductId()));
|
||||
@ -48,8 +44,8 @@ public class OrdersController {
|
||||
}
|
||||
|
||||
@GetMapping
|
||||
public List<OrdersDto> getAll(@PathVariable(name = "user") Long userId, @RequestParam(name = "typeId", defaultValue = "0") ProductsEntity product) {
|
||||
return ordersService.getAll(userId, categoryId).stream().map(this::toDto).toList();
|
||||
public List<OrdersDto> getAll(@PathVariable(name = "user") Long userId, @RequestParam(name = "productId", defaultValue = "0") Long productId) {
|
||||
return ordersService.getAll(userId, productId).stream().map(this::toDto).toList();
|
||||
}
|
||||
|
||||
@GetMapping("/{id}")
|
||||
@ -71,9 +67,4 @@ public class OrdersController {
|
||||
public OrdersDto delete(@PathVariable(name = "user") Long userId, @PathVariable(name = "id") Long 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();
|
||||
}
|
||||
}
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
@ -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();
|
||||
}
|
@ -10,4 +10,6 @@ public interface OrdersRepository extends CrudRepository<OrdersEntity, Long> {
|
||||
|
||||
List<OrdersEntity> findByUserId(long userId);
|
||||
|
||||
List<OrdersEntity> findByUserIdAndProductId(long userId, long productId);
|
||||
|
||||
}
|
||||
|
@ -1,14 +1,14 @@
|
||||
package com.example.demo.itemOrders.service;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
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.itemOrders.model.OrdersEntity;
|
||||
import com.example.demo.itemOrders.repository.OrdersRepository;
|
||||
import com.example.demo.itemUsers.model.UsersEntity;
|
||||
import com.example.demo.itemUsers.service.UsersService;
|
||||
|
||||
@Service
|
||||
@ -21,31 +21,43 @@ public class OrdersService {
|
||||
this.userService = userService;
|
||||
}
|
||||
|
||||
public List<OrdersEntity> getAll(Long productId) {
|
||||
if (Objects.equals(productId, 0L)) {
|
||||
return repository.getAll();
|
||||
@Transactional(readOnly = true)
|
||||
public List<OrdersEntity> getAll(long userId, Long productId) {
|
||||
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) {
|
||||
return Optional.ofNullable(repository.get(id)).orElseThrow(() -> new NotFoundException(id));
|
||||
public OrdersEntity create(long userId, OrdersEntity entity) {
|
||||
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) {
|
||||
return repository.create(entity);
|
||||
}
|
||||
|
||||
public OrdersEntity update(Long id, OrdersEntity entity) {
|
||||
final OrdersEntity existsEntity = get(id);
|
||||
public OrdersEntity update(long userId, Long id, OrdersEntity entity) {
|
||||
userService.get(userId);
|
||||
final OrdersEntity existsEntity = get(userId, id);
|
||||
existsEntity.setProduct(entity.getProduct());
|
||||
existsEntity.setCount(entity.getCount());
|
||||
existsEntity.setDate(entity.getDate());
|
||||
return repository.update(existsEntity);
|
||||
return repository.save(existsEntity);
|
||||
}
|
||||
|
||||
public OrdersEntity delete(Long id) {
|
||||
final OrdersEntity existsEntity = get(id);
|
||||
return repository.delete(existsEntity);
|
||||
public OrdersEntity delete(long userId, Long id) {
|
||||
userService.get(userId);
|
||||
final OrdersEntity existsEntity = get(userId, id);
|
||||
repository.delete(existsEntity);
|
||||
return existsEntity;
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user