import { GradeService } from '#services/grade_service' import { inject } from '@adonisjs/core' import type { HttpContext } from '@adonisjs/core/http' import { DateTime } from 'luxon' const PERIODS = ['MONTH', 'SEMESTER', 'YEAR'] @inject() export default class GradesController { constructor(private gradeService: GradeService) {} // GET /grades async index({ auth, request, response }: HttpContext) { const period = request.input('period', PERIODS[0]).toUpperCase() if (!PERIODS.includes(period)) { return response.badRequest({ message: `Invalid period. Allowed values are: ${PERIODS.join(', ')}`, }) } // TODO: Choose a start date // const { startDate: rawStartDate } = request.qs() // Validate startDate // const startDate = rawStartDate ? DateTime.fromISO(rawStartDate, { zone: 'local' }) : null // if (!rawStartDate || !startDate || !startDate.isValid) { // return response.badRequest({ message: 'Invalid start date format' }) // } const userId = auth.user!.id if (period === PERIODS[0]) { const startDate = DateTime.now().minus({ month: 1 }) return this.gradeService.getMonthGrade(userId, startDate) } const months = period === PERIODS[1] ? 3 : 12 const startDate = DateTime.now().minus({ months }) return this.gradeService.getPeriodGrade(userId, startDate, months) } // TODO: Radar chart for subjects }