mirror of
https://github.com/Sonny93/my-links.git
synced 2025-12-08 14:43:24 +00:00
40 lines
1.3 KiB
TypeScript
40 lines
1.3 KiB
TypeScript
import { CollectionService } from '#collections/services/collection_service';
|
|
import { LinkService } from '#links/services/link_service';
|
|
import { deleteLinkValidator } from '#links/validators/delete_link_validator';
|
|
import { inject } from '@adonisjs/core';
|
|
import { HttpContext } from '@adonisjs/core/http';
|
|
import db from '@adonisjs/lucid/services/db';
|
|
|
|
@inject()
|
|
export default class DeleteLinkController {
|
|
constructor(
|
|
protected collectionsService: CollectionService,
|
|
protected linkService: LinkService
|
|
) {}
|
|
|
|
async render({ auth, inertia, request }: HttpContext) {
|
|
const linkId = request.qs()?.linkId;
|
|
if (!linkId) {
|
|
return this.collectionsService.redirectToDashboard();
|
|
}
|
|
|
|
const link = await this.linkService.getLinkById(linkId, auth.user!.id);
|
|
await link.load('collection');
|
|
return inertia.render('links/delete', { link });
|
|
}
|
|
|
|
async execute({ request, auth }: HttpContext) {
|
|
const { params } = await request.validateUsing(deleteLinkValidator);
|
|
|
|
const link = await this.linkService.getLinkById(params.id, auth.user!.id);
|
|
await this.linkService.deleteLink(params.id);
|
|
|
|
return this.collectionsService.redirectToCollectionId(link.collectionId);
|
|
}
|
|
|
|
async getTotalLinksCount() {
|
|
const totalCount = await db.from('links').count('* as total');
|
|
return Number(totalCount[0].total);
|
|
}
|
|
}
|