From 5f7fad26fa51d4052f3f3c8e0b3eb4227a6fe751 Mon Sep 17 00:00:00 2001 From: Sonny Date: Thu, 21 Aug 2025 02:24:11 +0200 Subject: [PATCH] refactor: shared collection controller --- .../services/collection_service.ts | 11 +++- .../shared_collections_controller.ts | 58 +++---------------- 2 files changed, 18 insertions(+), 51 deletions(-) diff --git a/app/collections/services/collection_service.ts b/app/collections/services/collection_service.ts index 994492f..4c3b514 100644 --- a/app/collections/services/collection_service.ts +++ b/app/collections/services/collection_service.ts @@ -61,7 +61,16 @@ export class CollectionService { .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(); if (!context.auth.user || !context.auth.user.id) { throw new Error('User not authenticated'); diff --git a/app/shared_collections/controllers/shared_collections_controller.ts b/app/shared_collections/controllers/shared_collections_controller.ts index a16c4f0..9cf398b 100644 --- a/app/shared_collections/controllers/shared_collections_controller.ts +++ b/app/shared_collections/controllers/shared_collections_controller.ts @@ -1,61 +1,19 @@ -import { Visibility } from '#collections/enums/visibility'; -import Collection from '#collections/models/collection'; -import Link from '#links/models/link'; -import User from '#user/models/user'; +import { CollectionService } from '#collections/services/collection_service'; +import { inject } from '@adonisjs/core'; import type { HttpContext } from '@adonisjs/core/http'; import { getSharedCollectionValidator } from '../validators/shared_collection.js'; -class LinkWithoutFavoriteDto { - 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(), - }); -} - +@inject() export default class SharedCollectionsController { + constructor(private collectionService: CollectionService) {} + 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']) { - 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(), - }; + const activeCollection = + await this.collectionService.getPublicCollectionById(params.id); + return inertia.render('shared', { activeCollection }); } }