Skip to content

Commit

Permalink
added all needed
Browse files Browse the repository at this point in the history
  • Loading branch information
RostyslavKuzmych committed Nov 14, 2023
1 parent f8d9ea0 commit 7a02ffa
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 24 deletions.
13 changes: 13 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,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 {
}
12 changes: 11 additions & 1 deletion src/main/java/mate/academy/mapstruct/mapper/GroupMapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,19 @@
import mate.academy.mapstruct.model.Group;
import org.mapstruct.Mapper;

@Mapper(config = MapperConfig.class)
import java.util.Optional;

@Mapper(config = MapperC)
public interface GroupMapper {
GroupDto toDto(Group group);

Group toModel(CreateGroupRequestDto requestDto);

default Group getGroupById(Long id) {
return new Group(Optional.ofNullable(id).orElse(null));
}

default Long getIdFromGroup(Group group) {
return Optional.ofNullable(group.getId()).orElse(null);
}
}
25 changes: 2 additions & 23 deletions src/main/java/mate/academy/mapstruct/mapper/StudentMapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,41 +3,20 @@
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.Group;
import mate.academy.mapstruct.model.Student;
import mate.academy.mapstruct.model.Subject;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;

import java.util.List;
import java.util.Optional;

@Mapper(config = MapperConfig.class)
@Mapper(config = MapperConfig.class, uses = {SubjectMapper.class, GroupMapper.class})
public interface StudentMapper {
@Mapping(target = "groupId", source = "group", qualifiedByName = "getIdFromGroup")
@Mapping(target = "subjectIds", source = "subjects", qualifiedByName = "getIdsFromSubjects")
StudentDto toDto(Student student);

@Mapping(target = "groupId", source = "group", qualifiedByName = "getId")
@Mapping(target = "groupId", source = "group", qualifiedByName = "getIdFromGroup")
StudentWithoutSubjectsDto toEmployeeWithoutSubjectsDto(Student student);

@Mapping(target = "group", source = "groupId", qualifiedByName = "getGroupById")
@Mapping(target = "subjects", source = "subjects", qualifiedByName = "getSubjectsByIds")
Student toModel(CreateStudentRequestDto requestDto);

default Long getIdFromGroup(Group group) {
return Optional.ofNullable(group.getId()).orElse(null);
}

default List<Long> getIdsFromSubjects(List<Subject> subjects) {
return subjects.stream().map(s -> s.getId()).toList();
}

default Group getGroupById(Long id) {
return Optional.
}

default List<Subject> getSubjectsByIds(List<Long> longList) {
return longList.stream().map(Subject::new).toList();
}
}
10 changes: 10 additions & 0 deletions src/main/java/mate/academy/mapstruct/mapper/SubjectMapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,19 @@
import mate.academy.mapstruct.model.Subject;
import org.mapstruct.Mapper;

import java.util.List;

@Mapper(config = MapperConfig.class)
public interface SubjectMapper {
SubjectDto toDto(Subject subject);

Subject toModel(CreateSubjectRequestDto requestDto);

default List<Subject> getSubjectsByIds(List<Long> longList) {
return longList.stream().map(Subject::new).toList();
}

default List<Long> getIdsFromSubjects(List<Subject> subjects) {
return subjects.stream().map(s -> s.getId()).toList();
}
}

0 comments on commit 7a02ffa

Please sign in to comment.