Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Task 2 : Done #13

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"nuxt.isNuxtApp": false
}
141 changes: 110 additions & 31 deletions server.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
import express from 'express';
import Database from 'better-sqlite3';
import express from "express";
import Database from "better-sqlite3";

const app = express();

app.use(express.json());

app.get('/', (req, res) => {
return res.status(200).send({'message': 'SHIPTIVITY API. Read documentation to see API docs'});
app.get("/", (req, res) => {
return res
.status(200)
.send({ message: "SHIPTIVITY API. Read documentation to see API docs" });
});

// We are keeping one connection alive for the rest of the life application for simplicity
const db = new Database('./clients.db');
const db = new Database("./clients.db");

// Don't forget to close connection when server gets terminated
const closeDb = () => db.close();
process.on('SIGTERM', closeDb);
process.on('SIGINT', closeDb);
process.on("SIGTERM", closeDb);
process.on("SIGINT", closeDb);

/**
* Validate id input
Expand All @@ -26,25 +28,27 @@ const validateId = (id) => {
return {
valid: false,
messageObj: {
'message': 'Invalid id provided.',
'long_message': 'Id can only be integer.',
message: "Invalid id provided.",
long_message: "Id can only be integer.",
},
};
}
const client = db.prepare('select * from clients where id = ? limit 1').get(id);
const client = db
.prepare("select * from clients where id = ? limit 1")
.get(id);
if (!client) {
return {
valid: false,
messageObj: {
'message': 'Invalid id provided.',
'long_message': 'Cannot find client with that id.',
message: "Invalid id provided.",
long_message: "Cannot find client with that id.",
},
};
}
return {
valid: true,
};
}
};

/**
* Validate priority input
Expand All @@ -55,34 +59,41 @@ const validatePriority = (priority) => {
return {
valid: false,
messageObj: {
'message': 'Invalid priority provided.',
'long_message': 'Priority can only be positive integer.',
message: "Invalid priority provided.",
long_message: "Priority can only be positive integer.",
},
};
}
return {
valid: true,
}
}
};
};

/**
* Get all of the clients. Optional filter 'status'
* GET /api/v1/clients?status={status} - list all clients, optional parameter status: 'backlog' | 'in-progress' | 'complete'
*/
app.get('/api/v1/clients', (req, res) => {
app.get("/api/v1/clients", (req, res) => {
const status = req.query.status;
if (status) {
// status can only be either 'backlog' | 'in-progress' | 'complete'
if (status !== 'backlog' && status !== 'in-progress' && status !== 'complete') {
if (
status !== "backlog" &&
status !== "in-progress" &&
status !== "complete"
) {
return res.status(400).send({
'message': 'Invalid status provided.',
'long_message': 'Status can only be one of the following: [backlog | in-progress | complete].',
message: "Invalid status provided.",
long_message:
"Status can only be one of the following: [backlog | in-progress | complete].",
});
}
const clients = db.prepare('select * from clients where status = ?').all(status);
const clients = db
.prepare("select * from clients where status = ?")
.all(status);
return res.status(200).send(clients);
}
const statement = db.prepare('select * from clients');
const statement = db.prepare("select * from clients");
const clients = statement.all();
return res.status(200).send(clients);
});
Expand All @@ -91,13 +102,15 @@ app.get('/api/v1/clients', (req, res) => {
* Get a client based on the id provided.
* GET /api/v1/clients/{client_id} - get client by id
*/
app.get('/api/v1/clients/:id', (req, res) => {
const id = parseInt(req.params.id , 10);
app.get("/api/v1/clients/:id", (req, res) => {
const id = parseInt(req.params.id, 10);
const { valid, messageObj } = validateId(id);
if (!valid) {
res.status(400).send(messageObj);
}
return res.status(200).send(db.prepare('select * from clients where id = ?').get(id));
return res
.status(200)
.send(db.prepare("select * from clients where id = ?").get(id));
});

/**
Expand All @@ -114,23 +127,89 @@ app.get('/api/v1/clients/:id', (req, res) => {
* priority (optional): integer,
*
*/
app.put('/api/v1/clients/:id', (req, res) => {
const id = parseInt(req.params.id , 10);
app.put("/api/v1/clients/:id", (req, res) => {
const id = parseInt(req.params.id, 10);
const { valid, messageObj } = validateId(id);
if (!valid) {
res.status(400).send(messageObj);
}

let { status, priority } = req.body;
let clients = db.prepare('select * from clients').all();
const client = clients.find(client => client.id === id);
let clients = db.prepare("select * from clients").all();
const client = clients.find((client) => client.id === id);

/* ---------- Update code below ----------*/
if (status) {
if (
status !== "backlog" &&
(status !== "in-progress") & (status !== "complete")
) {
return res.status(400).send({
message: "Invalid status provided",
long_message:
"Status can only be one of the following : [backlog | in-progress | complete]",
});
}
}

const newStatus = status;
const oldStatus = client.status;
const oldPriority = client.priority;

// There are 3 possible use cases:
// 1. oldStatus == newStatus AND oldPriority == priority, do nothing.
// 2. oldStatus == newStatus AND oldPriority != priority, reorder clients with the same status.
// 3. oldStatus != newStatus, reorder clients in oldStatus and newStatus. If priority is provided, rearranged accordingly.

if (oldStatus === newStatus && priority && oldPriority !== priority) {
const clientWithDifferentStatus = clients.filter(
(client) => client.status === newStatus
);
client.priority = priority - 0.5;

const clientWithSameStatus = clients
.filter((client) => client.status === newStatus)
.sort((a, b) => a.priority - b.priority)
.map((client, index) => ({ ...client, priority: index + 1 }));
clients = [...clientWithDifferentStatus, ...clientWithSameStatus];
} else if (oldStatus !== newStatus) {
client.status = newStatus;
client.priority = priority ? priority - 0.5 : Number.MAX_SAFE_INTEGER;

const clientWithDifferentStatus = clients.filter(
(client) => client.status !== oldStatus && client.status !== newStatus
);
const clientWithOldStatus = clients
.filter((client) => client.status === oldStatus)
.sort((a, b) => a.priority - b.priority)
.map((client, index) => ({ ...client, priority: index + 1 }));

const clientWithNewStatus = client
.filter((client) => client.status === newStatus)
.sort((a, b) => a.priority - b.priority)
.map((client, index) => ({
...client,
priority: index + 1,
}));

client.priority = clientWithNewStatus.length;

clients = [
...clientWithDifferentStatus,
...clientWithOldStatus,
...clientWithNewStatus,
];
}

const updateStmt = db.prepare(
"Update clients set status = ?, priority = ?, where id = ?"
);
clients.forEach((client) => {
updateStmt.run(client.status, client.priority, client.id);
});

return res.status(200).send(clients);
});

app.listen(3001);
console.log('app running on port ', 3001);
console.log("app running on port ", 3001);
Binary file added task2.patch
Binary file not shown.