Skip to content

Commit

Permalink
chore: add backend prettier for format (#485)
Browse files Browse the repository at this point in the history
  • Loading branch information
Dun-sin authored Oct 19, 2023
1 parent ab7070d commit b3bd24d
Show file tree
Hide file tree
Showing 25 changed files with 3,997 additions and 2,135 deletions.
11 changes: 1 addition & 10 deletions client/.eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,6 @@
"no-unreachable": "warn",
"no-unreachable-loop": "warn",
"block-scoped-var": "error",
"capitalized-comments": [
"error",
"always",
{
"ignoreInlineComments": true,
"ignoreConsecutiveComments": true
}
],
"class-methods-use-this": [
"error",
{
Expand Down Expand Up @@ -105,7 +97,6 @@
"react/jsx-uses-react": "error",
"react/react-in-jsx-scope": "off",
"react/jsx-boolean-value": "error",
"react/jsx-fragments": ["warn", "syntax"],
"spaced-comment": "error"
"react/jsx-fragments": ["warn", "syntax"]
}
}
3 changes: 1 addition & 2 deletions client/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
/build

.env
.eslintcache

# misc
.DS_Store
Expand All @@ -23,5 +24,3 @@
npm-debug.log*
yarn-debug.log*
yarn-error.log*

.eslintcache
6 changes: 3 additions & 3 deletions client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
"vite-plugin-eslint": "^1.8.1"
},
"lint-staged": {
"*.{js,jsx,ts,tsx}": "eslint --cache --fix",
"*.--write": "prettier --write"
"*.{js,jsx,ts,tsx}": "eslint --fix",
"*.--write": "prettier --write ."
}
}
}
84 changes: 84 additions & 0 deletions server/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
{
"parserOptions": {
"ecmaVersion": 2020
},
"env": {
"node": true,
"es6": true
},
"rules": {
"array-bracket-newline": "error",
"camelcase": "error",
"no-constant-binary-expression": "error",
"no-duplicate-imports": "error",
"no-self-compare": "error",
"no-unreachable": "warn",
"no-unreachable-loop": "warn",
"block-scoped-var": "error",
"class-methods-use-this": [
"error",
{
"enforceForClassFields": false
}
],
"curly": "error",
"default-case": "error",
"default-case-last": "error",
"default-param-last": ["error"],
"dot-notation": [
"error",
{
"allowKeywords": true
}
],
"eqeqeq": ["warn", "smart"],
"func-style": [
"error",
"declaration",
{
"allowArrowFunctions": true
}
],
"max-classes-per-file": "error",
"max-depth": "warn",
"max-len": [
"warn",
{
"ignoreStrings": true,
"ignoreTemplateLiterals": true,
"code": 100
}
],
"max-statements-per-line": "warn",
"no-confusing-arrow": "error",
"no-empty-function": "error",
"no-eq-null": "error",
"no-extra-bind": "error",
"no-implicit-coercion": "error",
"no-invalid-this": "error",
"no-lonely-if": "warn",
"no-loop-func": "error",
"no-unused-expressions": [
"error",
{
"allowShortCircuit": true,
"allowTernary": true,
"enforceForJSX": true
}
],
"no-use-before-define": [
"error",
{
"allowNamedExports": true
}
],
"no-useless-concat": "error",
"no-useless-constructor": "warn",
"no-useless-return": "warn",
"no-var": "error",
"no-template-curly-in-string": "warn",
"one-var-declaration-per-line": ["error", "initializations"],
"prefer-arrow-callback": "warn",
"prefer-const": "warn"
}
}
1 change: 1 addition & 0 deletions server/.husky/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
_
4 changes: 4 additions & 0 deletions server/.husky/pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/sh
. "$(dirname "$0")/_/husky.sh"

cd server && npx lint-staged
8 changes: 8 additions & 0 deletions server/.prettierrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"semi": true,
"singleQuote": true,
"trailingComma": "es5",
"bracketSameLine": false,
"endOfLine": "lf",
"editorconfig": true
}
187 changes: 97 additions & 90 deletions server/controllers/userController.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,120 +3,127 @@ const validator = require('validator').default;

const User = require('../models/UserSchema');

const { OK, NOT_FOUND, NOT_ACCEPTABLE, INTERNAL_SERVER_ERROR } = require('../httpStatusCodes.js');
const {
OK,
NOT_FOUND,
NOT_ACCEPTABLE,
INTERNAL_SERVER_ERROR,
} = require('../httpStatusCodes.js');

// Defining separate email validation middleware
const emailValidator = (req, res, next) => {
const { email } = req.body;

if (typeof email !== 'string' || !validator.isEmail(email)) {
return res.status(NOT_ACCEPTABLE).json({
message: 'Email is invalid',
});
} else {
next();
}
const { email } = req.body;

if (typeof email !== 'string' || !validator.isEmail(email)) {
return res.status(NOT_ACCEPTABLE).json({
message: 'Email is invalid',
});
} else {
next();
}
};

const loginUser = async (req, res) => {
const { email } = req.body;
const { email } = req.body;

try {
let findUser = await User.findOne({ email });
try {
const findUser = await User.findOne({ email });

if (!findUser) {
const newUser = await User.create({ email });
if (!findUser) {
const newUser = await User.create({ email });

res.status(OK).json({
id: newUser._id,
});
res.status(OK).json({
id: newUser._id,
});

return;
}
return;
}

res.status(OK).json({
id: findUser._id,
});
} catch (err) {
res.status(INTERNAL_SERVER_ERROR).json({
message: 'An error occured while logging in',
});
}
res.status(OK).json({
id: findUser._id,
});
} catch (err) {
res.status(INTERNAL_SERVER_ERROR).json({
message: 'An error occured while logging in',
});
}
};

const getProfile = async (req, res) => {
try {
const { email } = req.params;

// Find the user by email
const user = await User.findOne({ email });

if (!user) {
return res.status(NOT_FOUND).json({ error: 'User not found' });
}

// Send the user profile data as JSON response
res.status(OK).json(user);
} catch (error) {
console.error('Error fetching user profile:', error);
res.status(INTERNAL_SERVER_ERROR).json({ error: 'Internal server error' });
}
try {
const { email } = req.params;

// Find the user by email
const user = await User.findOne({ email });

if (!user) {
return res.status(NOT_FOUND).json({ error: 'User not found' });
}

// Send the user profile data as JSON response
res.status(OK).json(user);
} catch (error) {
console.error('Error fetching user profile:', error);
res.status(INTERNAL_SERVER_ERROR).json({ error: 'Internal server error' });
}
};

const updateProfile = async (req, res) => {
const { username, aboutMe, gender, age, email, settings } = req.body;

try {
// Find the user by email
const user = await User.findOne({ email });

if (!user) {
return res.status(NOT_FOUND).json({ error: 'User not found' });
}

// Update user's profile with provided fields or the User fields or defaults
user.username = username || user.username || 'Anonymous';
user.aboutMe = aboutMe || user.aboutMe || null;
user.gender = gender || user.gender || 'Unknown';
user.age = age || user.age || null;
user.settings = settings || user.settings;

// Save the updated user profile
await user.save();

return res.status(OK).json({ message: 'Profile updated successfully' });
} catch (error) {
console.error(error);
return res.status(INTERNAL_SERVER_ERROR).json({ error: 'Internal server error' });
}
const { username, aboutMe, gender, age, email, settings } = req.body;

try {
// Find the user by email
const user = await User.findOne({ email });

if (!user) {
return res.status(NOT_FOUND).json({ error: 'User not found' });
}

// Update user's profile with provided fields or the User fields or defaults
user.username = username || user.username || 'Anonymous';
user.aboutMe = aboutMe || user.aboutMe || null;
user.gender = gender || user.gender || 'Unknown';
user.age = age || user.age || null;
user.settings = settings || user.settings;

// Save the updated user profile
await user.save();

return res.status(OK).json({ message: 'Profile updated successfully' });
} catch (error) {
console.error(error);
return res
.status(INTERNAL_SERVER_ERROR)
.json({ error: 'Internal server error' });
}
};

const deleteUser = async (req, res) => {
const { email } = req.body;

try {
// Find the user by email
const user = await User.findOne({ email });

if (!user) {
return res.status(NOT_FOUND).json({ error: 'User not found' });
}

// Delete the user
await user.deleteOne();

return res.status(OK).json({ message: 'User deleted successfully' });
} catch (error) {
console.error(error);
return res.status(INTERNAL_SERVER_ERROR).json({ error: 'Internal server error' });
}
const { email } = req.body;

try {
// Find the user by email
const user = await User.findOne({ email });

if (!user) {
return res.status(NOT_FOUND).json({ error: 'User not found' });
}

// Delete the user
await user.deleteOne();

return res.status(OK).json({ message: 'User deleted successfully' });
} catch (error) {
console.error(error);
return res
.status(INTERNAL_SERVER_ERROR)
.json({ error: 'Internal server error' });
}
};

UserRouter.route('/login').post(emailValidator, loginUser);
UserRouter.route('/profile').post(emailValidator, updateProfile);
UserRouter.route('/profile/:email').get(getProfile);
UserRouter.route('/deleteUser').delete(emailValidator, deleteUser); //Email validation applied to the required request handlers
UserRouter.route('/deleteUser').delete(emailValidator, deleteUser); //Email validation applied to the required request handlers

module.exports = UserRouter;


2 changes: 1 addition & 1 deletion server/httpStatusCodes.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,5 +60,5 @@ module.exports = {
INSUFFICIENT_STORAGE: 507,
LOOP_DETECTED: 508,
NOT_EXTENDED: 510,
NETWORK_AUTHENTICATION_REQUIRED: 511
NETWORK_AUTHENTICATION_REQUIRED: 511,
};
Loading

1 comment on commit b3bd24d

@vercel
Copy link

@vercel vercel bot commented on b3bd24d Oct 19, 2023

Choose a reason for hiding this comment

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

Please sign in to comment.