Skip to content
Open
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
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -104,3 +104,8 @@ dist

# TernJS port file
.tern-port

# development.josn file
config/environments/


7 changes: 5 additions & 2 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ app.use(cors()); // will configure later

// routes
app.use("/api", router);

// catch 404 later
// app.use((req, res, next) => next("Not Found"));

Expand All @@ -51,7 +50,11 @@ app.use((err, req, res, next) => {
return next(err);
}
req.log.error(err);
return res.status(500).send(err.message);
return res.status(err.status || err.code || 500).send({
code: err.status || err.code || 500,
success: false,
message: err.message,
});
});

module.exports = app;
19 changes: 0 additions & 19 deletions config/environments/development.json

This file was deleted.

20 changes: 20 additions & 0 deletions controllers/firebaseTest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
const ApiError = require("../utils/ApiError");
const { db } = require("../config/environments/firebase.js");
const { collection, getDocs } = require("firebase/firestore");
module.exports = {
getAll: async (req, res, next) => {
try {
const users = collection(db, "users");
const userDocs = await getDocs(users);
const userList = userDocs.docs.map((doc) => doc.data());
res.send({
code: 200,
success: true,
message: "Users list",
data: userList,
});
} catch (err) {
next(err);
}
},
};
58 changes: 0 additions & 58 deletions controllers/foo.js

This file was deleted.

137 changes: 137 additions & 0 deletions controllers/user.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
const { User } = require("../models");
const ApiError = require("../utils/ApiError");
const bcrypt = require("bcrypt");
module.exports = {
getAll: async (req, res, next) => {
try {
const users = await User.findAll({ where: { archive: false } });
if (!users.length) throw new ApiError(404, "User not found");
res.send({
code: 200,
success: true,
message: "Users list",
data: users,
});
} catch (err) {
next(err);
}
},
getOne: async (req, res, next) => {
try {
const { id } = req.params;
const user = await User.findOne({
where: {
id,
archive: false,
},
});
if (!user) {
throw new ApiError(404, "User not found");
}
res.send({
code: 200,
success: true,
message: "User data",
data: user,
});
} catch (err) {
next(err);
}
},
signIn: async (req, res, next) => {
try {
const { email, password } = req.body;
if (!email || !password) {
throw new ApiError(400, "Some data is missing");
}
const user = await User.findOne({
where: { email: email, archive: false },
});
if (!user) throw new ApiError(400, "Email or Password is Incorrect");
const verifyPassword = await bcrypt.compare(password, user.password);
if (!verifyPassword)
throw new ApiError(400, "Email or Password is Incorrect");
res.send({
code: 200,
success: true,
message: "User Signed In SuccessFully",
data: user.email,
});
} catch (err) {
next(err);
}
},
create: async (req, res, next) => {
try {
const { name, email, password } = req.body;
if (!name || !email || !password) {
throw new ApiError(400, "Some data is missing");
}
const genSalt = await bcrypt.genSalt(10);
const hashedPassword = await bcrypt.hash(password, genSalt);
const createdUser = await User.create({
email,
password: hashedPassword,
name,
});
res.send({
code: 200,
success: true,
message: "User Created Succcessfully",
data: createdUser.email,
});
} catch (err) {
next(err);
}
},

archive: async (req, res, next) => {
try {
const { id } = req.params;

const user = await User.update(
{
archive: true,
},
{
where: { id },
}
);
if (!user[0]) throw new ApiError(404, "User not found");
res.send({
code: 200,
success: true,
message: "User Removed",
data: user.email,
});
} catch (err) {
next(err);
}
},

update: async (req, res, next) => {
try {
const { id } = req.params;
const { name, password, email } = req.body;
const user = await User.update(
{
name,
email,
password,
},
{
where: { id },
}
);
if (!user[0]) throw new ApiError(404, "User not found");
res.send({
code: 200,
success: true,
message: "User Updated Successfully",
data: user.email,
});
} catch (err) {
next(err);

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

next always take it to the next function, can you explain where is the next leading to in this case?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the function(middleware) is located in app.js . when ever we pass the err obj to next function it take to the error handler function.

}
},
};
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"use strict";

const table = "Foos";
const table = "Users";

module.exports = {
up: (queryInterface, Sequelize) => {
Expand All @@ -11,8 +11,22 @@ module.exports = {
primaryKey: true,
type: Sequelize.INTEGER,
},
firstName: {
email: {
allowNull: false,
unique: true,
type: Sequelize.STRING,
},
password: {
type: Sequelize.STRING,
allowNull: false,
},
name: {
type: Sequelize.STRING,
allowNull: false,
},
archive: {
type: Sequelize.BOOLEAN,
allowNull: true,
},
createdAt: {
allowNull: false,
Expand Down
14 changes: 0 additions & 14 deletions models/foo.js

This file was deleted.

41 changes: 41 additions & 0 deletions models/user.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
const { isEmailValid } = require("../utils/");
("use strict");
module.exports = (sequelize, DataTypes) => {
const User = sequelize.define(
"User",
{
email: {
allowNull: false,
type: DataTypes.STRING,
unique: true,
validate: { isEmail: true },
},
password: {
type: DataTypes.STRING,
allowNull: false,
},
name: {
type: DataTypes.STRING,
allowNull: false,
},
archive: {
type: DataTypes.BOOLEAN,
},
createdAt: {
allowNull: false,
type: DataTypes.DATE,
},
updatedAt: {
allowNull: false,
type: DataTypes.DATE,
},
},
{}
);

User.beforeCreate(async (user, options) => {
if (!isEmailValid(user.email)) throw new Error();
if (!user.archive) user.archive = false;
});
return User;
};
Loading