generated from agiledev-students-fall2023/generic-project-repository
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #105 from agiledev-students-fall2023/database-set-up
set up database, create expense model, update addExpense routes, fix a bug in addExpense component
- Loading branch information
Showing
6 changed files
with
118 additions
and
21 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
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,43 @@ | ||
const mongoose = require("mongoose"); | ||
const Schema = mongoose.Schema; | ||
|
||
// const splitDetailsSchema = new Schema({ | ||
// user: userSchema, | ||
// settlement: settlementSchema | ||
// }); | ||
|
||
const expenseSchema = new Schema({ | ||
name: { | ||
type: String, | ||
required: true, | ||
}, | ||
description: String, | ||
totalAmount: { | ||
type: Number, | ||
required: true, | ||
}, | ||
date: { | ||
type: Date, | ||
required: true, | ||
}, | ||
// paidBy: {type: mongoose.Schema.Types.ObjectId, ref: 'User'} | ||
paidBy: { | ||
type: String, | ||
required: true, | ||
}, | ||
// splitDetails: [splitDetailsSchema] | ||
splitDetails: [ | ||
{ | ||
user: String, | ||
settlement: String, | ||
}, | ||
], | ||
}); | ||
|
||
// create mongoose Model | ||
const Expense = mongoose.model("Expense", expenseSchema); | ||
|
||
// export the model so other modules can import it | ||
module.exports = { | ||
Expense, | ||
}; |
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 |
---|---|---|
@@ -1,23 +1,56 @@ | ||
const express = require('express'); | ||
const express = require("express"); | ||
const { body, validationResult } = require("express-validator"); | ||
const router = express.Router(); | ||
const { Expense } = require("../models/Expense.js"); | ||
|
||
router.post("/", async (req, res) => { | ||
// assemble an object with the data we want to send | ||
const response = { | ||
router.post( | ||
"/", | ||
// validation rules | ||
[ | ||
body("name").not().isEmpty().withMessage("Name is required"), | ||
body("amount").isNumeric().withMessage("Amount should be a number"), | ||
body("date").not().isEmpty().withMessage("Date is required"), | ||
body("personPaid") | ||
.not() | ||
.isEmpty() | ||
.withMessage("Person who paid is required"), | ||
// Add other validation rules as needed | ||
], | ||
async (req, res) => { | ||
// Check for validation errors | ||
const errors = validationResult(req); | ||
if (!errors.isEmpty()) { | ||
return res.status(400).json({ errors: errors.array() }); | ||
} | ||
|
||
try { | ||
// Create a new Expense object | ||
const newExpense = new Expense({ | ||
name: req.body.name, | ||
description: req.body.description, | ||
totalAmount: req.body.amount, | ||
date: new Date(req.body.date), | ||
paidBy: req.body.personPaid, | ||
splitDetails: req.body.peopleSplit.map((split) => ({ | ||
user: split.user, | ||
settlement: split.settlement, | ||
})), | ||
}); | ||
|
||
// Save the Expense object to the database | ||
await newExpense.save(); | ||
|
||
// Send a success response | ||
res.status(201).json({ | ||
status: "Success", | ||
message: "congratulations on sending us this data!", | ||
data: { | ||
name: req.body.name, | ||
amount: req.body.amount, | ||
date: req.body.date, | ||
personPaid: req.body.personPaid, | ||
peopleSplit: req.body.peopleSplit, | ||
splitMethod: req.body.splitMethod, | ||
amountDetails: req.body.amountDetails | ||
}, | ||
}; | ||
// ... then send a response of some kind to client | ||
res.json(response); | ||
}); | ||
message: "Expense added successfully", | ||
data: newExpense, | ||
}); | ||
} catch (error) { | ||
// Handle any errors that occur during saving to database | ||
res.status(500).json({ status: "Error", message: error.message }); | ||
} | ||
} | ||
); | ||
|
||
module.exports = router; | ||
module.exports = router; |
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,5 +1,6 @@ | ||
{ | ||
"dependencies": { | ||
"dotenv": "^16.3.1", | ||
"react-router-dom": "^6.17.0" | ||
}, | ||
"devDependencies": { | ||
|
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 |
---|---|---|
|
@@ -177,6 +177,11 @@ [email protected]: | |
resolved "https://registry.npmjs.org/diff/-/diff-5.0.0.tgz" | ||
integrity sha512-/VTCrvm5Z0JGty/BWHljh+BAiw3IK+2j87NGMu8Nwc/f48WoDAC395uomO9ZD117ZOBaHmkX1oyLvkVM/aIT3w== | ||
|
||
dotenv@^16.3.1: | ||
version "16.3.1" | ||
resolved "https://registry.npmjs.org/dotenv/-/dotenv-16.3.1.tgz" | ||
integrity sha512-IPzF4w4/Rd94bA9imS68tZBaYyBWSCE47V1RGuMrB94iyTOIEwRmVL2x/4An+6mETpLrKJ5hQkB8W4kFAadeIQ== | ||
|
||
emoji-regex@^8.0.0: | ||
version "8.0.0" | ||
resolved "https://registry.npmjs.org/emoji-regex/-/emoji-regex-8.0.0.tgz" | ||
|