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

Adding new fields to use-model and add a user to database #69

Merged
merged 1 commit into from
Feb 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions users/userservice/user-model.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,22 @@ const userSchema = new mongoose.Schema({
type: Date,
default: Date.now,
},
email: {
type: String,
required: true,
},
questions_answered: {
type: int,
required: true,
},
correctly_answered_questions: {
type: int,
required: false,
}




});

const User = mongoose.model('User', userSchema);
Expand Down
25 changes: 21 additions & 4 deletions users/userservice/user-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,27 @@ const User = require('./user-model')
const app = express();
const port = 8001;

// Middleware to parse JSON in request body
// Middleware to parse JSON in request bodyUsersDB
app.use(bodyParser.json());


// Connect to MongoDB
const mongoUri = process.env.MONGODB_URI || 'mongodb://localhost:27017/userdb';
mongoose.connect(mongoUri);
// Connect to MongoDB - testing
const mongoUri = 'mongodb+srv://prueba:prueba@cluster0.kjzbhst.mongodb.net/?retryWrites=true&w=majority&appName=Cluster0';
mongoose.connect(mongoUri).then(
console.log('Succesfully connected to MongoDB')
);


// GET route to retrieve all users - not working
app.get('/users', async (req, res) => {
try {
const users = await User.find(); // Retrieve all users from the database
console.log("Users:", users); // Print users in the terminal
res.json(users); // Send the array of users as JSON response
} catch (error) {
res.status(500).json({ error: 'Internal Server Error' });
}
});



Expand All @@ -26,6 +41,7 @@ function validateRequiredFields(req, requiredFields) {
}
}


app.post('/adduser', async (req, res) => {
try {
// Check if required fields are present in the request body
Expand All @@ -37,6 +53,7 @@ app.post('/adduser', async (req, res) => {
const newUser = new User({
username: req.body.username,
password: hashedPassword,
email: req.body.email
});

await newUser.save();
Expand Down
Loading