forked from b00tc4mp/isdi-bootcamp-202405
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
create logic updates of email, password, user address, user phone and…
…e of create product in app/logic b00tc4mp#101
- Loading branch information
1 parent
a52d561
commit 192023e
Showing
8 changed files
with
169 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { validate, errors } from 'com' | ||
|
||
const { SystemError } = errors | ||
|
||
export default (name, type, minprice, maxprice, image) => { | ||
validate.string(name, 'name') | ||
validate.string(type, 'type') | ||
validate.number(minprice, 'minprice') | ||
validate.number(maxprice, 'maxprice') | ||
validate.url(image, 'image') | ||
return fetch(`${import.meta.env.VITE_API_URL}/products`, { | ||
method: 'POST', | ||
headers: { | ||
Authorization: `Bearer ${sessionStorage.token}`, | ||
'Content-Type': 'application/json' | ||
}, | ||
body: JSON.stringify({ name, type, minprice, maxprice, image }) | ||
}) | ||
.catch(error => { throw new SystemError(error.message) }) | ||
.then(response => { | ||
const { status } = response | ||
|
||
if (status === 201) return | ||
|
||
return response.json() | ||
.then(body => { | ||
const { error, message } = body | ||
|
||
const constructor = errors[error] | ||
|
||
throw new constructor(message) | ||
}) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { validate, errors } from '../../com/index.js' | ||
|
||
const { SystemError } = errors | ||
|
||
export default (email, password) => { | ||
validate.email(email) | ||
validate.password(password) | ||
|
||
return fetch(`${import.meta.env.VITE_API_URL}/users/email`, { | ||
method: 'PATCH', | ||
headers: { | ||
Authorization: `Bearer ${sessionStorage.token}`, | ||
'Content-Type': 'application/json' | ||
}, | ||
body: JSON.stringify({ email, password }) | ||
}) | ||
.catch(error => { throw new SystemError(error.message) }) | ||
.then(response => { | ||
const { status } = response | ||
|
||
if (status === 204) return | ||
|
||
return response.json() | ||
.then(body => { | ||
const { error, message } = body | ||
|
||
const constructor = errors[error] | ||
|
||
throw new constructor(message) | ||
}) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { validate, errors } from '../../com/index.js' | ||
|
||
const { SystemError } = errors | ||
|
||
export default (oldPassword, newPassword, newPasswordRepeat) => { | ||
validate.password(oldPassword, 'oldPassword') | ||
validate.password(newPassword, 'newPassword') | ||
validate.password(newPasswordRepeat, 'newPasswordRepeat') | ||
|
||
return fetch(`${import.meta.env.VITE_API_URL}/users/password`, { | ||
method: 'PATCH', | ||
headers: { | ||
Authorization: `Bearer ${sessionStorage.token}`, | ||
'Content-Type': 'application/json' | ||
}, | ||
body: JSON.stringify({ oldPassword, newPassword, newPasswordRepeat }) | ||
}) | ||
.catch(error => { throw new SystemError(error.message) }) | ||
.then(response => { | ||
const { status } = response | ||
|
||
if (status === 204) return | ||
|
||
return response.json() | ||
.then(body => { | ||
const { error, message } = body | ||
|
||
const constructor = errors[error] | ||
|
||
throw new constructor(message) | ||
}) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { validate, errors } from '../../com/index.js' | ||
|
||
const { SystemError } = errors | ||
|
||
export default (address) => { | ||
validate.address(address) | ||
|
||
return fetch(`${import.meta.env.VITE_API_URL}/users/address`, { | ||
method: 'PATCH', | ||
headers: { | ||
Authorization: `Bearer ${sessionStorage.token}`, | ||
'Content-Type': 'application/json' | ||
}, | ||
body: JSON.stringify({ address }) | ||
}) | ||
.catch(error => { throw new SystemError(error.message) }) | ||
.then(response => { | ||
const { status } = response | ||
|
||
if (status === 204) return | ||
|
||
return response.json() | ||
.then(body => { | ||
const { error, message } = body | ||
|
||
const constructor = errors[error] | ||
|
||
throw new constructor(message) | ||
}) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { validate, errors } from '../../com/index.js' | ||
|
||
const { SystemError } = errors | ||
|
||
export default (phone) => { | ||
validate.phone(phone) | ||
|
||
return fetch(`${import.meta.env.VITE_API_URL}/users/phone`, { | ||
method: 'PATCH', | ||
headers: { | ||
Authorization: `Bearer ${sessionStorage.token}`, | ||
'Content-Type': 'application/json' | ||
}, | ||
body: JSON.stringify({ phone }) | ||
}) | ||
.catch(error => { throw new SystemError(error.message) }) | ||
.then(response => { | ||
const { status } = response | ||
|
||
if (status === 204) return | ||
|
||
return response.json() | ||
.then(body => { | ||
const { error, message } = body | ||
|
||
const constructor = errors[error] | ||
|
||
throw new constructor(message) | ||
}) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters