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..eba22bb 100644 --- a/src/main/java/mate/academy/mapstruct/mapper/GroupMapper.java +++ b/src/main/java/mate/academy/mapstruct/mapper/GroupMapper.java @@ -1,11 +1,25 @@ 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.Mapping; +import org.mapstruct.Named; +@Mapper(config = MapperConfig.class) public interface GroupMapper { GroupDto toDto(Group group); + @Mapping(target = "id", ignore = true) 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 853e6b1..584f3c4 100644 --- a/src/main/java/mate/academy/mapstruct/mapper/StudentMapper.java +++ b/src/main/java/mate/academy/mapstruct/mapper/StudentMapper.java @@ -1,14 +1,47 @@ 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(source = "group.id", target = "groupId") + @Mapping(target = "subjectIds", ignore = true) StudentDto toDto(Student student); + @AfterMapping + default void setSubjectIds(@MappingTarget StudentDto studentDto, Student student) { + List subjects = student.getSubjects() + .stream() + .map(Subject::getId) + .toList(); + studentDto.setSubjectIds(subjects); + } + + @Mapping(source = "group.id", target = "groupId") StudentWithoutSubjectsDto toStudentWithoutSubjectsDto(Student student); + @Mapping(target = "group", source = "groupId", qualifiedByName = "groupById") + @Mapping(target = "subjects", ignore = true) + @Mapping(target = "id", ignore = true) + @Mapping(target = "socialSecurityNumber", 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);