in progress: table for units of measurements, etc
This commit is contained in:
36
server/controllers/DropdownCtl.ts
Normal file
36
server/controllers/DropdownCtl.ts
Normal file
@@ -0,0 +1,36 @@
|
||||
import Dropdown from "../models/dropdownValues";
|
||||
import { DropdownDataType } from "../schemas";
|
||||
import ControllerResponse from "../util/ControllerResponse";
|
||||
import { StatusCode } from "../util/types";
|
||||
const DDInstance = new Dropdown();
|
||||
|
||||
export default class DropdownCtl {
|
||||
async getMeasurements() {
|
||||
try {
|
||||
const result = await DDInstance.getMeasurements();
|
||||
return new ControllerResponse<any[] | string>(
|
||||
((result !== null) ? StatusCode.OK : StatusCode.NotFound),
|
||||
result || "Measurement unit data not found",
|
||||
(result !== null)
|
||||
);
|
||||
} catch (error: any) {
|
||||
throw new Error(error);
|
||||
}
|
||||
}
|
||||
|
||||
async getByType(type: DropdownDataType) {
|
||||
switch (type) {
|
||||
case "measurement":
|
||||
const result = await DDInstance.getMeasurements();
|
||||
return new ControllerResponse<any[] | string>(
|
||||
((result !== null) ? StatusCode.OK : StatusCode.NotFound),
|
||||
result || "Measurement unit data not found",
|
||||
(result !== null)
|
||||
);
|
||||
case "course":
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -101,10 +101,25 @@ export default async function populate() {
|
||||
;
|
||||
`
|
||||
|
||||
const populateMeasurements = `
|
||||
INSERT INTO recipin.dropdownVals
|
||||
(name, datatype, datecreated)
|
||||
VALUES
|
||||
('cup', 'MEASUREMENTS', $1),
|
||||
('tablespoon', 'MEASUREMENTS', $1),
|
||||
('teaspoon', 'MEASUREMENTS', $1),
|
||||
('gram', 'MEASUREMENTS', $1),
|
||||
('ounce', 'MEASUREMENTS', $1),
|
||||
('fluid ounce', 'MEASUREMENTS', $1),
|
||||
('pound', 'MEASUREMENTS', $1)
|
||||
;
|
||||
`
|
||||
|
||||
const allStatements: Array<string> = [
|
||||
populateUsers, populateCuisines, populateCourses,
|
||||
populateCollection, populateIngredients, populateRecipes,
|
||||
populateGroceryList, populateFriendships, populateComments
|
||||
populateGroceryList, populateFriendships, populateComments,
|
||||
populateMeasurements
|
||||
];
|
||||
|
||||
await pool.query(setup);
|
||||
|
||||
@@ -28,11 +28,12 @@ dotenv.config();
|
||||
const recipecollection = fs.readFileSync(appRoot + '/db/sql/create/createcmp_recipecollection.sql').toString();
|
||||
const usersubscriptions = fs.readFileSync(appRoot + '/db/sql/create/createcmp_usersubscriptions.sql').toString();
|
||||
const userfriendships = fs.readFileSync(appRoot + '/db/sql/create/createcmp_userfriendships.sql').toString();
|
||||
const dropdownValues = fs.readFileSync(appRoot + '/db/sql/create/createdropdown.sql').toString();
|
||||
|
||||
const allStatements = [
|
||||
setRole, appusers, ingredient, collection, cuisine, course,
|
||||
recipe, recipecomments, groceryList, recipeingredient,
|
||||
recipecollection, usersubscriptions, userfriendships
|
||||
recipecollection, usersubscriptions, userfriendships, dropdownValues
|
||||
]
|
||||
|
||||
try {
|
||||
|
||||
6
server/db/sql/create/createdropdown.sql
Normal file
6
server/db/sql/create/createdropdown.sql
Normal file
@@ -0,0 +1,6 @@
|
||||
CREATE TABLE IF NOT EXISTS recipin.dropdownVals (
|
||||
id INT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
|
||||
name VARCHAR NOT NULL,
|
||||
datatype VARCHAR CHECK(datatype in ('MEASUREMENTS', 'COURSE', 'INGREDIENT')),
|
||||
datecreated VARCHAR NOT NULL
|
||||
);
|
||||
14
server/models/dropdownValues.ts
Normal file
14
server/models/dropdownValues.ts
Normal file
@@ -0,0 +1,14 @@
|
||||
import pool from "../db";
|
||||
|
||||
export default class Dropdown {
|
||||
async getMeasurements() {
|
||||
try {
|
||||
const statement = `SELECT * FROM recipin.dropdownVals WHERE datatype = MEASUREMENTS`;
|
||||
const result = await pool.query(statement);
|
||||
if (result.rows.length) return result.rows;
|
||||
return null;
|
||||
} catch (error: any) {
|
||||
throw new Error(error);
|
||||
}
|
||||
}
|
||||
}
|
||||
27
server/routes/dropdownValues.ts
Normal file
27
server/routes/dropdownValues.ts
Normal file
@@ -0,0 +1,27 @@
|
||||
import { Express, Router } from 'express';
|
||||
import DropdownCtl from '../controllers/DropdownCtl';
|
||||
import { DropdownDataType } from '../schemas';
|
||||
|
||||
const router = Router();
|
||||
const DDInstance = new DropdownCtl();
|
||||
|
||||
export const dropdownValue = (app: Express) => {
|
||||
app.use('/app/dropdown', router);
|
||||
|
||||
router.get('/', async (req, res, next) => {
|
||||
const { datatype } = req.query;
|
||||
|
||||
try {
|
||||
switch (datatype) {
|
||||
case "measurement":
|
||||
const { code, data } = await DDInstance.getMeasurements();
|
||||
res.status(code).send(data);
|
||||
break;
|
||||
default: break;
|
||||
}
|
||||
} catch (error) {
|
||||
next(error);
|
||||
}
|
||||
})
|
||||
|
||||
}
|
||||
@@ -72,4 +72,11 @@ export interface ICourse extends HasHistory, CanDeactivate {
|
||||
export interface FlavorProfile extends HasHistory, CanDeactivate {
|
||||
name: string
|
||||
description?: string
|
||||
}
|
||||
}
|
||||
|
||||
export interface DropdownData extends HasHistory {
|
||||
name: string
|
||||
datatype: DropdownDataType
|
||||
}
|
||||
|
||||
export type DropdownDataType = "measurement" | "course"
|
||||
Reference in New Issue
Block a user