Skip to content

Commit

Permalink
implemented GET and POST for work
Browse files Browse the repository at this point in the history
  • Loading branch information
alxford45 committed Nov 29, 2020
1 parent 5bc7d00 commit 1a7f30c
Show file tree
Hide file tree
Showing 2 changed files with 189 additions and 0 deletions.
49 changes: 49 additions & 0 deletions api/src/work/work.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import {
Controller,
Get,
Post,
Body,
Put,
Param,
Delete,
} from '@nestjs/common';
import { WorkService } from './work.service';
import { CreateWorkDTO } from './dto/create-work.dto';
import { ApiTags } from '@nestjs/swagger';
import { WorkType } from './dto/work.dto';

@ApiTags('work')
@Controller('/api/work')
export class WorkController {
constructor(private readonly workService: WorkService) {}

/* Working Implementation */
@Post()
async create(@Body() createWorkDTO: CreateWorkDTO) {
return await this.workService.create(createWorkDTO);
}

/* Working Implementation */
@Get()
findAll() {
return this.workService.findAll();
}

/* Working Implementation */
@Get('ticket/:ticket_id')
findAllByTicketId(@Param('ticket_id') ticket_id: number) {
return this.workService.findAllById(WorkType.TICKET_ID, ticket_id);
}

/* Working Implementation */
@Get('user/:lsu_id')
findAllByLsuId(@Param('lsu_id') lsu_id: number) {
return this.workService.findAllById(WorkType.LSU_ID, lsu_id);
}

/* Working Implementation */
@Get(':work_id')
findOne(@Param('work_id') id: number) {
return this.workService.findOne(+id);
}
}
140 changes: 140 additions & 0 deletions api/src/work/work.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
import { HttpException, HttpStatus, Inject, Injectable } from '@nestjs/common';
import { Pool, QueryConfig } from 'pg';
import { PG_CONNECTION } from 'src/connection';
import { CreateWorkDTO } from './dto/create-work.dto';
import { WorkDTO, WorkType } from './dto/work.dto';

/* TODO:
- Implement remaining methods
- Find solution to abstract SQL
*/
@Injectable()
export class WorkService {
constructor(@Inject(PG_CONNECTION) private connection: Pool) {}

async create(createTicketWorkDto: CreateWorkDTO) {
const {
lsu_id,
ticket_id,
start_datetime,
end_datetime,
} = createTicketWorkDto;

/*Insert work into db*/
const query: QueryConfig = {
name: 'insert_work',
text:
'INSERT INTO work(lsu_id, ticket_id, start_datetime, end_datetime) VALUES ($1, $2, $3, $4) RETURNING *',
values: [lsu_id, ticket_id, start_datetime, end_datetime],
};
try {
const res = await this.connection.query<WorkDTO>(query);
return res.rows[0];
} catch (error) {
throw new HttpException(
{
query: query,
error: error,
},
HttpStatus.INTERNAL_SERVER_ERROR,
);
}
}

/* TODO */
async findAll() {
const query: QueryConfig = {
name: 'find_all_work',
text: 'SELECT * FROM work',
};
try {
const res = await this.connection.query<WorkDTO>(query);
if (res.rows.length < 1) {
return [];
}
return res.rows;
} catch (error) {
throw new HttpException(
{
query: query,
error: error,
},
HttpStatus.INTERNAL_SERVER_ERROR,
);
}
}

/* TODO */
async findOne(work_id: number) {
const query: QueryConfig = {
name: 'find_work_by_work_id',
text: 'SELECT * FROM work WHERE work_id = $1',
values: [work_id],
};
try {
const res = await this.connection.query<WorkDTO>(query);
if (res.rows.length < 1) {
throw new Error('BAD_REQUEST');
}
return res.rows[0];
} catch (error) {
if (error.message === 'BAD_REQUEST') {
throw new HttpException(
{
query: query,
error: `No work exists with work_id: ${query.values[0]}`,
},
HttpStatus.BAD_REQUEST,
);
}
throw new HttpException(
{
query: query,
error: error,
},
HttpStatus.INTERNAL_SERVER_ERROR,
);
}
}
async findAllById(type: WorkType, id: number) {
let query: QueryConfig;
switch (type) {
case WorkType.LSU_ID:
query = {
name: 'find_all_work_by_lsu_id',
text: 'SELECT * from work WHERE lsu_id = $1',
values: [id],
};
break;
case WorkType.TICKET_ID:
query = {
name: 'find_all_work_by_ticket_id',
text: 'SELECT * from work WHERE ticket_id = $1',
values: [id],
};
break;
default:
throw new HttpException(
{
error: 'Switch statement in work.service.findAllById failed',
},
HttpStatus.INTERNAL_SERVER_ERROR,
);
}
try {
const res = await this.connection.query<WorkDTO>(query);
if (res.rows.length < 1) {
return [];
}
return res.rows;
} catch (error) {
throw new HttpException(
{
query: query,
error: error,
},
HttpStatus.INTERNAL_SERVER_ERROR,
);
}
}
}

0 comments on commit 1a7f30c

Please sign in to comment.