58 lines
2 KiB
TypeScript
58 lines
2 KiB
TypeScript
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', 'TRIMESTER', '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)
|
|
}
|
|
|
|
// GET /averages
|
|
async averages({ 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(', ')}`,
|
|
})
|
|
}
|
|
|
|
const userId = auth.user!.id
|
|
if (period === PERIODS[0]) {
|
|
const startDate = DateTime.now().minus({ month: 1 })
|
|
return this.gradeService.getSubjectsAverages(userId, startDate, 1)
|
|
}
|
|
const months = period === PERIODS[1] ? 3 : 12
|
|
const startDate = DateTime.now().minus({ months })
|
|
return this.gradeService.getSubjectsAverages(userId, startDate, months)
|
|
}
|
|
}
|