1+ import { bind } from '@adonisjs/route-model-binding'
12import type { HttpContextContract } from '@ioc:Adonis/Core/HttpContext'
23import Database from '@ioc:Adonis/Lucid/Database'
34import Vote from 'App/Models/Vote'
5+ import Year from 'App/Models/Year'
46import { DateTime } from 'luxon'
57
68export default class ChartsController {
79 public async index ( { view } : HttpContextContract ) {
8- return view . render ( 'votes/charts/index' )
10+ const years = await Year . query ( ) . orderBy ( 'year' , 'desc' )
11+ return view . render ( 'votes/charts/index' , { years } )
912 }
1013
1114 public async totalByDay ( { view, request } : HttpContextContract ) {
@@ -52,14 +55,17 @@ export default class ChartsController {
5255 } )
5356 }
5457
55- public async topAssociations ( { request, view } : HttpContextContract ) {
58+ @bind ( )
59+ public async topAssociations ( { request, view } : HttpContextContract , year : Year ) {
5660 const limit = request . input ( 'limit' , 10 )
5761
5862 const votes = await Database . from ( 'votes' )
59- . select ( 'name as label' )
63+ . select ( 'associations.name as label' )
64+ . where ( 'votes.year_id' , year . id )
6065 . count ( 'votes.id as value' )
61- . join ( 'associations' , 'votes.association_id' , '=' , 'associations.id' )
62- . groupBy ( 'association_id' , 'name' )
66+ . join ( 'participations' , 'votes.participation_id' , '=' , 'participations.id' )
67+ . join ( 'associations' , 'participations.association_id' , '=' , 'associations.id' )
68+ . groupBy ( 'votes.participation_id' , 'associations.name' )
6369 . orderBy ( 'value' , 'desc' )
6470 . limit ( limit )
6571
@@ -81,29 +87,34 @@ export default class ChartsController {
8187 /**
8288 * Used to draw a chart of the most voted association day by day
8389 */
84- public async topAssociationsByDay ( { view, request } : HttpContextContract ) {
90+ @bind ( )
91+ public async topAssociationsByDay ( { view, request } : HttpContextContract , year : Year ) {
8592 const limit = request . input ( 'limit' , 10 )
8693
87- // Get the ten most voted associations ids
88- const associationsIds = Database . from (
94+ // Get the ten most voted associations ids for the given year
95+ const participationsIds = Database . from (
8996 Database . from ( 'votes' )
90- . select ( 'association_id' )
97+ . select ( 'participation_id' )
98+ . where ( 'votes.year_id' , year . id )
9199 . count ( 'id' )
92- . groupBy ( 'association_id ' )
100+ . groupBy ( 'participation_id ' )
93101 . orderBy ( 'count' , 'desc' )
94102 . limit ( limit )
95103 . as ( 'most_voted' )
96- ) . select ( 'association_id ' )
104+ ) . select ( 'participation_id ' )
97105
98106 // Group votes by date and association id
99107 const result = await Database . from ( 'votes' )
100108 . select ( 'name' )
101109 . select ( Database . raw ( "date_trunc('day', votes.created_at) as date" ) )
102- . count ( 'association_id' )
103- . join ( 'associations' , 'votes.association_id' , '=' , 'associations.id' )
104- . whereIn ( 'association_id' , associationsIds )
110+ . where ( 'votes.year_id' , year . id )
111+ . whereIn ( 'votes.participation_id' , participationsIds )
112+ . count ( 'participation_id' )
113+ . join ( 'participations' , 'votes.participation_id' , '=' , 'participations.id' )
114+ . join ( 'associations' , 'participations.association_id' , '=' , 'associations.id' )
105115 . groupBy ( 'name' )
106116 . groupByRaw ( "date_trunc('day', votes.created_at)" )
117+ . orderBy ( 'name' )
107118 . orderBy ( 'date' )
108119
109120 const votes = result as unknown as {
@@ -123,14 +134,14 @@ export default class ChartsController {
123134 label : vote . name ,
124135 data : [
125136 {
126- x : DateTime . fromISO ( vote . date . toISOString ( ) ) . toFormat ( 'dd/LL' ) ,
137+ x : vote . date . toISOString ( ) ,
127138 y : vote . count ,
128139 } ,
129140 ] ,
130141 } )
131142 } else {
132143 acc [ index ] . data . push ( {
133- x : DateTime . fromISO ( vote . date . toISOString ( ) ) . toFormat ( 'dd/LL' ) ,
144+ x : vote . date . toISOString ( ) ,
134145 y : vote . count ,
135146 } )
136147 }
@@ -144,33 +155,33 @@ export default class ChartsController {
144155 } )
145156 }
146157
147- public async acceptNewsClassement ( { view } ) {
148- const result = await Database . from ( 'votes' )
149- . select ( 'accept_classement' )
150- . count ( 'id' )
151- . groupBy ( 'accept_classement' )
152- . orderBy ( 'accept_classement' )
153-
154- return view . render ( 'votes/charts/accept-news-classement' , {
155- votes : {
156- labels : JSON . stringify ( result . map ( ( vote ) => ( vote . accept_classement ? 'Oui' : 'Non' ) ) ) ,
157- data : JSON . stringify ( result . map ( ( vote ) => vote . count ) ) ,
158- } ,
159- } )
160- }
161-
162- public async acceptNewsActivities ( { view } ) {
163- const result = await Database . from ( 'votes' )
164- . select ( 'accept_activities' )
165- . count ( 'id' )
166- . groupBy ( 'accept_activities' )
167- . orderBy ( 'accept_activities' )
168-
169- return view . render ( 'votes/charts/accept-news-activities' , {
170- votes : {
171- labels : JSON . stringify ( result . map ( ( vote ) => ( vote . accept_activities ? 'Oui' : 'Non' ) ) ) ,
172- data : JSON . stringify ( result . map ( ( vote ) => vote . count ) ) ,
173- } ,
174- } )
175- }
158+ // public async acceptNewsClassement({ view }) {
159+ // const result = await Database.from('votes')
160+ // .select('accept_classement')
161+ // .count('id')
162+ // .groupBy('accept_classement')
163+ // .orderBy('accept_classement')
164+
165+ // return view.render('votes/charts/accept-news-classement', {
166+ // votes: {
167+ // labels: JSON.stringify(result.map((vote) => (vote.accept_classement ? 'Oui' : 'Non'))),
168+ // data: JSON.stringify(result.map((vote) => vote.count)),
169+ // },
170+ // })
171+ // }
172+
173+ // public async acceptNewsActivities({ view }) {
174+ // const result = await Database.from('votes')
175+ // .select('accept_activities')
176+ // .count('id')
177+ // .groupBy('accept_activities')
178+ // .orderBy('accept_activities')
179+
180+ // return view.render('votes/charts/accept-news-activities', {
181+ // votes: {
182+ // labels: JSON.stringify(result.map((vote) => (vote.accept_activities ? 'Oui' : 'Non'))),
183+ // data: JSON.stringify(result.map((vote) => vote.count)),
184+ // },
185+ // })
186+ // }
176187}
0 commit comments