feat: add api controllers and routes for browser extension

This commit is contained in:
Sonny
2025-08-28 16:44:31 +02:00
parent 9aa71dad30
commit 208f2c631f
28 changed files with 353 additions and 5 deletions

View File

@@ -0,0 +1,23 @@
import { LinkService } from '#links/services/link_service';
import { createLinkValidator } from '#links/validators/create_link_validator';
import { inject } from '@adonisjs/core';
import { HttpContext } from '@adonisjs/core/http';
@inject()
export default class CreateLinkController {
constructor(private linkService: LinkService) {}
async execute({ request, response }: HttpContext) {
const { collectionId, ...payload } =
await request.validateUsing(createLinkValidator);
const link = await this.linkService.createLink({
...payload,
collectionId,
});
return response.json({
message: 'Link created successfully',
link: link.serialize(),
});
}
}

View File

@@ -0,0 +1,39 @@
import { CollectionService } from '#collections/services/collection_service';
import { LinkService } from '#links/services/link_service';
import { deleteLinkValidator } from '#links/validators/delete_link_validator';
import { inject } from '@adonisjs/core';
import { HttpContext } from '@adonisjs/core/http';
import db from '@adonisjs/lucid/services/db';
@inject()
export default class DeleteLinkController {
constructor(
protected collectionsService: CollectionService,
protected linkService: LinkService
) {}
async render({ auth, inertia, request }: HttpContext) {
const linkId = request.qs()?.linkId;
if (!linkId) {
return this.collectionsService.redirectToDashboard();
}
const link = await this.linkService.getLinkById(linkId, auth.user!.id);
await link.load('collection');
return inertia.render('links/delete', { link });
}
async execute({ request, auth }: HttpContext) {
const { params } = await request.validateUsing(deleteLinkValidator);
const link = await this.linkService.getLinkById(params.id, auth.user!.id);
await this.linkService.deleteLink(params.id);
return this.collectionsService.redirectToCollectionId(link.collectionId);
}
async getTotalLinksCount() {
const totalCount = await db.from('links').count('* as total');
return Number(totalCount[0].total);
}
}

View File

@@ -0,0 +1,13 @@
import { LinkService } from '#links/services/link_service';
import { inject } from '@adonisjs/core';
import { HttpContext } from '@adonisjs/core/http';
@inject()
export default class GetFavoriteLinksController {
constructor(private linkService: LinkService) {}
public async execute({ response }: HttpContext) {
const links = await this.linkService.getFavoriteLinksForAuthenticatedUser();
return response.json(links);
}
}

View File

@@ -0,0 +1,19 @@
import { LinkService } from '#links/services/link_service';
import { updateLinkValidator } from '#links/validators/update_link_validator';
import { inject } from '@adonisjs/core';
import { HttpContext } from '@adonisjs/core/http';
@inject()
export default class UpdateLinkController {
constructor(private linkService: LinkService) {}
async execute({ request, response }: HttpContext) {
const { params, ...payload } =
await request.validateUsing(updateLinkValidator);
await this.linkService.updateLink(params.id, payload);
return response.json({
message: 'Link updated successfully',
});
}
}

View File

@@ -0,0 +1,12 @@
import { middleware } from '#start/kernel';
import router from '@adonisjs/core/services/router';
const CreateLinkController = () =>
import('#api/links/controllers/create_link_controller');
router
.group(() => {
router.post('', [CreateLinkController, 'execute']).as('api-links.create');
})
.prefix('/api/v1/links')
.middleware([middleware.auth({ guards: ['api'] })]);

View File

@@ -0,0 +1,14 @@
import { middleware } from '#start/kernel';
import router from '@adonisjs/core/services/router';
const DeleteLinkController = () =>
import('#api/links/controllers/delete_link_controller');
router
.group(() => {
router
.delete('/:id', [DeleteLinkController, 'execute'])
.as('api-links.delete');
})
.prefix('/api/v1/links')
.middleware([middleware.auth({ guards: ['api'] })]);

View File

@@ -0,0 +1,14 @@
import { middleware } from '#start/kernel';
import router from '@adonisjs/core/services/router';
const GetFavoriteLinksController = () =>
import('#api/links/controllers/get_favorite_links_controller');
router
.group(() => {
router
.get('', [GetFavoriteLinksController, 'execute'])
.as('api-links.get-favorite-links');
})
.prefix('/api/v1/links/favorites')
.middleware([middleware.auth({ guards: ['api'] })]);

View File

@@ -0,0 +1,14 @@
import { middleware } from '#start/kernel';
import router from '@adonisjs/core/services/router';
const UpdateLinkController = () =>
import('#api/links/controllers/update_link_controller');
router
.group(() => {
router
.put('/:id', [UpdateLinkController, 'execute'])
.as('api-links.update');
})
.prefix('/api/v1/links')
.middleware([middleware.auth({ guards: ['api'] })]);

View File

@@ -0,0 +1,4 @@
import '#api/links/routes/api_create_link_routes';
import '#api/links/routes/api_delete_link_route';
import '#api/links/routes/api_get_favorite_links_routes';
import '#api/links/routes/api_update_link_route';