Files
sugarr/src/app/controllers/RequestController.ts

43 lines
1.3 KiB
TypeScript

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) });
}
}
}