mirror of
https://github.com/Sonny93/my-links.git
synced 2025-12-09 15:05:35 +00:00
refactor: split backend by context instead of type 'controllers/models/...'
This commit is contained in:
@@ -0,0 +1,61 @@
|
||||
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 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(),
|
||||
});
|
||||
}
|
||||
|
||||
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(),
|
||||
};
|
||||
}
|
||||
}
|
||||
1
app/shared_collections/routes/routes.ts
Normal file
1
app/shared_collections/routes/routes.ts
Normal file
@@ -0,0 +1 @@
|
||||
import './shared_collections_routes.js';
|
||||
@@ -0,0 +1,6 @@
|
||||
import router from '@adonisjs/core/services/router';
|
||||
|
||||
const SharedCollectionsController = () =>
|
||||
import('#shared_collections/controllers/shared_collections_controller');
|
||||
|
||||
router.get('/shared/:id', [SharedCollectionsController, 'index']).as('shared');
|
||||
8
app/shared_collections/validators/shared_collection.ts
Normal file
8
app/shared_collections/validators/shared_collection.ts
Normal file
@@ -0,0 +1,8 @@
|
||||
import { params } from '#core/validators/params_object';
|
||||
import vine from '@vinejs/vine';
|
||||
|
||||
export const getSharedCollectionValidator = vine.compile(
|
||||
vine.object({
|
||||
params,
|
||||
})
|
||||
);
|
||||
Reference in New Issue
Block a user