- npm updates

- throw error exception if, there is no db connection
This commit is contained in:
Arno Kaimbacher 2023-04-27 15:58:45 +02:00
parent 77d620bdbd
commit df6f05a032
4 changed files with 93 additions and 67 deletions

View File

@ -7,6 +7,7 @@ import { DatasetController } from "./controllers/dataset.controller";
import { OaiController } from "./controllers/oai.controller";
import { FileController } from "./controllers/file.controller";
import * as path from "path";
import HTTPException from "./exceptions/HttpException.js";
export class App extends Server {
// private app;
@ -66,12 +67,14 @@ export class App extends Server {
response.sendFile("/home/administrator/tethys.api/new-book.html");
});
// Error handling middleware
// // Error handling middleware
// this.app.use(errorHandler); // registration of handler
// this.app.use((err: HTTPException, req: Request, res: Response, next: NextFunction) => {
// console.log('Oops !!, Error occured in req->res cycle ', err.message);
// res.status(err.status).json({ err }); // Send back Error to the Frontend
// })
// Error handling middleware
this.app.use((error: HTTPException, req: express.Request, res: express.Response) => {
console.log("Oops !!, Error occured in req->res cycle ", error.message);
const status = error.status || 500;
res.status(status).json(error.message); // Send back Error to the Frontend
});
})
.catch((err) => {
console.error("Unable to connect to the database:", err);

View File

@ -3,7 +3,7 @@ import dbContext from "../models/init-models.js";
import { Dataset, User, Person } from "../models/init-models.js";
import Sequelize from "sequelize";
const Op = Sequelize.Op;
import { Request, Response } from "express";
import { NextFunction, Request, Response } from "express";
// import Logger from 'jet-logger';
import { StatusCodes } from "http-status-codes";
@ -22,7 +22,7 @@ export class DatasetController {
order: ["server_date_published"],
})
.then((data) => {
res.send(data);
res.status(StatusCodes.OK).send(data);
})
.catch((err) => {
res.status(500).send({
@ -32,9 +32,10 @@ export class DatasetController {
}
@Get(":publish_id")
public async findOne(req: Request, res: Response) {
public async findOne(req: Request, res: Response, next: NextFunction) {
const publish_id = req.params.publish_id;
try {
const dataset = await dbContext.Dataset.findOne({
where: { publish_id: publish_id },
include: [
@ -70,6 +71,20 @@ export class DatasetController {
// order: ['server_date_published'],
// order: ['server_date_published'],
});
if (dataset) {
res.status(StatusCodes.OK).send(dataset);
} else {
res.status(StatusCodes.NOT_FOUND).send({
message: `Cannot find Dataset with publish_id=${publish_id}.`,
});
}
} catch (error) {
// res.status(500).send({
// message: "Error retrieving Dataset with publish_id=" + publish_id,
// });
return next(error);
}
// .then((data) => {
// if (data) {
// res.send(data);
@ -84,13 +99,13 @@ export class DatasetController {
// message: "Error retrieving Dataset with publish_id=" + publish_id,
// });
// });
if (dataset) {
res.status(StatusCodes.OK).send(dataset);
} else {
res.status(StatusCodes.NOT_FOUND).send({
message: `Cannot find Dataset with publish_id=${publish_id}.`,
});
}
// if (dataset) {
// res.status(StatusCodes.OK).send(dataset);
// } else {
// res.status(StatusCodes.NOT_FOUND).send({
// message: `Cannot find Dataset with publish_id=${publish_id}.`,
// });
// }
}
}

View File

@ -4,9 +4,10 @@ import Sequelize from "sequelize";
const Op = Sequelize.Op;
import { Person } from "../models/init-models.js";
export async function findYears(req, res) {
export async function findYears(req, res, next) {
const serverState = "published";
// Use raw SQL queries to select all cars which belongs to the user
try {
const datasets = await sequelizeConnection.query(
"SELECT distinct EXTRACT(YEAR FROM server_date_published) as published_date FROM gba.documents WHERE server_state = (:serverState)",
{
@ -18,8 +19,11 @@ export async function findYears(req, res) {
// Pluck the ids of the cars
const years = datasets.map((dataset) => dataset.published_date);
// check if the cars is returned
if (years.length > 0) {
// if (years.length > 0) {
return res.status(200).json(years);
// }
} catch (error) {
return next(error);
}
}

View File

@ -83,10 +83,14 @@ export class OaiController {
const xsltParameter = (this.xsltParameter = {});
let earliestDateFromDb;
try {
const firstPublishedDataset: Dataset | null = await Dataset.earliestPublicationDate();
firstPublishedDataset != null &&
(earliestDateFromDb = dayjs(firstPublishedDataset.server_date_published).format("YYYY-MM-DDTHH:mm:ss[Z]"));
this.xsltParameter["earliestDatestamp"] = earliestDateFromDb;
} catch (error) {
return next(error);
}
let oaiRequest: OaiParameter = {};
if (request.method == "POST") {