@@ -75,6 +75,85 @@ export class ArticlesService {
7575 return await this . articlesRepository . updateOneArticle ( { _id : new ObjectId ( id ) } , update ) ;
7676 }
7777
78+ async getArticleWithMostParticipants ( ) {
79+ const res = await this . articlesRepository . articles
80+ . aggregate ( [
81+ {
82+ $match : {
83+ participants : { $exists : true , $ne : [ ] } ,
84+ } ,
85+ } ,
86+ {
87+ $project : {
88+ _id : 1 ,
89+ title : 1 ,
90+ participants : 1 ,
91+ numParticipants : { $size : '$participants' } ,
92+ } ,
93+ } ,
94+ {
95+ $sort : { numParticipants : - 1 } ,
96+ } ,
97+ {
98+ $limit : 5 ,
99+ } ,
100+ ] )
101+ . toArray ( ) ;
102+ return res ;
103+ }
104+ async getTopCreateur ( ) {
105+ const res = await this . articlesRepository . articles
106+ . aggregate ( [
107+ {
108+ $unwind : '$owner' ,
109+ } ,
110+ {
111+ $group : {
112+ _id : '$owner' ,
113+ firstName : { $first : '$owner.firstName' } ,
114+ lastName : { $first : '$owner.lastName' } ,
115+ count : { $sum : 1 } ,
116+ } ,
117+ } ,
118+ {
119+ $sort : { count : - 1 } ,
120+ } ,
121+ {
122+ $limit : 10 ,
123+ } ,
124+ ] )
125+ . toArray ( ) ;
126+ return res ;
127+ }
128+
129+ async getTopParticipant ( ) {
130+ const res = await this . articlesRepository . articles
131+ . aggregate ( [
132+ {
133+ $match : { type : 'Evenement' } ,
134+ } ,
135+ {
136+ $unwind : '$participants' ,
137+ } ,
138+ {
139+ $group : {
140+ _id : '$participants._id' ,
141+ firstName : { $first : '$participants.firstName' } ,
142+ lastName : { $first : '$participants.lastName' } ,
143+ count : { $sum : 1 } ,
144+ } ,
145+ } ,
146+ {
147+ $sort : { count : - 1 } ,
148+ } ,
149+ {
150+ $limit : 10 ,
151+ } ,
152+ ] )
153+ . toArray ( ) ;
154+ return res ;
155+ }
156+
78157 // remove participant from the array of participants in article in the database
79158 async removeParticipant ( id , queryParticipant ) {
80159 queryParticipant . _id = new ObjectId ( queryParticipant . _id ) ;
0 commit comments