From 9d54cc11048a3bad07ceaeebfc8416e208cea472 Mon Sep 17 00:00:00 2001 From: Alex Date: Sat, 28 Nov 2020 18:09:59 -0600 Subject: [PATCH] implemented assignment endpoints --- api/src/assignment/assignment.controller.ts | 62 ++++---- api/src/assignment/assignment.service.ts | 153 ++++++++++++++++---- 2 files changed, 151 insertions(+), 64 deletions(-) diff --git a/api/src/assignment/assignment.controller.ts b/api/src/assignment/assignment.controller.ts index 1cc3cbc..00d2b80 100644 --- a/api/src/assignment/assignment.controller.ts +++ b/api/src/assignment/assignment.controller.ts @@ -1,47 +1,45 @@ -import { - Controller, - Get, - Post, - Body, - Put, - Param, - Delete, -} from '@nestjs/common'; +import { Body, Controller, Get, Param, Post } from '@nestjs/common'; +import { ApiTags } from '@nestjs/swagger'; import { AssignmentService } from './assignment.service'; +import { AssignmentType } from './dto/assignment.dto'; import { CreateAssignmentDTO } from './dto/create-assignment.dto'; -import { UpdateAssignmentDTO } from './dto/update-assignment.dto'; -import { ApiTags } from '@nestjs/swagger'; -@ApiTags('ticket') -@Controller('/api/ticket/assign') +@ApiTags('assignment') +@Controller('/api/assign') export class AssignmentController { constructor(private readonly ticketAssignService: AssignmentService) {} - /* Working Implementation */ + + /* WORKING Implementation */ + /* TODO: fix date formatting */ @Post() - async create(@Body() createTicketAssignDto: CreateAssignmentDTO) { - return await this.ticketAssignService.create(createTicketAssignDto); + async create(@Body() createAssignmentDTO: CreateAssignmentDTO) { + return await this.ticketAssignService.create(createAssignmentDTO); } - /* TODO */ + + /* WORKING Implementation */ @Get() findAll() { return this.ticketAssignService.findAll(); } - /* TODO */ - @Get(':id') - findOne(@Param('id') id: string) { - return this.ticketAssignService.findOne(+id); + + /* WORKING Implementation */ + @Get('user/:lsu_id') + findAllByLsuId(@Param('lsu_id') lsu_id: number) { + return this.ticketAssignService.findAllById(AssignmentType.LSU_ID, lsu_id); } - /* TODO */ - @Put(':id') - update( - @Param('id') id: string, - @Body() updateTicketAssignDto: UpdateAssignmentDTO, - ) { - return this.ticketAssignService.update(+id, updateTicketAssignDto); + + /* WORKING Implementation */ + @Get('ticket/:ticket_id') + findAllByTicketId(@Param('ticket_id') ticket_id: number) { + return this.ticketAssignService.findAllById( + AssignmentType.TICKET_ID, + ticket_id, + ); } - /* TODO */ - @Delete(':id') - remove(@Param('id') id: string) { - return this.ticketAssignService.remove(+id); + + /* WORKING Implementation */ + @Get(':assignment_id') + findOne(@Param('assignment_id') assignment_id: string) { + return this.ticketAssignService.findOne(+assignment_id); } } diff --git a/api/src/assignment/assignment.service.ts b/api/src/assignment/assignment.service.ts index 08f78ec..97d9b8b 100644 --- a/api/src/assignment/assignment.service.ts +++ b/api/src/assignment/assignment.service.ts @@ -1,9 +1,9 @@ import { HttpException, HttpStatus, Inject, Injectable } from '@nestjs/common'; -import { Pool } from 'pg'; +import { Pool, Query, QueryConfig } from 'pg'; import { PG_CONNECTION } from 'src/connection'; import { CreateAssignmentDTO } from './dto/create-assignment.dto'; -import { UpdateAssignmentDTO } from './dto/update-assignment.dto'; -import { AssignmentDTO } from './dto/assignment.dto'; +import { AssignmentDTO, AssignmentType } from './dto/assignment.dto'; +import { createDate } from 'src/util'; /* TODO: - Implement remaining methods @@ -14,39 +14,128 @@ export class AssignmentService { constructor(@Inject(PG_CONNECTION) private connection: Pool) {} async create(createAssignmentDTO: CreateAssignmentDTO) { - const { assignedby, assignedto, comment, assigndate } = createAssignmentDTO; - - /* Insert new user into db - TODO: implement error handling for pg request - */ - const text = - 'INSERT INTO ticketwork(assignedby, assignedto, comment, assigndate) VALUES ($1, $2, $3, $4) RETURNING *'; - const values = [assignedby, assignedto, comment, assigndate]; - const res = (await this.connection.query(text, values)) - .rows[0]; - - /* Return user object without password */ - const ticketassign = new TicketAssign(); - ticketassign.assignedby = res.assignedby; - ticketassign.assignedto = res.assignedto; - ticketassign.comment = res.comment; - ticketassign.assigndate = res.assigndate; - return ticketassign; - } - /* TODO */ - findAll() { - return `This action returns all user`; + const { lsu_id, ticket_id } = createAssignmentDTO; + const assigned_date = createDate(); + + /* Insert new assignment into db */ + const query: QueryConfig = { + name: 'insert_assignment', + text: + 'INSERT INTO assignment(lsu_id, ticket_id, assigned_date) VALUES ($1, $2, $3) RETURNING *', + values: [lsu_id, ticket_id, assigned_date], + }; + try { + const res = await this.connection.query(query); + + /* Return newly inserted assignment */ + return res.rows[0]; + } catch (error) { + throw new HttpException( + { + query: query, + error: error, + }, + HttpStatus.INTERNAL_SERVER_ERROR, + ); + } } /* TODO */ - findOne(id: number) { - return `This action returns a #${id} user`; + async findAllById(type: AssignmentType, id: number) { + let query: QueryConfig; + switch (type) { + case AssignmentType.LSU_ID: + query = { + name: 'select_all_assignment_by_lsu_id', + text: 'SELECT * FROM assignment WHERE lsu_id = $1', + values: [id], + }; + break; + case AssignmentType.TICKET_ID: + query = { + name: 'select_all_assignment_by_ticket_id', + text: 'SELECT * FROM assignment WHERE ticket_id = $1', + values: [id], + }; + break; + + default: + /* default error caused by not using controller properly */ + throw new HttpException( + { + error: + 'Switch statment in assignment.service.findAllById somehow failed', + }, + HttpStatus.INTERNAL_SERVER_ERROR, + ); + } + try { + const res = await this.connection.query(query); + + /* If none exist, return empty array */ + if (res.rows.length < 1) { + return []; + } + /* return array of Assignment objects */ + return res.rows; + } catch (error) { + throw new HttpException( + { + query: query, + error: error, + }, + HttpStatus.INTERNAL_SERVER_ERROR, + ); + } } - /* TODO */ - update(id: number, updateAssignmentDTO: UpdateAssignmentDTO) { - return `This action updates a #${id} user`; + + async findAll() { + const query: QueryConfig = { + name: 'select_all_assignment', + text: 'SELECT * FROM assignment', + }; + try { + const res = await this.connection.query(query); + + /* If none exist, return empty array */ + if (res.rows.length < 1) { + return []; + } + /* return array of Assignment objects */ + return res.rows; + } catch (error) { + throw new HttpException( + { + query: query, + error: error, + }, + HttpStatus.INTERNAL_SERVER_ERROR, + ); + } } /* TODO */ - remove(id: number) { - return `This action removes a #${id} user`; + async findOne(assignment_id: number) { + const query: QueryConfig = { + name: 'select_assignment_by_assignment_id', + text: 'SELECT * FROM assignment WHERE assignment_id = $1', + values: [assignment_id], + }; + try { + const res = await this.connection.query(query); + + /* If none exist, return empty object */ + if (res.rows.length < 1) { + return {}; + } + /* return Assignment object */ + return res.rows[0]; + } catch (error) { + throw new HttpException( + { + query: query, + error: error, + }, + HttpStatus.INTERNAL_SERVER_ERROR, + ); + } } }