Spaces:
Sleeping
Sleeping
| package com.cs102.attendance.controller; | |
| import com.cs102.attendance.entity.Session; | |
| import com.cs102.attendance.dto.SessionDto; | |
| import com.cs102.attendance.service.SessionService; | |
| import org.springframework.beans.factory.annotation.Autowired; | |
| import org.springframework.http.ResponseEntity; | |
| import org.springframework.web.bind.annotation.*; | |
| import java.time.LocalDate; | |
| import java.time.LocalTime; | |
| import java.util.List; | |
| import java.util.UUID; | |
| import java.util.Optional; | |
| public class SessionController { | |
| private SessionService sessionService; | |
| // Create a new session | |
| public ResponseEntity<Session> createSession( CreateSessionRequest request) { | |
| try { | |
| Session session = sessionService.createSession( | |
| request.getName(), | |
| request.getDate(), | |
| request.getStartTime(), | |
| request.getEndTime() | |
| ); | |
| return ResponseEntity.ok(session); | |
| } catch (Exception e) { | |
| return ResponseEntity.badRequest().build(); | |
| } | |
| } | |
| // Get all sessions | |
| public ResponseEntity<List<Session>> getAllSessions() { | |
| List<Session> sessions = sessionService.getAllSessions(); | |
| return ResponseEntity.ok(sessions); | |
| } | |
| // Get session by ID | |
| public ResponseEntity<Session> getSessionById( UUID id) { | |
| Optional<Session> session = sessionService.getSessionById(id); | |
| return session.map(ResponseEntity::ok) | |
| .orElse(ResponseEntity.notFound().build()); | |
| } | |
| // Get today's sessions | |
| public ResponseEntity<List<SessionDto>> getTodaySessions() { | |
| List<SessionDto> sessions = sessionService.getTodaySessionDtos(); | |
| return ResponseEntity.ok(sessions); | |
| } | |
| // Get sessions for specific date | |
| public ResponseEntity<List<Session>> getSessionsByDate( String date) { | |
| try { | |
| LocalDate localDate = LocalDate.parse(date); | |
| List<Session> sessions = sessionService.getTodaySessions(localDate); | |
| return ResponseEntity.ok(sessions); | |
| } catch (Exception e) { | |
| return ResponseEntity.badRequest().build(); | |
| } | |
| } | |
| // Close session | |
| public ResponseEntity<Session> closeSession( UUID id) { | |
| try { | |
| Session session = sessionService.closeSession(id); | |
| return ResponseEntity.ok(session); | |
| } catch (RuntimeException e) { | |
| return ResponseEntity.notFound().build(); | |
| } | |
| } | |
| // Delete session | |
| public ResponseEntity<Void> deleteSession( UUID id) { | |
| try { | |
| sessionService.deleteSession(id); | |
| return ResponseEntity.ok().build(); | |
| } catch (Exception e) { | |
| return ResponseEntity.notFound().build(); | |
| } | |
| } | |
| // DTO for request body | |
| public static class CreateSessionRequest { | |
| private String name; | |
| private LocalDate date; | |
| private LocalTime startTime; | |
| private LocalTime endTime; | |
| // Getters and setters | |
| public String getName() { return name; } | |
| public void setName(String name) { this.name = name; } | |
| public LocalDate getDate() { return date; } | |
| public void setDate(LocalDate date) { this.date = date; } | |
| public LocalTime getStartTime() { return startTime; } | |
| public void setStartTime(LocalTime startTime) { this.startTime = startTime; } | |
| public LocalTime getEndTime() { return endTime; } | |
| public void setEndTime(LocalTime endTime) { this.endTime = endTime; } | |
| } | |
| } |