Skip to content

Commit

Permalink
implementation mapstruct task, first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Stelmaschyk committed Jul 29, 2024
1 parent c4ce110 commit 4c7048a
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 5 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: 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.exception.EntityNotFoundException;
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)
.orElseThrow(() -> new EntityNotFoundException("Can't find group by id: " + id));
}
}
25 changes: 25 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,39 @@
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<Long> subjectIds = student.getSubjects().stream().map(Subject::getId).toList();
studentDto.setSubjectIds(subjectIds);
}

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

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

@AfterMapping
default void setSubjects(@MappingTarget Student student, CreateStudentRequestDto requestDto) {
List<Subject> subjects = requestDto.subjects().stream().map(Subject::new).toList();
student.setSubjects(subjects);
}
}
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
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public Subject save(Subject subject) {
public List<Subject> findAll() {
try (EntityManager entityManager = entityManagerFactory.createEntityManager()) {
return entityManager.createQuery("SELECT s FROM Subject s", Subject.class)
.getResultList();
.getResultList();
}
}
}
8 changes: 4 additions & 4 deletions src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
spring.datasource.url=jdbc:mysql://localhost:3306/mapstruct?createDatabaseIfNotExist=true
spring.datasource.username=<YOUR_LOGIN>
spring.datasource.password=<YOUR_PASSWORD>
spring.datasource.url=jdbc:mysql://localhost:3306/university
spring.datasource.username=root
spring.datasource.password=1234
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.jpa.hibernate.ddl-auto=create-drop
spring.jpa.show-sql=true
spring.jpa.open-in-view=false
spring.jpa.open-in-view=false

0 comments on commit 4c7048a

Please sign in to comment.