feat: added all mappers

This commit is contained in:
Petr Rudichev 2025-02-19 11:26:14 +03:00
parent 1feca4fa25
commit ff360be1eb
7 changed files with 228 additions and 1 deletions

View File

@ -1,4 +1,48 @@
package com.example.nto.dto.mappers;
import com.example.nto.domain.entity.Office;
import com.example.nto.dto.entity.OfficeDTO;
import com.example.nto.dto.mappers.employee.EmployeeItemMapper;
import lombok.experimental.UtilityClass;
import java.util.stream.Collectors;
@UtilityClass
public class OfficeMapper {
public static OfficeDTO convertToDTO(Office office) {
OfficeDTO officeDTO = new OfficeDTO();
officeDTO.setId(office.getId());
officeDTO.setName(office.getName());
officeDTO.setDescription(office.getDescription());
officeDTO.setAddress(office.getAddress());
officeDTO.setLatitude(office.getLatitude());
officeDTO.setLongitude(office.getLongitude());
officeDTO.setLinkLogo(office.getLinkLogo());
officeDTO.setTelephone(office.getTelephone());
officeDTO.setEmail(office.getEmail());
officeDTO.setEmployees(office.getEmployeeList().stream().map(EmployeeItemMapper::convertToDTO).collect(Collectors.toList()));
officeDTO.setTerminals(office.getTerminals().stream().map(TerminalMapper::convertToDTO).collect(Collectors.toList()));
return officeDTO;
}
public static Office convertFromDTO(OfficeDTO officeDTO) {
Office office = new Office();
office.setId(officeDTO.getId());
office.setName(officeDTO.getName());
office.setDescription(officeDTO.getDescription());
office.setAddress(officeDTO.getAddress());
office.setLatitude(officeDTO.getLatitude());
office.setLongitude(officeDTO.getLongitude());
office.setLinkLogo(officeDTO.getLinkLogo());
office.setTelephone(officeDTO.getTelephone());
office.setEmail(officeDTO.getEmail());
// todo: Проверить, возможно, здесь нужно office.setEmployeeList() и office.setTerminals()
return office;
}
}

View File

@ -1,4 +1,21 @@
package com.example.nto.dto.mappers;
import com.example.nto.domain.entity.Position;
import com.example.nto.dto.entity.PositionDTO;
import com.example.nto.dto.mappers.employee.EmployeeItemMapper;
import lombok.experimental.UtilityClass;
import java.util.stream.Collectors;
@UtilityClass
public class PositionMapper {
}
public static PositionDTO convertToDTO(Position position) {
PositionDTO positionDTO = new PositionDTO();
positionDTO.setId(position.getId());
positionDTO.setName(position.getName());
positionDTO.setEmployeeItemDTOList(position.getEmployees().stream().map(EmployeeItemMapper::convertToDTO).collect(Collectors.toList()));
return positionDTO;
}
}

View File

@ -1,4 +1,29 @@
package com.example.nto.dto.mappers;
import com.example.nto.domain.entity.Office;
import com.example.nto.domain.entity.Terminal;
import com.example.nto.dto.entity.TerminalDTO;
import lombok.experimental.UtilityClass;
@UtilityClass
public class TerminalMapper {
public static TerminalDTO convertToDTO(Terminal terminal) {
TerminalDTO terminalDTO = new TerminalDTO();
terminalDTO.setId(terminal.getId());
terminalDTO.setName(terminal.getName());
terminalDTO.setCode(terminal.getCode());
terminalDTO.setOfficeName(terminal.getOffice().getName());
return terminalDTO;
}
public static Terminal convertFromDTO(TerminalDTO terminalDTO, Office office) {
Terminal terminal = new Terminal();
terminal.setName(terminalDTO.getName());
terminal.setOffice(office);
return terminal;
}
}

View File

@ -1,4 +1,42 @@
package com.example.nto.dto.mappers;
import com.example.nto.domain.entity.Employee;
import com.example.nto.domain.entity.Passage;
import com.example.nto.domain.entity.Terminal;
import com.example.nto.domain.entity.Visit;
import com.example.nto.dto.entity.VisitDTO;
import com.example.nto.utils.Utils;
import lombok.experimental.UtilityClass;
import java.time.LocalDateTime;
@UtilityClass
public class VisitMapper {
public static VisitDTO convertToDTO(Visit visit) {
VisitDTO visitDTO = new VisitDTO();
visitDTO.setId(visit.getId());
visitDTO.setStartVisit(visit.getStartVisit().toString());
visitDTO.setEndVisit(visit.getEndVisit().toString());
visitDTO.setFinished(visit.isFinished());
visitDTO.setDurationVisit(Utils.period(visit.getStartVisit(), visit.getEndVisit()).toString());
visitDTO.setOfficeName(visit.getStartTerminal().getOffice().getName());
visitDTO.setTypePassage(visit.getPassage().getPassageName());
return visitDTO;
}
public static Visit convertFromDTO(Terminal terminal, Employee employee, Passage passage) {
Visit visit = new Visit();
visit.setEmployee(employee);
visit.setStartVisit(LocalDateTime.now());
visit.setFinished(false);
visit.setStartTerminal(terminal);
visit.setPassage(passage);
return visit;
}
}

View File

@ -26,6 +26,7 @@ public class EmployeeCreateMapper {
employee.setPosition(position);
employee.setRole(role);
employee.setProfileImageUrl(Utils.getRandomUrlProfileImage());
employee.setBlocked(false);
return employee;
}

View File

@ -1,4 +1,31 @@
package com.example.nto.dto.mappers.employee;
import com.example.nto.domain.entity.Employee;
import com.example.nto.domain.entity.Visit;
import com.example.nto.dto.entity.employee.EmployeeItemDTO;
import com.example.nto.utils.Utils;
import lombok.experimental.UtilityClass;
import java.util.List;
@UtilityClass
public class EmployeeItemMapper {
public static EmployeeItemDTO convertToDTO(Employee employee) {
EmployeeItemDTO employeeItemDTO = new EmployeeItemDTO();
employeeItemDTO.setId(employee.getId());
employeeItemDTO.setName(employee.getName());
employeeItemDTO.setSurname(employee.getSurname());
employeeItemDTO.setPatronymic(employee.getPatronymic());
employeeItemDTO.setProfileImageUrl(employee.getProfileImageUrl());
employeeItemDTO.setOfficeName(employee.getOffice().getName());
employeeItemDTO.setPosition(employee.getPosition().getName());
employeeItemDTO.setBlocked(employee.isBlocked());
// todo: Протестировать работу!
List<Visit> visitsLast30Days = Utils.filterDateLast30Days(employee.getVisits());
employeeItemDTO.setVisitStatus(visitsLast30Days.stream().anyMatch(visit -> !visit.isFinished()));
return employeeItemDTO;
}
}

View File

@ -1,4 +1,79 @@
package com.example.nto.dto.mappers.employee;
import com.example.nto.domain.entity.*;
import com.example.nto.dto.entity.employee.EmployeeDTO;
import com.example.nto.utils.Utils;
import lombok.experimental.UtilityClass;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
@UtilityClass
public class EmployeeMapper {
public static EmployeeDTO convertToDTO(Employee employee) {
EmployeeDTO employeeDTO = new EmployeeDTO();
employeeDTO.setId(employee.getId());
employeeDTO.setName(employee.getName());
employeeDTO.setSurname(employee.getSurname());
employeeDTO.setPatronymic(employee.getPatronymic());
employeeDTO.setTelephone(employee.getTelephone());
employeeDTO.setEmail(employee.getEmail());
employeeDTO.setOfficeId(employee.getOffice().getId());
employeeDTO.setOfficeName(employee.getOffice().getName());
employeeDTO.setOfficeImageUrl(employee.getOffice().getLinkLogo());
employeeDTO.setPosition(employee.getPosition().getName());
employeeDTO.setRole(employee.getRole().getRoleName());
employeeDTO.setRole(employee.getProfileImageUrl());
employeeDTO.setBlocked(employee.isBlocked());
List<Visit> visitsLast30Days = Utils.filterDateLast30Days(employee.getVisits());
boolean visitStatus = false;
String startVisitDateTime = null;
String currentOfficeName = null;
List<Long> visitsIdLast30Days = new ArrayList<>(visitsLast30Days.size());
List<List<LocalDateTime>> periods = new ArrayList<>(visitsLast30Days.size());
for (Visit visit : visitsLast30Days) {
if (!visit.isFinished()) {
if (visitStatus) throw new IllegalStateException("У работника два посещения одновременно!");
visitStatus = true;
startVisitDateTime = visit.getStartVisit().toString();
currentOfficeName = visit.getStartTerminal().getOffice().getName();
}
visitsIdLast30Days.add(visit.getId());
periods.add(List.of(visit.getStartVisit(), visit.getEndVisit()));
}
employeeDTO.setVisitStatus(visitStatus);
employeeDTO.setStartVisitDateTime(startVisitDateTime);
employeeDTO.setCurrentOfficeName(currentOfficeName);
employeeDTO.setVisitsIdLast30Days(visitsIdLast30Days);
employeeDTO.setTotalTimeVisitsLast30Days(Utils.periods(periods));
employeeDTO.setCreateAt(employee.getCreatedAt().toString());
return employeeDTO;
}
public static Employee convertFromDTO(EmployeeDTO employeeDTO, 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.setOffice(office);
employee.setPosition(position);
employee.setRole(role);
employee.setProfileImageUrl(employeeDTO.getProfileImageUrl());
employee.setBlocked(employeeDTO.isBlocked());
return employee;
}
}