From 6605d90d194f10a22bcfb37227b4a3be2520bbac Mon Sep 17 00:00:00 2001 From: Jakub Date: Sun, 1 Sep 2024 18:05:25 +0100 Subject: [PATCH] First attempt --- .../mapstruct/config/MapperConfig.java | 13 ++++++++++ .../academy/mapstruct/mapper/GroupMapper.java | 14 ++++++++++ .../mapstruct/mapper/StudentMapper.java | 12 +++++++++ .../mapstruct/mapper/SubjectMapper.java | 26 +++++++++++++++++++ src/main/resources/application.properties | 4 +-- 5 files changed, 67 insertions(+), 2 deletions(-) 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..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..b24c783 100644 --- a/src/main/java/mate/academy/mapstruct/mapper/StudentMapper.java +++ b/src/main/java/mate/academy/mapstruct/mapper/StudentMapper.java @@ -1,14 +1,26 @@ package mate.academy.mapstruct.mapper; +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 org.mapstruct.Mapper; +import org.mapstruct.Mapping; +@Mapper(config = MapperConfig.class, uses = {GroupMapper.class, SubjectMapper.class}) public interface StudentMapper { + + @Mapping(source = "group.id", target = "groupId") + @Mapping(source = "subjects", target = "subjectIds", qualifiedByName = "subjectsById") StudentDto toDto(Student student); + @Mapping(target = "groupId", source = "group.id") StudentWithoutSubjectsDto toStudentWithoutSubjectsDto(Student student); + @Mapping(target = "id", ignore = true) + @Mapping(target = "group", source = "groupId", qualifiedByName = "groupById") + @Mapping(target = "subjects", source = "subjects", qualifiedByName = "subjectIdToSubject") + @Mapping(target = "socialSecurityNumber", ignore = true) Student toModel(CreateStudentRequestDto requestDto); } diff --git a/src/main/java/mate/academy/mapstruct/mapper/SubjectMapper.java b/src/main/java/mate/academy/mapstruct/mapper/SubjectMapper.java index 2906e78..0f8d0ab 100644 --- a/src/main/java/mate/academy/mapstruct/mapper/SubjectMapper.java +++ b/src/main/java/mate/academy/mapstruct/mapper/SubjectMapper.java @@ -1,11 +1,37 @@ package mate.academy.mapstruct.mapper; +import java.util.List; +import java.util.stream.Collectors; +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; +import org.mapstruct.Mapping; +import org.mapstruct.Named; +@Mapper(config = MapperConfig.class) public interface SubjectMapper { SubjectDto toDto(Subject subject); + @Mapping(target = "id", ignore = true) Subject toModel(CreateSubjectRequestDto requestDto); + + @Named("subjectsById") + default List subjectsById(List subjects) { + return subjects == null ? null : subjects.stream() + .map(Subject::getId) + .collect(Collectors.toList()); + } + + @Named("subjectIdToSubject") + default List subjectIdToSubject(List subjects) { + return subjects == null ? null : subjects.stream() + .map(id -> { + Subject subject = new Subject(); + subject.setId(id); + return subject; + }) + .collect(Collectors.toList()); + } } diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 9b8695a..2baeb96 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -1,6 +1,6 @@ spring.datasource.url=jdbc:mysql://localhost:3306/mapstruct?createDatabaseIfNotExist=true -spring.datasource.username= -spring.datasource.password= +spring.datasource.username=root +spring.datasource.password=Styczen10! spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver spring.jpa.hibernate.ddl-auto=create-drop spring.jpa.show-sql=true