user.service.ts 1022 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. import { Injectable } from '@angular/core';
  2. import {User} from '../dtcservice/user';
  3. import { Observable, of } from 'rxjs';
  4. import { HttpClient, HttpHeaders } from '@angular/common/http';
  5. const httpOptions = {
  6. headers: new HttpHeaders({ 'Content-Type': 'application/json' })
  7. };
  8. @Injectable({
  9. providedIn: 'root'
  10. })
  11. export class UserService {
  12. constructor(private http: HttpClient) { }
  13. private apihost = 'http://localhost:8080';
  14. getUsers(): Observable<User[]> {
  15. return this.http.get<User[]>(this.apihost+'/user');
  16. }
  17. addUser(user:User):Observable<User>{
  18. return this.http.post<User>(this.apihost+"/user",user,httpOptions);
  19. }
  20. updateUser (user: User, id:number): Observable<any> {
  21. return this.http.put(`${this.apihost}/user/${id}`, user, httpOptions);
  22. }
  23. getUser(id: number): Observable<User> {
  24. const url = `${this.apihost}/user/${id}`;
  25. return this.http.get<User>(url);
  26. }
  27. deleteUser(id:number):Observable<any>{
  28. return this.http.delete(`${this.apihost}/user/${id}`);
  29. }
  30. }