Guest User

Untitled

a guest
Jul 22nd, 2018
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.86 KB | None | 0 0
  1. @Component({
  2. selector: 'app-operators',
  3. templateUrl: './operators.component.html',
  4. styleUrls: ['./operators.component.css'],
  5. providers: [OperatorService]
  6. })
  7.  
  8. export class OperatorsComponent implements OnInit {
  9.  
  10. displayedColumns: string[] = ['codigo', 'nome'];
  11. private operUrl: 'api/Operators';
  12.  
  13. public message: string;
  14. public dataSource: Model.Itens[];
  15. createForm :FormGroup;
  16.  
  17. public form:any;
  18.  
  19.  
  20. constructor(private _operService: OperatorService, private builder: FormBuilder) {
  21. }
  22.  
  23. ngOnInit() {
  24.  
  25. this.onGet();
  26. }
  27.  
  28. onDelete(operator: Model.Itens) {
  29. debugger;
  30. if (confirm('Deseja excluir o operador: ' + operator.name + '?')) {
  31.  
  32. this._operService
  33. .delete<any>(operator.operatorId)
  34. .subscribe((res) => {
  35. this.onGet();
  36. });
  37. }
  38. }
  39.  
  40. onGet() {
  41. this._operService
  42. .getAll<Model.Result<Model.Itens>>()
  43. .subscribe((data: Model.Result<Model.Itens>) => {
  44. debugger;
  45. this.dataSource = data.itens;
  46. });
  47. }
  48.  
  49. initEditOperator(operator: any){
  50. operator.edit = true;
  51. this.form = this.builder.group({
  52. id: operator.id,
  53. name: operator.name
  54. });
  55. }
  56.  
  57. checkEdit() {
  58. if (this.dataSource == null || this.dataSource.length == 0) return false;
  59. return this.dataSource.filter((item: any) => item.edit === true).length == 0;
  60. }
  61.  
  62. onUpdate(){
  63.  
  64. this._operService.update<Model.Result<Model.Itens>>(this.form.id, this.form.name)
  65. .subscribe(success =>{
  66. // if(success.Result){
  67.  
  68. // }
  69. },
  70. error =>{
  71. }
  72. );
  73.  
  74. }
  75. }
  76.  
  77. <div class="container-fluid">
  78. <div class="row">
  79. <div class="col-md-12">
  80. <table class="table table-striped table-bordered table-hover">
  81. <caption>Lista de Operadores</caption>
  82. <thead class="thead-dark">
  83. <tr>
  84. <th>Código</th>
  85. <th>Nome</th>
  86. </tr>
  87. </thead>
  88. <tbody>
  89. <tr *ngFor="let operator of dataSource">
  90. <form *ngIf="operator.edit != true">
  91. <td>{{ operator.operatorId}}</td>
  92. <td>{{ operator.name }}</td>
  93. <td>
  94. <fa *ngIf="checkEdit()" name="pencil" (click)="initEditOperator(operator)"></fa>
  95. </td>
  96. <td>
  97. <fa *ngIf="checkEdit()" name="times" (click)="onDelete(operator)"></fa>
  98. </td>
  99. </form>
  100. <div *ngIf="operator.edit === true">
  101. <td> {{ operator.operatorId}}<input type="hidden" [(ngModel)]="operator.operatorId"></td>
  102. <td> <input type="text" [(ngModel)]="operator.name"></td>
  103. <td>
  104. <fa name="save" (click)="onUpdate()"></fa>
  105. </td>
  106. <td>
  107. <fa name="ban" (click)="operator.edit = null;"></fa>
  108. </td>
  109. </div>
  110. </tr>
  111. </tbody>
  112. </table>
  113. </div>
  114. </div>
  115. </div>
  116.  
  117. @Injectable({
  118. providedIn: 'root'
  119. })
  120. export class OperatorService {
  121.  
  122. private actionUrl: string;
  123.  
  124. constructor(private http: HttpClient, private _configuration: Configuration) {
  125. this.actionUrl = _configuration.ServerWithApiUrl + 'Operators/';
  126. }
  127.  
  128. public getAll<T>(): Observable<T> {
  129. return this.http.get<T>(this.actionUrl);
  130. }
  131.  
  132. public getSingle<T>(id: number): Observable<T> {
  133. return this.http.get<T>(this.actionUrl + id);
  134. }
  135.  
  136. public add<T>(itemName: string): Observable<T> {
  137. const toAdd = JSON.stringify({ ItemName: itemName });
  138.  
  139. return this.http.post<T>(this.actionUrl, toAdd);
  140. }
  141.  
  142. public update<T>(id: string, itemToUpdate: any): Observable<T> {
  143. return this.http
  144. .put<T>(this.actionUrl + id, JSON.stringify(itemToUpdate));
  145. }
  146.  
  147. public delete<T>(id: string): Observable<T> {
  148. return this.http.delete<T>(this.actionUrl + id);
  149. }
  150. }
  151.  
  152. @Injectable()
  153. export class CustomInterceptor implements HttpInterceptor {
  154.  
  155. intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
  156. if (!req.headers.has('Content-Type')) {
  157. req = req.clone({ headers: req.headers.set('Content-Type', 'application/json') });
  158. }
  159.  
  160. req = req.clone({ headers: req.headers.set('Accept', 'application/json') });
  161. console.log(JSON.stringify(req.headers));
  162. return next.handle(req);
  163. }
  164. }
  165.  
  166. declare namespace Model{
  167. export interface Result<T>{
  168. error: boolean,
  169. itens: Array<T>,
  170. message: string
  171. }
  172.  
  173. export interface Itens{
  174. operatorId: string,
  175. name: string,
  176. }
  177. }
Add Comment
Please, Sign In to add comment