|
| 1 | +import User from '../models/userModel.ts'; |
| 2 | +import Board from '../models/boardModel.ts'; |
| 3 | +import { NextFunction, Request, Response } from 'express'; |
| 4 | +import { BoardListItemState } from '../../src/types.ts'; |
| 5 | + |
| 6 | +const boardsController = { |
| 7 | + getMyBoards: async (req: Request, res: Response, next: NextFunction) => { |
| 8 | + try { |
| 9 | + // find all boards beloning to the user, push board names into an array and save on locals |
| 10 | + console.log('req.params.userId: ', req.params.userId); |
| 11 | + const user = await User.findOne({ _id: req.params.userId }).populate( |
| 12 | + 'boards' |
| 13 | + ); |
| 14 | + if (!user) { |
| 15 | + return next({ |
| 16 | + log: `boardsController.getMyBoards ERROR: User cannot be found.`, |
| 17 | + status: 500, |
| 18 | + message: { err: 'Cannot find user.' }, |
| 19 | + }); |
| 20 | + } |
| 21 | + res.locals.boards = user.boards; |
| 22 | + return next(); |
| 23 | + } catch (err) { |
| 24 | + // pass error through to global error handler |
| 25 | + return next({ |
| 26 | + log: `boardssController.getMyBoards ERROR: ${err}`, |
| 27 | + status: 500, |
| 28 | + message: { err: 'Error getting my boards' }, |
| 29 | + }); |
| 30 | + } |
| 31 | + }, |
| 32 | + getBoardNamesAndIds: async ( |
| 33 | + _req: Request, |
| 34 | + res: Response, |
| 35 | + next: NextFunction |
| 36 | + ) => { |
| 37 | + try { |
| 38 | + console.log('res.locals.boards: ', res.locals.boards); |
| 39 | + const namesAndIds: BoardListItemState[] = []; |
| 40 | + res.locals.boards.forEach((board: any) => { |
| 41 | + console.log('board.name: ', board.name); |
| 42 | + namesAndIds.push({ |
| 43 | + name: board.name, |
| 44 | + id: board._id, |
| 45 | + }); |
| 46 | + }); |
| 47 | + res.locals.namesAndIds = namesAndIds; |
| 48 | + console.log('res.locals.namesAndIds: ', res.locals.namesAndIds); |
| 49 | + return next(); |
| 50 | + } catch (err) { |
| 51 | + // pass error through to global error handler |
| 52 | + return next({ |
| 53 | + log: `boardssController.getBoardNamesAndIds ERROR: ${err}`, |
| 54 | + status: 500, |
| 55 | + message: { err: 'Error refining boards down to names and ids' }, |
| 56 | + }); |
| 57 | + } |
| 58 | + }, |
| 59 | + createBoard: async (req: Request, res: Response, next: NextFunction) => { |
| 60 | + try { |
| 61 | + const createdBoard = await Board.create({ |
| 62 | + name: req.body.boardName, |
| 63 | + backlog: [], |
| 64 | + inProgress: [], |
| 65 | + inReview: [], |
| 66 | + completed: [], |
| 67 | + boardOwner: req.body.userId, |
| 68 | + }); |
| 69 | + res.locals.createdBoard = createdBoard; |
| 70 | + return next(); |
| 71 | + } catch (err) { |
| 72 | + // pass error through to global error handler |
| 73 | + return next({ |
| 74 | + log: `boardssController.createBoard ERROR: ${err}`, |
| 75 | + status: 500, |
| 76 | + message: { err: 'Error creating new board' }, |
| 77 | + }); |
| 78 | + } |
| 79 | + }, |
| 80 | + assignNewBoard: async (req: Request, res: Response, next: NextFunction) => { |
| 81 | + try { |
| 82 | + await User.updateOne( |
| 83 | + { _id: req.body.userId }, |
| 84 | + { $push: { boards: res.locals.createdBoard._id } } |
| 85 | + ); |
| 86 | + return next(); |
| 87 | + } catch (err) { |
| 88 | + // pass error through to global error handler |
| 89 | + return next({ |
| 90 | + log: `boardssController.assignNewBoard ERROR: ${err}`, |
| 91 | + status: 500, |
| 92 | + message: { err: 'Error assigning board to user' }, |
| 93 | + }); |
| 94 | + } |
| 95 | + }, |
| 96 | + getCurrentBoard: async (req: Request, res: Response, next: NextFunction) => { |
| 97 | + try { |
| 98 | + // find all boards beloning to the user, push board names into an array and save on locals |
| 99 | + const board = await User.findOne({ _id: req.params.boardID }); |
| 100 | + console.log(board); |
| 101 | + if (!board) { |
| 102 | + return next({ |
| 103 | + log: `boardsController.getCurrentBoard ERROR: Board cannot be found.`, |
| 104 | + status: 500, |
| 105 | + message: { err: 'Cannot find board.' }, |
| 106 | + }); |
| 107 | + } |
| 108 | + res.locals.boardInfo = board; |
| 109 | + return next(); |
| 110 | + } catch (err) { |
| 111 | + // pass error through to global error handler |
| 112 | + return next({ |
| 113 | + log: `boardssController.getCurrentBoard ERROR: ${err}`, |
| 114 | + status: 500, |
| 115 | + message: { err: 'Error getting current board' }, |
| 116 | + }); |
| 117 | + } |
| 118 | + }, |
| 119 | +}; |
| 120 | + |
| 121 | +export default boardsController; |
0 commit comments