diff --git a/src/app/http.service.ts b/src/app/http.service.ts new file mode 100644 index 0000000..93e1e15 --- /dev/null +++ b/src/app/http.service.ts @@ -0,0 +1,69 @@ +import { Injectable } from '@angular/core'; +import { User } from './shared/user'; +import { Company } from './shared/company'; +import { Email } from './shared/email'; +import { HttpClient, HttpErrorResponse } from '@angular/common/http'; +import { Observable } from 'rxjs/Observable'; +import { ErrorObservable } from 'rxjs/observable/ErrorObservable'; +import { catchError, retry } from 'rxjs/operators'; +import { HttpHeaders } from '@angular/common/http'; + +@Injectable() +export class HttpService { + + static httpOptions = { + headers: new HttpHeaders({ + 'Content-Type': 'application/json' + }) + }; + + constructor(public http: HttpClient) { } + + static apiURL = 'http://35.196.111.251:3000'; + static usersURL = '/users'; + static usersURL2 = '/username'; + static companiesURL = '/companies'; + static loginURL = '/login'; + static emailURL = '/emails'; + + // All services related to Users + getAllUsers() { + return this.http.get(HttpService.apiURL + HttpService.usersURL); + } + getUserById(userId: string) { + return this.http.get(HttpService.apiURL + HttpService.usersURL + '/' + userId); + } + getUserByUsername(username: string) { + return this.http.get(HttpService.apiURL + HttpService.usersURL + HttpService.usersURL2 + '/' + username); + } + createUser(user: User) { + return this.http.post(HttpService.apiURL + HttpService.usersURL, + JSON.stringify(user), HttpService.httpOptions); + } + updateUser(user, userId) { + return this.http.put(HttpService.apiURL + HttpService.usersURL + '/' + userId, + JSON.stringify(user), HttpService.httpOptions); + } + + // All services related to companies + getAllCompanies() { + return this.http.get(HttpService.apiURL + HttpService.companiesURL); + } + getCompanyById(companyId: string) { + return this.http.get(HttpService.apiURL + HttpService.companiesURL + '/' + companyId); + } + + // All services related to companies + getSession() { + return this.http.get(HttpService.apiURL + HttpService.loginURL); + } + login(username, password) { + return this.http.post(HttpService.apiURL + HttpService.loginURL, + JSON.stringify({ username: username, password: password }), HttpService.httpOptions); + } + + //All services related to email + read(idUsuario) { + return this.http.get(HttpService.apiURL + HttpService.emailURL + '/read/' + idUsuario); + } +} \ No newline at end of file diff --git a/src/app/shared/email.ts b/src/app/shared/email.ts new file mode 100644 index 0000000..e0d6e61 --- /dev/null +++ b/src/app/shared/email.ts @@ -0,0 +1,17 @@ +export class Email{ + sender: string; + receivers: [string]; + content: string; + state: string; + createdAt: string; + acknowledgment: [string]; + constructor(sender?: string, receivers?: [string], content?: string, state?: string, + createdAt?: string, acknowledgment?: [string]){ + this.receivers = receivers; + this.sender = sender; + this.content = content; + this.state = state; + this.createdAt = createdAt; + this.acknowledgment = acknowledgment; + } +} \ No newline at end of file