Skip to content

Commit

Permalink
Fixes #724: Push search data to server using api/push.json
Browse files Browse the repository at this point in the history
  • Loading branch information
simsausaurabh committed Jun 19, 2018
1 parent 2559bbd commit cc4f8c8
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/app/effects/api-search.effects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
skip
} from 'rxjs/operators';

import { SearchService, SearchServiceConfig } from '../services';
import { SearchService, SearchServiceConfig, PushService } from '../services';
import * as fromRoot from '../reducers';
import * as apiAction from '../actions/api';
import * as queryAction from '../actions/query';
Expand Down Expand Up @@ -118,6 +118,7 @@ export class ApiSearchEffects {
),
withLatestFrom(this.store$),
map(([action, state]) => {
this.pushService.postData(action['payload']);
return {
doRelocate: state.query.relocateAfter,
relocateTo: state.query.routerString
Expand Down Expand Up @@ -227,6 +228,7 @@ export class ApiSearchEffects {
private store$: Store<fromRoot.State>,
private actions$: Actions,
private apiSearchService: SearchService,
private pushService: PushService,
private location: Location,
private titleService: Title
) { }
Expand Down
1 change: 1 addition & 0 deletions src/app/models/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ export * from './time-bound';
export * from './media-wall-custom';
export * from './media-wall-filters';
export * from './media-wall-direct-url';
export * from './push';
6 changes: 6 additions & 0 deletions src/app/models/push.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export interface PushApiResponse {
status: string;
records: number;
mps: number;
message: string;
}
1 change: 1 addition & 0 deletions src/app/services/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ export * from './search.service';
export * from './suggest.service';
export * from './user.service';
export * from './speech.service';
export * from './push.service';
21 changes: 21 additions & 0 deletions src/app/services/push.service.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { TestBed, inject } from '@angular/core/testing';
import { HttpClientModule, HttpClient } from '@angular/common/http';
import { PushService } from './push.service';

describe('PushService', () => {
beforeEach(() => {
TestBed.configureTestingModule({
imports: [
HttpClientModule
],
providers: [
PushService,
HttpClient
]
});
});

it('should be created', inject([PushService], (service: PushService) => {
expect(service).toBeTruthy();
}));
});
48 changes: 48 additions & 0 deletions src/app/services/push.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { Injectable } from '@angular/core';
import {
HttpClient,
HttpHeaders,
HttpParams
} from '@angular/common/http';
import { Observable } from 'rxjs';
import { ApiResponse, PushApiResponse } from '../models';
@Injectable({
providedIn: 'root'
})
/*
* The PushService is called on SEARCH_COMPLETE_SUCCESS
* to push the result data back to server in the same
* search result object format obtained from server.
**/
export class PushService {

constructor( private http: HttpClient ) { }

// Returns an Observable of PushApiResponse.
public postData(data: ApiResponse): Observable<PushApiResponse> {

// End point to make a Post to.
const httpUrl = 'https://api.loklak.org/api/push.json';

const headers = new HttpHeaders({
'Content-Type': 'application/x-www-form-urlencoded',
'Accept': 'application/json',
'cache-control': 'no-cache'
});
// Extracting search_metadata and statuses
// from the data obtained from search result.
const {search_metadata, statuses} = data;

// Converting the object to JSON string.
const dataToSend = JSON.stringify({search_metadata: search_metadata, statuses});

// Setting the data to send in HttpParams()
// with key as 'data'
const body = new HttpParams().set('data', dataToSend);

// Making a Post request to api/push.json endpoint
// of server with required header and data body.
// Response Object is converted to PushApiResponse type.
return this.http.post<PushApiResponse>(httpUrl, body, {headers: headers});
}
}

0 comments on commit cc4f8c8

Please sign in to comment.