Commit 08fe0e6f authored by Kyle Muldoon's avatar Kyle Muldoon

Merge branch 'marksModel' into 'master'

Add mark model and refactor

See merge request !10
parents 4ff3ea6c 6a5921c5
#server.port=8081
spring.data.mongodb.uri=mongodb+srv://userNisum:NisumCohort2021@cluster0.8ekps.mongodb.net/myFirstDatabase?retryWrites=true&w=majority spring.data.mongodb.uri=mongodb+srv://userNisum:NisumCohort2021@cluster0.8ekps.mongodb.net/myFirstDatabase?retryWrites=true&w=majority
spring.data.mongodb.database=test spring.data.mongodb.database=test
...@@ -6,11 +6,13 @@ com/StudentServices/MarksService/MarksServiceApplication.java ...@@ -6,11 +6,13 @@ com/StudentServices/MarksService/MarksServiceApplication.java
com.StudentServices.MarksService.MarksServiceApplication com.StudentServices.MarksService.MarksServiceApplication
com/StudentServices/MarksService/exception/GlobalExceptionHandler.java com/StudentServices/MarksService/exception/GlobalExceptionHandler.java
com.StudentServices.MarksService.exception.GlobalExceptionHandler com.StudentServices.MarksService.exception.GlobalExceptionHandler
com/StudentServices/MarksService/model/StudentMarks.java
com.StudentServices.MarksService.model.StudentMarks
com/StudentServices/MarksService/repository/StudentRepository.java com/StudentServices/MarksService/repository/StudentRepository.java
com.StudentServices.MarksService.repository.StudentRepository com.StudentServices.MarksService.repository.StudentRepository
com/StudentServices/MarksService/model/Mark.java
com.StudentServices.MarksService.model.Mark
com/StudentServices/MarksService/service/StudentService.java com/StudentServices/MarksService/service/StudentService.java
com.StudentServices.MarksService.service.StudentService com.StudentServices.MarksService.service.StudentService
com/StudentServices/MarksService/model/StudentMarks.java
com.StudentServices.MarksService.model.StudentMarks
com/StudentServices/MarksService/controller/StudentMarksController.java com/StudentServices/MarksService/controller/StudentMarksController.java
com.StudentServices.MarksService.controller.StudentMarksController com.StudentServices.MarksService.controller.StudentMarksController
package com.StudentServices.MarksService.controller; package com.StudentServices.MarksService.controller;
import com.StudentServices.MarksService.exception.ResourceNotFoundException; import com.StudentServices.MarksService.exception.ResourceNotFoundException;
import com.StudentServices.MarksService.model.Mark;
import com.StudentServices.MarksService.model.StudentMarks; import com.StudentServices.MarksService.model.StudentMarks;
import com.StudentServices.MarksService.service.StudentService; import com.StudentServices.MarksService.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
...@@ -11,6 +12,8 @@ import org.springframework.web.bind.annotation.*; ...@@ -11,6 +12,8 @@ import org.springframework.web.bind.annotation.*;
import java.util.Arrays; import java.util.Arrays;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
@CrossOrigin(origins = "*") @CrossOrigin(origins = "*")
@RestController @RestController
...@@ -25,6 +28,7 @@ public class StudentMarksController { ...@@ -25,6 +28,7 @@ public class StudentMarksController {
ResponseEntity<List<StudentMarks>> findAllStudentMarks(){ ResponseEntity<List<StudentMarks>> findAllStudentMarks(){
return ResponseEntity.ok(studentService.findAllStudents()); return ResponseEntity.ok(studentService.findAllStudents());
} }
// (POST) create studentmark // (POST) create studentmark
@PostMapping("/studentMarks") @PostMapping("/studentMarks")
public ResponseEntity<StudentMarks> createMarks(@RequestBody StudentMarks studentMarks) { public ResponseEntity<StudentMarks> createMarks(@RequestBody StudentMarks studentMarks) {
...@@ -46,9 +50,10 @@ public class StudentMarksController { ...@@ -46,9 +50,10 @@ public class StudentMarksController {
ResponseEntity<StudentMarks> updateStudent(@PathVariable String id, @PathVariable String course, @RequestBody String grade) throws ResourceNotFoundException { ResponseEntity<StudentMarks> updateStudent(@PathVariable String id, @PathVariable String course, @RequestBody String grade) throws ResourceNotFoundException {
StudentMarks student = studentService.getStudentById(id). StudentMarks student = studentService.getStudentById(id).
orElseThrow( () -> new ResourceNotFoundException("Student was not found: " + id)); orElseThrow( () -> new ResourceNotFoundException("Student was not found: " + id));
Optional<Mark> currentStudentMarks = student.getMarks().stream().filter(c -> c.getCourseId().equals(course)).findFirst();
if (student.getMarks().get(course).length() > 0) { if (currentStudentMarks.isPresent()) {
student.getMarks().put(course, grade); currentStudentMarks.get().setGrade(grade);
} else { } else {
throw new ResourceNotFoundException("course not found"); throw new ResourceNotFoundException("course not found");
} }
......
package com.StudentServices.MarksService.model;
import lombok.AllArgsConstructor;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.Setter;
@Setter
@Getter
@AllArgsConstructor
@EqualsAndHashCode
public class Mark {
private String courseId;
private String grade;
}
...@@ -3,7 +3,7 @@ package com.StudentServices.MarksService.model; ...@@ -3,7 +3,7 @@ package com.StudentServices.MarksService.model;
import org.springframework.data.annotation.Id; import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document; import org.springframework.data.mongodb.core.mapping.Document;
import java.util.HashMap; import java.util.ArrayList;
@Document(collection = "StudentScemaV1") @Document(collection = "StudentScemaV1")
public class StudentMarks { public class StudentMarks {
...@@ -11,12 +11,12 @@ public class StudentMarks { ...@@ -11,12 +11,12 @@ public class StudentMarks {
@Id @Id
private String emailAddress; private String emailAddress;
private HashMap<String, String> marks; private ArrayList<Mark> marks;
public StudentMarks() {} public StudentMarks() {}
public StudentMarks(String emailAddress, HashMap<String, String> marks) { public StudentMarks(String emailAddress, ArrayList<Mark> marks) {
this.emailAddress = emailAddress; this.emailAddress = emailAddress;
this.marks = marks; this.marks = marks;
} }
...@@ -30,11 +30,11 @@ public class StudentMarks { ...@@ -30,11 +30,11 @@ public class StudentMarks {
this.emailAddress = emailAddress; this.emailAddress = emailAddress;
} }
public HashMap<String, String> getMarks() { public ArrayList<Mark> getMarks() {
return marks; return marks;
} }
public void setMarks(HashMap<String, String> marks) { public void setMarks(ArrayList<Mark> marks) {
this.marks = marks; this.marks = marks;
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment