Skip to content

Commit

Permalink
Added Accessibility, ContactInfo, Services and PostalAddress endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
TheSloanRanger committed Jan 24, 2024
1 parent 1663478 commit e2f54e3
Showing 1 changed file with 144 additions and 82 deletions.
226 changes: 144 additions & 82 deletions api/api_server.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,124 +26,186 @@ db.once("open", () => {
// GET FUNCTIONS
// GET all ATMs
app.get("/api/atms", async (req, res) => {
try {
const atms = await db.collection("ATMs").find({}).toArray();
console.log(atms);
res.json(atms);
} catch (error) {
res.status(500).json({ error: error.message });
}
try {
const atms = await db.collection("ATMs").find({}).toArray();
console.log(atms);
res.json(atms);
} catch (error) {
res.status(500).json({ error: error.message });
}
});

// GET ATM by Identification
app.get("/api/atms/:id", async (req, res) => {
try {
const atm = await db
.collection("ATMs")
.findOne({ Identification: req.params.id });
console.log(atm);
res.json(atm);
} catch (error) {
res.status(500).json({ error: error.message });
}
try {
const atm = await db
.collection("ATMs")
.findOne({ Identification: req.params.id });
console.log(atm);
res.json(atm);
} catch (error) {
res.status(500).json({ error: error.message });
}
});

// GET ATM services by Identification
app.get("/api/atms/:id/services", async (req, res) => {
try {
const atm = await db
.collection("ATMs")
.findOne({ Identification: req.params.id }, { projection: { ATMServices: 1 } });
res.json(atm.ATMServices || []);
} catch (error) {
res.status(500).json({ error: error.message });
}
try {
const atm = await db
.collection("ATMs")
.findOne(
{ Identification: req.params.id },
{ projection: { ATMServices: 1 } }
);
res.json(atm.ATMServices || []);
} catch (error) {
res.status(500).json({ error: error.message });
}
});

// GET ATMs with search functionality
app.get("/api/atms/search", async (req, res) => {
try {
const query = {}; // Construct query based on req.query parameters
// Example: if (req.query.location) query['Location.TownName'] = req.query.location;
const atms = await db.collection("ATMs").find(query).toArray();
res.json(atms);
} catch (error) {
res.status(500).json({ error: error.message });
}
try {
const query = {}; // Construct query based on req.query parameters
// Example: if (req.query.location) query['Location.TownName'] = req.query.location;
const atms = await db.collection("ATMs").find(query).toArray();
res.json(atms);
} catch (error) {
res.status(500).json({ error: error.message });
}
});



// UPDATE FUNCTIONS
// UPDATE an ATM by Identification
app.put("/api/atms/:id", async (req, res) => {
try {
const updatedAtm = await db.collection("ATMs")
.findOneAndUpdate(
{ Identification: req.params.id },
{ $set: req.body },
{ returnDocument: 'after' }
);
console.log(updatedAtm);
res.json(updatedAtm);
} catch (error) {
res.status(500).json({ error: error.message });
}
try {
const updatedAtm = await db
.collection("ATMs")
.findOneAndUpdate(
{ Identification: req.params.id },
{ $set: req.body },
{ returnDocument: "after" }
);
console.log(updatedAtm);
res.json(updatedAtm);
} catch (error) {
res.status(500).json({ error: error.message });
}
});

// DELETE FUNCTIONS
// DELETE an ATM by Identification
app.delete("/api/atms/:id", async (req, res) => {
try {
const deletedAtm = await db.collection("ATMs")
.deleteOne({ Identification: req.params.id });
if (deletedAtm.deletedCount === 0) {
return res.status(404).json({ message: "ATM not found" });
}
console.log(deletedAtm);
res.status(204).json();
} catch (error) {
res.status(500).json({ error: error.message });
try {
const deletedAtm = await db
.collection("ATMs")
.deleteOne({ Identification: req.params.id });
if (deletedAtm.deletedCount === 0) {
return res.status(404).json({ message: "ATM not found" });
}
console.log(deletedAtm);
res.status(204).json();
} catch (error) {
res.status(500).json({ error: error.message });
}
});

// POST FUNCTIONS
// POST a new ATM
app.post("/api/atms", async (req, res) => {
try {
const newAtm = await db.collection("ATMs").insertOne(req.body);
console.log(newAtm);
res.status(201).json(newAtm);
} catch (error) {
res.status(500).json({ error: error.message });
}
try {
const newAtm = await db.collection("ATMs").insertOne(req.body);
console.log(newAtm);
res.status(201).json(newAtm);
} catch (error) {
res.status(500).json({ error: error.message });
}
});


// BRANCH ENDPOINTS

// GET all branches
// GET all Branches
app.get("/api/branches", async (req, res) => {
try {
const branches = await db.collection("Branches").find({}).toArray();
console.log(branches);
res.json(branches);
} catch (error) {
res.status(500).json({ error: error.message });
}
try {
const branches = await db.collection("Branches").find({}).toArray();
console.log(branches);
res.json(branches);
} catch (error) {
res.status(500).json({ error: error.message });
}
});

// GET branch by Identification
// GET Branch by Identification
app.get("/api/branches/:id", async (req, res) => {
try {
const branch = await db
.collection("Branches")
.findOne({ Identification: req.params.id });
console.log(branch);
res.json(branch);
} catch (error) {
res.status(500).json({ error: error.message });
}
try {
const branch = await db
.collection("Branches")
.findOne({ Identification: req.params.id });
console.log(branch);
res.json(branch);
} catch (error) {
res.status(500).json({ error: error.message });
}
});

// GET Branch Accessibility by Identification
app.get("/api/branches/:id/accessibility", async (req, res) => {
try {
const branch = await db
.collection("Branches")
.findOne(
{ Identification: req.params.id },
{ projection: { Accessibility: 1 } }
);
res.json(branch.Accessibility || []);
} catch (error) {
res.status(500).json({ error: error.message });
}
});

// GET Branch ServiceAndFacility by Identification
app.get("/api/branches/:id/service_and_facility", async (req, res) => {
try {
const branch = await db
.collection("Branches")
.findOne(
{ Identification: req.params.id },
{ projection: { ServiceAndFacility: 1 } }
);
res.json(branch.ServiceAndFacility || []);
} catch (error) {
res.status(500).json({ error: error.message });
}
});

// GET Branch PostalAddress by Identification
app.get("/api/branches/:id/postal_address", async (req, res) => {
try {
const branch = await db
.collection("Branches")
.findOne(
{ Identification: req.params.id },
{ projection: { PostalAddress: 1 } }
);
res.json(branch.PostalAddress || []);
} catch (error) {
res.status(500).json({ error: error.message });
}
});

// GET Branch ContactInfo by Identification
app.get("/api/branches/:id/contact_info", async (req, res) => {
try {
const branch = await db
.collection("Branches")
.findOne(
{ Identification: req.params.id },
{ projection: { ContactInfo: 1 } }
);
res.json(branch.ContactInfo || []);
} catch (error) {
res.status(500).json({ error: error.message });
}
});

// Listen on the specified port
Expand Down

0 comments on commit e2f54e3

Please sign in to comment.