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

Backend scout #22

Merged
merged 8 commits into from
Dec 24, 2023
330 changes: 330 additions & 0 deletions server/controllers/scout.controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,330 @@
import db from '../database/db.js'

const scoutController = {
allScoutsCount: async (req, res) => {
try {
const result = await db.query(`
SELECT COUNT(*)
FROM "Scout"
`)

res.status(200).json({
message: "Successful retrieval",
body: result,
})

} catch (error) {
console.log(error);
res.status(500).json({
message: 'An error occured while retrieving data',
error
})
}
},
allScoutsInfo: async (req, res) => {
try {
const result = await db.query(`
SELECT *
FROM "Scout"
`)

res.status(200).json({
message: "Successful retrieval",
body: result,
})

} catch (error) {
console.log(error);
res.status(500).json({
message: 'An error occured while retrieving data',
error
})
}
},
scoutsInSectorInfo: async (req, res) => {
try {
const { sectorBaseName, sectorSuffixName } = req.body;

if (sectorBaseName === undefined && sectorSuffixName === undefined) {
return res.status(400).json({
error: "Please enter the sector base name and/or suffix name"
})
}

const result = await db.query(`
SELECT *
FROM "Scout"
WHERE "sectorBaseName" = $1 AND "sectorSuffixName" = $2
`,
[sectorBaseName, sectorSuffixName])

if (!result.rows.length) {
return res.status(404).json({
error: "No scouts found in this sector"
})
}

res.status(200).json({
message: "Successful retrieval",
body: result,
})

} catch (error) {
console.log(error);
res.status(500).json({
message: 'An error occured while retrieving data',
error
})
}
},
scoutsInSectorCount: async (req, res) => {
try {
const { sectorBaseName, sectorSuffixName } = req.body;

if (sectorBaseName === undefined && sectorSuffixName === undefined) {
return res.status(400).json({
error: "Please enter the sector base name and/or suffix name"
})
}

const result = await db.query(`
SELECT COUNT(*)
FROM "Scout"
WHERE "sectorBaseName" = $1 AND "sectorSuffixName" = $2
`,
[sectorBaseName, sectorSuffixName])

res.status(200).json({
message: "Successful retrieval",
body: result,
})

} catch (error) {
console.log(error);
res.status(500).json({
message: 'An error occured while retrieving data',
error
})
}
},
allScoutsInUnitInfo: async (req, res) => {
try {
const { unitCaptainId } = req.body;

// Make sure that the id is provided (not undefined)
if (!unitCaptainId){
return res.status(404).json({
error: "Enter a valid unit captain id"
})
}

const result = await db.query(`
SELECT scout.*
FROM "Scout" AS scout, "Sector" AS sector
WHERE sector."unitCaptainId" = $1 AND scout."sectorBaseName" = sector."baseName" AND scout."sectorSuffixName" = sector."suffixName";
`,
[unitCaptainId])

if (!result.rows.length) {
return res.status(404).json({
error: "No scouts found in this unit"
})
}

res.status(200).json({
message: "Successful retrieval",
body: result,
})

} catch (error) {
console.log(error);
res.status(500).json({
message: 'An error occured while retrieving data',
error
})
}
},
allScoutsInUnitCount: async (req, res) => {
try {
const { unitCaptainId } = req.body;

// Make sure that the id is provided (not undefined)
if (!unitCaptainId){
return res.status(404).json({
error: "Enter a valid unit captain id"
})
}

const result = await db.query(`
SELECT Count(*)
FROM "Scout" AS scout, "Sector" AS sector
WHERE sector."unitCaptainId" = $1 AND scout."sectorBaseName" = sector."baseName" AND scout."sectorSuffixName" = sector."suffixName";
`,
[unitCaptainId])

if (!result.rows.length) {
return res.status(404).json({
error: "No scouts found in this unit"
})
}

res.status(200).json({
message: "Successful retrieval",
body: result,
})

} catch (error) {
console.log(error);
res.status(500).json({
message: 'An error occured while retrieving data',
error
})
}
},
certainScoutInfo: async (req, res) => {
try {
const { scoutId } = req.body;

// Make sure that the id is provided (not undefined)
if (!scoutId){
return res.status(404).json({
error: "Enter a valid scout id"
})
}

const result = await db.query(`
SELECT *
FROM "Scout"
WHERE "scoutId" = $1;
`,
[scoutId])

if (!result.rows.length) {
return res.status(404).json({
error: "No scout found"
})
}

res.status(200).json({
message: "Successful retrieval",
body: result,
})

} catch (error) {
console.log(error);
res.status(500).json({
message: 'An error occured while retrieving data',
error
})
}
},
updateScout: async (req, res) => {
try {
// Destructuring the req.body to get the required info to update a scout
const { scoutId, firstName, middleName, lastName, gender, sectorBaseName,
sectorSuffixName, birthDate, enrollDate, schoolGrade, photo, birthCertificate } = req.body;

// If no scout id is provided give an error
if (!scoutId) {
return res.status(400).json({
error: "Please enter a valid scout id"
})
}

// Update the scout data
const result1 = await db.query(`
UPDATE "Scout"
SET "firstName" = $1, "middleName" = $2, "lastName" = $3, "gender" = $4, "sectorBaseName" = $5,
"sectorSuffixName" = $6
WHERE "scoutId" = $7
RETURNING *
`,
[firstName, middleName, lastName, gender, sectorBaseName, sectorSuffixName, scoutId])

// If no rows are effected respond with an error status and message
if (result1.rowCount == 0) {
return res.status(404).json({
error: "No rows updated for the scout",
body: result1
})
}

// Update the scout profile data
const result2 = await db.query(`
UPDATE "ScoutProfile"
SET "birthDate" = $1, "enrollDate" = $2, "schoolGrade" = $3, "photo" = $4,
"birthCertificate" = $5
WHERE "scoutId" = $6
RETURNING *
`,
[birthDate, enrollDate, schoolGrade, photo, birthCertificate, scoutId])

// Respond with the updated data
res.status(200).json({
message: "Successful update",
body: { result1, result2 }
})

} catch (error) {
console.log(error);
res.status(500).json({
message: 'An error occured while updating the scout',
error
})
}
},
insertScout: async (req, res) => {
try {
// Destructuring the req.body to get the required info to update a scout
const { firstName, middleName, lastName, gender, sectorBaseName,
sectorSuffixName, birthDate, enrollDate, schoolGrade, photo, birthCertificate } = req.body;

// Insert a new scout into the database
const result1 = await db.query(`
INSERT INTO "Scout" ("firstName", "middleName", "lastName", "gender", "sectorBaseName", "sectorSuffixName")
VALUES ($1, $2, $3, $4, $5, $6)
RETURNING *;
`,
[firstName, middleName, lastName, gender, sectorBaseName, sectorSuffixName])

// If nothing was inserted return an error
if (result1.rowCount == 0) {
return res.status(400).json({
error: "No data was inserted for the scout",
body: result1
})
}

// Insert the scout profile
const result2 = await db.query(`
INSERT INTO "ScoutProfile" ("birthDate", "enrollDate", "schoolGrade", "photo", "birthCertificate", "scoutId")
VALUES ($1, $2, $3, $4, $5, $6)
RETURNING *;
`,
[birthDate, enrollDate, schoolGrade, photo, birthCertificate, result1.rows[0]["scoutId"]])

// If nothing was inserted return an error
if (result2.rowCount == 0) {
return res.status(400).json({
error: "No data was inserted for the scout profile",
body: result2
})
}

// Return the data
res.status(200).json({
message: "Successful insertion",
body: { result1, result2 }
})

} catch (error) {
console.log(error);
res.status(500).json({
message: 'An error occured while inserting a new scout',
error
})
}
}
}

export default scoutController;
2 changes: 2 additions & 0 deletions server/routes/api.route.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ import authRouter from './auth.route.js'
import captainRouter from './captain.route.js'
import authMiddleware from '../middlewares/auth.middleware.js'
import alertRouter from './alert.route.js'
import scoutRouter from './scout.route.js'

const apiRouter = Router()

apiRouter.use('/auth', authRouter)
apiRouter.use('/captain', authMiddleware, captainRouter)
apiRouter.use('/notifications', alertRouter)
apiRouter.use('/scout', authMiddleware, scoutRouter)

export default apiRouter
17 changes: 17 additions & 0 deletions server/routes/scout.route.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { Router } from "express"
import scoutController from "../controllers/scout.controller.js";


const scoutRouter = Router();

scoutRouter.get('/allScouts/count', scoutController.allScoutsCount)
scoutRouter.get('/allScouts/info', scoutController.allScoutsInfo)
scoutRouter.get('/scoutsInSector/count', scoutController.scoutsInSectorCount)
scoutRouter.get('/scoutsInSector/info', scoutController.scoutsInSectorInfo)
scoutRouter.get('/allScoutsInUnit/count', scoutController.allScoutsInUnitCount)
scoutRouter.get('/allScoutsInUnit/info', scoutController.allScoutsInUnitInfo)
scoutRouter.get('/certainScout/info', scoutController.certainScoutInfo)
scoutRouter.put('/updateScout', scoutController.updateScout)
scoutRouter.post('/insertScout', scoutController.insertScout)

export default scoutRouter