mirror of
https://github.com/Sonny93/my-links.git
synced 2025-12-09 15:05:35 +00:00
feat: create edit collection page
This commit is contained in:
@@ -1,13 +1,16 @@
|
||||
import PATHS from '#constants/paths';
|
||||
import Collection from '#models/collection';
|
||||
import User from '#models/user';
|
||||
import { collectionValidator } from '#validators/collection';
|
||||
import {
|
||||
createCollectionValidator,
|
||||
updateCollectionValidator,
|
||||
} from '#validators/collection';
|
||||
import type { HttpContext } from '@adonisjs/core/http';
|
||||
|
||||
export default class CollectionsController {
|
||||
// Dashboard
|
||||
async index({ auth, inertia, request, response }: HttpContext) {
|
||||
const collections = await this.getCollectionByAuthorId(auth.user!.id);
|
||||
const collections = await this.getCollectionsByAuthorId(auth.user!.id);
|
||||
if (collections.length === 0) {
|
||||
return response.redirect('/collections/create');
|
||||
}
|
||||
@@ -29,7 +32,7 @@ export default class CollectionsController {
|
||||
|
||||
// Create collection form
|
||||
async showCreatePage({ inertia, auth }: HttpContext) {
|
||||
const collections = await this.getCollectionByAuthorId(auth.user!.id);
|
||||
const collections = await this.getCollectionsByAuthorId(auth.user!.id);
|
||||
return inertia.render('collections/create', {
|
||||
disableHomeLink: collections.length === 0,
|
||||
});
|
||||
@@ -37,7 +40,7 @@ export default class CollectionsController {
|
||||
|
||||
// Method called when creating a collection
|
||||
async store({ request, response, auth }: HttpContext) {
|
||||
const payload = await request.validateUsing(collectionValidator);
|
||||
const payload = await request.validateUsing(createCollectionValidator);
|
||||
const collection = await Collection.create({
|
||||
...payload,
|
||||
authorId: auth.user?.id!,
|
||||
@@ -45,11 +48,54 @@ export default class CollectionsController {
|
||||
return this.redirectToCollectionId(response, collection.id);
|
||||
}
|
||||
|
||||
async getCollectionById(id: Collection['id']) {
|
||||
return await Collection.find(id);
|
||||
async showEditPage({ auth, request, inertia, response }: HttpContext) {
|
||||
const collectionId = request.qs()?.collectionId;
|
||||
if (!collectionId) {
|
||||
return response.redirect('/dashboard');
|
||||
}
|
||||
|
||||
const collection = await this.getCollectionById(
|
||||
collectionId,
|
||||
auth.user!.id
|
||||
);
|
||||
return inertia.render('collections/edit', {
|
||||
collection,
|
||||
});
|
||||
}
|
||||
|
||||
async getCollectionByAuthorId(authorId: User['id']) {
|
||||
/**
|
||||
* Get collection by id.
|
||||
*
|
||||
* /!\ Only return private collection (create by the current user)
|
||||
*/
|
||||
async getCollectionById(id: Collection['id'], userId: User['id']) {
|
||||
return await Collection.query()
|
||||
.where('id', id)
|
||||
.andWhere('author_id', userId)
|
||||
.firstOrFail();
|
||||
}
|
||||
|
||||
async update({ request, auth, response }: HttpContext) {
|
||||
const { params, ...payload } = await request.validateUsing(
|
||||
updateCollectionValidator
|
||||
);
|
||||
|
||||
// Cant use validator (vinejs) custom rule 'cause its too generic,
|
||||
// because we have to find a collection by identifier and
|
||||
// check whether the current user is the author.
|
||||
// https://vinejs.dev/docs/extend/custom_rules
|
||||
await this.getCollectionById(params.id, auth.user!.id);
|
||||
|
||||
await Collection.updateOrCreate(
|
||||
{
|
||||
id: params.id,
|
||||
},
|
||||
payload
|
||||
);
|
||||
return this.redirectToCollectionId(response, params.id);
|
||||
}
|
||||
|
||||
async getCollectionsByAuthorId(authorId: User['id']) {
|
||||
return await Collection.query()
|
||||
.where('author_id', authorId)
|
||||
.preload('links');
|
||||
|
||||
Reference in New Issue
Block a user