Skip to content
This repository has been archived by the owner on Oct 16, 2024. It is now read-only.

Commit

Permalink
Merge pull request #26 from gtaEPIC/user-registration
Browse files Browse the repository at this point in the history
User registration
  • Loading branch information
gtaEPIC authored Nov 12, 2023
2 parents a574589 + 56e0a97 commit 9655820
Show file tree
Hide file tree
Showing 7 changed files with 153 additions and 13 deletions.
4 changes: 2 additions & 2 deletions config/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ var cookieParser = require('cookie-parser');
var logger = require('morgan');

var indexRouter = require('../routes/index');
var usersRouter = require('../routes/users');
let ticketRouter = require('../routes/ticketRoute');
var userRegistrationRouter = require('../routes/userRegistration');

var app = express();

Expand All @@ -21,8 +21,8 @@ app.use(cookieParser());
app.use(express.static(path.join(__dirname, '../public')));

app.use('/', indexRouter);
app.use('/users', usersRouter);
app.use('/ticket', ticketRouter);
app.use('/users', userRegistrationRouter);

// catch 404 and forward to error handler
app.use(function(req, res, next) {
Expand Down
97 changes: 97 additions & 0 deletions controllers/userResgistration.js
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' });
}
};



13 changes: 13 additions & 0 deletions models/userResgistration.js
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;
12 changes: 12 additions & 0 deletions routes/userRegistration.js
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;
9 changes: 0 additions & 9 deletions routes/users.js

This file was deleted.

27 changes: 27 additions & 0 deletions views/index.ejs
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>
4 changes: 2 additions & 2 deletions views/ticketEdit.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@
</tr>
<tr>
<td><label for="description">Description</label></td>
<td><textarea id="description" content="<%= ticket.description %>"></textarea></td>
<td><textarea id="description"><%= ticket.description %></textarea></td>
</tr>
<tr>
<td><label for="status">Status</label></td>
Expand All @@ -86,7 +86,7 @@
</tr>
<tr>
<td><label for="resolution">Resolution</label></td>
<td><textarea id="resolution" content="<%= ticket.resolution %>"></textarea></td>
<td><textarea id="resolution"><%= ticket.resolution %></textarea></td>
</tr>
<tr>
<td><label for="comment">Edit Comment</label></td>
Expand Down

0 comments on commit 9655820

Please sign in to comment.