-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added API to allow user to cancel match request
- Loading branch information
Showing
4 changed files
with
59 additions
and
28 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 |
---|---|---|
@@ -1,37 +1,48 @@ | ||
import { CategoryService } from '../services/category.service'; | ||
import { HttpClient, HttpHeaders } from '@angular/common/http'; | ||
import { Injectable} from '@angular/core'; | ||
import {MatchRequest, MatchResponse} from '../app/models/match.model'; | ||
import { UserData } from '../types/userdata'; | ||
import { baseUrlProduction } from '../../constants'; | ||
import { HttpClient, HttpHeaders } from "@angular/common/http" | ||
import { Injectable } from "@angular/core" | ||
import { lastValueFrom, Observable } from "rxjs" | ||
|
||
import { lastValueFrom, Observable } from 'rxjs'; | ||
import { baseUrlProduction } from "../../constants" | ||
import { MatchRequest, MatchRequestCancel, MatchRequestCancelResponse, MatchResponse } from "../app/models/match.model" | ||
import { CategoryService } from "../services/category.service" | ||
import { UserData } from "../types/userdata" | ||
|
||
@Injectable({ | ||
providedIn: 'root' | ||
providedIn: "root" | ||
}) | ||
|
||
export class MatchService { | ||
private apiUrl = this.isProduction() ? `${baseUrlProduction}/match` : 'http://localhost:3002/match'; | ||
private apiUrl = this.isProduction() ? `${baseUrlProduction}/match` : "http://localhost:3002/match" | ||
|
||
constructor(private http: HttpClient) {} | ||
|
||
isProduction(): boolean { | ||
return window.location.hostname !== "localhost" | ||
} | ||
|
||
constructor(private http: HttpClient) {} | ||
// send user data and difficulty to rabbitMQ (match request) | ||
// this is a post request, which returns a response | ||
async sendMatchRequest(userData: UserData, queueName: string): Promise<MatchResponse> { | ||
const matchRequest: MatchRequest = { | ||
userData, | ||
key: queueName | ||
} | ||
// return lastValueFrom(this.http.post<MatchResponse>(`${this.apiUrl}`, matchRequest)); | ||
const response = await this.http.post<MatchResponse>(`${this.apiUrl}`, matchRequest).toPromise() | ||
if (!response) { | ||
throw new Error("Match response is undefined") | ||
} | ||
return response | ||
} | ||
|
||
isProduction (): boolean { | ||
return window.location.hostname !== 'localhost'; | ||
async cancelMatchRequest(user_id: string) { | ||
const cancelMatchRequest: MatchRequestCancel = { | ||
user_id | ||
} | ||
|
||
// send user data and difficulty to rabbitMQ (match request) | ||
// this is a post request, which returns a response | ||
async sendMatchRequest(userData: UserData, queueName: string): Promise<MatchResponse> { | ||
const matchRequest: MatchRequest = { | ||
userData, | ||
key: queueName | ||
} | ||
// return lastValueFrom(this.http.post<MatchResponse>(`${this.apiUrl}`, matchRequest)); | ||
const response = await this.http.post<MatchResponse>(`${this.apiUrl}`, matchRequest).toPromise(); | ||
if (!response) { | ||
throw new Error('Match response is undefined'); | ||
} | ||
return response; | ||
const response = await this.http.post<MatchRequestCancelResponse>(`${this.apiUrl}/cancel`, cancelMatchRequest).toPromise() | ||
if (!response) { | ||
throw new Error("Given user_id is invalid, request cancelled unsuccessfully") | ||
} | ||
return response; | ||
} | ||
} |