mirror of
https://github.com/Sonny93/my-links.git
synced 2025-12-09 23:15:36 +00:00
feat: add delete collection form and controller method
This commit is contained in:
@@ -2,6 +2,7 @@ import Collection from '#models/collection';
|
||||
import User from '#models/user';
|
||||
import {
|
||||
createCollectionValidator,
|
||||
deleteCollectionValidator,
|
||||
updateCollectionValidator,
|
||||
} from '#validators/collection';
|
||||
import type { HttpContext } from '@adonisjs/core/http';
|
||||
@@ -82,6 +83,28 @@ export default class CollectionsController {
|
||||
return this.redirectToCollectionId(response, params.id);
|
||||
}
|
||||
|
||||
async showDeletePage({ auth, request, inertia, response }: HttpContext) {
|
||||
const collectionId = request.qs()?.collectionId;
|
||||
if (!collectionId) {
|
||||
return response.redirectToNamedRoute('dashboard');
|
||||
}
|
||||
|
||||
const collection = await this.getCollectionById(
|
||||
collectionId,
|
||||
auth.user!.id
|
||||
);
|
||||
return inertia.render('collections/delete', {
|
||||
collection,
|
||||
});
|
||||
}
|
||||
|
||||
async delete({ request, auth, response }: HttpContext) {
|
||||
const { params } = await request.validateUsing(deleteCollectionValidator);
|
||||
const collection = await this.getCollectionById(params.id, auth.user!.id);
|
||||
await collection.delete();
|
||||
return response.redirectToNamedRoute('dashboard');
|
||||
}
|
||||
|
||||
/**
|
||||
* Get collection by id.
|
||||
*
|
||||
|
||||
@@ -1,6 +1,10 @@
|
||||
import vine, { SimpleMessagesProvider } from '@vinejs/vine';
|
||||
import { Visibility } from '../enums/visibility.js';
|
||||
|
||||
const params = vine.object({
|
||||
id: vine.string().trim(),
|
||||
});
|
||||
|
||||
export const createCollectionValidator = vine.compile(
|
||||
vine.object({
|
||||
name: vine.string().trim().minLength(1).maxLength(254),
|
||||
@@ -17,9 +21,13 @@ export const updateCollectionValidator = vine.compile(
|
||||
visibility: vine.enum(Visibility),
|
||||
nextId: vine.string().optional(),
|
||||
|
||||
params: vine.object({
|
||||
id: vine.string().trim(),
|
||||
}),
|
||||
params,
|
||||
})
|
||||
);
|
||||
|
||||
export const deleteCollectionValidator = vine.compile(
|
||||
vine.object({
|
||||
params,
|
||||
})
|
||||
);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user