This repository has been archived by the owner on Oct 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
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 #26 from gtaEPIC/user-registration
User registration
- Loading branch information
Showing
7 changed files
with
153 additions
and
13 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,97 @@ | ||
const User = require('../models/userResgistration'); | ||
|
||
module.exports.createUser = async (req, res, next) => { | ||
try { | ||
const { username, password, email, type } = req.body; | ||
|
||
|
||
const newUser = new User({ username, password, email, type }); | ||
|
||
// Save the user to the database | ||
await newUser.save(); | ||
|
||
res.status(201).json({ message: 'User Created Successfully' }); | ||
} catch (error) { | ||
console.error(error); | ||
res.status(500).json({ message: 'Internal Server Error' }); | ||
} | ||
}; | ||
|
||
module.exports.getAllUsers = async (req, res, next) => { | ||
try{ | ||
const users = await User.find(); | ||
res.status(200).json({ | ||
success: true, | ||
message: 'Users retrieved successfully', | ||
users: users | ||
}); | ||
} catch (error) { | ||
console.error(error); | ||
res.status(500).json({ | ||
success: false, | ||
message: 'Internal Server Error' | ||
}); | ||
} | ||
}; | ||
|
||
module.exports.userByusername = async (req, res, next) => { | ||
try { | ||
const { username } = req.params; // Assuming username is part of the route parameters | ||
|
||
const user = await User.findOne({ username }); | ||
|
||
if (!user) { | ||
res.status(404).json({ success: false, message: 'User not found' }); | ||
return; | ||
} | ||
|
||
res.status(200).json({ success: true, message: 'User retrieved successfully', user }); | ||
} catch (error) { | ||
console.error(error); | ||
res.status(500).json({ success: false, message: 'Internal Server Error' }); | ||
} | ||
}; | ||
|
||
module.exports.update = async (req, res, next) => { | ||
try { | ||
const { username } = req.params; // Assuming username is part of the route parameters | ||
const { password, email, type } = req.body; | ||
|
||
const updatedUser = await User.findOneAndUpdate( | ||
{ username }, | ||
{ password, email, type }, | ||
{ new: true } // Return the updated document | ||
); | ||
|
||
if (!updatedUser) { | ||
res.status(404).json({ success: false, message: 'User not found' }); | ||
return; | ||
} | ||
|
||
res.status(200).json({ success: true, message: 'User updated successfully', user: updatedUser }); | ||
} catch (error) { | ||
console.error(error); | ||
res.status(500).json({ success: false, message: 'Internal Server Error' }); | ||
} | ||
}; | ||
|
||
module.exports.deleteUser = async (req, res, next) => { | ||
try { | ||
const { username } = req.params; // Assuming username is part of the route parameters | ||
|
||
const deletedUser = await User.findOneAndDelete({ username }); | ||
|
||
if (!deletedUser) { | ||
res.status(404).json({ success: false, message: 'User not found' }); | ||
return; | ||
} | ||
|
||
res.status(200).json({ success: true, message: 'User deleted successfully', user: deletedUser }); | ||
} catch (error) { | ||
console.error(error); | ||
res.status(500).json({ success: false, message: 'Internal Server Error' }); | ||
} | ||
}; | ||
|
||
|
||
|
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,13 @@ | ||
// models/User.js | ||
const mongoose = require('mongoose'); | ||
|
||
const userSchema = new mongoose.Schema({ | ||
username: String, | ||
password: String, | ||
email: String, | ||
type: String, | ||
}); | ||
|
||
const User = mongoose.model('User', userSchema); | ||
|
||
module.exports = User; |
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,12 @@ | ||
var express = require('express'); | ||
var router = express.Router(); | ||
|
||
let userRegistrationController = require("../controllers/userResgistration"); | ||
|
||
router.get("/", userRegistrationController.getAllUsers); | ||
router.post("/", userRegistrationController.createUser); | ||
router.get("/:username", userRegistrationController.userByusername); | ||
router.put("/:username", userRegistrationController.update); | ||
router.delete("/:username", userRegistrationController.deleteUser); | ||
|
||
module.exports = router; |
This file was deleted.
Oops, something went wrong.
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,27 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||
<title>Registration Form</title> | ||
<link rel='stylesheet' href='/stylesheets/style.css' /> | ||
</head> | ||
<body> | ||
<h1>User Creation Form</h1> | ||
|
||
<form id="userForm"> | ||
<label for="username">Username:</label> | ||
<input type="text" id="username" name="username" required> | ||
|
||
<label for="password">Password:</label> | ||
<input type="password" id="password" name="password" required> | ||
|
||
<label for="email">Email:</label> | ||
<input type="email" id="email" name="email" required> | ||
|
||
<label for="type">User Type:</label> | ||
<input type="text" id="type" name="type" required> | ||
|
||
<button type="button" onclick="createUser()">Create User</button> | ||
</form> | ||
</body> | ||
</html> |
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