From d61fc4b6971751845c839397c6d295acbd3bef47 Mon Sep 17 00:00:00 2001 From: Natalia Kurhanska Date: Wed, 15 Nov 2023 00:13:27 +0200 Subject: [PATCH] impl mappers --- .../mapstruct/config/MapperConfig.java | 13 ++++++++ .../academy/mapstruct/mapper/GroupMapper.java | 12 ++++++++ .../mapstruct/mapper/StudentMapper.java | 30 ++++++++++++++++++- .../mapstruct/mapper/SubjectMapper.java | 3 ++ 4 files changed, 57 insertions(+), 1 deletion(-) create mode 100644 src/main/java/mate/academy/mapstruct/config/MapperConfig.java diff --git a/src/main/java/mate/academy/mapstruct/config/MapperConfig.java b/src/main/java/mate/academy/mapstruct/config/MapperConfig.java new file mode 100644 index 0000000..6b9d1cc --- /dev/null +++ b/src/main/java/mate/academy/mapstruct/config/MapperConfig.java @@ -0,0 +1,13 @@ +package mate.academy.mapstruct.config; + +import org.mapstruct.InjectionStrategy; +import org.mapstruct.NullValueCheckStrategy; + +@org.mapstruct.MapperConfig( + componentModel = "spring", + injectionStrategy = InjectionStrategy.CONSTRUCTOR, + nullValueCheckStrategy = NullValueCheckStrategy.ALWAYS, + implementationPackage = ".impl" +) +public class MapperConfig { +} diff --git a/src/main/java/mate/academy/mapstruct/mapper/GroupMapper.java b/src/main/java/mate/academy/mapstruct/mapper/GroupMapper.java index d60c9df..d0e414b 100644 --- a/src/main/java/mate/academy/mapstruct/mapper/GroupMapper.java +++ b/src/main/java/mate/academy/mapstruct/mapper/GroupMapper.java @@ -1,11 +1,23 @@ package mate.academy.mapstruct.mapper; +import java.util.Optional; +import mate.academy.mapstruct.config.MapperConfig; import mate.academy.mapstruct.dto.group.CreateGroupRequestDto; import mate.academy.mapstruct.dto.group.GroupDto; import mate.academy.mapstruct.model.Group; +import org.mapstruct.Mapper; +import org.mapstruct.Named; +@Mapper(config = MapperConfig.class) public interface GroupMapper { GroupDto toDto(Group group); Group toModel(CreateGroupRequestDto requestDto); + + @Named("groupById") + default Group groupById(Long id) { + return Optional.ofNullable(id) + .map(Group::new) + .orElse(null); + } } diff --git a/src/main/java/mate/academy/mapstruct/mapper/StudentMapper.java b/src/main/java/mate/academy/mapstruct/mapper/StudentMapper.java index 6ca53e1..97c16ab 100644 --- a/src/main/java/mate/academy/mapstruct/mapper/StudentMapper.java +++ b/src/main/java/mate/academy/mapstruct/mapper/StudentMapper.java @@ -1,15 +1,43 @@ package mate.academy.mapstruct.mapper; +import java.util.List; +import mate.academy.mapstruct.config.MapperConfig; import mate.academy.mapstruct.dto.student.CreateStudentRequestDto; import mate.academy.mapstruct.dto.student.StudentDto; import mate.academy.mapstruct.dto.student.StudentWithoutSubjectsDto; import mate.academy.mapstruct.model.Student; +import mate.academy.mapstruct.model.Subject; +import org.mapstruct.AfterMapping; +import org.mapstruct.Mapper; +import org.mapstruct.Mapping; +import org.mapstruct.MappingTarget; +@Mapper(config = MapperConfig.class, uses = GroupMapper.class) public interface StudentMapper { + @Mapping(target = "groupId", source = "group.id") + @Mapping(target = "subjectIds", ignore = true) StudentDto toDto(Student student); - StudentWithoutSubjectsDto toEmployeeWithoutSubjectsDto(Student student); + @AfterMapping + default void setSubjectIds(@MappingTarget StudentDto studentDto, Student student) { + List subjectIds = student.getSubjects().stream() + .map(Subject::getId) + .toList(); + studentDto.setSubjectIds(subjectIds); + } + @Mapping(target = "groupId", source = "group.id") + StudentWithoutSubjectsDto toEmployeeWithoutSubjectsDto(Student student); + @Mapping(target = "group", source = "groupId", qualifiedByName = "groupById") + @Mapping(target = "subjects", ignore = true) Student toModel(CreateStudentRequestDto requestDto); + + @AfterMapping + default void setSubjects(@MappingTarget Student student, CreateStudentRequestDto requestDto) { + List subjects = requestDto.subjects().stream() + .map(Subject::new) + .toList(); + student.setSubjects(subjects); + } } diff --git a/src/main/java/mate/academy/mapstruct/mapper/SubjectMapper.java b/src/main/java/mate/academy/mapstruct/mapper/SubjectMapper.java index 2906e78..7f6ffc0 100644 --- a/src/main/java/mate/academy/mapstruct/mapper/SubjectMapper.java +++ b/src/main/java/mate/academy/mapstruct/mapper/SubjectMapper.java @@ -1,9 +1,12 @@ package mate.academy.mapstruct.mapper; +import mate.academy.mapstruct.config.MapperConfig; import mate.academy.mapstruct.dto.subject.CreateSubjectRequestDto; import mate.academy.mapstruct.dto.subject.SubjectDto; import mate.academy.mapstruct.model.Subject; +import org.mapstruct.Mapper; +@Mapper(config = MapperConfig.class) public interface SubjectMapper { SubjectDto toDto(Subject subject);