Skip to content
This repository has been archived by the owner on Apr 4, 2024. It is now read-only.

Commit

Permalink
Merge pull request #3 from digicatapult/feature/clients
Browse files Browse the repository at this point in the history
reorganising helper files/functions, helper routes file for clients, …
  • Loading branch information
darrylmorton-digicatapult authored May 3, 2022
2 parents 792bc15 + 4ab7ed5 commit 060e92d
Show file tree
Hide file tree
Showing 21 changed files with 1,023 additions and 292 deletions.
51 changes: 51 additions & 0 deletions app/api-v1/api-doc.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,57 @@ const apiDoc = {
},
},
schemas: {
GetClient: {
description: 'Schema for retrieving a client',
type: 'object',
properties: {
id: {
description: 'Id of the client',
type: 'string',
format: 'uuid',
},
firstName: {
description: 'First name of the client',
type: 'string',
},
lastName: {
description: 'Last name of the client',
type: 'string',
},
company: {
description: 'Company of the client',
type: 'string',
},
role: {
description: 'Role of the client',
type: 'string',
},
},
required: ['id', 'firstName', 'lastName', 'company', 'role'],
},
PostAndPutClient: {
description: 'Schema for creating/updating a client',
type: 'object',
properties: {
firstName: {
description: 'First name of the client',
type: 'string',
},
lastName: {
description: 'Last name of the client',
type: 'string',
},
company: {
description: 'Company of the client',
type: 'string',
},
role: {
description: 'Role of the client',
type: 'string',
},
},
required: ['firstName', 'lastName', 'company', 'role'],
},
GetProject: {
description: 'GET project',
type: 'object',
Expand Down
62 changes: 62 additions & 0 deletions app/api-v1/routes/profiler/client.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
const {
POST_CLIENT_RESPONSES,
validatePostClientResponse,
} = require('../../validators/client/postClientResponseValidator')
const {
GET_CLIENTS_RESPONSES,
validateGetClientsResponse,
} = require('../../validators/client/getClientsResponseValidator')

module.exports = function (apiService) {
const doc = {
GET: async function (req, res) {
const { statusCode, result } = await apiService.getClients()

const validationErrors = validateGetClientsResponse(statusCode, result)

if (validationErrors) {
res.status(statusCode).json(validationErrors)
return
} else {
res.status(statusCode).json(result)
return
}
},
POST: async function (req, res) {
const { statusCode, result } = await apiService.postClient(req.body)

const validationErrors = validatePostClientResponse(statusCode, result)

if (validationErrors) {
res.status(statusCode).json(validationErrors)
return
} else {
res.status(statusCode).json(result)
return
}
},
}

doc.GET.apiDoc = {
summary: 'Retrieve clients',
responses: GET_CLIENTS_RESPONSES,
tags: ['clients'],
}

doc.POST.apiDoc = {
summary: 'Create client',
requestBody: {
content: {
'application/json': {
schema: {
$ref: '#/components/schemas/PostAndPutClient',
},
},
},
},
responses: POST_CLIENT_RESPONSES,
tags: ['clients'],
}

return doc
}
123 changes: 123 additions & 0 deletions app/api-v1/routes/profiler/client/{id}.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
const {
validateGetClientByIdResponse,
GET_CLIENT_BY_ID_RESPONSES,
} = require('../../../validators/client/getClientByIdResponseValidator')
const {
PUT_CLIENT_RESPONSES,
validatePutClientResponse,
} = require('../../../validators/client/putClientResponseValidator')
const {
validateDeleteClientResponse,
DELETE_CLIENT_RESPONSES,
} = require('../../../validators/client/deleteClientResponseValidator')

module.exports = function (apiService) {
const doc = {
GET: async function (req, res) {
const { id } = req.params
const { statusCode, result } = await apiService.getClientById(id)

const validationErrors = validateGetClientByIdResponse(statusCode, result)

if (validationErrors) {
res.status(statusCode).json(validationErrors)
return
} else {
res.status(statusCode).json(result)
return
}
},
PUT: async function (req, res) {
const { id } = req.params
const { statusCode, result } = await apiService.putClient(id, req.body)

const validationErrors = validatePutClientResponse(statusCode, result)

if (validationErrors) {
res.status(statusCode).json(validationErrors)
return
} else {
res.status(statusCode).json(result)
return
}
},
DELETE: async function (req, res) {
const { id } = req.params
const { statusCode, result } = await apiService.deleteClient(id)

const validationErrors = validateDeleteClientResponse(statusCode, result)

if (validationErrors) {
res.status(statusCode).json(validationErrors)
return
} else {
res.status(statusCode).send()
}
},
}

doc.GET.apiDoc = {
summary: 'Retrieve client by id',
parameters: [
{
description: 'Id of the client',
in: 'path',
required: true,
name: 'id',
schema: {
type: 'string',
format: 'uuid',
},
},
],
responses: GET_CLIENT_BY_ID_RESPONSES,
tags: ['clients'],
}

doc.PUT.apiDoc = {
summary: 'Update client',
parameters: [
{
description: 'Id of the client',
in: 'path',
required: true,
name: 'id',
schema: {
type: 'string',
format: 'uuid',
},
},
],
requestBody: {
content: {
'application/json': {
schema: {
$ref: '#/components/schemas/PostAndPutClient',
},
},
},
},
responses: PUT_CLIENT_RESPONSES,
tags: ['clients'],
}

doc.DELETE.apiDoc = {
summary: 'Remove client',
parameters: [
{
description: 'Id of the client',
in: 'path',
required: true,
name: 'id',
schema: {
type: 'string',
format: 'uuid',
},
},
],
responses: DELETE_CLIENT_RESPONSES,
tags: ['clients'],
}

return doc
}
56 changes: 56 additions & 0 deletions app/api-v1/services/apiService.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,59 @@ const {
getProjectByIdDb,
deleteProjectByIdDb,
updateProjectDb,
addClientDb,
findClientsDb,
findClientByIdDb,
updateClientDb,
removeClientDb,
} = require('../../db')

async function getClients() {
const result = await findClientsDb()

return { statusCode: 200, result }
}

async function getClientById(id) {
const result = await findClientByIdDb(id)

if (result.length === 1) {
return { statusCode: 200, result: result[0] }
} else {
return { statusCode: 404, result: {} }
}
}

async function postClient(reqBody) {
const result = await addClientDb(reqBody)

return { statusCode: 201, result: result[0] }
}

async function putClient(id, reqBody) {
const findResult = await findClientByIdDb(id)

if (findResult.length === 1) {
const result = await updateClientDb(id, reqBody)

return { statusCode: 200, result: result[0] }
} else {
return { statusCode: 404, result: {} }
}
}

async function deleteClient(id) {
const findResult = await findClientByIdDb(id)

if (findResult.length === 1) {
await removeClientDb(id)

return { statusCode: 204 }
} else {
return { statusCode: 404, result: {} }
}
}

async function getProjects() {
const result = await getProjectsDb()

Expand Down Expand Up @@ -62,6 +113,11 @@ async function deleteProjectById(id) {
}

module.exports = {
getClients,
getClientById,
postClient,
putClient,
deleteClient,
getProjects,
postProject,
getProjectById,
Expand Down
22 changes: 22 additions & 0 deletions app/api-v1/validators/client/deleteClientResponseValidator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
const { default: OpenAPIResponseValidator } = require('openapi-response-validator')

const apiDocResponses = require('../../api-doc-responses')

const DELETE_CLIENT_RESPONSES = {
204: {
description: 'Deleted client',
},
404: apiDocResponses['404'],
default: apiDocResponses.default,
}

const validateDeleteClientResponse = (statusCode, result) => {
const responseValidator = new OpenAPIResponseValidator({ responses: DELETE_CLIENT_RESPONSES })

return responseValidator.validateResponse(statusCode, result)
}

module.exports = {
DELETE_CLIENT_RESPONSES,
validateDeleteClientResponse,
}
28 changes: 28 additions & 0 deletions app/api-v1/validators/client/getClientByIdResponseValidator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
const { default: OpenAPIResponseValidator } = require('openapi-response-validator')

const apiDocResponses = require('../../api-doc-responses')
const apiDoc = require('../../api-doc')

const GET_CLIENT_BY_ID_RESPONSES = {
200: {
description: 'Retrieved client by id',
content: {
'application/json': {
schema: apiDoc.components.schemas.GetClient,
},
},
},
404: apiDocResponses['404'],
default: apiDocResponses.default,
}

const validateGetClientByIdResponse = (statusCode, result) => {
const responseValidator = new OpenAPIResponseValidator({ responses: GET_CLIENT_BY_ID_RESPONSES })

return responseValidator.validateResponse(statusCode, result)
}

module.exports = {
GET_CLIENT_BY_ID_RESPONSES,
validateGetClientByIdResponse,
}
30 changes: 30 additions & 0 deletions app/api-v1/validators/client/getClientsResponseValidator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
const { default: OpenAPIResponseValidator } = require('openapi-response-validator')

const apiDocResponses = require('../../api-doc-responses')
const apiDoc = require('../../api-doc')

const GET_CLIENTS_RESPONSES = {
200: {
description: 'Retrieved clients',
content: {
'application/json': {
schema: {
type: 'array',
items: apiDoc.components.schemas.GetClient,
},
},
},
},
default: apiDocResponses.default,
}

const validateGetClientsResponse = (statusCode, result) => {
const responseValidator = new OpenAPIResponseValidator({ responses: GET_CLIENTS_RESPONSES })

return responseValidator.validateResponse(statusCode, result)
}

module.exports = {
GET_CLIENTS_RESPONSES,
validateGetClientsResponse,
}
Loading

0 comments on commit 060e92d

Please sign in to comment.