mirror of
https://github.com/Sonny93/my-links.git
synced 2025-12-08 14:43:24 +00:00
refactor: compute favorite links in backend
This commit is contained in:
@@ -1,17 +1,24 @@
|
|||||||
import { CollectionService } from '#collections/services/collection_service';
|
import { CollectionService } from '#collections/services/collection_service';
|
||||||
|
import { LinkService } from '#links/services/link_service';
|
||||||
import { inject } from '@adonisjs/core';
|
import { inject } from '@adonisjs/core';
|
||||||
import type { HttpContext } from '@adonisjs/core/http';
|
import type { HttpContext } from '@adonisjs/core/http';
|
||||||
|
|
||||||
@inject()
|
@inject()
|
||||||
export default class ShowCollectionsController {
|
export default class ShowCollectionsController {
|
||||||
constructor(private collectionService: CollectionService) {}
|
constructor(
|
||||||
|
private collectionService: CollectionService,
|
||||||
|
private linkService: LinkService
|
||||||
|
) {}
|
||||||
|
|
||||||
// Dashboard
|
// Dashboard
|
||||||
async render({ inertia, response }: HttpContext) {
|
async render({ inertia, response }: HttpContext) {
|
||||||
const activeCollectionId =
|
const activeCollectionId =
|
||||||
await this.collectionService.validateCollectionId(false);
|
await this.collectionService.validateCollectionId(false);
|
||||||
const collections =
|
const [collections, favoriteLinks] = await Promise.all([
|
||||||
await this.collectionService.getCollectionsForAuthenticatedUser();
|
this.collectionService.getCollectionsForAuthenticatedUser(),
|
||||||
|
this.linkService.getFavoriteLinksForAuthenticatedUser(),
|
||||||
|
]);
|
||||||
|
|
||||||
if (collections.length === 0) {
|
if (collections.length === 0) {
|
||||||
return response.redirectToNamedRoute('collection.create-form');
|
return response.redirectToNamedRoute('collection.create-form');
|
||||||
}
|
}
|
||||||
@@ -26,8 +33,8 @@ export default class ShowCollectionsController {
|
|||||||
|
|
||||||
return inertia.render('dashboard', {
|
return inertia.render('dashboard', {
|
||||||
collections: collections.map((collection) => collection.serialize()),
|
collections: collections.map((collection) => collection.serialize()),
|
||||||
activeCollection:
|
favoriteLinks: favoriteLinks.map((link) => link.serialize()),
|
||||||
activeCollection?.serialize() || collections[0].serialize(),
|
activeCollection: activeCollection?.serialize(),
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -50,6 +50,14 @@ export class LinkService {
|
|||||||
.update({ favorite });
|
.update({ favorite });
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async getFavoriteLinksForAuthenticatedUser() {
|
||||||
|
const context = this.getAuthContext();
|
||||||
|
return await Link.query()
|
||||||
|
.where('author_id', context.auth.user!.id)
|
||||||
|
.where('favorite', true)
|
||||||
|
.orderBy('created_at');
|
||||||
|
}
|
||||||
|
|
||||||
getAuthContext() {
|
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) {
|
||||||
|
|||||||
Reference in New Issue
Block a user