File size: 3,139 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
package com.cs102.attendance.service;

import com.cs102.attendance.entity.Session;
import com.cs102.attendance.repository.SessionRepository;
import com.cs102.attendance.repository.AttendanceRepository;
import com.cs102.attendance.dto.SessionDto;
import com.cs102.attendance.enums.Status;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.time.LocalTime;
import java.util.List;
import java.util.UUID;
import java.util.Optional;
import java.util.stream.Collectors;

@Service
@Transactional
public class SessionService {
    
    @Autowired
    private SessionRepository sessionRepository;
    
    @Autowired
    private AttendanceRepository attendanceRepository;
    
    public Session createSession(String name, LocalDate date, LocalTime startTime, LocalTime endTime) {
        Session session = new Session(name, date, startTime, endTime);
        return sessionRepository.save(session);
    }
    
    public Session closeSession(UUID sessionId) {
        Optional<Session> optionalSession = sessionRepository.findById(sessionId);
        if (optionalSession.isPresent()) {
            Session session = optionalSession.get();
            // Session is considered closed when end time has passed
            // Additional logic can be added here if needed
            return session;
        }
        throw new RuntimeException("Session not found with id: " + sessionId);
    }
    
    public List<Session> getTodaySessions() {
        return getTodaySessions(LocalDate.now());
    }
    
    public List<Session> getTodaySessions(LocalDate date) {
        return sessionRepository.findAll().stream()
                .filter(session -> session.getDate().equals(date))
                .collect(Collectors.toList());
    }
    
    public SessionDto getSessionDto(Session session) {
        SessionDto dto = new SessionDto(
            session.getId(),
            session.getName(),
            session.getDate(),
            session.getStartTime(),
            session.getEndTime()
        );
        
        // Calculate attendance counts
        dto.setPresentCount(attendanceRepository.countBySessionAndStatus(session, Status.PRESENT));
        dto.setAbsentCount(attendanceRepository.countBySessionAndStatus(session, Status.ABSENT));
        dto.setLateCount(attendanceRepository.countBySessionAndStatus(session, Status.LATE));
        dto.setTotalStudents(dto.getPresentCount() + dto.getAbsentCount() + dto.getLateCount());
        
        return dto;
    }
    
    public List<SessionDto> getTodaySessionDtos() {
        return getTodaySessions().stream()
                .map(this::getSessionDto)
                .collect(Collectors.toList());
    }
    
    public List<Session> getAllSessions() {
        return sessionRepository.findAll();
    }
    
    public Optional<Session> getSessionById(UUID id) {
        return sessionRepository.findById(id);
    }
    
    public void deleteSession(UUID id) {
        sessionRepository.deleteById(id);
    }
}