mirror of
https://github.com/Sonny93/my-links.git
synced 2025-12-08 22:53:25 +00:00
feat: add shared collection page
This commit is contained in:
24
app/controllers/shared_collections_controller.ts
Normal file
24
app/controllers/shared_collections_controller.ts
Normal file
@@ -0,0 +1,24 @@
|
||||
import { Visibility } from '#enums/visibility';
|
||||
import Collection from '#models/collection';
|
||||
import { getSharedCollectionValidator } from '#validators/shared_collection';
|
||||
import type { HttpContext } from '@adonisjs/core/http';
|
||||
|
||||
export default class SharedCollectionsController {
|
||||
async index({ request, inertia }: HttpContext) {
|
||||
const { params } = await request.validateUsing(
|
||||
getSharedCollectionValidator
|
||||
);
|
||||
|
||||
const collection = await this.getSharedCollectionById(params.id);
|
||||
return inertia.render('shared', { collection });
|
||||
}
|
||||
|
||||
private async getSharedCollectionById(id: Collection['id']) {
|
||||
return await Collection.query()
|
||||
.where('id', id)
|
||||
.andWhere('visibility', Visibility.PUBLIC)
|
||||
.preload('links')
|
||||
.preload('author')
|
||||
.firstOrFail();
|
||||
}
|
||||
}
|
||||
@@ -21,7 +21,7 @@ export default class Collection extends AppBaseModel {
|
||||
@column()
|
||||
declare authorId: number;
|
||||
|
||||
@belongsTo(() => User, { foreignKey: 'author_id' })
|
||||
@belongsTo(() => User, { foreignKey: 'authorId' })
|
||||
declare author: BelongsTo<typeof User>;
|
||||
|
||||
@hasMany(() => Link)
|
||||
|
||||
11
app/validators/shared_collection.ts
Normal file
11
app/validators/shared_collection.ts
Normal file
@@ -0,0 +1,11 @@
|
||||
import vine from '@vinejs/vine';
|
||||
|
||||
const params = vine.object({
|
||||
id: vine.number(),
|
||||
});
|
||||
|
||||
export const getSharedCollectionValidator = vine.compile(
|
||||
vine.object({
|
||||
params,
|
||||
})
|
||||
);
|
||||
Reference in New Issue
Block a user