-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathservice.ts
More file actions
94 lines (81 loc) · 2.35 KB
/
Copy pathservice.ts
File metadata and controls
94 lines (81 loc) · 2.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import * as Joi from 'joi';
import UserModel, { IUserModel } from './model';
import UserValidation from './validation';
import { IUserService } from './interface';
import { Types } from 'mongoose';
/**
* @export
* @implements {IUserModelService}
*/
const UserService: IUserService = {
/**
* @returns {Promise < IUserModel[] >}
* @memberof UserService
*/
async findAll(): Promise < IUserModel[] > {
try {
return await UserModel.find({});
} catch (error) {
throw new Error(error.message);
}
},
/**
* @param {string} id
* @returns {Promise < IUserModel >}
* @memberof UserService
*/
async findOne(id: string): Promise < IUserModel > {
try {
const validate: Joi.ValidationResult = UserValidation.getUser({
id
});
if (validate.error) {
throw new Error(validate.error.message);
}
return await UserModel.findOne({
_id: new Types.ObjectId(id)
});
} catch (error) {
throw new Error(error.message);
}
},
/**
* @param {IUserModel} user
* @returns {Promise < IUserModel >}
* @memberof UserService
*/
async insert(body: IUserModel): Promise < IUserModel > {
try {
const validate: Joi.ValidationResult = UserValidation.createUser(body);
if (validate.error) {
throw new Error(validate.error.message);
}
const user: IUserModel = await UserModel.create(body);
return user;
} catch (error) {
throw new Error(error.message);
}
},
/**
* @param {string} id
* @returns {Promise < IUserModel >}
* @memberof UserService
*/
async remove(id: string): Promise < IUserModel > {
try {
const validate: Joi.ValidationResult = UserValidation.removeUser({
id
});
if (validate.error) {
throw new Error(validate.error.message);
}
const user: IUserModel = await UserModel.findOneAndRemove({
_id: new Types.ObjectId(id)
});
return user;
} catch (error) {
throw new Error(error.message);
}
}
};
export default UserService;