Spaces:
Sleeping
Sleeping
File size: 6,049 Bytes
03549e5 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 |
package com.cs102.attendance.controller;
import com.cs102.attendance.entity.Student;
import com.cs102.attendance.entity.FaceData;
import com.cs102.attendance.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.UUID;
import java.util.Optional;
@RestController
@RequestMapping("/api/students")
@CrossOrigin(origins = "*") // Allow CORS for testing
public class StudentController {
@Autowired
private StudentService studentService;
// Create a new student
@PostMapping
public ResponseEntity<Student> createStudent(@RequestBody CreateStudentRequest request) {
try {
Student student = studentService.enrol(
request.getCode(),
request.getName(),
request.getClassName(),
request.getStudentGroup(),
request.getEmail(),
request.getPhone()
);
return ResponseEntity.ok(student);
} catch (Exception e) {
return ResponseEntity.badRequest().build();
}
}
// Get all students
@GetMapping
public ResponseEntity<List<Student>> getAllStudents() {
try {
List<Student> students = studentService.getAllStudents();
return ResponseEntity.ok(students);
} catch (Exception e) {
e.printStackTrace();
return ResponseEntity.status(500).build();
}
}
// Simple test endpoint
@GetMapping("/health-check")
public ResponseEntity<String> testEndpoint() {
return ResponseEntity.ok("Students endpoint is working!");
}
// Count endpoint
@GetMapping("/count")
public ResponseEntity<Long> getStudentCount() {
try {
List<Student> students = studentService.getAllStudents();
return ResponseEntity.ok((long) students.size());
} catch (Exception e) {
e.printStackTrace();
return ResponseEntity.status(500).body(-1L);
}
}
// Get student by ID
@GetMapping("/{id}")
public ResponseEntity<Student> getStudentById(@PathVariable UUID id) {
Optional<Student> student = studentService.getStudentById(id);
return student.map(ResponseEntity::ok)
.orElse(ResponseEntity.notFound().build());
}
// Update student
@PutMapping("/{id}")
public ResponseEntity<Student> updateStudent(@PathVariable UUID id, @RequestBody UpdateStudentRequest request) {
try {
Student updated = studentService.updateProfile(
id,
request.getName(),
request.getClassName(),
request.getStudentGroup(),
request.getEmail(),
request.getPhone()
);
return ResponseEntity.ok(updated);
} catch (RuntimeException e) {
return ResponseEntity.notFound().build();
}
}
// Delete student
@DeleteMapping("/{id}")
public ResponseEntity<Void> deleteStudent(@PathVariable UUID id) {
try {
studentService.deleteStudent(id);
return ResponseEntity.ok().build();
} catch (Exception e) {
return ResponseEntity.notFound().build();
}
}
// Upload face image (URL)
@PostMapping("/{id}/face-image")
public ResponseEntity<FaceData> uploadFaceImage(@PathVariable UUID id, @RequestBody FaceImageRequest request) {
try {
FaceData faceData = studentService.uploadFaceImage(id, request.getImageUrl());
return ResponseEntity.ok(faceData);
} catch (RuntimeException e) {
return ResponseEntity.notFound().build();
}
}
// DTOs for request bodies
public static class CreateStudentRequest {
private String code;
private String name;
private String className;
private String studentGroup;
private String email;
private String phone;
// Getters and setters
public String getCode() { return code; }
public void setCode(String code) { this.code = code; }
public String getName() { return name; }
public void setName(String name) { this.name = name; }
public String getClassName() { return className; }
public void setClassName(String className) { this.className = className; }
public String getStudentGroup() { return studentGroup; }
public void setStudentGroup(String studentGroup) { this.studentGroup = studentGroup; }
public String getEmail() { return email; }
public void setEmail(String email) { this.email = email; }
public String getPhone() { return phone; }
public void setPhone(String phone) { this.phone = phone; }
}
public static class UpdateStudentRequest {
private String name;
private String className;
private String studentGroup;
private String email;
private String phone;
// Getters and setters
public String getName() { return name; }
public void setName(String name) { this.name = name; }
public String getClassName() { return className; }
public void setClassName(String className) { this.className = className; }
public String getStudentGroup() { return studentGroup; }
public void setStudentGroup(String studentGroup) { this.studentGroup = studentGroup; }
public String getEmail() { return email; }
public void setEmail(String email) { this.email = email; }
public String getPhone() { return phone; }
public void setPhone(String phone) { this.phone = phone; }
}
public static class FaceImageRequest {
private String imageUrl;
public String getImageUrl() { return imageUrl; }
public void setImageUrl(String imageUrl) { this.imageUrl = imageUrl; }
}
} |