Skip to content

Commit

Permalink
refactored DTO object names to be consistent
Browse files Browse the repository at this point in the history
  • Loading branch information
alxford45 committed Nov 28, 2020
1 parent 19ce4bd commit 84db8b7
Show file tree
Hide file tree
Showing 14 changed files with 92 additions and 84 deletions.
13 changes: 8 additions & 5 deletions api/src/ticket/dto/combined.dto.ts
Original file line number Diff line number Diff line change
@@ -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,
) {}
18 changes: 9 additions & 9 deletions api/src/ticket/dto/create-combined.dto.ts
Original file line number Diff line number Diff line change
@@ -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']),
) {}
4 changes: 2 additions & 2 deletions api/src/ticket/dto/create-device.dto.ts
Original file line number Diff line number Diff line change
@@ -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']) {}
4 changes: 2 additions & 2 deletions api/src/ticket/dto/create-ticket.dto.ts
Original file line number Diff line number Diff line change
@@ -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',
Expand Down
2 changes: 1 addition & 1 deletion api/src/ticket/dto/device.dto.ts
Original file line number Diff line number Diff line change
@@ -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;

Expand Down
2 changes: 1 addition & 1 deletion api/src/ticket/dto/ticket.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export enum TicketType {
'ANY',
}

export class Ticket {
export class TicketDTO {
@ApiProperty({ readOnly: true })
ticket_id: number;

Expand Down
6 changes: 3 additions & 3 deletions api/src/ticket/dto/update-ticket.dto.ts
Original file line number Diff line number Diff line change
@@ -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',
Expand Down
8 changes: 4 additions & 4 deletions api/src/ticket/ticket.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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 */
Expand Down
71 changes: 36 additions & 35 deletions api/src/ticket/ticket.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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,
Expand All @@ -36,7 +36,7 @@ export class TicketService {
phone_number,
department,
admin,
} = createUser;
} = createUserDTO;

/* Check to see if user exists */
const findQuery: QueryConfig = {
Expand All @@ -46,7 +46,7 @@ export class TicketService {
};

try {
const res = await this.connection.query<User>(findQuery);
const res = await this.connection.query<UserDTO>(findQuery);

/* If user exists abort insert operation by returning early */
if (res.rows.length > 0) {
Expand Down Expand Up @@ -79,7 +79,7 @@ export class TicketService {
],
};
try {
const res = await this.connection.query<User>(insertQuery);
const res = await this.connection.query<UserDTO>(insertQuery);

/* Return newly inserted user */
return res.rows[0];
Expand All @@ -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';
Expand All @@ -132,7 +132,7 @@ export class TicketService {
};

try {
const res = await this.connection.query<Ticket>(query);
const res = await this.connection.query<TicketDTO>(query);

/* Return newly inserted ticket */
return res.rows[0];
Expand All @@ -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 = {
Expand All @@ -175,7 +175,7 @@ export class TicketService {
],
};
try {
const res = await this.connection.query<Device>(query);
const res = await this.connection.query<DeviceDTO>(query);

/* Return newly inserted device */
return res.rows[0];
Expand Down Expand Up @@ -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);

Expand All @@ -235,7 +235,7 @@ export class TicketService {
}

/* Create new Ticket */
const createTicket: CreateTicket = { ...createCombined };
const createTicket: CreateTicketDTO = { ...createCombinedDTO };
try {
ticket = await this.createTicket(createTicket);

Expand All @@ -247,8 +247,8 @@ export class TicketService {
}

/* Create new Device */
const createDevice: CreateDevice = {
...createCombined,
const createDevice: CreateDeviceDTO = {
...createCombinedDTO,
ticket_id: ticket.ticket_id,
};
try {
Expand Down Expand Up @@ -301,7 +301,7 @@ export class TicketService {
break;
}
try {
const queryRes = await this.connection.query<Ticket>(query);
const queryRes = await this.connection.query<TicketDTO>(query);

/* If no tickets return empty array */
if (queryRes.rows.length === 0) {
Expand All @@ -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<Ticket>(query);
const res = await this.connection.query<TicketDTO>(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(
{
Expand Down Expand Up @@ -364,7 +365,7 @@ export class TicketService {
values: [ticket_id],
};
try {
const res = await this.connection.query<Ticket>(findQuery);
const res = await this.connection.query<TicketDTO>(findQuery);

/* If no ticket found throw custom error*/
if (res.rows.length < 1) {
Expand Down
4 changes: 2 additions & 2 deletions api/src/user/dto/create-user.dto.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import { User } from './user.dto';
import { UserDTO } from './user.dto';

export class CreateUser extends User {}
export class CreateUserDTO extends UserDTO {}
4 changes: 2 additions & 2 deletions api/src/user/dto/update-user.dto.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import { User } from './user.dto';
import { UserDTO } from './user.dto';

export class UpdateUser extends User {}
export class UpdateUserDTO extends UserDTO {}
2 changes: 1 addition & 1 deletion api/src/user/dto/user.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ export enum UserType {
'STUDENT',
'USER',
}
export class User {
export class UserDTO {
@ApiProperty({
minLength: 9,
maxLength: 9,
Expand Down
13 changes: 8 additions & 5 deletions api/src/user/user.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,18 @@ 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')
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 */
Expand Down Expand Up @@ -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);
}
}
Loading

0 comments on commit 84db8b7

Please sign in to comment.