Skip to content

Commit

Permalink
implemented assignment endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
alxford45 committed Nov 29, 2020
1 parent b0a0c42 commit 9d54cc1
Show file tree
Hide file tree
Showing 2 changed files with 151 additions and 64 deletions.
62 changes: 30 additions & 32 deletions api/src/assignment/assignment.controller.ts
Original file line number Diff line number Diff line change
@@ -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);
}
}
153 changes: 121 additions & 32 deletions api/src/assignment/assignment.service.ts
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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<TicketAssign>(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<AssignmentDTO>(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<AssignmentDTO>(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<AssignmentDTO>(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<AssignmentDTO>(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,
);
}
}
}

0 comments on commit 9d54cc1

Please sign in to comment.