Commit 1c0496a1 authored by Kevin Kaminski's avatar Kevin Kaminski

Still broken.

parent 3f565697
package com.nisum.attendance.attendance.controller; package com.nisum.attendance.attendance.controller;
import com.nisum.attendance.attendance.exceptions.ResourceNotFoundException;
import com.nisum.attendance.attendance.model.Attendance; import com.nisum.attendance.attendance.model.Attendance;
import com.nisum.attendance.attendance.repository.AttendanceRepository; import com.nisum.attendance.attendance.repository.AttendanceRepository;
import com.nisum.attendance.attendance.service.AttendanceService; import com.nisum.attendance.attendance.service.AttendanceService;
...@@ -22,8 +23,9 @@ public class AttendanceController { ...@@ -22,8 +23,9 @@ public class AttendanceController {
} }
@GetMapping("/attendance/{id}") @GetMapping("/attendance/{id}")
public ResponseEntity<Attendance> getAttendance(@PathVariable(value = "id") Long attendanceId) { public ResponseEntity<Attendance> getAttendance(@PathVariable(value = "id") Long attendanceId) throws ResourceNotFoundException {
Attendance attendance = attendanceService.getAttendanceById(attendanceId); Attendance attendance = attendanceService.getAttendanceById(attendanceId)
.orElseThrow(() -> new ResourceNotFoundException("Attendance records by id not found." + attendanceId));
return ResponseEntity.ok(attendance); return ResponseEntity.ok(attendance);
} }
......
package com.nisum.attendance.attendance.exceptions;
import java.util.Date;
public class ErrorDetails {
private Date timeStamp;
private String message;
private String details;
public ErrorDetails(Date timeStamp, String message, String details) {
this.timeStamp = timeStamp;
this.message = message;
this.details = details;
}
public Date getTimeStamp() {
return timeStamp;
}
public void setTimeStamp(Date timeStamp) {
this.timeStamp = timeStamp;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public String getDetails() {
return details;
}
public void setDetails(String details) {
this.details = details;
}
}
package com.nisum.attendance.attendance.exceptions;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.context.request.WebRequest;
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;
import java.util.Date;
public class GlobalExceptionHandler extends ResponseEntityExceptionHandler {
public ResponseEntity<?> resourceNotFoundException(ResourceNotFoundException ex, WebRequest request) {
ErrorDetails errorDetails = new ErrorDetails(new Date(), ex.getMessage(), request.getDescription(false));
return new ResponseEntity<>(errorDetails, HttpStatus.NOT_FOUND);
}
}
package com.nisum.attendance.attendance.exceptions;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ResponseStatus;
@ResponseStatus(value = HttpStatus.NOT_FOUND)
public class ResourceNotFoundException extends Exception{
public ResourceNotFoundException(String message) {
super(message);
}
}
...@@ -12,15 +12,16 @@ import java.util.Date; ...@@ -12,15 +12,16 @@ import java.util.Date;
public class Attendance { public class Attendance {
@Id @Id
@GeneratedValue(strategy = GenerationType.IDENTITY) @GeneratedValue(strategy = GenerationType.IDENTITY)
private Long Id; Long id;
private Long studentId; private String studentEmail;
Date attendanceDate; Date attendanceDate;
Boolean didAttend; Boolean didAttend;
public Attendance(){} public Attendance(){}
public Attendance(Long studentId, Date attendanceDate, Boolean didAttend) { public Attendance(Long id, String studentEmail, Date attendanceDate, Boolean didAttend) {
this.studentId = studentId; this.id = id;
this.studentEmail = studentEmail;
this.attendanceDate = attendanceDate; this.attendanceDate = attendanceDate;
this.didAttend = didAttend; this.didAttend = didAttend;
} }
...@@ -33,10 +34,6 @@ public class Attendance { ...@@ -33,10 +34,6 @@ public class Attendance {
return didAttend; return didAttend;
} }
public Long getStudentId() {
return studentId;
}
public Date getAttendanceDate() { public Date getAttendanceDate() {
return attendanceDate; return attendanceDate;
} }
...@@ -45,8 +42,15 @@ public class Attendance { ...@@ -45,8 +42,15 @@ public class Attendance {
this.attendanceDate = attendanceDate; this.attendanceDate = attendanceDate;
} }
public Long getId() { public String getStudentEmail() {
return Id; return studentEmail;
} }
public void setStudentEmail(String studentEmail) {
this.studentEmail = studentEmail;
}
public Long getId() {
return id;
}
} }
...@@ -2,9 +2,14 @@ package com.nisum.attendance.attendance.repository; ...@@ -2,9 +2,14 @@ package com.nisum.attendance.attendance.repository;
import com.nisum.attendance.attendance.model.Attendance; import com.nisum.attendance.attendance.model.Attendance;
import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository; import org.springframework.stereotype.Repository;
import java.util.List;
import java.util.Optional; import java.util.Optional;
@Repository @Repository
public interface AttendanceRepository extends JpaRepository<Attendance, Long> {} public interface AttendanceRepository extends JpaRepository<Attendance, Long> {
@Query("FROM Attendance a WHERE a.studentEmail = ?1")
List<Attendance> findByStudentEmail(String studentEmail);
}
...@@ -20,8 +20,12 @@ public class AttendanceService { ...@@ -20,8 +20,12 @@ public class AttendanceService {
return attendanceRepository.findAll(); return attendanceRepository.findAll();
} }
public Attendance getAttendanceById(Long attendanceId) { public List<Attendance> getAttendanceByEmail(String studentEmail) {
return attendanceRepository.findById(attendanceId).get(); return attendanceRepository.findByStudentEmail(studentEmail);
}
public Optional<Attendance> getAttendanceById(Long attendanceId) {
return attendanceRepository.findById(attendanceId);
} }
public Attendance addAttendance(Attendance attendance) { public Attendance addAttendance(Attendance attendance) {
...@@ -29,9 +33,13 @@ public class AttendanceService { ...@@ -29,9 +33,13 @@ public class AttendanceService {
} }
public Attendance updateAttendance(Long attendanceId, Attendance attendanceDetails) { public Attendance updateAttendance(Long attendanceId, Attendance attendanceDetails) {
Attendance attendance = getAttendanceById(attendanceId); Optional<Attendance> attendanceOptional = getAttendanceById(attendanceId);
attendance.setAttendanceDate(attendanceDetails.getAttendanceDate()); if (attendanceOptional.isPresent()) {
attendance.setDidAttend(attendanceDetails.getDidAttend()); Attendance attendance = attendanceOptional.get();
return attendanceRepository.save(attendance); attendance.setAttendanceDate(attendanceDetails.getAttendanceDate());
attendance.setDidAttend(attendanceDetails.getDidAttend());
return attendanceRepository.save(attendance);
}
return null;
} }
} }
...@@ -2,18 +2,18 @@ DROP TABLE IF EXISTS ATTENDANCE; ...@@ -2,18 +2,18 @@ DROP TABLE IF EXISTS ATTENDANCE;
CREATE TABLE ATTENDANCE ( CREATE TABLE ATTENDANCE (
ID INT AUTO_INCREMENT PRIMARY KEY, ID INT AUTO_INCREMENT PRIMARY KEY,
STUDENT_ID INT NOT NULL, STUDENT_EMAIL VARCHAR(255) NOT NULL,
ATTENDANCE_DATE DATE NOT NULL, ATTENDANCE_DATE DATE NOT NULL,
DID_ATTEND BOOLEAN DEFAULT FALSE DID_ATTEND BOOLEAN DEFAULT FALSE
); );
INSERT INTO ATTENDANCE VALUES INSERT INTO ATTENDANCE VALUES
(1, 10, '0001-02-01', TRUE), (1, 'a@a.com', '0001-02-01', TRUE),
(2, 20, '0001-02-02', FALSE), (2, 'b@b.com', '0001-02-02', FALSE),
(3, 30, '0001-02-03', TRUE), (3, 'c@c.com', '0001-02-03', TRUE),
(4, 30, '0001-02-04', TRUE), (3, 'c@c.com', '0001-02-04', TRUE),
(5, 20, '0001-02-03', TRUE), (2, 'b@b.com', '0001-02-03', TRUE),
(6, 10, '0001-02-02', FALSE), (1, 'a@a.com', '0001-02-02', FALSE),
(7, 10, '0001-02-03', TRUE), (1, 'a@a.com', '0001-02-03', TRUE),
(8, 30, '0001-02-05', TRUE), (3, 'c@c.com', '0001-02-05', TRUE),
(9, 10, '0001-02-04', FALSE); (1, 'a@a.com', '0001-02-04', FALSE);
\ No newline at end of file \ No newline at end of file
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