Courses Controller getCourses Mapping
This commit is contained in:
parent
f97e22fa5f
commit
29851dac0d
@ -8,6 +8,8 @@ import java.util.List;
|
||||
import com.plannaplan.App;
|
||||
import com.plannaplan.entities.Course;
|
||||
import com.plannaplan.entities.Groups;
|
||||
import com.plannaplan.responses.mappers.CoursesResponseMappers;
|
||||
import com.plannaplan.responses.models.CoursesResponse;
|
||||
import com.plannaplan.services.CourseService;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
@ -26,16 +28,9 @@ public class CoursesController {
|
||||
private CourseService courseService;
|
||||
|
||||
@GetMapping("/getCourses")
|
||||
public ResponseEntity<List<Dictionary<String, Object>>> getMethodName() {
|
||||
public ResponseEntity<List<CoursesResponse>> getMethodName() {
|
||||
List<Course> courses = this.courseService.getAllCourses();
|
||||
List<Dictionary<String, Object>> response = new ArrayList<>();
|
||||
for (Course c : courses) {
|
||||
Dictionary<String, Object> element = new Hashtable<>();
|
||||
element.put("id", c.getId());
|
||||
element.put("name", c.getName());
|
||||
response.add(element);
|
||||
}
|
||||
|
||||
List<CoursesResponse> response = CoursesResponseMappers.mapCoursesListToCoursesResponseList(courses);
|
||||
return new ResponseEntity<>(response, HttpStatus.OK);
|
||||
}
|
||||
|
||||
|
@ -0,0 +1,14 @@
|
||||
package com.plannaplan.responses.mappers;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import com.plannaplan.entities.Course;
|
||||
import com.plannaplan.responses.models.CoursesResponse;
|
||||
|
||||
public class CoursesResponseMappers {
|
||||
public static final List<CoursesResponse> mapCoursesListToCoursesResponseList(List<Course> courses) {
|
||||
return courses.stream().filter(Objects::nonNull).map(CoursesResponse::new).collect(Collectors.toList());
|
||||
}
|
||||
}
|
@ -0,0 +1,23 @@
|
||||
package com.plannaplan.responses.models;
|
||||
|
||||
import com.plannaplan.entities.Course;
|
||||
|
||||
public class CoursesResponse {
|
||||
|
||||
private Long id;
|
||||
private String name;
|
||||
|
||||
public CoursesResponse(Course course) {
|
||||
this.id = course.getId() != null ? course.getId() : null;
|
||||
this.name = course.getName() != null ? course.getName() : "";
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,23 @@
|
||||
package com.plannaplan.responses.mappers;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import com.plannaplan.entities.Course;
|
||||
import com.plannaplan.responses.models.CoursesResponse;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class CoursesResponseMappersTest {
|
||||
@Test
|
||||
public void shouldMapListCoursesToResponseList() {
|
||||
final List<Course> courses = Arrays.asList(new Course(), new Course());
|
||||
final List<CoursesResponse> response = CoursesResponseMappers.mapCoursesListToCoursesResponseList(courses);
|
||||
|
||||
assertTrue(response.size() == 2);
|
||||
assertTrue(response.get(0) instanceof CoursesResponse);
|
||||
|
||||
}
|
||||
}
|
@ -0,0 +1,27 @@
|
||||
package com.plannaplan.responses.models;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
import com.plannaplan.entities.Course;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class CoursesResponseTest {
|
||||
@Test
|
||||
public void shouldMapFilledCourse() {
|
||||
final Course course = new Course("Test", "XD");
|
||||
final CoursesResponse response = new CoursesResponse(course);
|
||||
|
||||
assertTrue(response.getId() == null);
|
||||
assertTrue(response.getName().equals("Test"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldMapEmptyCourse() {
|
||||
final Course course = new Course();
|
||||
final CoursesResponse response = new CoursesResponse(course);
|
||||
|
||||
assertTrue(response.getId() == null);
|
||||
assertTrue(response.getName().equals(""));
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user