mirror of
https://github.com/Sonny93/my-links.git
synced 2025-12-08 22:53:25 +00:00
feat: add create collection controller + validator
This commit is contained in:
@@ -1,3 +1,30 @@
|
||||
// import type { HttpContext } from '@adonisjs/core/http';
|
||||
import PATHS from '#constants/paths';
|
||||
import Collection from '#models/collection';
|
||||
import { collectionValidator } from '#validators/collection';
|
||||
import type { HttpContext } from '@adonisjs/core/http';
|
||||
|
||||
export default class CollectionsController {}
|
||||
export default class CollectionsController {
|
||||
async index({ inertia }: HttpContext) {
|
||||
return inertia.render('app');
|
||||
}
|
||||
|
||||
async showCreatePage({ inertia }: HttpContext) {
|
||||
return inertia.render('collection/create');
|
||||
}
|
||||
|
||||
async store({ request, response, auth }: HttpContext) {
|
||||
const payload = await request.validateUsing(collectionValidator);
|
||||
const collection = await Collection.create({
|
||||
...payload,
|
||||
authorId: auth.user?.id!,
|
||||
});
|
||||
return this.redirectToCollectionId(response, collection.id);
|
||||
}
|
||||
|
||||
private redirectToCollectionId(
|
||||
response: HttpContext['response'],
|
||||
collectionId: Collection['id']
|
||||
) {
|
||||
return response.redirect(`${PATHS.APP}?categoryId=${collectionId}`);
|
||||
}
|
||||
}
|
||||
|
||||
9
app/middleware/log_request.ts
Normal file
9
app/middleware/log_request.ts
Normal file
@@ -0,0 +1,9 @@
|
||||
import { HttpContext } from '@adonisjs/core/http';
|
||||
import logger from '@adonisjs/core/services/logger';
|
||||
|
||||
export default class LogRequest {
|
||||
async handle({ request }: HttpContext, next: () => Promise<void>) {
|
||||
logger.info(`-> ${request.method()}: ${request.url()}`);
|
||||
await next();
|
||||
}
|
||||
}
|
||||
16
app/validators/collection.ts
Normal file
16
app/validators/collection.ts
Normal file
@@ -0,0 +1,16 @@
|
||||
import vine, { SimpleMessagesProvider } from '@vinejs/vine';
|
||||
import { Visibility } from '../enums/visibility.js';
|
||||
|
||||
export const collectionValidator = vine.compile(
|
||||
vine.object({
|
||||
name: vine.string().trim().minLength(1).maxLength(254),
|
||||
description: vine.string().trim().maxLength(300).optional(),
|
||||
visibility: vine.enum(Visibility),
|
||||
nextId: vine.string().optional(),
|
||||
})
|
||||
);
|
||||
|
||||
collectionValidator.messagesProvider = new SimpleMessagesProvider({
|
||||
name: 'Collection name is required',
|
||||
'visibility.required': 'Collection visibiliy is required',
|
||||
});
|
||||
@@ -96,12 +96,12 @@
|
||||
"semi": true,
|
||||
"singleQuote": true,
|
||||
"useTabs": false,
|
||||
"quoteProps": "consistent",
|
||||
"quoteProps": "as-needed",
|
||||
"bracketSpacing": true,
|
||||
"arrowParens": "always",
|
||||
"printWidth": 100
|
||||
"printWidth": 80
|
||||
},
|
||||
"lint-staged": {
|
||||
"*.js,*.ts,*.jsx,*.tsx": "eslint --cache --fix"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -25,6 +25,7 @@ server.errorHandler(() => import('#exceptions/handler'));
|
||||
server.use([
|
||||
() => import('#middleware/container_bindings_middleware'),
|
||||
() => import('@adonisjs/static/static_middleware'),
|
||||
() => import('#middleware/log_request'),
|
||||
() => import('@adonisjs/cors/cors_middleware'),
|
||||
() => import('@adonisjs/vite/vite_middleware'),
|
||||
() => import('@adonisjs/inertia/inertia_middleware'),
|
||||
|
||||
@@ -1,12 +1,26 @@
|
||||
import PATHS from '#constants/paths';
|
||||
import { middleware } from '#start/kernel';
|
||||
import router from '@adonisjs/core/services/router';
|
||||
|
||||
const CollectionsController = () =>
|
||||
import('#controllers/collections_controller');
|
||||
const UsersController = () => import('#controllers/users_controller');
|
||||
const AppsController = () => import('#controllers/apps_controller');
|
||||
|
||||
router.get(PATHS.HOME, [AppsController, 'index']);
|
||||
|
||||
router.get(PATHS.AUTH.LOGIN, [UsersController, 'login']);
|
||||
router.get(PATHS.AUTH.GOOGLE, [UsersController, 'google']);
|
||||
router.get('/auth/callback', [UsersController, 'callbackAuth']);
|
||||
router.get(PATHS.AUTH.LOGOUT, [UsersController, 'logout']);
|
||||
|
||||
router
|
||||
.group(() => {
|
||||
router.get(PATHS.AUTH.LOGOUT, [UsersController, 'logout']);
|
||||
router.get(PATHS.APP, [CollectionsController, 'index']);
|
||||
|
||||
router.get('/collections/create', [
|
||||
CollectionsController,
|
||||
'showCreatePage',
|
||||
]);
|
||||
router.post('/collections', [CollectionsController, 'store']);
|
||||
})
|
||||
.middleware([middleware.auth()]);
|
||||
|
||||
Reference in New Issue
Block a user