Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion __tests__/app.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const request = require("supertest");
const db = require("../db/connection.js");
const seed = require("../db/seeds/seed.js");
const testData = require("../db/data/test-data");
const endPoints = require("../endpoints.json");

beforeEach(() => {
return seed(testData);
Expand Down Expand Up @@ -31,7 +32,6 @@ describe('Non existant end-points',()=>{
.get("/api/topic")
.expect(404)
.then((response)=>{

expect(response.body.msg).toBe('Not Found')
})
});
Expand Down Expand Up @@ -73,3 +73,15 @@ describe("GET /api/articles/:article_id", () => {

})



describe('get api',()=>{
test("return 200 and api endpoints", () => {
return request(app)
.get("/api")
.expect(200)
.then(({body})=>{
expect(body).toEqual({'endpoints': endPoints})
})
});
})
6 changes: 4 additions & 2 deletions app.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
const express = require("express");
const app = express();

const { getTopics, getArticleById } = require("./db/controllers/app.controllers.js");
const { getTopics, getArticleById, getAllApis } = require("./controllers/app.controllers.js");

app.get("/api/topics", getTopics);

app.get("/api/articles/:article_id", getArticleById);


app.get("/api", getAllApis)

app.all("/*",(req,res)=>{
res.status(404).send({msg: 'Not Found'})
})

app.use((err, rex, res, next)=>{
app.use((err, req, res, next)=>{
if (err.status){
res.status(err.status).send({message: err.message})
} else {
Expand Down
22 changes: 16 additions & 6 deletions controllers/app.controllers.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,27 @@
const { fetchTopics } = require("../models/app.models.js")
const { fetchTopics, fetchArticleById } = require("../models/app.models.js")
const endPoints = require("../endpoints.json");

exports.getAllApis = (req, res) =>{
res.status(200).send({ 'endpoints': endPoints})
}
exports.getTopics = (req, res, next) => {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is for the previous task and shouldn't be included in this PR.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have no idea how it got there...


fetchTopics()
.then((topics)=>{
res.status(200).send({ topics })
})
.catch((err)=>{
next(err)
})
.catch(next)
}

exports.getAllApis = (req, res) =>{
res.status(200).send({ 'endpoints': endPoints})
exports.getArticleById = (req, res, next) => {
const {article_id} = req.params
if (isNaN(article_id)) return next({ status: 400, message: 'Not a number, please enter valid id'});

fetchArticleById(article_id)
.then((article)=>{
res.status(200).send({ article })
})
.catch((err)=>{
next(err);
})
}
24 changes: 0 additions & 24 deletions db/controllers/app.controllers.js

This file was deleted.

2 changes: 1 addition & 1 deletion db/models/app.models.js → models/app.models.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const db = require("../connection.js");
const db = require("../db/connection.js");

exports.fetchTopics = () => {
return db.query('SELECT * FROM topics;').then(({ rows }) => {
Expand Down