Skip to content

Commit

Permalink
Merge pull request #10 from aloatias/development
Browse files Browse the repository at this point in the history
Release v0.0.1.0
  • Loading branch information
aloatias authored May 8, 2020
2 parents 9d51dd3 + b0ceed0 commit 1cbabdf
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 13 deletions.
2 changes: 1 addition & 1 deletion src/app/Shared/BaseUrl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ export class Urls {
public static TASK_GETALL = environment.url + Global.TASK_ENDPOINT + "GetAll";
public static TASK_ADD = environment.url + Global.TASK_ENDPOINT + "Add";
public static TASK_UPDATESTATUS = environment.url + Global.TASK_ENDPOINT + "UpdateStatus";
public static TASK_DELETE = environment.url + Global.TASK_ENDPOINT + "Delete?taskId=";
public static TASK_DELETE = environment.url + Global.TASK_ENDPOINT + "Delete";
}
55 changes: 43 additions & 12 deletions src/app/task.service.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import { Injectable } from '@angular/core';
import { Observable } from 'rxjs';
import { Observable, throwError } from 'rxjs';
import { Task } from './Dtos/Task';
import { HttpClient, HttpHeaders, HttpParams, HttpRequest } from '@angular/common/http';
import { HttpClient, HttpHeaders, HttpParams, HttpRequest, HttpErrorResponse } from '@angular/common/http';
import { Urls } from './Shared/BaseUrl';
import { retry, catchError } from 'rxjs/operators';

const httpOptions = {
headers: new HttpHeaders({
"Content-Type": "application/json"
})
}),
params: new HttpParams()
};

@Injectable({
Expand All @@ -18,21 +20,50 @@ export class TaskService {
constructor(
private _http: HttpClient) {
}

AddTask(taskDescription: string) : Observable<Task> {
return this._http.post<Task>(Urls.TASK_ADD, { "description": taskDescription }, httpOptions);

AddTask(taskDescription: string): Observable<Task> {
return this._http
.post<Task>(Urls.TASK_ADD, { "description": taskDescription }, httpOptions)
.pipe(catchError(this.handleError));
}

DeleteTask(taskId: string) {
let url = Urls.TASK_DELETE + taskId;
return this._http.delete(url, httpOptions);
let url = Urls.TASK_DELETE;
httpOptions.params = httpOptions.params.set("taskId", taskId);

return this._http
.delete(url, httpOptions)
.pipe(catchError(this.handleError));
}

GetAllTasks() : Observable<Task[]> {
return this._http.get<Task[]>(Urls.TASK_GETALL);
GetAllTasks(): Observable<Task[]> {
return this._http
.get<Task[]>(Urls.TASK_GETALL)
.pipe(catchError(this.handleError));
}

UpdateTaskStatus(taskId: string): Observable<Task> {
return this._http.put<Task>(Urls.TASK_UPDATESTATUS, { "id": taskId }, httpOptions);
return this._http
.put<Task>(Urls.TASK_UPDATESTATUS, { "id": taskId }, httpOptions)
.pipe(catchError(this.handleError));
}

private handleError(error: HttpErrorResponse) {
let errorMessage = '';
if (error.error instanceof ErrorEvent) {
// client-side error
errorMessage = `Error: ${error.error.message}`;
} else {
// server-side error
if (error.status !== 500) {
errorMessage = error.error;
} else {
errorMessage = "Un error occured. Please try again later";
}
}

alert(errorMessage);

return throwError(errorMessage);
}
}

0 comments on commit 1cbabdf

Please sign in to comment.