🔦 Adding redis to the moreInfo route in the API

pull/33/head
capitanwesler 4 years ago
parent aefe45a723
commit c04b349995

@ -2,6 +2,7 @@ import { NextFunction, Request, Response } from 'express';
import { requestGot } from '../utils/requestCall'; import { requestGot } from '../utils/requestCall';
import AnimeModel, { Anime } from '../database/models/anime.model'; import AnimeModel, { Anime } from '../database/models/anime.model';
import GenreModel, { Genre } from '../database/models/genre.model'; import GenreModel, { Genre } from '../database/models/genre.model';
import util from 'util';
import { import {
animeExtraInfo, animeExtraInfo,
getAnimeVideoPromo, getAnimeVideoPromo,
@ -10,6 +11,10 @@ import {
getRelatedAnimesMAL, getRelatedAnimesMAL,
} from '../utils/util'; } from '../utils/util';
import urls from '../utils/urls'; import urls from '../utils/urls';
import { redisClient } from '../database/connection';
// @ts-ignore
redisClient.get = util.promisify(redisClient.get);
/* /*
DirectoryController - async functions controlling the directory DirectoryController - async functions controlling the directory
@ -185,46 +190,59 @@ export default class DirectoryController {
async getMoreInfo(req: Request, res: Response, next: NextFunction) { async getMoreInfo(req: Request, res: Response, next: NextFunction) {
const { title } = req.params; const { title } = req.params;
let resultAnime: Anime | null; let resultQuery: Anime | null;
let resultAnime: any;
try { try {
resultAnime = await AnimeModel.findOne({ const resultQueryRedis: any = await redisClient.get(title);
if (resultQueryRedis) {
const resultRedis: any = JSON.parse(resultQueryRedis);
return res.status(200).json(resultRedis);
} else {
resultQuery = await AnimeModel.findOne({
$or: [{ title: { $eq: title } }, { title: { $eq: `${title} (TV)` } }], $or: [{ title: { $eq: title } }, { title: { $eq: `${title} (TV)` } }],
}); });
if (resultQuery?.jkanime) {
resultAnime = {
title: resultQuery?.title,
poster: resultQuery?.poster,
synopsis: resultQuery?.description,
status: resultQuery?.state,
type: resultQuery?.type,
rating: resultQuery?.score,
genres: resultQuery?.genres,
moreInfo: [await animeExtraInfo(resultQuery!.mal_id)],
promo: await getAnimeVideoPromo(resultQuery!.mal_id),
characters: await getAnimeCharacters(resultQuery!.mal_id),
related: await getRelatedAnimesFLV(resultQuery!.id),
};
} else {
resultAnime = {
title: resultQuery?.title,
poster: resultQuery?.poster,
synopsis: resultQuery?.description,
status: resultQuery?.state,
type: resultQuery?.type,
rating: resultQuery?.score,
genres: resultQuery?.genres,
moreInfo: await animeExtraInfo(resultQuery!.mal_id),
promo: await getAnimeVideoPromo(resultQuery!.mal_id),
characters: await getAnimeCharacters(resultQuery!.mal_id),
related: await getRelatedAnimesMAL(resultQuery!.mal_id),
};
}
redisClient.set(title, JSON.stringify(resultAnime));
}
} catch (err) { } catch (err) {
return next(err); return next(err);
} }
if (resultAnime) { if (resultAnime) {
if (!resultAnime?.jkanime) { res.status(200).json(resultAnime);
res.status(200).json({
title: resultAnime.title || null,
poster: resultAnime.poster || null,
synopsis: resultAnime.description || null,
status: resultAnime.state || null,
type: resultAnime.type || null,
rating: resultAnime.score || null,
genres: resultAnime.genres || null,
moreInfo: [await animeExtraInfo(resultAnime.mal_id)],
promo: await getAnimeVideoPromo(resultAnime.mal_id),
characters: await getAnimeCharacters(resultAnime.mal_id),
related: await getRelatedAnimesFLV(resultAnime.id),
});
} else {
res.status(200).json({
title: resultAnime.title || null,
poster: resultAnime.poster || null,
synopsis: resultAnime.description || null,
status: resultAnime.state || null,
type: resultAnime.type || null,
rating: resultAnime.score || null,
genres: resultAnime.genres || null,
moreInfo: await animeExtraInfo(resultAnime.mal_id),
promo: await getAnimeVideoPromo(resultAnime.mal_id),
characters: await getAnimeCharacters(resultAnime.mal_id),
related: await getRelatedAnimesMAL(resultAnime.mal_id),
});
}
} else { } else {
res.status(500).json({ message: 'Aruppi lost in the shell' }); res.status(500).json({ message: 'Aruppi lost in the shell' });
} }

@ -1,5 +1,5 @@
import mongoose from 'mongoose'; import mongoose from 'mongoose';
// import redis, { RedisClient } from 'redis'; import redis, { RedisClient } from 'redis';
/* /*
Create the connection to the database Create the connection to the database
@ -26,16 +26,17 @@ export const createConnectionMongo = (databaseObj: {
}); });
}; };
// export const createConnectionRedis = (redisObj: { /*
// host: string; Create the connection to the cache of
// port: number; redis, and exporting the redis client
// }) => { with the call of this file.
// const client: RedisClient = redis.createClient({ */
// host: redisObj.host,
// port: redisObj.port, export const redisClient: RedisClient = redis.createClient({
// }); host: process.env.REDIS_HOST,
port: parseInt(process.env.REDIS_PORT!),
});
// client.on('connect', () => { redisClient.on('connect', () => {
// console.log('Redis connected: redis.'); console.log('Redis connected: redis.');
// }); });
// };

@ -5,7 +5,6 @@ import dotenv from 'dotenv';
import { errorHandler, notFound } from './middlewares/middleware'; import { errorHandler, notFound } from './middlewares/middleware';
import { import {
createConnectionMongo, createConnectionMongo,
// createConnectionRedis,
} from './database/connection'; } from './database/connection';
import routes from './routes'; import routes from './routes';
import { Server } from 'node:http'; import { Server } from 'node:http';
@ -17,10 +16,6 @@ createConnectionMongo({
host: process.env.DATABASE_HOST, host: process.env.DATABASE_HOST,
port: process.env.DATABASE_PORT, port: process.env.DATABASE_PORT,
}); });
// createConnectionRedis({
// host: process.env.REDIS_HOST!,
// port: parseInt(process.env.REDIS_PORT!),
// });
app.use(cors()); app.use(cors());
app.use(helmet()); app.use(helmet());
app.use(express.json()); app.use(express.json());

File diff suppressed because it is too large Load Diff
Loading…
Cancel
Save