Advertisement
Mateus_Costa

Untitled

May 21st, 2020
1,474
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.57 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections.ObjectModel;
  4. using System.Reactive;
  5. using System.Reactive.Concurrency;
  6. using CTR.Infrastructure.Repository;
  7. using CTR.Models.POCO;
  8. using CTR.Utils;
  9. using ReactiveUI;
  10.  
  11. namespace CTR.ViewModels
  12. {
  13.     public class CadastroDepartamentoViewModel : ViewModelBase
  14.     {
  15.         public CadastroDepartamentoViewModel(IRepository repository, DispatcherScheduler uiDispatcherScheduler)
  16.             : base(repository, uiDispatcherScheduler)
  17.         {
  18.             Secretarias = new ObservableCollection<Secretaria>();
  19.             SalvarCommand = ReactiveCommand.Create(SalvarDepartamentoExecute);
  20.  
  21.             Repository.GetAll<Secretaria>()
  22.                 .Busy(this)
  23.                 .Subscribe(CarregarSecretarias);
  24.         }
  25.  
  26.         public string Descricao { get; set; }
  27.         public ObservableCollection<Secretaria> Secretarias { get; }
  28.         public Secretaria SecretariaSelecionada { get; set; }
  29.         public ReactiveCommand<Unit, Unit> SalvarCommand { get; }
  30.  
  31.  
  32.         private void CarregarSecretarias(IEnumerable<Secretaria> secretarias)
  33.         {
  34.             foreach(var secretaria in secretarias)
  35.             {
  36.                 Secretarias.Add(secretaria);
  37.             }
  38.         }
  39.  
  40.         private void SalvarDepartamentoExecute()
  41.         {
  42.             var dotacao = new Departamento
  43.             {
  44.                 Descricao = Descricao,
  45.                 SecretariaId = SecretariaSelecionada.SecretariaId
  46.             };
  47.  
  48.             Repository.Add(dotacao).Subscribe();
  49.         }
  50.     }
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement