From 3fab35882356d35eb1fdd602a2dfc7e998264f3f Mon Sep 17 00:00:00 2001 From: Petr Rudichev Date: Wed, 19 Feb 2025 10:31:58 +0300 Subject: [PATCH] feat: add func get random image --- .../employee/EmployeeCreateMapper.java | 28 +++++++++++++++++++ .../java/com/example/nto/utils/Utils.java | 9 ++++-- 2 files changed, 35 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/example/nto/dto/mappers/employee/EmployeeCreateMapper.java b/src/main/java/com/example/nto/dto/mappers/employee/EmployeeCreateMapper.java index 408eae4..4a2098d 100644 --- a/src/main/java/com/example/nto/dto/mappers/employee/EmployeeCreateMapper.java +++ b/src/main/java/com/example/nto/dto/mappers/employee/EmployeeCreateMapper.java @@ -1,4 +1,32 @@ package com.example.nto.dto.mappers.employee; + import com.example.nto.domain.entity.Employee; +import com.example.nto.domain.entity.Office; +import com.example.nto.domain.entity.Position; +import com.example.nto.domain.entity.Role; +import com.example.nto.dto.entity.employee.EmployeeCreateDTO; +import com.example.nto.utils.Utils; +import lombok.experimental.UtilityClass; + +@UtilityClass public class EmployeeCreateMapper { + public static Employee convertFromDTO( + EmployeeCreateDTO employeeDTO, String password, Office office, + Position position, Role role + ) { + Employee employee = new Employee(); + + employee.setName(employeeDTO.getName()); + employee.setSurname(employeeDTO.getSurname()); + employee.setPatronymic(employeeDTO.getPatronymic()); + employee.setTelephone(employeeDTO.getTelephone()); + employee.setEmail(employeeDTO.getEmail()); + employee.setPassword(password); + employee.setOffice(office); + employee.setPosition(position); + employee.setRole(role); + employee.setProfileImageUrl(Utils.getRandomUrlProfileImage()); + + return employee; + } } diff --git a/src/main/java/com/example/nto/utils/Utils.java b/src/main/java/com/example/nto/utils/Utils.java index b8bf742..10a4f48 100644 --- a/src/main/java/com/example/nto/utils/Utils.java +++ b/src/main/java/com/example/nto/utils/Utils.java @@ -4,9 +4,8 @@ import lombok.experimental.UtilityClass; import java.time.*; import java.time.format.DateTimeFormatter; -import java.time.temporal.ChronoUnit; -import java.util.ArrayList; import java.util.List; +import java.util.Random; import java.util.UUID; @UtilityClass @@ -52,6 +51,12 @@ public class Utils { return hours; } + public static String getRandomUrlProfileImage() { + int max = 26, min = 1; + String fileName = profileFileName(new Random().nextInt(max - min + 1) + min) + ".jpg"; + return storageConfig.getS3Endpoint() + "/" + storageConfig.getBucketName() + "/standard/" + fileName; + } + public static String convertDistance(float distance) { if (distance > 1000) return String.format("%.1f", distance / 1000) + " км"; else return String.format("%.1f", distance) + " м";