Moved CoursesResponse To Abstract

This commit is contained in:
Filip Izydorczyk 2020-10-13 16:19:40 +02:00
parent 29851dac0d
commit bdd0278880
6 changed files with 24 additions and 13 deletions

View File

@ -9,7 +9,7 @@ import com.plannaplan.App;
import com.plannaplan.entities.Course; import com.plannaplan.entities.Course;
import com.plannaplan.entities.Groups; import com.plannaplan.entities.Groups;
import com.plannaplan.responses.mappers.CoursesResponseMappers; import com.plannaplan.responses.mappers.CoursesResponseMappers;
import com.plannaplan.responses.models.CoursesResponse; import com.plannaplan.responses.models.GetCoursesResponse;
import com.plannaplan.services.CourseService; import com.plannaplan.services.CourseService;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
@ -28,9 +28,9 @@ public class CoursesController {
private CourseService courseService; private CourseService courseService;
@GetMapping("/getCourses") @GetMapping("/getCourses")
public ResponseEntity<List<CoursesResponse>> getMethodName() { public ResponseEntity<List<GetCoursesResponse>> getMethodName() {
List<Course> courses = this.courseService.getAllCourses(); List<Course> courses = this.courseService.getAllCourses();
List<CoursesResponse> response = CoursesResponseMappers.mapCoursesListToCoursesResponseList(courses); List<GetCoursesResponse> response = CoursesResponseMappers.mapCoursesListToCoursesResponseList(courses);
return new ResponseEntity<>(response, HttpStatus.OK); return new ResponseEntity<>(response, HttpStatus.OK);
} }

View File

@ -5,10 +5,10 @@ import java.util.Objects;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import com.plannaplan.entities.Course; import com.plannaplan.entities.Course;
import com.plannaplan.responses.models.CoursesResponse; import com.plannaplan.responses.models.GetCoursesResponse;
public class CoursesResponseMappers { public class CoursesResponseMappers {
public static final List<CoursesResponse> mapCoursesListToCoursesResponseList(List<Course> courses) { public static final List<GetCoursesResponse> mapCoursesListToCoursesResponseList(List<Course> courses) {
return courses.stream().filter(Objects::nonNull).map(CoursesResponse::new).collect(Collectors.toList()); return courses.stream().filter(Objects::nonNull).map(GetCoursesResponse::new).collect(Collectors.toList());
} }
} }

View File

@ -2,7 +2,7 @@ package com.plannaplan.responses.models;
import com.plannaplan.entities.Course; import com.plannaplan.entities.Course;
public class CoursesResponse { public abstract class CoursesResponse {
private Long id; private Long id;
private String name; private String name;

View File

@ -0,0 +1,11 @@
package com.plannaplan.responses.models;
import com.plannaplan.entities.Course;
public class GetCoursesResponse extends CoursesResponse {
public GetCoursesResponse(Course course) {
super(course);
}
}

View File

@ -6,7 +6,7 @@ import java.util.Arrays;
import java.util.List; import java.util.List;
import com.plannaplan.entities.Course; import com.plannaplan.entities.Course;
import com.plannaplan.responses.models.CoursesResponse; import com.plannaplan.responses.models.GetCoursesResponse;
import org.junit.Test; import org.junit.Test;
@ -14,10 +14,10 @@ public class CoursesResponseMappersTest {
@Test @Test
public void shouldMapListCoursesToResponseList() { public void shouldMapListCoursesToResponseList() {
final List<Course> courses = Arrays.asList(new Course(), new Course()); final List<Course> courses = Arrays.asList(new Course(), new Course());
final List<CoursesResponse> response = CoursesResponseMappers.mapCoursesListToCoursesResponseList(courses); final List<GetCoursesResponse> response = CoursesResponseMappers.mapCoursesListToCoursesResponseList(courses);
assertTrue(response.size() == 2); assertTrue(response.size() == 2);
assertTrue(response.get(0) instanceof CoursesResponse); assertTrue(response.get(0) instanceof GetCoursesResponse);
} }
} }

View File

@ -6,11 +6,11 @@ import com.plannaplan.entities.Course;
import org.junit.Test; import org.junit.Test;
public class CoursesResponseTest { public class GetCoursesResponseTest {
@Test @Test
public void shouldMapFilledCourse() { public void shouldMapFilledCourse() {
final Course course = new Course("Test", "XD"); final Course course = new Course("Test", "XD");
final CoursesResponse response = new CoursesResponse(course); final GetCoursesResponse response = new GetCoursesResponse(course);
assertTrue(response.getId() == null); assertTrue(response.getId() == null);
assertTrue(response.getName().equals("Test")); assertTrue(response.getName().equals("Test"));
@ -19,7 +19,7 @@ public class CoursesResponseTest {
@Test @Test
public void shouldMapEmptyCourse() { public void shouldMapEmptyCourse() {
final Course course = new Course(); final Course course = new Course();
final CoursesResponse response = new CoursesResponse(course); final GetCoursesResponse response = new GetCoursesResponse(course);
assertTrue(response.getId() == null); assertTrue(response.getId() == null);
assertTrue(response.getName().equals("")); assertTrue(response.getName().equals(""));