diff --git a/src/app/components/fragments/answers-list/answers-list.component.ts b/src/app/components/fragments/answers-list/answers-list.component.ts index f551b2c259d8243e4afcf09344a101287b93f7bb..8a73bb9f5d7c3d690dde2a497d5e0d19c5626fb1 100644 --- a/src/app/components/fragments/answers-list/answers-list.component.ts +++ b/src/app/components/fragments/answers-list/answers-list.component.ts @@ -25,7 +25,7 @@ export class AnswersListComponent implements OnInit { } getAnswerTexts(contentId: string): void { - this.contentAnswerService.getTextAnswers(contentId) + this.contentAnswerService.getAnswers(contentId) .subscribe(textAnswers => { this.textAnswers = textAnswers; }); diff --git a/src/app/components/fragments/statistics/statistics.component.ts b/src/app/components/fragments/statistics/statistics.component.ts index 1e7deec03b75e2b414f069afe58a453d646015a1..8880c11d8e7864ba6dc3c98a7971762a8cfca9ca 100644 --- a/src/app/components/fragments/statistics/statistics.component.ts +++ b/src/app/components/fragments/statistics/statistics.component.ts @@ -53,10 +53,10 @@ export class StatisticsComponent implements OnInit { getAnswers(): void { for (const question of this.content) { - this.contentAnswerService.getTextAnswers(question.contentId).subscribe( answer => { + this.contentAnswerService.getAnswers(question.contentId).subscribe( answer => { [].push.apply(this.textAnswers, answer); }); - this.contentAnswerService.getChoiceAnswers(question.contentId).subscribe( answer => { + this.contentAnswerService.getAnswers(question.contentId).subscribe( answer => { [].push.apply(this.choiceAnswers, answer); }); } diff --git a/src/app/services/http/content-answer.service.ts b/src/app/services/http/content-answer.service.ts index 1d024e95a0fe08720df4fd8b8f99f37bd33fcc23..84de302f13466ced00065f59afed0e1e707eab13 100644 --- a/src/app/services/http/content-answer.service.ts +++ b/src/app/services/http/content-answer.service.ts @@ -5,52 +5,91 @@ import { Observable } from 'rxjs/Observable'; import { catchError, tap } from 'rxjs/operators'; import { BaseHttpService } from './base-http.service'; import { AnswerChoice } from '../../models/answer-choice'; - const httpOptions = { headers: new HttpHeaders({ 'Content-Type': 'application/json' }) }; @Injectable() export class ContentAnswerService extends BaseHttpService { - private textAnswerUrl = 'api/textAnswers'; - private choiceAnswerUrl = 'api/choiceAnswers'; + private apiUrl = { + base: 'https://arsnova-staging.mni.thm.de/api', + answer: '/answer', + text: '/text', + choice: '/choice', + find: '/find' + }; constructor(private http: HttpClient) { super(); } - getTextAnswers(contentId: string): Observable<AnswerText[]> { - const url = `${this.textAnswerUrl}/?contentId=${contentId}`; - return this.http.get<AnswerText[]>(url).pipe( - catchError(this.handleError('getTextAnswers', [])) - ); - } - - getChoiceAnswers(contentId: string): Observable<AnswerText[]> { - const url = `${this.choiceAnswerUrl}/?contentId=${contentId}`; - return this.http.get<AnswerText[]>(url).pipe( - catchError(this.handleError('getChoiceAnswers', [])) + getAnswers(contentId: string): Observable<AnswerText[]> { + const url = this.apiUrl.base + this.apiUrl.answer + this.apiUrl.find; + return this.http.post<AnswerText[]>(url, { + properties: { contentId: contentId }, + externalFilters: {} + }, httpOptions).pipe( + catchError(this.handleError('getAnswers', [])) ); } addAnswerText(answerText: AnswerText): Observable<AnswerText> { - return this.http.post<AnswerText>(this.textAnswerUrl, answerText, httpOptions).pipe( + const url = this.apiUrl.base + this.apiUrl.answer + this.apiUrl.text; + return this.http.post<AnswerText>(url, answerText, httpOptions).pipe( catchError(this.handleError<AnswerText>('addAnswerText')) ); } addAnswerChoice(answerChoice: AnswerChoice): Observable<AnswerChoice> { - // Dummy method copied from addAnswerText. - // Todo: Implement correct method with api - return this.http.post<AnswerChoice>(this.textAnswerUrl, answerChoice, httpOptions).pipe( - catchError(this.handleError<AnswerChoice>('addAnswerText')) + const url = this.apiUrl.base + this.apiUrl.answer + this.apiUrl.choice; + return this.http.post<AnswerChoice>(url, answerChoice, httpOptions).pipe( + catchError(this.handleError<AnswerChoice>('addAnswerChoice')) ); } getAnswerText(id: string): Observable<AnswerText> { - const url = `${this.textAnswerUrl}/${id}`; + const url = `${ this.apiUrl.base + this.apiUrl.answer + this.apiUrl.text}/${ id }`; return this.http.get<AnswerText>(url).pipe( - catchError(this.handleError<AnswerText>(`getAnswerText id=${id}`)) + catchError(this.handleError<AnswerText>(`getAnswerText id=${ id }`)) + ); + } + + getAnswerChoice(id: string): Observable<AnswerChoice> { + const url = `${ this.apiUrl.base + this.apiUrl.answer + this.apiUrl.choice }/${ id }`; + return this.http.get<AnswerChoice>(url).pipe( + catchError(this.handleError<AnswerChoice>(`getAnswerChoice id=${ id }`)) + ); + } + + updateAnswerText(updatedAnswerText: AnswerText): Observable<AnswerText> { + const connectionUrl = `${ this.apiUrl.base + this.apiUrl.answer + this.apiUrl.text}/${ updatedAnswerText.id }`; + return this.http.put(connectionUrl, updatedAnswerText , httpOptions).pipe( + tap(() => ''), + catchError(this.handleError<any>('updateAnswerText')) + ); + } + + updateAnswerChoice(updatedAnswerChoice: AnswerChoice): Observable<AnswerChoice> { + const connectionUrl = `${ this.apiUrl.base + this.apiUrl.answer + this.apiUrl.choice}/${ updatedAnswerChoice.id }`; + return this.http.put(connectionUrl, updatedAnswerChoice , httpOptions).pipe( + tap(() => ''), + catchError(this.handleError<any>('updateAnswerChoice')) + ); + } + + deleteAnswerText(id: string): Observable<AnswerText> { + const url = `${ this.apiUrl.base + this.apiUrl.answer }/${ id }`; + return this.http.delete<AnswerText>(url, httpOptions).pipe( + tap(() => ''), + catchError(this.handleError<AnswerText>('deleteAnswerText')) + ); + } + + deleteAnswerChoice(id: string): Observable<AnswerChoice> { + const url = `${ this.apiUrl.base + this.apiUrl.answer }/${ id }`; + return this.http.delete<AnswerChoice>(url, httpOptions).pipe( + tap(() => ''), + catchError(this.handleError<AnswerChoice>('deleteAnswerText')) ); } }