generated from WildCodeSchool/create-js-monorepo
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
32 changed files
with
778 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
[{ "label": "Accepté" }, { "label": "Refusé" }, { "label": "En cours" }] | ||
[{ "label": "Accepté" }, { "label": "En cours" }, { "label": "Refusé" }] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
// Import access to database tables | ||
const tables = require("../tables"); | ||
|
||
// The B of BREAD - Browse (Read All) operation | ||
const browse = async (req, res, next) => { | ||
try { | ||
// Fetch all applications from the database | ||
const applications = await tables.application.readAll(); | ||
|
||
// Respond with the applications in JSON format | ||
res.status(200).json(applications); | ||
} catch (err) { | ||
// Pass any errors to the error-handling middleware | ||
next(err); | ||
} | ||
}; | ||
|
||
// The R of BREAD - Read operation | ||
const read = async (req, res, next) => { | ||
try { | ||
// Fetch a specific application from the database based on the provided ID | ||
const application = await tables.application.read(req.params.id); | ||
|
||
// If the application is not found, respond with HTTP 404 (Not Found) | ||
// Otherwise, respond with the application in JSON format | ||
if (application == null) { | ||
res.sendStatus(404); | ||
} else { | ||
res.status(200).json(application); | ||
} | ||
} catch (err) { | ||
// Pass any errors to the error-handling middleware | ||
next(err); | ||
} | ||
}; | ||
|
||
const readProfileApplications = async (req, res, next) => { | ||
try { | ||
const applications = await tables.application.getProfileApplications( | ||
req.user.id | ||
); | ||
|
||
if (applications == null) { | ||
res.sendStatus(404); | ||
} else { | ||
res.status(200).json(applications); | ||
} | ||
} catch (err) { | ||
next(err); | ||
} | ||
}; | ||
|
||
const readConsultantApplications = async (req, res, next) => { | ||
try { | ||
const applications = await tables.application.getConsultantApplications( | ||
req.user.id | ||
); | ||
|
||
if (applications == null) { | ||
res.sendStatus(404); | ||
} else { | ||
res.status(200).json(applications); | ||
} | ||
} catch (err) { | ||
next(err); | ||
} | ||
}; | ||
|
||
// The E of BREAD - Edit (Update) operation | ||
const edit = async (req, res, next) => { | ||
// Extract the application data from the request body | ||
const application = req.body; | ||
try { | ||
// Fetch a specific city from the database based on the provided ID | ||
const result = await tables.application.update(req.params.id, application); | ||
|
||
// If the application is not found, respond with HTTP 404 (Not Found) | ||
if (result.affectedRows === 1) { | ||
res.sendStatus(204); | ||
} else { | ||
res.sendStatus(404); | ||
} | ||
} catch (err) { | ||
// Pass any errors to the error-handling middleware | ||
next(err); | ||
} | ||
}; | ||
|
||
// The A of BREAD - Add (Create) operation | ||
const add = async (req, res, next) => { | ||
// Extract the application data from the request body | ||
const application = req.body; | ||
|
||
try { | ||
// Insert the application into the database | ||
const insertId = await tables.application.create(application); | ||
|
||
// Respond with HTTP 201 (Created) and the ID of the newly inserted application | ||
res.status(201).json({ insertId }); | ||
} catch (err) { | ||
// Pass any errors to the error-handling middleware | ||
next(err); | ||
} | ||
}; | ||
|
||
// The D of BREAD - Destroy (Delete) operation | ||
// This operation is not yet implemented | ||
|
||
// Ready to export the controller functions | ||
module.exports = { | ||
browse, | ||
read, | ||
readProfileApplications, | ||
readConsultantApplications, | ||
edit, | ||
add, | ||
// destroy, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
// Import access to database tables | ||
const tables = require("../tables"); | ||
|
||
// The B of BREAD - Browse (Read All) operation | ||
const browse = async (req, res, next) => { | ||
try { | ||
// Fetch all status from the database | ||
const status = await tables.application_status.readAll(); | ||
|
||
// Respond with the status in JSON format | ||
res.status(200).json(status); | ||
} catch (err) { | ||
// Pass any errors to the error-handling middleware | ||
next(err); | ||
} | ||
}; | ||
|
||
// Ready to export the controller functions | ||
module.exports = { | ||
browse, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
const AbstractManager = require("./AbstractManager"); | ||
|
||
class ApplicationManager extends AbstractManager { | ||
constructor() { | ||
// Call the constructor of the parent class (AbstractManager) | ||
// and pass the table name "application" as configuration | ||
super({ table: "application" }); | ||
} | ||
|
||
// The C of CRUD - Create operation | ||
|
||
async create(application) { | ||
// Execute the SQL INSERT query to add a new application to the "application" table | ||
const [result] = await this.database.query( | ||
`insert into ${this.table} (user_id, job_id) values (?, ?)`, | ||
[application.user_id, application.job_id] | ||
); | ||
|
||
// Return the ID of the newly inserted application | ||
return result.insertId; | ||
} | ||
|
||
// The Rs of CRUD - Read operations | ||
|
||
async read(id) { | ||
// Execute the SQL SELECT query to retrieve a specific application by its ID | ||
const [rows] = await this.database.query( | ||
`select * from ${this.table} where id = ?`, | ||
[id] | ||
); | ||
|
||
// Return the first row of the result, which represents the application | ||
return rows[0]; | ||
} | ||
|
||
async readAll() { | ||
// Execute the SQL SELECT query to retrieve all applications from the "application" table | ||
const [rows] = await this.database.query(`select * from ${this.table}`); | ||
|
||
// Return the array of applications | ||
return rows; | ||
} | ||
|
||
async getProfileApplications(userId) { | ||
const [rows] = await this.database.query( | ||
`SELECT | ||
application.id, | ||
application.job_id, | ||
application.status_id, | ||
job.title AS job_title, | ||
job.consultant_id, | ||
consultant.email AS consultant_email, | ||
application_status.label as status_label | ||
FROM ${this.table} AS application | ||
INNER JOIN job ON application.job_id = job.id | ||
INNER JOIN user AS consultant ON job.consultant_id = consultant.id | ||
INNER JOIN user ON application.user_id = user.id | ||
INNER JOIN application_status ON application.status_id = application_status.id | ||
WHERE user.id = ?`, | ||
[userId] | ||
); | ||
return rows; | ||
} | ||
|
||
async getConsultantApplications(consultantId) { | ||
const [rows] = await this.database.query( | ||
`SELECT | ||
application.id AS application_id, | ||
application.job_id, | ||
application.status_id, | ||
job.title AS job_title, | ||
consultant.id AS consultant_id, | ||
consultant.email AS consultant_email, | ||
user.id AS candidate_id, | ||
user.email AS candidate_email, | ||
application_status.label AS status_label, | ||
company.name AS company_name | ||
FROM ${this.table} AS application | ||
INNER JOIN job ON application.job_id = job.id | ||
INNER JOIN user AS consultant ON job.consultant_id = consultant.id | ||
INNER JOIN user ON application.user_id = user.id | ||
INNER JOIN application_status ON application.status_id = application_status.id | ||
INNER JOIN company ON job.company_id = company.id | ||
WHERE consultant.id = ? | ||
ORDER BY company.name`, | ||
[consultantId] | ||
); | ||
return rows; | ||
} | ||
|
||
// The U of CRUD - Update operation | ||
async update(id, application) { | ||
// Execute the SQL SELECT query to retrieve a specific application by its ID | ||
const [result] = await this.database.query( | ||
`UPDATE ${this.table} set ? WHERE id = ?`, | ||
[application, id] | ||
); | ||
|
||
// Return the first row of the result, which represents the item | ||
return result; | ||
} | ||
|
||
// The D of CRUD - Delete operation | ||
// TODO: Implement the delete operation to remove an application by its ID | ||
|
||
// async delete(id) { | ||
// ... | ||
// } | ||
} | ||
|
||
module.exports = ApplicationManager; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
const AbstractManager = require("./AbstractManager"); | ||
|
||
class ApplicationStatusManager extends AbstractManager { | ||
constructor() { | ||
// Call the constructor of the parent class (AbstractManager) | ||
// and pass the table name "application_status" as configuration | ||
super({ table: "application_status" }); | ||
} | ||
|
||
async readAll() { | ||
// Execute the SQL SELECT query to retrieve all status from the "application_status" table | ||
const [rows] = await this.database.query(`select * from ${this.table}`); | ||
|
||
// Return the array of status | ||
return rows; | ||
} | ||
} | ||
|
||
module.exports = ApplicationStatusManager; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.