import { Request , Response , NextFunction } from "express" ; import generate from "nanoid/generate" ; import Projects from "../models/Projects" ; import { Project } from "../types/Project.type" ; async function add ( req , res , next ) { const project = req . body . project ; const projectId = generate ( "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ" , 10 ) ; try { if ( project . name === "" ) { throw new Error ( "Name cannot be empty" ) ; } await Projects . add ( projectId , project . name , project . timezone ) ; const projects = await Projects . getAll ( ) ; req . app . locals . projects = projects ; res . sendStatus ( 200 ) ; } catch ( e ) { next ( e ) ; } } async function update ( req , res , next ) { const project_id = req . params . project_id ; const project = req . body . project ; try { if ( project . name === "" ) { throw new Error ( "Name cannot be empty" ) ; } await Projects . update ( project_id , project ) ; const projects = await Projects . getAll ( ) ; req . app . locals . projects = projects ; res . sendStatus ( 200 ) ; } catch ( e ) { next ( e ) ; } } async function remove ( req , res , next ) { const project_id = req . params . project_id ; try { await Projects . remove ( project_id ) ; const projects = await Projects . getAll ( ) ; req . app . locals . projects = projects ; res . sendStatus ( 200 ) ; } catch ( e ) { next ( e ) ; } } async function getAll ( req , res , next ) { try { const projects = await Projects . getAll ( ) ; res . status ( 200 ) . send ( projects ) ; } catch ( e ) { next ( e ) ; } } async function getById ( req , res , next ) { const project_id = req . params . project_id ; try { const project = await Projects . getById ( project_id ) ; res . status ( 200 ) . send ( project ) ; } catch ( e ) { next ( e ) ; } } export default { add , update , remove , getAll , getById } ;