post.service.ts 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. import { Injectable } from '@angular/core';
  2. import { HttpClient, HttpResponse } from '@angular/common/http';
  3. import { Observable } from 'rxjs';
  4. import * as moment from 'moment';
  5. import { DATE_FORMAT } from 'app/shared/constants/input.constants';
  6. import { map } from 'rxjs/operators';
  7. import { SERVER_API_URL } from 'app/app.constants';
  8. import { createRequestOption } from 'app/shared';
  9. import { IPost } from 'app/shared/model/post.model';
  10. type EntityResponseType = HttpResponse<IPost>;
  11. type EntityArrayResponseType = HttpResponse<IPost[]>;
  12. @Injectable({ providedIn: 'root' })
  13. export class PostService {
  14. public resourceUrl = SERVER_API_URL + 'api/posts';
  15. constructor(private http: HttpClient) {}
  16. create(post: IPost): Observable<EntityResponseType> {
  17. const copy = this.convertDateFromClient(post);
  18. return this.http
  19. .post<IPost>(this.resourceUrl, copy, { observe: 'response' })
  20. .pipe(map((res: EntityResponseType) => this.convertDateFromServer(res)));
  21. }
  22. update(post: IPost): Observable<EntityResponseType> {
  23. const copy = this.convertDateFromClient(post);
  24. return this.http
  25. .put<IPost>(this.resourceUrl, copy, { observe: 'response' })
  26. .pipe(map((res: EntityResponseType) => this.convertDateFromServer(res)));
  27. }
  28. find(id: number): Observable<EntityResponseType> {
  29. return this.http
  30. .get<IPost>(`${this.resourceUrl}/${id}`, { observe: 'response' })
  31. .pipe(map((res: EntityResponseType) => this.convertDateFromServer(res)));
  32. }
  33. query(req?: any): Observable<EntityArrayResponseType> {
  34. const options = createRequestOption(req);
  35. return this.http
  36. .get<IPost[]>(this.resourceUrl, { params: options, observe: 'response' })
  37. .pipe(map((res: EntityArrayResponseType) => this.convertDateArrayFromServer(res)));
  38. }
  39. delete(id: number): Observable<HttpResponse<any>> {
  40. return this.http.delete<any>(`${this.resourceUrl}/${id}`, { observe: 'response' });
  41. }
  42. protected convertDateFromClient(post: IPost): IPost {
  43. const copy: IPost = Object.assign({}, post, {
  44. timestamp: post.timestamp != null && post.timestamp.isValid() ? post.timestamp.format(DATE_FORMAT) : null
  45. });
  46. return copy;
  47. }
  48. protected convertDateFromServer(res: EntityResponseType): EntityResponseType {
  49. if (res.body) {
  50. res.body.timestamp = res.body.timestamp != null ? moment(res.body.timestamp) : null;
  51. }
  52. return res;
  53. }
  54. protected convertDateArrayFromServer(res: EntityArrayResponseType): EntityArrayResponseType {
  55. if (res.body) {
  56. res.body.forEach((post: IPost) => {
  57. post.timestamp = post.timestamp != null ? moment(post.timestamp) : null;
  58. });
  59. }
  60. return res;
  61. }
  62. }