mirror of
https://github.com/Sonny93/my-links.git
synced 2025-12-08 22:53:25 +00:00
refactor: create types instead of using models
This commit is contained in:
@@ -1,42 +1,46 @@
|
||||
import CollectionsController from '#controllers/collections_controller';
|
||||
import LinksController from '#controllers/links_controller';
|
||||
import UsersController from '#controllers/users_controller';
|
||||
import User from '#models/user';
|
||||
import { inject } from '@adonisjs/core';
|
||||
import type { HttpContext } from '@adonisjs/core/http';
|
||||
import db from '@adonisjs/lucid/services/db';
|
||||
|
||||
class UserWithRelationCountDto {
|
||||
constructor(private user: User) {}
|
||||
|
||||
public toJson() {
|
||||
return {
|
||||
id: this.user.id,
|
||||
email: this.user.email,
|
||||
fullname: this.user.name,
|
||||
avatarUrl: this.user.avatarUrl,
|
||||
isAdmin: this.user.isAdmin,
|
||||
createdAt: this.user.createdAt,
|
||||
updatedAt: this.user.updatedAt,
|
||||
count: {
|
||||
link: Number(this.user.$extras.totalLinks),
|
||||
collection: Number(this.user.$extras.totalCollections),
|
||||
},
|
||||
};
|
||||
}
|
||||
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,
|
||||
updatedAt: this.user.updatedAt,
|
||||
count: {
|
||||
link: Number(this.user.$extras.totalLinks),
|
||||
collection: Number(this.user.$extras.totalCollections),
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
@inject()
|
||||
export default class AdminController {
|
||||
constructor(protected usersController: UsersController) {}
|
||||
constructor(
|
||||
protected usersController: UsersController,
|
||||
protected linksController: LinksController,
|
||||
protected collectionsController: CollectionsController
|
||||
) {}
|
||||
|
||||
async index({ inertia }: HttpContext) {
|
||||
const users = await this.usersController.getAllUsersWithTotalRelations();
|
||||
const links = await db.from('links').count('* as total');
|
||||
const collections = await db.from('collections').count('* as total');
|
||||
const linksCount = await this.linksController.getTotalLinksCount();
|
||||
const collectionsCount =
|
||||
await this.collectionsController.getTotalCollectionsCount();
|
||||
|
||||
return inertia.render('admin/dashboard', {
|
||||
users: users.map((user) => new UserWithRelationCountDto(user).toJson()),
|
||||
totalLinks: Number(links[0].total),
|
||||
totalCollections: Number(collections[0].total),
|
||||
totalLinks: linksCount,
|
||||
totalCollections: collectionsCount,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,6 +6,7 @@ import {
|
||||
updateCollectionValidator,
|
||||
} from '#validators/collection';
|
||||
import type { HttpContext } from '@adonisjs/core/http';
|
||||
import db from '@adonisjs/lucid/services/db';
|
||||
|
||||
export default class CollectionsController {
|
||||
// Dashboard
|
||||
@@ -105,6 +106,11 @@ export default class CollectionsController {
|
||||
return response.redirectToNamedRoute('dashboard');
|
||||
}
|
||||
|
||||
async getTotalCollectionsCount() {
|
||||
const totalCount = await db.from('collections').count('* as total');
|
||||
return Number(totalCount[0].total);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get collection by id.
|
||||
*
|
||||
|
||||
@@ -8,6 +8,7 @@ import {
|
||||
} from '#validators/link';
|
||||
import { inject } from '@adonisjs/core';
|
||||
import type { HttpContext } from '@adonisjs/core/http';
|
||||
import db from '@adonisjs/lucid/services/db';
|
||||
|
||||
@inject()
|
||||
export default class LinksController {
|
||||
@@ -111,6 +112,11 @@ export default class LinksController {
|
||||
});
|
||||
}
|
||||
|
||||
async getTotalLinksCount() {
|
||||
const totalCount = await db.from('links').count('* as total');
|
||||
return Number(totalCount[0].total);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get link by id.
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user