mirror of
https://github.com/FJurmanovic/sugarr.git
synced 2026-11-11 07:41:08 +00:00
Add Health/Recommendations/Request/Webhook controllers
This commit is contained in:
42
src/app/controllers/RequestController.ts
Normal file
42
src/app/controllers/RequestController.ts
Normal file
@@ -0,0 +1,42 @@
|
||||
import type { FastifyInstance, FastifyReply, FastifyRequest } from 'fastify';
|
||||
import { Controller } from '../../core/server/Controller';
|
||||
import type { AppConfig } from '../config/AppConfig';
|
||||
import type { RadarrClient } from '../clients/radarr/RadarrClient';
|
||||
|
||||
interface RequestBody {
|
||||
tmdb_id?: number;
|
||||
}
|
||||
|
||||
export class RequestController extends Controller {
|
||||
constructor(
|
||||
private readonly config: AppConfig,
|
||||
private readonly radarr: RadarrClient | undefined
|
||||
) {
|
||||
super();
|
||||
}
|
||||
|
||||
register(app: FastifyInstance): void {
|
||||
app.post('/recommendations/request', (request, reply) =>
|
||||
this.handle(request as FastifyRequest<{ Body: RequestBody }>, reply)
|
||||
);
|
||||
}
|
||||
|
||||
private async handle(request: FastifyRequest<{ Body: RequestBody }>, reply: FastifyReply) {
|
||||
if (!this.config.RADARR_ENABLED || !this.radarr) {
|
||||
reply.code(400).send({ error: 'Radarr is not enabled' });
|
||||
return;
|
||||
}
|
||||
const tmdbId = request.body?.tmdb_id;
|
||||
if (!tmdbId) {
|
||||
reply.code(400).send({ error: 'body must be {"tmdb_id": 12345}' });
|
||||
return;
|
||||
}
|
||||
try {
|
||||
const movie = await this.radarr.lookup(tmdbId);
|
||||
await this.radarr.addMovie(movie);
|
||||
reply.send({ requested: true, title: movie.title });
|
||||
} catch (err) {
|
||||
reply.code(500).send({ error: String(err) });
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user