mirror of
https://github.com/Sonny93/my-links.git
synced 2025-12-08 14:43:24 +00:00
47 lines
1.5 KiB
TypeScript
47 lines
1.5 KiB
TypeScript
import AuthController from '#auth/controllers/auth_controller';
|
|
import { CollectionService } from '#collections/services/collection_service';
|
|
import LinksController from '#links/controllers/delete_link_controller';
|
|
import User from '#user/models/user';
|
|
import { inject } from '@adonisjs/core';
|
|
import { HttpContext } from '@adonisjs/core/http';
|
|
|
|
class UserWithRelationCountDto {
|
|
constructor(private user: User) {}
|
|
|
|
toJson = () => ({
|
|
id: this.user.id,
|
|
email: this.user.email,
|
|
fullname: this.user.name,
|
|
avatarUrl: this.user.avatarUrl,
|
|
isAdmin: this.user.isAdmin,
|
|
createdAt: this.user.createdAt.toString(),
|
|
updatedAt: this.user.updatedAt.toString(),
|
|
lastSeenAt:
|
|
this.user.lastSeenAt?.toString() ?? this.user.updatedAt.toString(),
|
|
linksCount: Number(this.user.$extras.totalLinks),
|
|
collectionsCount: Number(this.user.$extras.totalCollections),
|
|
});
|
|
}
|
|
|
|
@inject()
|
|
export default class AdminController {
|
|
constructor(
|
|
protected usersController: AuthController,
|
|
protected linksController: LinksController,
|
|
protected collectionService: CollectionService
|
|
) {}
|
|
|
|
async index({ inertia }: HttpContext) {
|
|
const users = await this.usersController.getAllUsersWithTotalRelations();
|
|
const linksCount = await this.linksController.getTotalLinksCount();
|
|
const collectionsCount =
|
|
await this.collectionService.getTotalCollectionsCount();
|
|
|
|
return inertia.render('admin/dashboard', {
|
|
users: users.map((user) => new UserWithRelationCountDto(user).toJson()),
|
|
totalLinks: linksCount,
|
|
totalCollections: collectionsCount,
|
|
});
|
|
}
|
|
}
|