Skip to content

Commit

Permalink
Merge pull request #17 from cdalton713/backend
Browse files Browse the repository at this point in the history
Backend
  • Loading branch information
alxford45 authored Nov 29, 2020
2 parents 40b7ea2 + deae680 commit eb94b4a
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 4 deletions.
8 changes: 8 additions & 0 deletions api/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,14 @@ req: none
res: body: {CombineDTO}
```

Get one ticket by lsu_id with user/ticket/device info

```
GET /api​/ticket​/user/{lsu_id}
req: none
res: body: {CombineDTO}
```

Updates one ticket by ticket_id; Does NOT update user or device

```
Expand Down
81 changes: 77 additions & 4 deletions api/src/ticket/ticket.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -300,30 +300,103 @@ export class TicketService {

/* WORKING implementation */
async findOne(ticket_id: number) {
let response: CombinedDTO;

/* Query ticket by ticket_id */
const query: QueryConfig = {
const ticketQuery: QueryConfig = {
name: 'select_ticket_by_ticket_id',
text: 'SELECT * FROM ticket WHERE ticket_id = $1',
values: [ticket_id],
};
try {
const res = await this.connection.query<TicketDTO>(query);
const res = await this.connection.query<TicketDTO>(ticketQuery);

/* If no ticket found return empty object*/
if (res.rows.length === 0) {
return {};
}
/* Accumulate ticket into response */
const ticket = res.rows[0];
response = { ...response, ...ticket };
} catch (error) {
throw new HttpException(
{
query: ticketQuery,
error: error,
},
HttpStatus.INTERNAL_SERVER_ERROR,
);
}

return res.rows[0];
/* Query user by ticket_id */
const userQuery: QueryConfig = {
name: 'select_user_by_lsu_id',
text: 'SELECT * FROM "user" WHERE lsu_id = $1',
values: [response.lsu_id],
};
try {
const res = await this.connection.query<UserDTO>(userQuery);

/* If no ticket throw custom error*/
if (res.rows.length === 0) {
throw new Error('BAD_REQUEST');
}
/* Accumulate ticket into response */
const user = res.rows[0];
response = { ...response, ...user };
} catch (error) {
if ((error.message = 'BAD_REQUEST')) {
throw new HttpException(
{
query: ticketQuery,
error: `Could not find user with lsu_id: ${userQuery.values[0]}`,
},
HttpStatus.INTERNAL_SERVER_ERROR,
);
}
throw new HttpException(
{
query: ticketQuery,
error: error,
},
HttpStatus.INTERNAL_SERVER_ERROR,
);
}
/* Query device by ticket_id */
const deviceQuery: QueryConfig = {
name: 'select_device_by_ticket_id',
text: 'SELECT * FROM "device" WHERE ticket_id = $1',
values: [ticket_id],
};
try {
const res = await this.connection.query<DeviceDTO>(deviceQuery);

/* If no device found throw custom error*/
if (res.rows.length === 0) {
throw new Error('BAD_REQUEST');
}
/* Accumulate device into response */
const device = res.rows[0];
response = { ...response, ...device };
} catch (error) {
if (error.message === 'BAD_REQUEST') {
throw new HttpException(
{
query: deviceQuery,
error: `Could not find device with ticket_id: ${deviceQuery.values[0]}`,
},
HttpStatus.INTERNAL_SERVER_ERROR,
);
}
throw new HttpException(
{
query: query,
query: ticketQuery,
error: error,
},
HttpStatus.INTERNAL_SERVER_ERROR,
);
}
return response;
}

/* WORKING Implementation */
Expand Down

0 comments on commit eb94b4a

Please sign in to comment.