Advertisement
Guest User

Untitled

a guest
Feb 21st, 2019
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.75 KB | None | 0 0
  1. public Usuario insert(Usuario usuario) {
  2. if (usuarioService.findByEmail(usuario.getEmail()) == null) {
  3. usuario = getRepository().save(usuario);
  4. return usuario;
  5. }else {
  6. throw new DataIntegrityException("Email já está em uso");
  7. }
  8. }
  9.  
  10. @Injectable
  11. export class UsuarioService{
  12.  
  13. url: string = `${API_CONFIG.baseUrl}/usuarios`;
  14.  
  15. usuariosChanged = new EventEmitter<Observable<Usuario[]>>();
  16.  
  17. constructor(private http: HttpClient){
  18. }
  19.  
  20. findAll(): Observable<Usuario[]>{
  21. return this.http.get<Usuario[]>(${this.url});
  22. }
  23.  
  24. findById(id: number): Observable<Usuario>{
  25. return this.http.get<Usuario>(`${this.url}/id/${id}`);
  26. }
  27.  
  28. post(usuario: Usuario): Observable<Usuario>{
  29. return this.http.post<Usuario>(this.url, usuario)
  30. .pipe(
  31. tap(data => this.usuariosChanged.emit(this.findAll())),
  32. catchError(this.handleError))
  33. }
  34.  
  35. private handleError(error: any) {
  36. let erro = error.message || 'Server error';
  37. console.error('Ocorreu um erro', error);
  38. return Observable.throw(erro);
  39. }
  40. }
  41.  
  42. onSave(){
  43. this.usuarioService.post(this.form.value)
  44. .subscribe(data => {
  45. this.notificationService.notify("Usuário salvo com sucesso!");
  46. },
  47. error => {
  48. alert("Ocorreu um erro ao salvar!");
  49. });
  50. }
  51.  
  52. @Injectable()
  53. export class ErrorInterceptor implements HttpInterceptor{
  54.  
  55. constructor(){
  56. }
  57.  
  58. intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>>{
  59. return next.handle(req).pipe(catchError(err => {
  60. const error = err.error.message || err.statusText;
  61. return throwError(error);
  62. }))
  63. }
  64. }
  65.  
  66. export const ErrorInterceptorProvider = {
  67. provide : HTTP_INTERCEPTORS,
  68. useClass: ErrorInterceptor,
  69. multi: true,
  70. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement