From 84db8b705b3cc1f7d549c5dcb234da97d9953e69 Mon Sep 17 00:00:00 2001 From: Alex Date: Sat, 28 Nov 2020 16:42:18 -0600 Subject: [PATCH] refactored DTO object names to be consistent --- api/src/ticket/dto/combined.dto.ts | 13 +++-- api/src/ticket/dto/create-combined.dto.ts | 18 +++--- api/src/ticket/dto/create-device.dto.ts | 4 +- api/src/ticket/dto/create-ticket.dto.ts | 4 +- api/src/ticket/dto/device.dto.ts | 2 +- api/src/ticket/dto/ticket.dto.ts | 2 +- api/src/ticket/dto/update-ticket.dto.ts | 6 +- api/src/ticket/ticket.controller.ts | 8 +-- api/src/ticket/ticket.service.ts | 71 ++++++++++++----------- api/src/user/dto/create-user.dto.ts | 4 +- api/src/user/dto/update-user.dto.ts | 4 +- api/src/user/dto/user.dto.ts | 2 +- api/src/user/user.controller.ts | 13 +++-- api/src/user/user.service.ts | 25 ++++---- 14 files changed, 92 insertions(+), 84 deletions(-) diff --git a/api/src/ticket/dto/combined.dto.ts b/api/src/ticket/dto/combined.dto.ts index 50d1870..19a9be4 100644 --- a/api/src/ticket/dto/combined.dto.ts +++ b/api/src/ticket/dto/combined.dto.ts @@ -1,8 +1,11 @@ import { IntersectionType } from '@nestjs/swagger'; -import { Ticket } from './ticket.dto'; -import { User } from 'src/user/dto/user.dto'; -import { Device } from './device.dto'; +import { TicketDTO } from './ticket.dto'; +import { UserDTO } from 'src/user/dto/user.dto'; +import { DeviceDTO } from './device.dto'; -class TicketAndDevice extends IntersectionType(Ticket, Device) {} +class TicketAndDeviceDTO extends IntersectionType(TicketDTO, DeviceDTO) {} -export class Combined extends IntersectionType(TicketAndDevice, User) {} +export class CombinedDTO extends IntersectionType( + TicketAndDeviceDTO, + UserDTO, +) {} diff --git a/api/src/ticket/dto/create-combined.dto.ts b/api/src/ticket/dto/create-combined.dto.ts index 2abca50..c4a5807 100644 --- a/api/src/ticket/dto/create-combined.dto.ts +++ b/api/src/ticket/dto/create-combined.dto.ts @@ -1,14 +1,14 @@ import { IntersectionType, OmitType as Omit } from '@nestjs/swagger'; -import { CreateUser } from 'src/user/dto/create-user.dto'; -import { CreateTicket } from './create-ticket.dto'; -import { CreateDevice } from './create-device.dto'; +import { CreateUserDTO } from 'src/user/dto/create-user.dto'; +import { CreateTicketDTO } from './create-ticket.dto'; +import { CreateDeviceDTO } from './create-device.dto'; -class createTicketAndDevice extends IntersectionType( - CreateTicket, - Omit(CreateDevice, ['ticket_id']), +class createTicketAndDeviceDTO extends IntersectionType( + CreateTicketDTO, + Omit(CreateDeviceDTO, ['ticket_id']), ) {} -export class CreateCombined extends IntersectionType( - createTicketAndDevice, - Omit(CreateUser, ['admin']), +export class CreateCombinedDTO extends IntersectionType( + createTicketAndDeviceDTO, + Omit(CreateUserDTO, ['admin']), ) {} diff --git a/api/src/ticket/dto/create-device.dto.ts b/api/src/ticket/dto/create-device.dto.ts index 490c3f0..ee9b3ea 100644 --- a/api/src/ticket/dto/create-device.dto.ts +++ b/api/src/ticket/dto/create-device.dto.ts @@ -1,4 +1,4 @@ import { OmitType as Omit } from '@nestjs/swagger'; -import { Device } from './device.dto'; +import { DeviceDTO } from './device.dto'; -export class CreateDevice extends Omit(Device, ['device_id']) {} +export class CreateDeviceDTO extends Omit(DeviceDTO, ['device_id']) {} diff --git a/api/src/ticket/dto/create-ticket.dto.ts b/api/src/ticket/dto/create-ticket.dto.ts index 291f253..a8adb5f 100644 --- a/api/src/ticket/dto/create-ticket.dto.ts +++ b/api/src/ticket/dto/create-ticket.dto.ts @@ -1,7 +1,7 @@ -import { Ticket } from './ticket.dto'; +import { TicketDTO } from './ticket.dto'; import { OmitType as Omit } from '@nestjs/swagger'; -export class CreateTicket extends Omit(Ticket, [ +export class CreateTicketDTO extends Omit(TicketDTO, [ 'ticket_id', 'status', 'submission_date', diff --git a/api/src/ticket/dto/device.dto.ts b/api/src/ticket/dto/device.dto.ts index fcba3f0..022de62 100644 --- a/api/src/ticket/dto/device.dto.ts +++ b/api/src/ticket/dto/device.dto.ts @@ -1,6 +1,6 @@ import { ApiProperty } from '@nestjs/swagger'; -export class Device { +export class DeviceDTO { @ApiProperty({ description: 'id of device', example: 1 }) device_id: number; diff --git a/api/src/ticket/dto/ticket.dto.ts b/api/src/ticket/dto/ticket.dto.ts index a62c001..4d5fd08 100644 --- a/api/src/ticket/dto/ticket.dto.ts +++ b/api/src/ticket/dto/ticket.dto.ts @@ -6,7 +6,7 @@ export enum TicketType { 'ANY', } -export class Ticket { +export class TicketDTO { @ApiProperty({ readOnly: true }) ticket_id: number; diff --git a/api/src/ticket/dto/update-ticket.dto.ts b/api/src/ticket/dto/update-ticket.dto.ts index 8db254a..0da1bdd 100644 --- a/api/src/ticket/dto/update-ticket.dto.ts +++ b/api/src/ticket/dto/update-ticket.dto.ts @@ -1,9 +1,9 @@ import { PartialType } from '@nestjs/swagger'; -import { CreateTicket } from './create-ticket.dto'; -import { Ticket } from './ticket.dto'; +import { CreateTicketDTO } from './create-ticket.dto'; +import { TicketDTO } from './ticket.dto'; import { OmitType as Omit } from '@nestjs/swagger'; -export class UpdateTicketDto extends Omit(Ticket, [ +export class UpdateTicketDto extends Omit(TicketDTO, [ 'ticket_id', 'submission_date', 'lsu_id', diff --git a/api/src/ticket/ticket.controller.ts b/api/src/ticket/ticket.controller.ts index 5ce4f9a..a93d7f6 100644 --- a/api/src/ticket/ticket.controller.ts +++ b/api/src/ticket/ticket.controller.ts @@ -8,7 +8,7 @@ import { Put, } from '@nestjs/common'; import { ApiTags } from '@nestjs/swagger'; -import { CreateCombined } from './dto/create-combined.dto'; +import { CreateCombinedDTO } from './dto/create-combined.dto'; import { TicketType } from './dto/ticket.dto'; import { UpdateTicketDto } from './dto/update-ticket.dto'; import { TicketService } from './ticket.service'; @@ -20,19 +20,19 @@ export class TicketController { /* WORKING Implementation */ @Post() - create(@Body() createCombined: CreateCombined) { + create(@Body() createCombinedDTO: CreateCombinedDTO) { Logger.log( { req: { http: 'POST /api/ticket', params: 'none', - body: createCombined, + body: createCombinedDTO, }, }, 'TicketController.create', false, ); - return this.ticketService.create(createCombined); + return this.ticketService.create(createCombinedDTO); } /* WORKING Implementation */ diff --git a/api/src/ticket/ticket.service.ts b/api/src/ticket/ticket.service.ts index b73ba0b..b63aed3 100644 --- a/api/src/ticket/ticket.service.ts +++ b/api/src/ticket/ticket.service.ts @@ -7,14 +7,14 @@ import { } from '@nestjs/common'; import { Pool, QueryConfig } from 'pg'; import { PG_CONNECTION } from 'src/connection'; -import { CreateUser } from 'src/user/dto/create-user.dto'; -import { User } from 'src/user/dto/user.dto'; -import { Combined } from './dto/combined.dto'; -import { CreateCombined } from './dto/create-combined.dto'; -import { CreateDevice } from './dto/create-device.dto'; -import { CreateTicket } from './dto/create-ticket.dto'; -import { Device } from './dto/device.dto'; -import { Ticket, TicketType } from './dto/ticket.dto'; +import { CreateUserDTO } from 'src/user/dto/create-user.dto'; +import { UserDTO } from 'src/user/dto/user.dto'; +import { CombinedDTO } from './dto/combined.dto'; +import { CreateCombinedDTO } from './dto/create-combined.dto'; +import { CreateDeviceDTO } from './dto/create-device.dto'; +import { CreateTicketDTO } from './dto/create-ticket.dto'; +import { DeviceDTO } from './dto/device.dto'; +import { TicketDTO, TicketType } from './dto/ticket.dto'; import { UpdateTicketDto } from './dto/update-ticket.dto'; @Injectable() @@ -24,10 +24,10 @@ export class TicketService { /** * Queries and inserts user if user does not exists * - * @param createUser + * @param createUserDTO * @returns User */ - private async createUser(createUser: CreateUser) { + private async createUser(createUserDTO: CreateUserDTO) { const { lsu_id, email, @@ -36,7 +36,7 @@ export class TicketService { phone_number, department, admin, - } = createUser; + } = createUserDTO; /* Check to see if user exists */ const findQuery: QueryConfig = { @@ -46,7 +46,7 @@ export class TicketService { }; try { - const res = await this.connection.query(findQuery); + const res = await this.connection.query(findQuery); /* If user exists abort insert operation by returning early */ if (res.rows.length > 0) { @@ -79,7 +79,7 @@ export class TicketService { ], }; try { - const res = await this.connection.query(insertQuery); + const res = await this.connection.query(insertQuery); /* Return newly inserted user */ return res.rows[0]; @@ -97,17 +97,17 @@ export class TicketService { /** * Inserts ticket into ticket table * - * @param createTicket + * @param createTicketDTO * @returns Ticket */ - private async createTicket(createTicket: CreateTicket) { + private async createTicket(createTicketDTO: CreateTicketDTO) { const { lsu_id, core_issue, description, problem_category, priority, - } = createTicket; + } = createTicketDTO; /* Status set to open for new ticket */ const status = 'OPEN'; @@ -132,7 +132,7 @@ export class TicketService { }; try { - const res = await this.connection.query(query); + const res = await this.connection.query(query); /* Return newly inserted ticket */ return res.rows[0]; @@ -149,17 +149,17 @@ export class TicketService { /** * Inserts new device into device table - * @param createDevice + * @param createDeviceDTO * @returns Device */ - private async createDevice(createDevice: CreateDevice) { + private async createDevice(createDeviceDTO: CreateDeviceDTO) { const { ticket_id, manufacturer, model, operating_system, operating_system_version, - } = createDevice; + } = createDeviceDTO; /* Insert Device into db */ const query: QueryConfig = { @@ -175,7 +175,7 @@ export class TicketService { ], }; try { - const res = await this.connection.query(query); + const res = await this.connection.query(query); /* Return newly inserted device */ return res.rows[0]; @@ -214,16 +214,16 @@ export class TicketService { } /* WORKING Implementation */ - async create(createCombined: CreateCombined) { + async create(createCombinedDTO: CreateCombinedDTO) { /* Response Accumulator */ - let response: Combined; + let response: CombinedDTO; - let device: Device; - let ticket: Ticket; - let user: User; + let device: DeviceDTO; + let ticket: TicketDTO; + let user: UserDTO; /* Insert or Retrieve User */ - const createUser: CreateUser = { ...createCombined, admin: false }; // tickets are created by students so admin is false + const createUser: CreateUserDTO = { ...createCombinedDTO, admin: false }; // tickets are created by students so admin is false try { user = await this.createUser(createUser); @@ -235,7 +235,7 @@ export class TicketService { } /* Create new Ticket */ - const createTicket: CreateTicket = { ...createCombined }; + const createTicket: CreateTicketDTO = { ...createCombinedDTO }; try { ticket = await this.createTicket(createTicket); @@ -247,8 +247,8 @@ export class TicketService { } /* Create new Device */ - const createDevice: CreateDevice = { - ...createCombined, + const createDevice: CreateDeviceDTO = { + ...createCombinedDTO, ticket_id: ticket.ticket_id, }; try { @@ -301,7 +301,7 @@ export class TicketService { break; } try { - const queryRes = await this.connection.query(query); + const queryRes = await this.connection.query(query); /* If no tickets return empty array */ if (queryRes.rows.length === 0) { @@ -322,20 +322,21 @@ export class TicketService { /* WORKING implementation */ async findOne(ticket_id: number) { + /* Query ticket by ticket_id */ const query: QueryConfig = { name: 'select_ticket_by_ticket_id', text: 'SELECT * FROM ticket WHERE ticket_id = $1', values: [ticket_id], }; try { - const queryRes = await this.connection.query(query); + const res = await this.connection.query(query); /* If no ticket found return empty object*/ - if (queryRes.rows.length === 0) { + if (res.rows.length === 0) { return {}; } - return queryRes.rows[0]; + return res.rows[0]; } catch (error) { throw new HttpException( { @@ -364,7 +365,7 @@ export class TicketService { values: [ticket_id], }; try { - const res = await this.connection.query(findQuery); + const res = await this.connection.query(findQuery); /* If no ticket found throw custom error*/ if (res.rows.length < 1) { diff --git a/api/src/user/dto/create-user.dto.ts b/api/src/user/dto/create-user.dto.ts index 9190090..3ed9592 100644 --- a/api/src/user/dto/create-user.dto.ts +++ b/api/src/user/dto/create-user.dto.ts @@ -1,3 +1,3 @@ -import { User } from './user.dto'; +import { UserDTO } from './user.dto'; -export class CreateUser extends User {} +export class CreateUserDTO extends UserDTO {} diff --git a/api/src/user/dto/update-user.dto.ts b/api/src/user/dto/update-user.dto.ts index 4f57edf..f4c017e 100644 --- a/api/src/user/dto/update-user.dto.ts +++ b/api/src/user/dto/update-user.dto.ts @@ -1,3 +1,3 @@ -import { User } from './user.dto'; +import { UserDTO } from './user.dto'; -export class UpdateUser extends User {} +export class UpdateUserDTO extends UserDTO {} diff --git a/api/src/user/dto/user.dto.ts b/api/src/user/dto/user.dto.ts index 6701000..8d43540 100644 --- a/api/src/user/dto/user.dto.ts +++ b/api/src/user/dto/user.dto.ts @@ -4,7 +4,7 @@ export enum UserType { 'STUDENT', 'USER', } -export class User { +export class UserDTO { @ApiProperty({ minLength: 9, maxLength: 9, diff --git a/api/src/user/user.controller.ts b/api/src/user/user.controller.ts index de3f6bd..0a5bfda 100644 --- a/api/src/user/user.controller.ts +++ b/api/src/user/user.controller.ts @@ -8,10 +8,10 @@ import { Delete, } from '@nestjs/common'; import { UserService } from './user.service'; -import { CreateUser } from './dto/create-user.dto'; +import { CreateUserDTO } from './dto/create-user.dto'; import { UserType } from './dto/user.dto'; import { ApiTags } from '@nestjs/swagger'; -import { UpdateUser } from './dto/update-user.dto'; +import { UpdateUserDTO } from './dto/update-user.dto'; @ApiTags('user') @Controller('/api/user') @@ -19,7 +19,7 @@ export class UserController { constructor(private readonly userService: UserService) {} /* WORKING implementation */ @Post() - async create(@Body() createUserDto: CreateUser) { + async create(@Body() createUserDto: CreateUserDTO) { return await this.userService.create(createUserDto); } /* WORKING implementation */ @@ -48,7 +48,10 @@ export class UserController { /* WORKING implementation */ @Put(':lsu_id') - update(@Body() updateUser: UpdateUser, @Param('lsu_id') lsu_id: number) { - return this.userService.update(updateUser, lsu_id); + update( + @Param('lsu_id') lsu_id: number, + @Body() updateUserDTO: UpdateUserDTO, + ) { + return this.userService.update(lsu_id, updateUserDTO); } } diff --git a/api/src/user/user.service.ts b/api/src/user/user.service.ts index 8577e09..a7d7ad5 100644 --- a/api/src/user/user.service.ts +++ b/api/src/user/user.service.ts @@ -7,16 +7,16 @@ import { } from '@nestjs/common'; import { Pool, QueryConfig } from 'pg'; import { PG_CONNECTION } from 'src/connection'; -import { CreateUser } from './dto/create-user.dto'; -import { UpdateUser } from './dto/update-user.dto'; -import { User, UserType } from './dto/user.dto'; +import { CreateUserDTO } from './dto/create-user.dto'; +import { UpdateUserDTO } from './dto/update-user.dto'; +import { UserDTO, UserType } from './dto/user.dto'; @Injectable() export class UserService { constructor(@Inject(PG_CONNECTION) private connection: Pool) {} /* WORKING implementation */ - async create(createUser: CreateUser) { + async create(createUserDTO: CreateUserDTO) { const { lsu_id, email, @@ -25,7 +25,7 @@ export class UserService { phone_number, department, admin, - } = createUser; + } = createUserDTO; const findQuery: QueryConfig = { name: 'select_user_by_id_or_email', @@ -80,7 +80,7 @@ export class UserService { ], }; try { - const res = await this.connection.query(insertQuery); + const res = await this.connection.query(insertQuery); return res.rows[0]; } catch (error) { throw new HttpException( @@ -114,7 +114,7 @@ export class UserService { } try { - const queryRes = await this.connection.query(query); + const queryRes = await this.connection.query(query); /* If no users found return empty array */ if (queryRes.rows.length < 1) { return []; @@ -137,7 +137,7 @@ export class UserService { text: 'SELECT * FROM "user" WHERE "lsu_id" = $1', }; try { - const queryRes = await this.connection.query(query, [lsu_id]); + const queryRes = await this.connection.query(query, [lsu_id]); /* If customer not found return empty object */ if (queryRes.rows.length < 1) { return {}; @@ -156,7 +156,7 @@ export class UserService { } /* WORKING implementation */ - async update(updateUser: UpdateUser, old_lsu_id: number) { + async update(old_lsu_id: number, updateUserDTO: UpdateUserDTO) { const { lsu_id, email, @@ -165,8 +165,9 @@ export class UserService { phone_number, department, admin, - } = updateUser; + } = updateUserDTO; + /* Find user by old_lsu_id supplied in route i.e. /api/user/{lsu_id} */ const findQuery: QueryConfig = { name: 'select_user_by_id', text: 'SELECT lsu_id FROM "user" WHERE lsu_id = $1', @@ -175,7 +176,7 @@ export class UserService { try { const res = await this.connection.query(findQuery); - /* Test to see if student exists */ + /* Test to see if user exists */ if (res.rows.length < 1) { /*Throw custom error to be handled in catch*/ throw new Error('BAD_REQUEST'); @@ -220,7 +221,7 @@ export class UserService { ], }; try { - const res = await this.connection.query(updateQuery); + const res = await this.connection.query(updateQuery); return res.rows[0]; } catch (error) { throw new HttpException(