-
Notifications
You must be signed in to change notification settings - Fork 17
/
ProfileController.ts
66 lines (59 loc) · 1.81 KB
/
ProfileController.ts
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
import { Attachment } from '@ioc:Adonis/Addons/AttachmentLite'
import { HttpContextContract } from '@ioc:Adonis/Core/HttpContext'
import UpdateUserAvatarValidator from 'App/Validators/UpdateUserAvatarValidator'
/**
* Profile controller for the currently logged in user.
*/
export default class ProfileController {
/**
* Action to show the user dashboard.
*/
public async index({ request, auth, view }: HttpContextContract) {
/**
* Get the pagination page number and make sure it is a valid number. If
* not a valid number, we fallback to 1.
*/
let page = Number(request.input('page'))
page = isNaN(page) ? 1 : page
/**
* Fetch all the polls created by the currently logged in
*/
const polls = await auth
.user!.related('polls')
.query()
.withAggregate('options', (query) => {
/**
* The aggregated property "votesCount" will be available on the
* poll instance as "poll.$extras.votesCont".
*/
query.sum('votes_count').as('votesCount')
})
.orderBy('id', 'desc')
.paginate(page, 10)
/**
* The pagination links will use the following as
* the base url
*/
polls.baseUrl(request.url())
/**
* Render the dashboard template
*/
return view.render('pages/dashboard', { polls })
}
/**
* Update user avatar
*/
public async updateAvatar({ request, auth, session, response }: HttpContextContract) {
/**
* Validate request
*/
const { avatar } = await request.validate(UpdateUserAvatarValidator)
/**
* Update avatar
*/
auth.user!.avatar = Attachment.fromFile(avatar)
await auth.user!.save()
session.flash({ notification: { success: 'Updated avatar successfully' } })
response.redirect().toRoute('ProfileController.index')
}
}