-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Migrate the API and Auth service from legacy to services
- Loading branch information
Showing
7 changed files
with
258 additions
and
7 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,183 @@ | ||
import { Injectable } from "@angular/core"; | ||
import { environment } from "../../environments/environment"; | ||
|
||
@Injectable({ providedIn: 'root' }) | ||
export class ApiService { | ||
constructor() {} | ||
|
||
baseUrl() { | ||
return environment.apiUrl; | ||
} | ||
|
||
async categories() { | ||
const response = await fetch(`${environment.apiUrl}/category/root`); | ||
|
||
if (response.status >= 400) { | ||
throw new Error(response.statusText); | ||
} | ||
|
||
const result = await response.json(); | ||
return result; | ||
} | ||
|
||
async collections() { | ||
const response = await fetch(`${environment.apiUrl}/collection`); | ||
|
||
if (response.status >= 400) { | ||
throw new Error(response.statusText); | ||
} | ||
|
||
const result = await response.json(); | ||
return result; | ||
} | ||
|
||
async projects() { | ||
const response = await fetch(`${environment.apiUrl}/project`); | ||
|
||
if (response.status >= 400) { | ||
throw new Error(response.statusText); | ||
} | ||
|
||
const result = await response.json(); | ||
return result; | ||
} | ||
|
||
async project(id: string) { | ||
const response = await fetch(`${environment.apiUrl}/project/${id}`); | ||
|
||
if (response.status >= 400) { | ||
throw new Error(response.statusText); | ||
} | ||
|
||
const result = await response.json(); | ||
return result; | ||
} | ||
|
||
async users() { | ||
const response = await fetch(`${environment.apiUrl}/user`); | ||
|
||
if (response.status >= 400) { | ||
throw new Error(response.statusText); | ||
} | ||
|
||
const result = await response.json(); | ||
return result; | ||
} | ||
|
||
// async deleteCategory() { | ||
// const response = await fetch(`${environment.apiUrl}/collection`); | ||
|
||
// if (response.status >= 400) { | ||
// throw new Error(response.statusText); | ||
// } | ||
|
||
// const result = await response.json(); | ||
// return result; | ||
// } | ||
|
||
async insertCategory(item: any) { | ||
const response = await fetch(`${environment.apiUrl}/category`, { | ||
method: 'POST', | ||
headers: { | ||
Accept: 'application/json', | ||
'Content-Type': 'application/json', | ||
}, | ||
body: JSON.stringify(item), | ||
}); | ||
|
||
if (response.status >= 400) { | ||
throw new Error(response.statusText); | ||
} | ||
|
||
const result = await response.json(); | ||
return result.item; | ||
} | ||
|
||
async updateCategory(id: string, item: any) { | ||
const response = await fetch(`${environment.apiUrl}/category/${id}`, { | ||
method: 'PUT', | ||
headers: { | ||
Accept: 'application/json', | ||
'Content-Type': 'application/json', | ||
}, | ||
body: JSON.stringify(item), | ||
}); | ||
|
||
if (response.status >= 400) { | ||
throw new Error(response.statusText); | ||
} | ||
|
||
const result = await response.json(); | ||
return result.item; | ||
} | ||
|
||
async deleteCategory(id: string) { | ||
const response = await fetch(`${environment.apiUrl}/category/${id}`, { | ||
method: 'DELETE', | ||
headers: { | ||
Accept: 'application/json', | ||
'Content-Type': 'application/json', | ||
}, | ||
}); | ||
|
||
if (response.status >= 400) { | ||
throw new Error(response.statusText); | ||
} | ||
|
||
const result = await response.json(); | ||
return result; | ||
} | ||
|
||
async insertUser(item: any) { | ||
const response = await fetch(`${environment.apiUrl}/user`, { | ||
method: 'POST', | ||
headers: { | ||
Accept: 'application/json', | ||
'Content-Type': 'application/json', | ||
}, | ||
body: JSON.stringify(item), | ||
}); | ||
|
||
if (response.status >= 400) { | ||
throw new Error(response.statusText); | ||
} | ||
|
||
const result = await response.json(); | ||
return result.item; | ||
} | ||
|
||
async updateUser(id: string, item: any) { | ||
const response = await fetch(`${environment.apiUrl}/user/${id}`, { | ||
method: 'PUT', | ||
headers: { | ||
Accept: 'application/json', | ||
'Content-Type': 'application/json', | ||
}, | ||
body: JSON.stringify(item), | ||
}); | ||
|
||
if (response.status >= 400) { | ||
throw new Error(response.statusText); | ||
} | ||
|
||
const result = await response.json(); | ||
return result.item; | ||
} | ||
|
||
async deleteUser(id: string) { | ||
const response = await fetch(`${environment.apiUrl}/user/${id}`, { | ||
method: 'DELETE', | ||
headers: { | ||
Accept: 'application/json', | ||
'Content-Type': 'application/json', | ||
}, | ||
}); | ||
|
||
if (response.status >= 400) { | ||
throw new Error(response.statusText); | ||
} | ||
|
||
const result = await response.json(); | ||
return result; | ||
} | ||
} |
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,67 @@ | ||
@@ -1,66 +0,0 @@ | ||
import { Injectable } from '@angular/core'; | ||
import { environment } from '../../environments/environment'; | ||
|
||
@Injectable({ providedIn: 'root' }) | ||
export class AuthService { | ||
constructor() {} | ||
|
||
baseUrl() { | ||
return environment.apiUrl; | ||
} | ||
|
||
async challenge() { | ||
const response = await fetch(`${environment.apiUrl}/authenticate`); | ||
|
||
if (response.status >= 400) { | ||
throw new Error('Unable to receive authentication challenge.'); | ||
} | ||
|
||
const result = await response.json(); | ||
return result; | ||
} | ||
|
||
async verify(challenge: string) { | ||
const response = await fetch(`${environment.apiUrl}/authenticate`, { | ||
method: 'POST', | ||
headers: { | ||
Accept: 'application/json', | ||
'Content-Type': 'application/json', | ||
}, | ||
body: JSON.stringify(challenge), | ||
}); | ||
|
||
if (response.status >= 400) { | ||
throw new Error('Unable to verify authentication challenge.'); | ||
} | ||
|
||
const result = await response.json(); | ||
return result; | ||
} | ||
|
||
async authenticated() { | ||
const response = await fetch( | ||
`${environment.apiUrl}/authenticate/protected` | ||
); | ||
|
||
if (response.status == 200) { | ||
const result = await response.json(); | ||
|
||
return result; | ||
} else { | ||
return null; | ||
} | ||
} | ||
|
||
async logout() { | ||
const response = await fetch(`${environment.apiUrl}/authenticate/logout`); | ||
|
||
// if (response.status >= 400) { | ||
// throw new Error('Unable to receive authentication challenge.'); | ||
// } | ||
|
||
const result = await response.json(); | ||
console.log('LOGOUT RESULT:', result); | ||
return result; | ||
} | ||
} |
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