Skip to content
Snippets Groups Projects
Commit 7ea14179 authored by Jonny Ngo Luong's avatar Jonny Ngo Luong
Browse files

Merge branch '6-organiser-filtrere-funksjon-etter-kategori' into 'master'

Resolve "Organiser/Filtrere funksjon etter kategori"

Closes #6

See merge request !9
parents 02391b4a 72f09ff0
No related branches found
No related tags found
1 merge request!9Resolve "Organiser/Filtrere funksjon etter kategori"
This diff is collapsed.
import { Response, Request } from "express";
import query from "../../services/db_query";
import express from "express";
import IPost from "../../models/post";
import Category from "../../models/category";
const router = express.Router();
const category = new Category();
/* ============================= CREATE ============================= */
// Create posts `/api/post/`
//'{"title":"test3","description":"test3","timestamp":123123,"owner":"test3","category":"test3","imageUrl":"test3"}'
router.route("/").post(async (request: Request, response: Response) => {
const {
title,
description,
timestamp,
owner,
category,
imageUrl,
} = request.body;
try {
const post: IPost = {
title: title,
description: description,
timestamp: timestamp,
owner: owner,
category: category,
imageUrl: imageUrl,
};
if (Object.values(post).filter((p) => p == undefined).length > 0)
return response.status(500).send("Error");
const input = `INSERT INTO post(title, description, timestamp, owner, category, imageUrl) VALUES (?,?,?,?,?,?)`;
return response.status(200).json(await query(input, Object.values(post)));
} catch (error) {
return response.status(400).send("Bad Request");
}
});
/* ============================= READ ============================= */
// Get all posts `/api/post/?categoryid=`
router.route("/").get(async (request: Request, response: Response) => {
const { categoryid } = request.query as { [key: string]: string };
try {
let input = `SELECT p.id, p.title, p.description, p.timestamp, p.owner, category.name, p.imageUrl
FROM post as p
INNER JOIN category ON category.categoryid = p.categoryid`;
if (categoryid) input += ` WHERE p.categoryid=${categoryid}`;
response.status(200).json(await query(input, ""));
} catch (error) {
response.status(400).send("Bad Request");
}
});
// Get post with id `/api/post/:id`
router.route("/:id").get(async (request: Request, response: Response) => {
const postId: string = request.params.id as string;
try {
const input = `SELECT p.id, p.title, p.description, p.timestamp, p.owner, category.name, p.imageUrl
FROM post as p
INNER JOIN category ON category.categoryid = p.categoryid WHERE p.id=?;`;
response.status(200).json(await query(input, [postId]));
} catch (error) {
response.status(400).send("Bad Request");
}
});
/* ============================= UPDATE ============================= */
// Edit post with id `/api/post/:id`
router.route("/:id").put(async (request: Request, response: Response) => {
const postId: string = request.params.id as string;
try {
response
.status(200)
.json(await query("SELECT * FROM post WHERE id=?;", [postId]));
} catch (error) {
response.status(400).send("Bad Request");
}
});
/* ============================= DELETE ============================= */
// Remove post with id `/api/post/:id`
router.route("/:id").delete(async (request: Request, response: Response) => {
const postId: string = request.params.id as string;
try {
response
.status(200)
.json(await query("SELECT * FROM post WHERE id=?;", [postId]));
} catch (error) {
response.status(400).send("Bad Request");
}
});
export default router;
import request from "supertest";
import app from "../../../app";
describe("Test postController", () => {
beforeAll(async () => {
// kjører før testing
console.log("Post controller test starting...");
});
afterAll(async () => {
// kjører når all testing er gått gjennom
console.log("...Test ending");
});
it("Request /api/post should return request of 200!", async () => {
const result = await request(app).get("/api/post").send();
expect(result.status).toBe(200);
});
it('Request /api/post/1 should return data with name "test"!', async () => {
const result = await request(app).get("/api/post/1").send();
expect(result.status).toBe(200);
expect(result.body.data[0]?.title).toBe("test");
});
it('Request /api/post/?categoryid=1 should return datas with categoryname = "Antikviteter og Kunst"', async () => {
const result = await request(app).get("/api/post/1").send();
expect(result.status).toBe(200);
expect(result.body.data[0]?.name).toBe("Antikviteter og Kunst");
});
});
import { Response, Request } from "express";
import query from '../../services/db_query';
import express from 'express';
import IPost from '../../models/post';
import Category from '../../models/category';
const router = express.Router();
const category = new Category();
/* ============================= CREATE ============================= */
// Create posts `/api/post/`
//'{"title":"test3","description":"test3","timestamp":123123,"owner":"test3","category":"test3","imageUrl":"test3"}'
router.route('/').post(async (request: Request, response: Response) => {
const {title, description, timestamp, owner, categoryid, imageUrl} = request.body;
try {
const post: IPost = {
"title": title,
"description": description,
"timestamp": timestamp,
"owner": owner,
"categoryid": categoryid,
"imageUrl": imageUrl
};
if (Object.values(post).filter(p => p == undefined).length > 0) return response.status(500).send("Error");
const input = (`INSERT INTO post(title, description, timestamp, owner, categoryid, imageUrl) VALUES (?,?,?,?,?,?)`)
return response.status(200).json(
await query(input,Object.values(post))
);
} catch (error) {
return response.status(400).send("Bad Request");
}
});
/* ============================= READ ============================= */
// Get all posts `/api/post/`
router.route('/').get(async (_: Request, response: Response) => {
try {
//response.status(200).json(await query("SELECT * FROM post;",""));
const input = `SELECT p.id, p.title, p.description, p.timestamp, p.owner, category.name, p.imageUrl
FROM post as p
INNER JOIN category ON category.categoryid = p.categoryid;`
response.status(200).json(await query(input,""));
} catch (error) {
response.status(400).send("Bad Request");
}
});
// Get post with id `/api/post/:id`
router.route('/:id').get(async (request: Request, response: Response) => {
const postId = request.params.id;
try {
//response.status(200).json(await query("SELECT * FROM post WHERE id=?;",[postId]));
const input = `SELECT p.id, p.title, p.description, p.timestamp, p.owner, category.name, p.imageUrl
FROM post as p
INNER JOIN category ON category.categoryid = p.categoryid WHERE p.id=?;`
response.status(200).json(await query(input,[postId]));
} catch (error) {
response.status(400).send("Bad Request");
}
});
/* ============================= UPDATE ============================= */
// Edit post with id `/api/post/:id`
router.route('/:id').put(async (request: Request, response: Response) => {
const postId = request.params.id;
try {
response.status(200).json(await query("SELECT * FROM post WHERE id=?;",[postId]));
} catch (error) {
response.status(400).send("Bad Request");
}
});
/* ============================= DELETE ============================= */
// Remove post with id `/api/post/:id`
router.route('/:id').delete(async (request: Request, response: Response) => {
const postId = request.params.id;
try {
response.status(200).json(await query("SELECT * FROM post WHERE id=?;",[postId]));
} catch (error) {
response.status(400).send("Bad Request");
}
});
export default router;
import request from 'supertest';
import app from '../../../app';
describe('Test postController', () => {
beforeAll(async () => { // kjører før testing
console.log("Post controller test starting...");
});
afterAll(async () => { // kjører når all testing er gått gjennom
console.log("...Test ending");
});
it('Request /api/post should return request of 200!', async () => {
const result = await request(app)
.get('/api/post')
.send()
expect(result.status).toBe(200);
});
it('Request /api/post/1 should return data with name "test"!', async () => {
const result = await request(app).get('/api/post/1').send();
expect(result.status).toBe(200);
expect(result.body.data[0]?.title).toBe('test');
});
});
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment