mirror of
https://github.com/Sonny93/my-links.git
synced 2025-12-09 15:05:35 +00:00
refactor: split links controller into multiple controllers
This commit is contained in:
@@ -1,32 +0,0 @@
|
||||
import Collection from '#collections/models/collection';
|
||||
import { HttpContext } from '@adonisjs/core/http';
|
||||
import vine from '@vinejs/vine';
|
||||
|
||||
export default class BaseCollectionController {
|
||||
protected collectionIdValidator = vine.compile(
|
||||
vine.object({
|
||||
collectionId: vine.number().positive().optional(),
|
||||
})
|
||||
);
|
||||
|
||||
async validateCollectionId(collectionIdRequired: boolean = true) {
|
||||
const ctx = HttpContext.getOrFail();
|
||||
const { collectionId } = await ctx.request.validateUsing(
|
||||
this.collectionIdValidator
|
||||
);
|
||||
if (!collectionId && collectionIdRequired) {
|
||||
console.log('redirecting to dashboard');
|
||||
ctx.response.redirectToNamedRoute('dashboard');
|
||||
return null;
|
||||
}
|
||||
console.log('collectionId', collectionId);
|
||||
return collectionId;
|
||||
}
|
||||
|
||||
redirectToCollectionId(collectionId: Collection['id']) {
|
||||
const ctx = HttpContext.getOrFail();
|
||||
return ctx.response.redirectToNamedRoute('dashboard', {
|
||||
qs: { collectionId },
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -1,17 +1,15 @@
|
||||
import BaseCollectionController from '#collections/controllers/base_collection_controller';
|
||||
import { CollectionService } from '#collections/services/collection_service';
|
||||
import { createCollectionValidator } from '#collections/validators/create_collection_validator';
|
||||
import { inject } from '@adonisjs/core';
|
||||
import { type HttpContext } from '@adonisjs/core/http';
|
||||
|
||||
@inject()
|
||||
export default class CreateCollectionController extends BaseCollectionController {
|
||||
constructor(private collectionService: CollectionService) {
|
||||
super();
|
||||
}
|
||||
export default class CreateCollectionController {
|
||||
constructor(private collectionService: CollectionService) {}
|
||||
|
||||
async render({ inertia }: HttpContext) {
|
||||
const collections = await this.collectionService.getCollectionsByAuthorId();
|
||||
const collections =
|
||||
await this.collectionService.getCollectionsForAuthenticatedUser();
|
||||
return inertia.render('collections/create', {
|
||||
disableHomeLink: collections.length === 0,
|
||||
});
|
||||
@@ -20,6 +18,6 @@ export default class CreateCollectionController extends BaseCollectionController
|
||||
async execute({ request }: HttpContext) {
|
||||
const payload = await request.validateUsing(createCollectionValidator);
|
||||
const collection = await this.collectionService.createCollection(payload);
|
||||
return this.redirectToCollectionId(collection.id);
|
||||
return this.collectionService.redirectToCollectionId(collection.id);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,17 +1,14 @@
|
||||
import BaseCollectionController from '#collections/controllers/base_collection_controller';
|
||||
import { CollectionService } from '#collections/services/collection_service';
|
||||
import { deleteCollectionValidator } from '#collections/validators/delete_collection_validator';
|
||||
import { inject } from '@adonisjs/core';
|
||||
import { HttpContext } from '@adonisjs/core/http';
|
||||
|
||||
@inject()
|
||||
export default class DeleteCollectionController extends BaseCollectionController {
|
||||
constructor(private collectionService: CollectionService) {
|
||||
super();
|
||||
}
|
||||
export default class DeleteCollectionController {
|
||||
constructor(private collectionService: CollectionService) {}
|
||||
|
||||
async render({ inertia }: HttpContext) {
|
||||
const collectionId = await this.validateCollectionId();
|
||||
const collectionId = await this.collectionService.validateCollectionId();
|
||||
if (!collectionId) return;
|
||||
|
||||
const collection =
|
||||
|
||||
@@ -1,18 +1,17 @@
|
||||
import BaseCollectionController from '#collections/controllers/base_collection_controller';
|
||||
import { CollectionService } from '#collections/services/collection_service';
|
||||
import { inject } from '@adonisjs/core';
|
||||
import type { HttpContext } from '@adonisjs/core/http';
|
||||
|
||||
@inject()
|
||||
export default class ShowCollectionsController extends BaseCollectionController {
|
||||
constructor(private collectionService: CollectionService) {
|
||||
super();
|
||||
}
|
||||
export default class ShowCollectionsController {
|
||||
constructor(private collectionService: CollectionService) {}
|
||||
|
||||
// Dashboard
|
||||
async render({ inertia, response }: HttpContext) {
|
||||
const activeCollectionId = await this.validateCollectionId(false);
|
||||
const collections = await this.collectionService.getCollectionsByAuthorId();
|
||||
const activeCollectionId =
|
||||
await this.collectionService.validateCollectionId(false);
|
||||
const collections =
|
||||
await this.collectionService.getCollectionsForAuthenticatedUser();
|
||||
if (collections.length === 0) {
|
||||
return response.redirectToNamedRoute('collection.create-form');
|
||||
}
|
||||
|
||||
@@ -1,17 +1,14 @@
|
||||
import BaseCollectionController from '#collections/controllers/base_collection_controller';
|
||||
import { CollectionService } from '#collections/services/collection_service';
|
||||
import { updateCollectionValidator } from '#collections/validators/update_collection_validator';
|
||||
import { inject } from '@adonisjs/core';
|
||||
import { HttpContext } from '@adonisjs/core/http';
|
||||
|
||||
@inject()
|
||||
export default class UpdateCollectionController extends BaseCollectionController {
|
||||
constructor(private collectionService: CollectionService) {
|
||||
super();
|
||||
}
|
||||
export default class UpdateCollectionController {
|
||||
constructor(private collectionService: CollectionService) {}
|
||||
|
||||
async render({ inertia }: HttpContext) {
|
||||
const collectionId = await this.validateCollectionId();
|
||||
const collectionId = await this.collectionService.validateCollectionId();
|
||||
if (!collectionId) return;
|
||||
|
||||
const collection =
|
||||
@@ -28,6 +25,6 @@ export default class UpdateCollectionController extends BaseCollectionController
|
||||
} = await request.validateUsing(updateCollectionValidator);
|
||||
|
||||
await this.collectionService.updateCollection(collectionId, payload);
|
||||
return this.redirectToCollectionId(collectionId);
|
||||
return this.collectionService.redirectToCollectionId(collectionId);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import { Visibility } from '#collections/enums/visibility';
|
||||
import Collection from '#collections/models/collection';
|
||||
import { collectionIdValidator } from '#collections/validators/collection_id_validator';
|
||||
import { HttpContext } from '@adonisjs/core/http';
|
||||
import db from '@adonisjs/lucid/services/db';
|
||||
|
||||
@@ -20,7 +21,7 @@ export class CollectionService {
|
||||
.firstOrFail();
|
||||
}
|
||||
|
||||
async getCollectionsByAuthorId() {
|
||||
async getCollectionsForAuthenticatedUser() {
|
||||
const context = this.getAuthContext();
|
||||
return await Collection.query()
|
||||
.where('author_id', context.auth.user!.id)
|
||||
@@ -67,4 +68,28 @@ export class CollectionService {
|
||||
}
|
||||
return context;
|
||||
}
|
||||
|
||||
async validateCollectionId(collectionIdRequired: boolean = true) {
|
||||
const ctx = HttpContext.getOrFail();
|
||||
const { collectionId } = await ctx.request.validateUsing(
|
||||
collectionIdValidator
|
||||
);
|
||||
if (!collectionId && collectionIdRequired) {
|
||||
this.redirectToDashboard();
|
||||
return null;
|
||||
}
|
||||
return collectionId;
|
||||
}
|
||||
|
||||
redirectToCollectionId(collectionId: Collection['id']) {
|
||||
const ctx = HttpContext.getOrFail();
|
||||
return ctx.response.redirectToNamedRoute('dashboard', {
|
||||
qs: { collectionId },
|
||||
});
|
||||
}
|
||||
|
||||
redirectToDashboard() {
|
||||
const ctx = HttpContext.getOrFail();
|
||||
return ctx.response.redirectToNamedRoute('dashboard');
|
||||
}
|
||||
}
|
||||
|
||||
7
app/collections/validators/collection_id_validator.ts
Normal file
7
app/collections/validators/collection_id_validator.ts
Normal file
@@ -0,0 +1,7 @@
|
||||
import vine from '@vinejs/vine';
|
||||
|
||||
export const collectionIdValidator = vine.compile(
|
||||
vine.object({
|
||||
collectionId: vine.number().positive().optional(),
|
||||
})
|
||||
);
|
||||
Reference in New Issue
Block a user