refactor: shared collection controller

This commit is contained in:
Sonny
2025-08-21 02:24:11 +02:00
parent 87ced07d20
commit 5f7fad26fa
2 changed files with 18 additions and 51 deletions

View File

@@ -61,7 +61,16 @@ export class CollectionService {
.delete(); .delete();
} }
getAuthContext() { getPublicCollectionById(id: Collection['id']) {
return Collection.query()
.where('id', id)
.andWhere('visibility', Visibility.PUBLIC)
.preload('links')
.preload('author')
.firstOrFail();
}
private getAuthContext() {
const context = HttpContext.getOrFail(); const context = HttpContext.getOrFail();
if (!context.auth.user || !context.auth.user.id) { if (!context.auth.user || !context.auth.user.id) {
throw new Error('User not authenticated'); throw new Error('User not authenticated');

View File

@@ -1,61 +1,19 @@
import { Visibility } from '#collections/enums/visibility'; import { CollectionService } from '#collections/services/collection_service';
import Collection from '#collections/models/collection'; import { inject } from '@adonisjs/core';
import Link from '#links/models/link';
import User from '#user/models/user';
import type { HttpContext } from '@adonisjs/core/http'; import type { HttpContext } from '@adonisjs/core/http';
import { getSharedCollectionValidator } from '../validators/shared_collection.js'; import { getSharedCollectionValidator } from '../validators/shared_collection.js';
class LinkWithoutFavoriteDto { @inject()
constructor(private link: Link) {}
toJson = () => ({
id: this.link.id,
name: this.link.name,
description: this.link.description,
url: this.link.url,
collectionId: this.link.collectionId,
createdAt: this.link.createdAt.toString(),
updatedAt: this.link.updatedAt.toString(),
});
}
class UserWithoutEmailDto {
constructor(private user: User) {}
toJson = () => ({
id: this.user.id,
fullname: this.user.name,
avatarUrl: this.user.avatarUrl,
isAdmin: this.user.isAdmin,
createdAt: this.user.createdAt.toString(),
updatedAt: this.user.updatedAt.toString(),
});
}
export default class SharedCollectionsController { export default class SharedCollectionsController {
constructor(private collectionService: CollectionService) {}
async index({ request, inertia }: HttpContext) { async index({ request, inertia }: HttpContext) {
const { params } = await request.validateUsing( const { params } = await request.validateUsing(
getSharedCollectionValidator getSharedCollectionValidator
); );
const collection = await this.getSharedCollectionById(params.id); const activeCollection =
return inertia.render('shared', { collection }); await this.collectionService.getPublicCollectionById(params.id);
} return inertia.render('shared', { activeCollection });
private async getSharedCollectionById(id: Collection['id']) {
const collection = await Collection.query()
.where('id', id)
.andWhere('visibility', Visibility.PUBLIC)
.preload('links')
.preload('author')
.firstOrFail();
return {
...collection.serialize(),
links: collection.links.map((link) =>
new LinkWithoutFavoriteDto(link).toJson()
),
author: new UserWithoutEmailDto(collection.author).toJson(),
};
} }
} }