feat: add create link form

This commit is contained in:
Sonny
2024-05-09 23:14:42 +02:00
committed by Sonny
parent 2cf8c5ae02
commit 73f8c0c513
16 changed files with 244 additions and 64 deletions

View File

@@ -1,26 +1,26 @@
import PATHS from '#constants/paths';
import Collection from '#models/collection';
import User from '#models/user';
import { collectionValidator } from '#validators/collection';
import type { HttpContext } from '@adonisjs/core/http';
export default class CollectionsController {
async index({ auth, inertia }: HttpContext) {
const collections = await Collection.findManyBy('author_id', auth.user!.id);
// Dashboard
async index({ auth, inertia, response }: HttpContext) {
const collections = await this.getCollectionByAuthorId(auth.user!.id);
if (collections.length === 0) {
return response.redirect('/collections/create');
}
const collectionsWithLinks = await Promise.all(
collections.map(async (collection) => {
await collection.load('links');
return collection;
})
);
return inertia.render('dashboard', { collections: collectionsWithLinks });
return inertia.render('dashboard', { collections });
}
// Create collection form
async showCreatePage({ inertia }: HttpContext) {
return inertia.render('collections/create');
}
// Method called when creating a collection
async store({ request, response, auth }: HttpContext) {
const payload = await request.validateUsing(collectionValidator);
const collection = await Collection.create({
@@ -30,7 +30,17 @@ export default class CollectionsController {
return this.redirectToCollectionId(response, collection.id);
}
private redirectToCollectionId(
async getCollectionById(id: Collection['id']) {
return await Collection.find(id);
}
async getCollectionByAuthorId(authorId: User['id']) {
return await Collection.query()
.where('author_id', authorId)
.preload('links');
}
redirectToCollectionId(
response: HttpContext['response'],
collectionId: Collection['id']
) {