1
1
/** @module Services/Users */
2
2
3
3
import { Domain } from '../domain' ;
4
+ import { v4 as uuidv4 } from 'uuid' ;
4
5
5
6
const {
6
7
Models : { Responses } ,
@@ -9,31 +10,86 @@ const {
9
10
/**
10
11
* Get user by ID.
11
12
* @function
12
- * @returns {import('../domain/models/responses.models.js').SimpleResponse } The response for the get by ID request.
13
+ * @param {db } db - DB client for the connection to the database.
14
+ * @param {string } id - The UUID for the user in the database.
15
+ * @returns {import('../domain/models/responses.models.js').UserResponse } The response for the get by ID request.
13
16
*/
14
- const getById = ( ) => Responses . simple ( 'This is a response from a GET request' ) ;
17
+ const getById = async ( db , id ) => {
18
+ try {
19
+ const query = 'SELECT id, first_name, last_name, email FROM users WHERE id=$1' ;
20
+ const result = await db . query ( query , [ id ] ) ;
21
+ if ( result . rows . length === 0 ) {
22
+ return Responses . simple ( `User with ${ id } is not in the database.` ) ;
23
+ } else {
24
+ return Responses . userResponse ( id , result . rows [ 0 ] . first_name , result . rows [ 0 ] . last_name , result . rows [ 0 ] . email ) ;
25
+ }
26
+ } catch ( error ) {
27
+ return error ;
28
+ }
29
+ }
15
30
16
31
/**
17
32
* Create a new user.
18
33
* @function
19
- * @returns {import('../domain/models/responses.models.js').SimpleResponse } The response for the create request.
34
+ * @param {db } db - DB client for the connection to the database.
35
+ * @param {string } firstName - The first name of the user.
36
+ * @param {string } lastName - The last name of the user.
37
+ * @param {string } email - The email from the user.
38
+ * @returns {import('../domain/models/responses.models.js').userResponse } The response for the create request.
20
39
*/
21
- const create = ( ) => Responses . simple ( 'This is a response from a POST request' ) ;
40
+ const create = ( db , firstName , lastName , email ) => {
41
+ const randomUuid = uuidv4 ( ) ;
42
+ const query = 'INSERT INTO users (id, first_name, last_name, email) VALUES ($1, $2, $3, $4)' ;
43
+ db . query ( query , [ randomUuid , firstName , lastName , email ] ) ;
44
+ return Responses . userResponse ( randomUuid , firstName , lastName , email ) ;
45
+ } ;
22
46
23
47
/**
24
48
* Update an existing user.
25
49
* @function
50
+ * @param {db } db - DB client for the connection to the database.
51
+ * @param {string } id - The id of the user.
52
+ * @param {Map } body - THe fields to be updated.
26
53
* @returns {import('../domain/models/responses.models.js').SimpleResponse } The response for the update request.
27
54
*/
28
- const update = ( ) =>
29
- Responses . simple ( 'This is a response from an UPDATE request' ) ;
55
+ const update = async ( db , id , body ) => {
56
+ try {
57
+ const updates = [ ] ;
58
+ const values = [ id ] ;
59
+ let index = 2 ;
60
+ for ( const value in body ) {
61
+ updates . push ( `${ value } = $${ index } ` ) ;
62
+ values . push ( body [ value ] ) ;
63
+ index ++ ;
64
+ }
65
+ const query = `UPDATE users SET ${ updates . join ( ',' ) } WHERE id = $1` ;
66
+ db . query ( query , values ) ;
67
+ return Responses . simple ( `User with ${ id } has been updated.` )
68
+ } catch ( error ) {
69
+ return error ;
70
+ }
71
+ }
30
72
31
73
/**
32
74
* Delete a user by ID.
33
75
* @function
34
76
* @returns {import('../domain/models/responses.models.js').SimpleResponse } The response for the delete request.
35
77
*/
36
- const deleteByID = ( ) =>
37
- Responses . simple ( 'This is a response from a DELETE request' ) ;
78
+ const deleteByID = async ( db , id ) => {
79
+ try {
80
+ const query = 'SELECT id FROM users WHERE id=$1' ;
81
+ const result = await db . query ( query , [ id ] ) ;
82
+ if ( result . rows . length === 0 ) {
83
+ return Responses . simple ( `User with ${ id } is not in the database.` ) ;
84
+ } else {
85
+ const query = 'DELETE FROM users WHERE id = $1' ;
86
+ db . query ( query , [ id ] ) ;
87
+ return Responses . simple ( `User with ${ id } has been deleted.` ) ;
88
+ }
89
+ } catch ( error ) {
90
+ return error ;
91
+ }
92
+ }
93
+
38
94
39
95
export const UserServices = { getById, create, update, deleteByID } ;
0 commit comments