Ukládání pizz do DB místo dočasného souboru
This commit is contained in:
@@ -1,9 +1,5 @@
|
||||
import os from 'os';
|
||||
import path from 'path';
|
||||
import fs from 'fs';
|
||||
import axios from 'axios';
|
||||
import { load } from 'cheerio';
|
||||
import { formatDate } from './utils';
|
||||
|
||||
// TODO přesunout do types
|
||||
type PizzaSize = {
|
||||
@@ -40,7 +36,7 @@ const boxPrices: { [key: string]: number } = {
|
||||
/**
|
||||
* Stáhne a scrapne aktuální pizzy ze stránek Pizza Chefie.
|
||||
*/
|
||||
const downloadPizzy = async () => {
|
||||
export async function downloadPizzy(): Promise<Pizza[]> {
|
||||
// Získáme seznam pizz
|
||||
const html = await axios.get(pizzyUrl).then(res => res.data);
|
||||
const $ = load(html);
|
||||
@@ -81,26 +77,4 @@ const downloadPizzy = async () => {
|
||||
});
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Vrátí pizzy z tempu, nebo čerstvě stažené, pokud v tempu nejsou.
|
||||
*/
|
||||
export const fetchPizzy = async (): Promise<Pizza[]> => {
|
||||
const tmpDir = os.tmpdir();
|
||||
const date_ob = new Date();
|
||||
const dateStr = formatDate(date_ob);
|
||||
const dataPath = path.join(tmpDir, `chefie-${dateStr}.json`);
|
||||
|
||||
if (fs.existsSync(dataPath)) {
|
||||
console.log(`Soubor pro ${dataPath} již existuje, bude použit.`);
|
||||
const rawdata = fs.readFileSync(dataPath);
|
||||
return JSON.parse(rawdata.toString());
|
||||
} else {
|
||||
console.log(`Soubor pro ${dataPath} neexistuje, stahuji...`);
|
||||
const pizzy = await downloadPizzy();
|
||||
fs.writeFileSync(dataPath, JSON.stringify(pizzy));
|
||||
console.log(`Zapsán ${dataPath}`);
|
||||
return pizzy;
|
||||
}
|
||||
}
|
||||
@@ -1,15 +1,15 @@
|
||||
import express from "express";
|
||||
import { Server } from "socket.io";
|
||||
import bodyParser from "body-parser";
|
||||
import { fetchPizzy } from "./chefie";
|
||||
import cors from 'cors';
|
||||
import { addChoice, addPizzaOrder, createPizzaDay, deletePizzaDay, finishPizzaDelivery, finishPizzaOrder, getData, lockPizzaDay, removeChoice, removeChoices, removePizzaOrder, unlockPizzaDay, updateDepartureTime, updateNote } from "./service";
|
||||
import { addChoice, addPizzaOrder, createPizzaDay, deletePizzaDay, finishPizzaDelivery, finishPizzaOrder, getData, getPizzaList, lockPizzaDay, removeChoice, removeChoices, removePizzaOrder, savePizzaList, unlockPizzaDay, updateDepartureTime, updateNote } from "./service";
|
||||
import dotenv from 'dotenv';
|
||||
import path from 'path';
|
||||
import { getMenuSladovnicka, getMenuTechTower, getMenuUMotliku } from "./restaurants";
|
||||
import { getQr } from "./qr";
|
||||
import { generateToken, getLogin, getTrusted, verify } from "./auth";
|
||||
import { Locations, Restaurants } from "../../types";
|
||||
import { downloadPizzy } from "./chefie";
|
||||
|
||||
const ENVIRONMENT = process.env.NODE_ENV || 'production';
|
||||
dotenv.config({ path: path.resolve(__dirname, `./.env.${ENVIRONMENT}`) });
|
||||
@@ -121,13 +121,6 @@ app.get("/api/food", async (req, res) => {
|
||||
res.status(200).json(data);
|
||||
});
|
||||
|
||||
/** Vrátí seznam dostupných pizz. */
|
||||
app.get("/api/pizza", async (req, res) => {
|
||||
const pizzaList = await fetchPizzy();
|
||||
// console.log("Výsledek", pizzaList);
|
||||
res.status(200).json(pizzaList);
|
||||
});
|
||||
|
||||
/** Založí pizza day pro aktuální den, za předpokladu že dosud neexistuje. */
|
||||
app.post("/api/createPizzaDay", async (req, res) => {
|
||||
const login = getLogin(parseToken(req));
|
||||
@@ -153,7 +146,11 @@ app.post("/api/addPizza", async (req, res) => {
|
||||
throw Error("Nebyl předán index velikosti pizzy");
|
||||
}
|
||||
const pizzaSizeIndex = req.body.pizzaSizeIndex;
|
||||
const pizzy = await fetchPizzy();
|
||||
let pizzy = await getPizzaList();
|
||||
if (!pizzy) {
|
||||
pizzy = await downloadPizzy();
|
||||
savePizzaList(pizzy);
|
||||
}
|
||||
if (!pizzy[pizzaIndex]) {
|
||||
throw Error("Neplatný index pizzy: " + pizzaIndex);
|
||||
}
|
||||
|
||||
@@ -26,6 +26,31 @@ export async function getData(): Promise<ClientData> {
|
||||
return await storage.getData(formatDate(getToday())) || getEmptyData();
|
||||
}
|
||||
|
||||
/**
|
||||
* Vrátí seznam dostupných pizz pro dnešní den.
|
||||
*/
|
||||
export async function getPizzaList(): Promise<Pizza[] | undefined> {
|
||||
await initIfNeeded();
|
||||
const today = formatDate(getToday());
|
||||
const clientData: ClientData = await storage.getData(today);
|
||||
return Promise.resolve(clientData.pizzaList);
|
||||
}
|
||||
|
||||
/**
|
||||
* Uloží seznam dostupných pizz pro dnešní den.
|
||||
*
|
||||
* @param pizzaList seznam dostupných pizz
|
||||
*/
|
||||
export async function savePizzaList(pizzaList: Pizza[]): Promise<ClientData> {
|
||||
await initIfNeeded();
|
||||
const today = formatDate(getToday());
|
||||
const clientData: ClientData = await storage.getData(today);
|
||||
clientData.pizzaList = pizzaList;
|
||||
clientData.pizzaListLastUpdate = new Date();
|
||||
await storage.setData(today, clientData);
|
||||
return clientData;
|
||||
}
|
||||
|
||||
/**
|
||||
* Vytvoří pizza day pro aktuální den a vrátí data pro klienta.
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user