Files
my-links/app/controllers/shared_collections_controller.ts
Sonny 83c1966946 feat: recreate shared page
+ improve security by not exposing author's email
2024-11-10 00:00:20 +01:00

62 lines
1.6 KiB
TypeScript

import { Visibility } from '#enums/visibility';
import Collection from '#models/collection';
import Link from '#models/link';
import User from '#models/user';
import { getSharedCollectionValidator } from '#validators/shared_collection';
import type { HttpContext } from '@adonisjs/core/http';
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(),
});
}
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']) {
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(),
};
}
}