added course route

This commit is contained in:
Mikayla Dobson
2022-12-08 14:11:16 -06:00
parent 174866e254
commit 836b10379d
5 changed files with 123 additions and 1 deletions

View File

@@ -0,0 +1,44 @@
import now from "../util/now";
import pool from "../db";
import { ICourse } from "../schemas";
export default class Course {
async getOne(id: string) {
try {
const statement = `SELECT * FROM recipin.course WHERE id = $1`;
const values = [id];
const result = await pool.query(statement, values);
if (result.rows) return result.rows[0];
return null;
} catch (error: any) {
throw new Error(error);
}
}
async getAll() {
try {
const statement = `SELECT * FROM recipin.course`;
const result = await pool.query(statement);
if (result.rows.length) return result.rows;
return null;
} catch (e: any) {
throw new Error(e);
}
}
async post(data: ICourse) {
try {
const { name } = data;
const statement = `
INSERT INTO recipin.course
(name, datecreated, datemodified, active)
VALUES ($1, $2, $3, $4) RETURNING *`;
const values = [name, now, now, true];
const result = await pool.query(statement, values);
if (result.rows.length) return result.rows[0];
return null;
} catch (e: any) {
throw new Error(e);
}
}
}