Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added implementation to mappers. #171

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions src/main/java/mate/academy/mapstruct/config/MapperConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
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 {

}
12 changes: 12 additions & 0 deletions src/main/java/mate/academy/mapstruct/mapper/GroupMapper.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
39 changes: 39 additions & 0 deletions src/main/java/mate/academy/mapstruct/mapper/StudentMapper.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,53 @@
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;
import org.mapstruct.Mappings;

@Mapper(config = MapperConfig.class, uses = GroupMapper.class)
public interface StudentMapper {
@Mappings({
@Mapping(source = "group.id", target = "groupId"),
@Mapping(ignore = true, target = "subjectIds")
})
StudentDto toDto(Student student);

@Mappings({
@Mapping(source = "group.id", target = "groupId"),
})
StudentWithoutSubjectsDto toStudentWithoutSubjectsDto(Student student);

@Mappings({
@Mapping(target = "group", source = "groupId", qualifiedByName = "groupById"),
@Mapping(target = "subjects", ignore = true),
})
Student toModel(CreateStudentRequestDto requestDto);

@AfterMapping
default void setSubjectsToModel(CreateStudentRequestDto requestDto,
@MappingTarget Student student) {
List<Subject> subjectList = requestDto.subjects().stream()
.map(Subject::new)
.toList();

student.setSubjects(subjectList);
}

@AfterMapping
default void setSubjectsToDto(Student student, @MappingTarget StudentDto studentDto) {
List<Long> subjectIdsList = student.getSubjects().stream()
.map(Subject::getId)
.toList();

studentDto.setSubjectIds(subjectIdsList);
}
}
Original file line number Diff line number Diff line change
@@ -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);

Expand Down
3 changes: 3 additions & 0 deletions src/main/java/mate/academy/mapstruct/model/Group.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,7 @@ public class Group {
public Group(Long id) {
this.id = id;
}

public Group(String s) {
}
}
3 changes: 3 additions & 0 deletions src/main/java/mate/academy/mapstruct/model/Subject.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,7 @@ public class Subject {
public Subject(Long id) {
this.id = id;
}

public Subject(String s) {
}
}
Loading