Skip to content

Commit

Permalink
refactored queries
Browse files Browse the repository at this point in the history
  • Loading branch information
alxford45 committed Nov 28, 2020
1 parent 6a48b57 commit 6f9472d
Showing 1 changed file with 45 additions and 24 deletions.
69 changes: 45 additions & 24 deletions api/src/user/user.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
Injectable,
Logger,
} from '@nestjs/common';
import { Pool } from 'pg';
import { Pool, QueryConfig } from 'pg';
import { PG_CONNECTION } from 'src/connection';
import { CreateUser } from './dto/create-user.dto';
import { User, UserType } from './dto/user.dto';
Expand All @@ -15,21 +15,25 @@ export class UserService {
constructor(@Inject(PG_CONNECTION) private connection: Pool) {}

/* WORKING implementation */
async create(createCustomerDto: CreateUser) {
async create(createUser: CreateUser) {
const {
lsu_id,
email,
first_name,
last_name,
department,
phone_number,
department,
admin,
} = createCustomerDto;
} = createUser;

/* Query student by lsuid or email */
const findQuery: QueryConfig = {
name: 'select_user_by_id_or_email',
text: 'SELECT lsu_id FROM "user" WHERE lsu_id = $1 OR email = $2',
values: [lsu_id, email],
};
try {
const query = 'SELECT lsu_id FROM "user" WHERE lsu_id = $1 OR email = $2';
const queryRes = await this.connection.query(query, [lsu_id, email]);
/* Query student by lsuid or email */
const queryRes = await this.connection.query(findQuery);

/* Test to see if student exists */
if (queryRes.rows.length > 0) {
Expand All @@ -51,30 +55,36 @@ export class UserService {
/* Catch postgres error and throw Internal Error HttpException */
throw new HttpException(
{
message: findQuery,
error: error,
},
HttpStatus.INTERNAL_SERVER_ERROR,
);
}

/* Insert new student into db */
const text =
'INSERT INTO "user" (lsu_id, email, first_name, last_name, phone_number, department, admin) VALUES ($1, $2, $3, $4, $5, $6, $7) RETURNING *';
const values = [
lsu_id,
email,
first_name,
last_name,
phone_number,
department,
admin,
];
const insertQuery: QueryConfig = {
name: 'insert_user',

text:
'INSERT INTO "user" (lsu_id, email, first_name, last_name, phone_number, department, admin) VALUES ($1, $2, $3, $4, $5, $6, $7) RETURNING *',
values: [
lsu_id,
email,
first_name,
last_name,
phone_number,
department,
admin,
],
};
try {
const res = await this.connection.query<User>(text, values);
const res = await this.connection.query<User>(insertQuery);
return res.rows[0];
} catch (error) {
throw new HttpException(
{
message: insertQuery,
error: error,
},
HttpStatus.INTERNAL_SERVER_ERROR,
Expand All @@ -83,16 +93,22 @@ export class UserService {
}
/* WORKING implementation */
async findAll(userType: UserType) {
let query;
let query: QueryConfig;
switch (userType) {
case UserType.STUDENT:
query = 'SELECT * FROM "user" WHERE admin = false';
query = {
name: 'select_all_students',
text: 'SELECT * FROM "user" WHERE admin = false',
};
break;
case UserType.ADMIN:
query = 'SELECT * FROM "user" WHERE admin = true';
query = {
name: 'select_all_admin',
text: 'SELECT * FROM "user" WHERE admin = true',
};
break;
default:
query = 'SELECT * FROM "user"';
query = { name: 'select_all_users', text: 'SELECT * FROM "user"' };
break;
}

Expand All @@ -106,6 +122,7 @@ export class UserService {
} catch (error) {
throw new HttpException(
{
message: query,
error: error,
},
HttpStatus.INTERNAL_SERVER_ERROR,
Expand All @@ -114,8 +131,11 @@ export class UserService {
}
/* WORKING implementation */
async findOne(lsu_id: number) {
const query = {
name: 'select_user_by_id',
text: 'SELECT * FROM "user" WHERE "lsu_id" = $1',
};
try {
const query = 'SELECT * FROM "user" WHERE "lsu_id" = $1';
const queryRes = await this.connection.query<User>(query, [lsu_id]);
/* If customer not found return empty object */
if (queryRes.rows.length < 1) {
Expand All @@ -126,6 +146,7 @@ export class UserService {
} catch (error) {
throw new HttpException(
{
message: query,
error: error,
},
HttpStatus.INTERNAL_SERVER_ERROR,
Expand Down

0 comments on commit 6f9472d

Please sign in to comment.