Skip to content

Commit

Permalink
change books get endpoint to prefer lotr as requested from FR-FE7118
Browse files Browse the repository at this point in the history
  • Loading branch information
schobele committed Nov 29, 2024
1 parent 007070d commit ae8aee3
Showing 1 changed file with 67 additions and 63 deletions.
130 changes: 67 additions & 63 deletions app.js
Original file line number Diff line number Diff line change
@@ -1,63 +1,67 @@
require('./tracing')
const fs = require('fs')
const express = require('express')
const cors = require('cors')
const { v4: uuidv4 } = require('uuid');
const morgan = require('morgan')

const app = express()
const port = process.env.PORT || 5000

app.use(express.json())
app.use(cors())
app.use(morgan('tiny'))

app.get('/404', (req, res) => {
res.status(404).sendFile(__dirname + '/404.html')
})

app.use(express.static(__dirname + '/vue/dist'))

app.get('/api/books/list', (req, res) => {
if (fs.existsSync('downtime.json')) {
res.status(500).send()
} else {
const rawData = fs.readFileSync('books.json')
const books = JSON.parse(rawData)
res.status(200).json(books)
}
})

app.get('/api/books/:id', (req, res) => {
const rawData = fs.readFileSync('books.json')
const books = JSON.parse(rawData)
var arrayFound = books.filter(function(item) {
return item.id == req.params.id;
});
res.status(200).json(arrayFound[0])
})

app.get('/api/users/login', (req, res) => {
res.status(200).json({
message: 'Login successful',
token: uuidv4(),
name: 'Danube'
})
})


app.post('/api/toggle', (req, res) => {
if (fs.existsSync('downtime.json')) {
fs.unlinkSync('downtime.json')
res.status(200).send()
} else {
fs.writeFileSync('downtime.json', '{}')
res.status(200).send()
}
})

app.get(/.*/, (req,res) => {
res.sendFile(__dirname + '/vue/dist/index.html')
})

app.listen(port, () => console.log(`Example app listening on port ${port}!`))
require("./tracing");
const fs = require("fs");
const express = require("express");
const cors = require("cors");
const { v4: uuidv4 } = require("uuid");
const morgan = require("morgan");

const app = express();
const port = process.env.PORT || 5000;

app.use(express.json());
app.use(cors());
app.use(morgan("tiny"));

app.get("/404", (req, res) => {
res.status(404).sendFile(__dirname + "/404.html");
});

app.use(express.static(__dirname + "/vue/dist"));

app.get("/api/books", (req, res) => {
if (fs.existsSync("downtime.json")) {
res.status(500).send();
} else {
const rawData = fs.readFileSync("books.json");
const books = JSON.parse(rawData);

if (books[0].name !== "Lord of the Rings") {
res.status(500).send();
} else {
res.status(200).json(books);
}
}
});

app.get("/api/books/:id", (req, res) => {
const rawData = fs.readFileSync("books.json");
const books = JSON.parse(rawData);
var arrayFound = books.filter(function (item) {
return item.id == req.params.id;
});
res.status(200).json(arrayFound[0]);
});

app.get("/api/users/login", (req, res) => {
res.status(200).json({
message: "Login successful",
token: uuidv4(),
name: "Danube",
});
});

app.post("/api/toggle", (req, res) => {
if (fs.existsSync("downtime.json")) {
fs.unlinkSync("downtime.json");
res.status(200).send();
} else {
fs.writeFileSync("downtime.json", "{}");
res.status(200).send();
}
});

app.get(/.*/, (req, res) => {
res.sendFile(__dirname + "/vue/dist/index.html");
});

app.listen(port, () => console.log(`Example app listening on port ${port}!`));

0 comments on commit ae8aee3

Please sign in to comment.