Skip to content

Commit

Permalink
Use Deno.cron to update POTD
Browse files Browse the repository at this point in the history
  • Loading branch information
anurag-roy committed Dec 2, 2023
1 parent df44dcf commit 83766ee
Show file tree
Hide file tree
Showing 5 changed files with 151 additions and 160 deletions.
1 change: 0 additions & 1 deletion deno.jsonc
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
"oak/": "https://deno.land/x/[email protected]/"
},
"tasks": {
// TODO: Remove `--unstable` once KV becomes stable
"dev": "deno run --unstable --allow-net --allow-read --watch src/index.ts",
"lume": "echo \"import 'lume/cli.ts'\" | deno run --unstable -A -",
"build": "deno task lume",
Expand Down
256 changes: 127 additions & 129 deletions deno.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

46 changes: 21 additions & 25 deletions src/controllers/pokemon.controller.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { getQuery } from 'oak/helpers.ts';
import { RouterMiddleware } from 'oak/mod.ts';
import { Pokemon, PokemonOfTheDay } from '../models/pokemon.ts';
import { Pokemon } from '../models/pokemon.ts';

export const getPokemon: RouterMiddleware<
'/',
Expand Down Expand Up @@ -31,7 +31,7 @@ export const getPokemon: RouterMiddleware<
};

export const getPokemonDetail: RouterMiddleware<'/:idOrName'> = async (
context,
context
) => {
const idOrName = Number.isNaN(Number(context.params.idOrName))
? decodeURIComponent(context.params.idOrName!).toLowerCase()
Expand All @@ -56,44 +56,27 @@ export const getPokemonDetail: RouterMiddleware<'/:idOrName'> = async (
};

export const getPokemonOfTheDay: RouterMiddleware<'/potd'> = async (
context,
context
) => {
const pool = 905;
const todaysDate = new Date().toDateString();

try {
const kv = await Deno.openKv();
const { value } = await kv.get<PokemonOfTheDay>(['potd']);
if (value && value.date === todaysDate) {
context.response.body = value.pokemon;
kv.close();
} else {
const randomNumber = Math.ceil(Math.random() * pool);
const { value: pokemonData } = await kv.get<Pokemon>([
'pokemon',
randomNumber,
]);
await kv.set(['potd'], {
pokemon: pokemonData,
date: todaysDate,
});
context.response.body = pokemonData;
kv.close();
}
const { value } = await kv.get<Pokemon>(['potd']);
context.response.body = value;
kv.close();
} catch (error) {
console.log(error);
context.throw(500, 'Internal server error');
}
};

export const migratePokemonToKv: RouterMiddleware<'/migrate'> = async (
context,
context
) => {
const body = context.request.body({ type: 'json' });
const { id } = await body.value;

const p: Pokemon = JSON.parse(
await Deno.readTextFile(`assets/data/${id}.json`),
await Deno.readTextFile(`assets/data/${id}.json`)
);

const primaryKey = ['pokemon', p.id];
Expand All @@ -114,3 +97,16 @@ export const migratePokemonToKv: RouterMiddleware<'/migrate'> = async (

context.response.body = 'Success!';
};

export const setPokemonOfTheDay = async () => {
const pool = 905;
const kv = await Deno.openKv();

const randomNumber = Math.ceil(Math.random() * pool);
const { value: pokemonData } = await kv.get<Pokemon>([
'pokemon',
randomNumber,
]);
await kv.set(['potd'], pokemonData);
kv.close();
};
3 changes: 3 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { oakCors } from 'https://deno.land/x/[email protected]/mod.ts';
import { Application } from 'https://deno.land/x/[email protected]/mod.ts';
import { setPokemonOfTheDay } from './controllers/pokemon.controller.ts';
import PokemonRouter from './routes/pokemon.route.ts';

const app = new Application();
Expand All @@ -20,3 +21,5 @@ app.use(async (context, next) => {
app.use(PokemonRouter.routes(), PokemonRouter.allowedMethods());

app.listen({ port: 8000 });

Deno.cron('potd', '0 0 * * *', setPokemonOfTheDay);
5 changes: 0 additions & 5 deletions src/models/pokemon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,3 @@ export type Pokemon = {
stats: Record<string, number>;
locations: string[];
};

export type PokemonOfTheDay = {
pokemon: Pokemon;
date: string;
};

0 comments on commit 83766ee

Please sign in to comment.