diff --git a/src/app.ts b/src/app.ts index bb9991c..15d7100 100644 --- a/src/app.ts +++ b/src/app.ts @@ -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); diff --git a/src/controllers/dataset.controller.ts b/src/controllers/dataset.controller.ts index be12f8f..0664fcb 100644 --- a/src/controllers/dataset.controller.ts +++ b/src/controllers/dataset.controller.ts @@ -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,44 +32,59 @@ 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; - const dataset = await dbContext.Dataset.findOne({ - where: { publish_id: publish_id }, - include: [ - "titles", - "abstracts", - { - model: User, - as: "user", - }, - { - model: Person, - through: { where: { role: "author" } }, - as: "authors", - // order: [['link_documents_persons.sort_order', 'ASC']], - }, - { - model: Person, - through: { where: { role: "contributor" } }, - as: "contributors", - }, - "subjects", - "coverage", - "licenses", - "references", - "project", - "files", - "identifier", - ], - order: [ - ["authors", dbContext.DocumentPersons, "sort_order", "ASC"], - ["contributors", dbContext.DocumentPersons, "sort_order", "ASC"], - ], - // order: ['server_date_published'], - // order: ['server_date_published'], - }); + try { + const dataset = await dbContext.Dataset.findOne({ + where: { publish_id: publish_id }, + include: [ + "titles", + "abstracts", + { + model: User, + as: "user", + }, + { + model: Person, + through: { where: { role: "author" } }, + as: "authors", + // order: [['link_documents_persons.sort_order', 'ASC']], + }, + { + model: Person, + through: { where: { role: "contributor" } }, + as: "contributors", + }, + "subjects", + "coverage", + "licenses", + "references", + "project", + "files", + "identifier", + ], + order: [ + ["authors", dbContext.DocumentPersons, "sort_order", "ASC"], + ["contributors", dbContext.DocumentPersons, "sort_order", "ASC"], + ], + // 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}.`, + // }); + // } } } diff --git a/src/controllers/home.controller.js b/src/controllers/home.controller.js index a1e9983..d00be44 100644 --- a/src/controllers/home.controller.js +++ b/src/controllers/home.controller.js @@ -4,22 +4,26 @@ 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 - const datasets = await sequelizeConnection.query( - "SELECT distinct EXTRACT(YEAR FROM server_date_published) as published_date FROM gba.documents WHERE server_state = (:serverState)", - { - replacements: { serverState: serverState }, - type: sequelizeConnection.QueryTypes.SELECT, - // attributes: [[sequelizeConnection.fn('DISTINCT', sequelizeConnection.col('published_date')), 'alias_name']], - }, - ); - // Pluck the ids of the cars - const years = datasets.map((dataset) => dataset.published_date); - // check if the cars is returned - if (years.length > 0) { + try { + const datasets = await sequelizeConnection.query( + "SELECT distinct EXTRACT(YEAR FROM server_date_published) as published_date FROM gba.documents WHERE server_state = (:serverState)", + { + replacements: { serverState: serverState }, + type: sequelizeConnection.QueryTypes.SELECT, + // attributes: [[sequelizeConnection.fn('DISTINCT', sequelizeConnection.col('published_date')), 'alias_name']], + }, + ); + // Pluck the ids of the cars + const years = datasets.map((dataset) => dataset.published_date); + // check if the cars is returned + // if (years.length > 0) { return res.status(200).json(years); + // } + } catch (error) { + return next(error); } } diff --git a/src/controllers/oai.controller.ts b/src/controllers/oai.controller.ts index 0929d8e..ce1e27e 100644 --- a/src/controllers/oai.controller.ts +++ b/src/controllers/oai.controller.ts @@ -83,10 +83,14 @@ export class OaiController { const xsltParameter = (this.xsltParameter = {}); let earliestDateFromDb; - 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; + 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") {