@@ -3,65 +3,13 @@ import password from "models/password.js";
33import { ValidationError , NotFoundError } from "infra/errors.js" ;
44
55async function create ( userInputValues ) {
6- await validadeUniqueEmail ( userInputValues . email ) ;
76 await validateUniqueUsername ( userInputValues . username ) ;
7+ await validadeUniqueEmail ( userInputValues . email ) ;
88 await hashPasswordInObject ( userInputValues ) ;
99
1010 const newUser = await runInsertQuery ( userInputValues ) ;
1111 return newUser ;
1212
13- async function validadeUniqueEmail ( email ) {
14- const result = await database . query ( {
15- text : `
16- SELECT
17- email
18- FROM
19- users
20- WHERE
21- LOWER(email) = LOWER($1)
22- LIMIT
23- 1
24- ;` ,
25- values : [ email ] ,
26- } ) ;
27-
28- if ( result . rowCount > 0 ) {
29- throw new ValidationError ( {
30- message : "The email address provided is already in use." ,
31- action : "Please use a different email to create your account." ,
32- } ) ;
33- }
34- }
35-
36- async function validateUniqueUsername ( username ) {
37- const result = await database . query ( {
38- text : `
39- SELECT
40- username
41- FROM
42- users
43- WHERE
44- LOWER(username) = LOWER($1)
45- LIMIT
46- 1
47- ;` ,
48- values : [ username ] ,
49- } ) ;
50-
51- if ( result . rowCount > 0 ) {
52- throw new ValidationError ( {
53- message : "The username provided is already in use." ,
54- action : "Please use a different username to create your account." ,
55- } ) ;
56- }
57- }
58-
59- async function hashPasswordInObject ( userInputValues ) {
60- const hashedPassword = await password . hash ( userInputValues . password ) ;
61-
62- userInputValues . password = hashedPassword ;
63- }
64-
6513 async function runInsertQuery ( userInputValues ) {
6614 const result = await database . query ( {
6715 text : `
@@ -114,5 +62,106 @@ async function findOneByUsername(username) {
11462 }
11563}
11664
117- const userModel = { create, findOneByUsername } ;
118- export default userModel ;
65+ async function update ( username , userInputValues ) {
66+ const user = await findOneByUsername ( username ) ;
67+
68+ if ( "username" in userInputValues ) {
69+ await validateUniqueUsername ( userInputValues . username ) ;
70+ }
71+
72+ if ( "email" in userInputValues ) {
73+ await validadeUniqueEmail ( userInputValues . email ) ;
74+ }
75+
76+ if ( "password" in userInputValues ) {
77+ await hashPasswordInObject ( userInputValues ) ;
78+ }
79+
80+ const userWithNewValues = { ...user , ...userInputValues } ;
81+
82+ const updatedUser = await runUpdateQuery ( userWithNewValues ) ;
83+
84+ return updatedUser ;
85+
86+ async function runUpdateQuery ( userWithNewValues ) {
87+ const result = await database . query ( {
88+ text : `
89+ UPDATE
90+ users
91+ SET
92+ username = $2,
93+ email = $3,
94+ password = $4,
95+ updated_at = timezone('utc', now())
96+ WHERE
97+ id = $1
98+ RETURNING
99+ *
100+ ` ,
101+ values : [
102+ userWithNewValues . id ,
103+ userWithNewValues . username ,
104+ userWithNewValues . email ,
105+ userWithNewValues . password ,
106+ ] ,
107+ } ) ;
108+
109+ return result . rows [ 0 ] ;
110+ }
111+ }
112+
113+ async function validateUniqueUsername ( username ) {
114+ const result = await database . query ( {
115+ text : `
116+ SELECT
117+ username
118+ FROM
119+ users
120+ WHERE
121+ LOWER(username) = LOWER($1)
122+ LIMIT
123+ 1
124+ ;` ,
125+ values : [ username ] ,
126+ } ) ;
127+
128+ if ( result . rowCount > 0 ) {
129+ throw new ValidationError ( {
130+ message : "The username provided is already in use." ,
131+ action : "Please use a different username." ,
132+ } ) ;
133+ }
134+ }
135+
136+ async function validadeUniqueEmail ( email ) {
137+ const result = await database . query ( {
138+ text : `
139+ SELECT
140+ email
141+ FROM
142+ users
143+ WHERE
144+ LOWER(email) = LOWER($1)
145+ LIMIT
146+ 1
147+ ;` ,
148+ values : [ email ] ,
149+ } ) ;
150+
151+ if ( result . rowCount > 0 ) {
152+ throw new ValidationError ( {
153+ message : "The email address provided is already in use." ,
154+ action : "Please use a different email." ,
155+ } ) ;
156+ }
157+ }
158+
159+ async function hashPasswordInObject ( userInputValues ) {
160+ const hashedPassword = await password . hash ( userInputValues . password ) ;
161+
162+ userInputValues . password = hashedPassword ;
163+ }
164+
165+ const user = { create, findOneByUsername, update } ;
166+
167+ export default user ;
0 commit comments