public AddDeviceViewModel(IDeviceRelatedRepository repo)
        {
            Repository = repo;

            AddDeviceCommand = RegisterCommandAction(
                (obj) =>
            {
                var newDevice = new Device
                {
                    InventoryNumber = InputtedInventoryNumber,
                    DeviceTypeID    = SelectedDeviceType.ID,
                    NetworkName     = InputtedNetworkName,
                };

                try
                {
                    Repository.AddDevice(newDevice);
                    Repository.SaveChanges();

                    // Device should be counted as added to storage when added
                    var addedDeviceNote = new DeviceMovementHistoryNote
                    {
                        // N/A cabinet in N/A housing
                        TargetCabinetID = -4,
                        DeviceID        = newDevice.ID,
                        Reason          = "Доставлено на склад",
                        Date            = DateTime.Now
                    };
                    Repository.FixDeviceMovement(addedDeviceNote);
                    Repository.SaveChanges();

                    DeviceEvents.RaiseOnNewDeviceAdded(newDevice);

                    InputtedInventoryNumber = "";
                    InputtedNetworkName     = "";
                    MessageToUser           = "******";
                }
                catch (Exception e)
                {
                    MessageToUser = e.Message;
                    Repository.RemoveDevice(newDevice);
                }
            },
                (obj) =>
            {
                return(!string.IsNullOrEmpty(InputtedInventoryNumber) &&
                       !string.IsNullOrEmpty(InputtedNetworkName) &&
                       SelectedDeviceType != null);
            }
                );
        }
Example #2
0
        public DevicesListViewModel(IDeviceRelatedRepository repo, IUserSession userSession)
        {
            Repository = repo;

            UserSession = userSession;

            AllDevices = Repository.AllDevices.ToList();

            InitDevicesLocationWithInstances();

            FilterDevicesAccordingToCriteria();

            ShowAddDeviceViewCommand = RegisterCommandAction(
                (obj) =>
            {
                AddDeviceView             = new AddDeviceView();
                AddDeviceView.DataContext = ResolveDependency <IAddDeviceViewModel>();
                AddDeviceView.ShowDialog();
            },
                (obj) =>
                UserSession.IsAuthorizedUserAllowedTo(UserActions.AddDevice)
                );

            RemoveDeviceCommand = RegisterCommandAction(
                (obj) =>
            {
                Repository.RemoveDevice(SelectedDevice);

                Repository.DeleteAllDeviceMovementHistory(SelectedDevice);

                Repository.SaveChanges();
                AllDevices.Remove(AllDevices.Find(d => d.ID == SelectedDevice.ID));
                FilteredDevices.Remove(SelectedDevice);
            },
                (obj) =>
            {
                if (UserSession.IsAuthorizedUserAllowedTo(UserActions.RemoveDevice))
                {
                    return(SelectedDevice != null);
                }
                else
                {
                    return(false);
                }
            }
                );

            SubscribeActionOnDeviceAddition(
                (device) =>
            {
                device.DeviceType      = Repository.AllDeviceTypes.Single(dt => dt.ID == device.DeviceTypeID);
                device.Cabinet         = Repository.FindCabinet(device.CabinetID);
                device.Cabinet.Housing = DeviceLocationViewModel.
                                         AllHousings.
                                         First(h => h.ID == device.Cabinet.HousingID);

                AllDevices.Add(device);
                if (DevicesFilter.DoesMeetSearchingAndFilteringCriteria(device))
                {
                    FilteredDevices.Add(device);
                }
            }
                );

            SubscribeActionOnFilteringCriteraChanges(
                (filteredDevices) =>
                FilteredDevices = filteredDevices.ToObservableCollection()
                );
        }