Skip to content

Commit d102ad1

Browse files
added schema
1 parent ebf9350 commit d102ad1

File tree

9 files changed

+83
-4
lines changed

9 files changed

+83
-4
lines changed

app.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ const cors = require('cors');
44
require('dotenv').config();
55

66
const contactsRouter = require('./routes/api/contacts');
7-
7+
const usersRouter = require('./routes/api/users');
88
const app = express();
99

1010
const formatsLogger = app.get('env') === 'development' ? 'dev' : 'short';
@@ -13,6 +13,7 @@ app.use(logger(formatsLogger));
1313
app.use(cors());
1414
app.use(express.json());
1515

16+
app.use('/api/users', usersRouter);
1617
app.use('/api/contacts', contactsRouter);
1718

1819
app.use((req, res) => {

controllers/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
const contacts = require('./contacts');
2-
2+
const users = require('./users');
33
module.exports = {
44
contacts,
5+
users,
56
};

controllers/users/index.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
const signup = require('./signup');
2+
3+
4+
module.exports = {
5+
signup
6+
}

controllers/users/signup.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
const signup = async (req, res) => {};
2+
3+
module.exports = signup;

models/contact.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,12 @@ const contactSchema = Schema(
99
},
1010
email: {
1111
type: String,
12+
required: [true, 'Email is required'],
13+
match: /[a-z0-9]+@[a-z]+\.[a-z]{2,3}/,
1214
},
1315
phone: {
1416
type: String,
17+
required: [true, 'Phone is required'],
1518
},
1619
favorite: {
1720
type: Boolean,
@@ -23,7 +26,9 @@ const contactSchema = Schema(
2326

2427
const joiSchema = Joi.object({
2528
name: Joi.string().min(3).max(30).required(),
26-
email: Joi.string().min(5).required().email(),
29+
email: Joi.string()
30+
.pattern(/[a-z0-9]+@[a-z]+\.[a-z]{2,3}/)
31+
.required(),
2732
phone: Joi.string().required(),
2833
favorite: Joi.bool(),
2934
});

models/user.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
const { Schema, model } = require('mongoose');
2+
const Joi = require('joi');
3+
4+
const userSchema = Schema({
5+
password: {
6+
type: String,
7+
required: [true, 'Password is required'],
8+
},
9+
email: {
10+
type: String,
11+
required: [true, 'Email is required'],
12+
match: /[a-z0-9]+@[a-z]+\.[a-z]{2,3}/,
13+
unique: true,
14+
},
15+
subscription: {
16+
type: String,
17+
enum: ['starter', 'pro', 'business'],
18+
default: 'starter',
19+
},
20+
token: {
21+
type: String,
22+
default: null,
23+
},
24+
});
25+
26+
const joiSchema = Joi.object({
27+
password: Joi.string().min(6).required(),
28+
email: Joi.string()
29+
.pattern(/[a-z0-9]+@[a-z]+\.[a-z]{2,3}/)
30+
.required(),
31+
subscription: Joi.string().valueOf('starter', 'pro', 'business'),
32+
});
33+
34+
const User = model('user', userSchema);
35+
36+
module.exports = {
37+
User,
38+
joiSchema,
39+
};

routes/api/users.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
const express = require('express');
2+
const router = express.Router();
3+
const { users: ctrl } = require('../../controllers');
4+
const { validation, ctrlWrapper } = require('../../middlewares');
5+
const { joiSchema } = require('../../models/user');
6+
7+
router.post('/signup', validation(joiSchema), ctrlWrapper(ctrl.signup));
8+
9+
module.exports = router;

utils/createError.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
const messages = {
2+
400: "Bad Request",
3+
401: "Unauthorized",
4+
403: "Forbidden",
5+
404: "Not found",
6+
409: "Conflict",
7+
}
8+
const createError = (status, message = messages[status]) => {
9+
const error = new Error(message);
10+
error.status = status;
11+
return error;
12+
}
13+
14+
module.exports = createError;

utils/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
const getErrorMessage = require('./getErrorMessage');
2-
2+
const createError = require('./createError');
33
module.exports = {
44
getErrorMessage,
5+
createError,
56
};

0 commit comments

Comments
 (0)