Skip to content

Commit

Permalink
#125 Resolve obsolete id use of appointments
Browse files Browse the repository at this point in the history
The newest convention is to use the `ROWID` attribute of the entities,
not a custom `id` attribute - this is meant to enhance the performance
of the queries, as the `ROWID` is indexed by default.
  • Loading branch information
michalspano committed Dec 30, 2023
1 parent 2e962b1 commit 14885e8
Show file tree
Hide file tree
Showing 7 changed files with 18 additions and 25 deletions.
6 changes: 2 additions & 4 deletions services/appointment-service/controllers/delete.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import {
type WhoisResponse
} from '../types/types';

const { DELETE } = QUERY;
const { GET, DELETE } = QUERY;

const TOPIC: string = utils.MQTT_PAIRS.whois.req;
const RESPONSE_TOPIC: string = utils.MQTT_PAIRS.whois.res;
Expand Down Expand Up @@ -86,9 +86,7 @@ export const deleteAppointment = async (req: Request, res: Response): AsyncResOb
const id: string = req.params.id; // id of the appointment to delete
let objToDelete: Appointment | undefined;
try {
objToDelete = database
.prepare('SELECT ROWID as id,* FROM appointments WHERE ROWID = ?')
.get(id) as Appointment;
objToDelete = GET.APPOINTMENT_BY_ID.get(id) as Appointment;
} catch (err: Error | unknown) {
return res.status(500).json({
message: 'Internal server error: query failed.'
Expand Down
4 changes: 1 addition & 3 deletions services/appointment-service/controllers/patch.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,7 @@ const bookAppointment = async (req: Request, res: Response): AsyncResObj => {
const id: string = req.params.id;
let appointment: Appointment | undefined;
try {
appointment = database
.prepare('SELECT ROWID as id,* FROM appointments WHERE id = ?')
.get(id) as Appointment;
appointment = GET.APPOINTMENT_BY_ID.get(id) as Appointment;
} catch (err: Error | unknown) {
return res.status(500).json({
message: 'Internal server error: query failed.'
Expand Down
13 changes: 4 additions & 9 deletions services/appointment-service/controllers/post.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import * as utils from '../utils';
import { client } from '../mqtt/mqtt';
import database from '../db/config';
import type Database from 'better-sqlite3';
import { type Statement } from 'better-sqlite3';
import type { Request, Response } from 'express';
import {
UserType,
Expand Down Expand Up @@ -92,27 +91,23 @@ const createAppointment = async (req: Request, res: Response): AsyncResObj => {

// All went well, proceed with creating the appointment.
const appointment: Appointment = {
id: '',
id: '', // The id is auto-generated by sqlite3 (ROWID)
start_timestamp: req.body.start_timestamp,
end_timestamp: req.body.end_timestamp,
dentistId: req.body.dentistId,
patientId: null // Initially, the appointment is not booked (hence NULL).
};

const stmt: Statement = database.prepare(`
INSERT INTO appointments
(start_timestamp, end_timestamp, dentistId, patientId)
VALUES (?, ?, ?, ?)
`);

let queryResult: Database.RunResult;

/* A query can fail because of a bad request (e.g. invalid object),
* or that something is wrong with the database (an internal server error).
* TODO: add proper error handling, so that the latter case is appropriately
* handled with a 500 status code. */
try {
queryResult = stmt.run(appointment.start_timestamp, appointment.end_timestamp, appointment.dentistId, appointment.patientId);
queryResult = POST.APPOINTMENT.run(
...Object.values(appointment).slice(1) as [number, number, string, (string | null)]
);
} catch (err: Error | unknown) {
return res.status(400).json({ message: 'Bad request: invalid appointment object.' });
}
Expand Down
2 changes: 1 addition & 1 deletion services/appointment-service/query/delete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import type { Statement } from 'better-sqlite3';
*/
const APPOINTMENT_BY_ID: Readonly<Statement<[string]>> = database?.prepare(`
DELETE FROM appointments
WHERE id = ?
WHERE ROWID = ?
`) as Statement;

/**
Expand Down
10 changes: 6 additions & 4 deletions services/appointment-service/query/get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import type { Statement } from 'better-sqlite3';
* @description retrieve a particular appointment based on its ID.
*/
const APPOINTMENT_BY_ID: Readonly<Statement<[string]>> = database?.prepare(`
SELECT *
SELECT ROWID as id,*
FROM appointments
WHERE id = ?
`) as Statement;
Expand All @@ -27,7 +27,7 @@ const APPOINTMENT_COUNT = (onlyAvailable: boolean): Readonly<Statement<[]>> =>
* given patient.
*/
const APPOINTMENTS_BY_PATIENT: Readonly<Statement<[string]>> = database?.prepare(`
SELECT *
SELECT ROWID as id,*
FROM appointments
WHERE patientId = ?
`) as Statement;
Expand All @@ -42,7 +42,9 @@ const APPOINTMENTS_BY_PATIENT: Readonly<Statement<[string]>> = database?.prepare
*/
const APPOINTMENTS_BY_DENTIST = (onlyAvailable: boolean, hasRange: boolean):
Readonly<Statement<[string, number?, number?]>> => database?.prepare(`
SELECT * FROM appointments WHERE dentistId = ?
SELECT ROWID as id,*
FROM appointments
WHERE dentistId = ?
${onlyAvailable ? ' AND patientId IS NULL' : ''}
${hasRange ? ' AND start_timestamp >= ? AND end_timestamp <= ?' : ''}
`) as Statement;
Expand All @@ -55,7 +57,7 @@ Readonly<Statement<[string, number?, number?]>> => database?.prepare(`
*/
const UNASSIGNED_APPOINTMENTS = (hasRange: boolean): Readonly<Statement<[number?, number?]>> =>
database?.prepare(`
SELECT *
SELECT ROWID as id,*
FROM appointments
WHERE patientId IS NULL
${hasRange ? ' AND start_timestamp >= ? AND end_timestamp <= ?' : ''}
Expand Down
2 changes: 1 addition & 1 deletion services/appointment-service/query/patch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import type { Statement } from 'better-sqlite3';
const BOOKING_STATUS: Readonly<Statement<[string | null, string]>> = database?.prepare(`
UPDATE appointments
SET patientId = ?
WHERE id = ?
WHERE ROWID = ?
`) as Statement;

export { BOOKING_STATUS };
6 changes: 3 additions & 3 deletions services/appointment-service/query/post.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import type { Statement } from 'better-sqlite3';
/**
* @description insert a new appointment into the database.
*/
const APPOINTMENT: Readonly<Statement<[string, number, number, string, (string | null)]>> = database?.prepare(`
const APPOINTMENT: Readonly<Statement<[number, number, string, (string | null)]>> = database?.prepare(`
INSERT INTO appointments
(id, start_timestamp, end_timestamp, dentistId, patientId)
VALUES (?, ?, ?, ?, ?)
(start_timestamp, end_timestamp, dentistId, patientId)
VALUES (?, ?, ?, ?)
`) as Statement;

/**
Expand Down

0 comments on commit 14885e8

Please sign in to comment.