use of com.ncedu.fooddelivery.api.v1.entities.User in project 2021-msk-food-delivery by netcracker-edu.
the class UserServiceTest method getUserDTOByIdSuccess.
@Test
public void getUserDTOByIdSuccess() {
Long userId = 1L;
User sheldonCooper = UserUtils.adminSheldonCooper(userId);
when(userRepoMock.findById(userId)).thenReturn(Optional.of(sheldonCooper));
UserInfoDTO resultDTO = userService.getUserDTOById(userId);
UserInfoDTO perfectDTO = UserUtils.createUserDTO(sheldonCooper);
assertEquals(perfectDTO, resultDTO);
verify(userRepoMock, times(1)).findById(userId);
}
use of com.ncedu.fooddelivery.api.v1.entities.User in project 2021-msk-food-delivery by netcracker-edu.
the class UserServiceTest method changePasswordSuccess.
@Test
public void changePasswordSuccess() {
Long userId = 1L;
User rajesh = UserUtils.clientRajeshKoothrappali(userId);
String oldPass = "password";
String newPass = "koothrappali";
PasswordChangeDTO passwordDTO = new PasswordChangeDTO();
passwordDTO.setNewPassword(newPass);
passwordDTO.setNewPasswordConfirm(newPass);
passwordDTO.setOldPassword(oldPass);
when(userRepoMock.save(any(User.class))).thenAnswer(invocation -> invocation.getArguments()[0]);
boolean result = userService.changePassword(rajesh, passwordDTO);
assertTrue(result);
verify(userRepoMock, times(1)).save(any(User.class));
}
use of com.ncedu.fooddelivery.api.v1.entities.User in project 2021-msk-food-delivery by netcracker-edu.
the class FileServiceImpl method replace.
@Override
public FileLinkDTO replace(MultipartFile newFile, File oldFile, User authedUser) {
boolean isAdminOrOwner = checkAdminOrOwner(oldFile, authedUser);
if (!isAdminOrOwner) {
log.error(authedUser.getEmail() + " not Admin and not Owner of the file " + oldFile.getId().toString());
throw new CustomAccessDeniedException();
}
try {
String originalFileName = newFile.getOriginalFilename();
String newFileName = getFileNameWithoutExt(originalFileName);
FileType fileType = getFileType(originalFileName);
Long fileSize = newFile.getSize();
Path fullPathToFile = createFullPathToFile(oldFile.getId());
if (authedUser.getRole() == Role.CLIENT || authedUser.getRole() == Role.COURIER) {
InputStream is = newFile.getInputStream();
BufferedImage bufferedImage = ImageIO.read(is);
boolean isExceedImageSize = checkExceedImageSize(bufferedImage);
if (isExceedImageSize) {
bufferedImage = resizeImage(bufferedImage);
}
if (fileType == FileType.PNG) {
bufferedImage = convertPNGtoJPG(bufferedImage);
}
ImageIO.write(bufferedImage, "jpg", fullPathToFile.toFile());
fileSize = Files.size(fullPathToFile);
fileType = FileType.JPEG;
} else {
Files.copy(newFile.getInputStream(), fullPathToFile, StandardCopyOption.REPLACE_EXISTING);
}
oldFile.setName(newFileName);
oldFile.setType(fileType);
oldFile.setSize(fileSize);
oldFile.setUploadDate(Timestamp.valueOf(LocalDateTime.now()));
fileRepo.save(oldFile);
String fileLink = createFileLink(oldFile.getId());
return new FileLinkDTO(fileLink, oldFile.getId().toString());
} catch (BadFileExtensionException e) {
throw e;
} catch (Exception e) {
log.error(e.getMessage(), e);
throw new FileStorageException();
}
}
use of com.ncedu.fooddelivery.api.v1.entities.User in project 2021-msk-food-delivery by netcracker-edu.
the class FileServiceImpl method delete.
@Override
public void delete(File file, User authedUser) {
boolean isAdminOrOwner = checkAdminOrOwner(file, authedUser);
if (!isAdminOrOwner) {
log.error(authedUser.getEmail() + " not Admin and not Owner of the file " + file.getId().toString());
throw new CustomAccessDeniedException();
}
try {
fileRepo.delete(file);
Path fullFilePath = createFullPathToFile(file.getId());
Files.deleteIfExists(fullFilePath);
Path fileParentDirPath = fullFilePath.getParent();
log.debug("PARENT DIR PATH: " + fileParentDirPath + "\n");
boolean isParentDirEmpty = checkParentDirEmpty(fileParentDirPath);
if (isParentDirEmpty) {
Files.delete(fileParentDirPath);
}
} catch (IOException e) {
throw new FileDeleteException();
}
}
use of com.ncedu.fooddelivery.api.v1.entities.User in project 2021-msk-food-delivery by netcracker-edu.
the class OrderServiceImpl1 method buildOrder.
private Order buildOrder(User user, Geometry coords, CreateOrderDTO dto, Map<Long, Double> productPosPriceMap, Map<Long, Integer> productPosAmountMap) {
Order order = new Order();
order.setDiscount(dto.getDiscount());
order.setHighDemandCoeff(dto.getHighDemandCoeff());
order.setOverallCost(dto.getOverallCost());
order.setClient(user.getClient());
order.setWarehouse(warehouseService.findById(dto.getWarehouseId()));
order.setAddress(dto.getAddress());
order.setCoordinates(coords);
order.setStatus(OrderStatus.CREATED);
order.setDateStart(LocalDateTime.now());
order = orderRepo.save(order);
// adding records in DB table 'orders_product_positions'
for (Long productPositionId : productPosAmountMap.keySet()) {
orderProductPositionRepo.save(new OrderProductPosition(order, productPositionRepo.findById(productPositionId).get(), productPosAmountMap.get(productPositionId), productPosPriceMap.get(productPositionId)));
}
return orderRepo.save(order);
}
Aggregations