-
Notifications
You must be signed in to change notification settings - Fork 324
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6f62478
commit 37f9496
Showing
4 changed files
with
71 additions
and
1 deletion.
There are no files selected for viewing
13 changes: 13 additions & 0 deletions
13
src/main/java/mate/academy/mapstruct/config/MapperConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 = "<PACKAGE_NAME>.impl" | ||
) | ||
public class MapperConfig { | ||
} |
14 changes: 14 additions & 0 deletions
14
src/main/java/mate/academy/mapstruct/mapper/GroupMapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} |
31 changes: 30 additions & 1 deletion
31
src/main/java/mate/academy/mapstruct/mapper/StudentMapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,44 @@ | ||
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 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 = {SubjectMapper.class, 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) { | ||
studentDto.setSubjectIds(student.getSubjects().stream() | ||
.map(Subject::getId) | ||
.toList()); | ||
} | ||
|
||
@Mapping(target = "groupId", source = "group.id") | ||
StudentWithoutSubjectsDto toEmployeeWithoutSubjectsDto(Student student); | ||
|
||
@Mapping(target = "id", ignore = true) | ||
@Mapping(target = "socialSecurityNumber", ignore = true) | ||
@Mapping(target = "group", source = "groupId", qualifiedByName = "groupById") | ||
@Mapping(target = "subjects", source = "subjects", qualifiedByName = "subjectById") | ||
Student toModel(CreateStudentRequestDto requestDto); | ||
|
||
@AfterMapping | ||
default void setSubjects(@MappingTarget Student student, | ||
CreateStudentRequestDto request | ||
) { | ||
student.setSubjects(request.subjects().stream() | ||
.map(Subject::new) | ||
.toList()); | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
src/main/java/mate/academy/mapstruct/mapper/SubjectMapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,25 @@ | ||
package mate.academy.mapstruct.mapper; | ||
|
||
import java.util.Optional; | ||
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("subjectById") | ||
default Subject subjectById(Long id) { | ||
return Optional.ofNullable(id) | ||
.map(Subject::new) | ||
.orElse(null); | ||
} | ||
} |