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);
            }
                );
        }
示例#2
0
        public DeviceLocationViewModel(IDeviceRelatedRepository repo)
        {
            Repository = repo;

            AllHousings = Repository.AllHousings.ToList();
            AllCabinets = Repository.AllCabinets.ToList();

            IsDeviceLocationChoosingAvailable = false;

            ApplyDeviceLocationChangesCommand = RegisterCommandAction(
                (obj) =>
            {
                SelectedDevice.Cabinet         = SelectedCabinet;
                SelectedDevice.Cabinet.Housing = SelectedHousing;

                try
                {
                    Repository.UpdateDevice(SelectedDevice);

                    var newRecord = new DeviceMovementHistoryNote()
                    {
                        DeviceID        = SelectedDevice.ID,
                        TargetCabinetID = SelectedDevice.Cabinet.ID,
                        Date            = DateTime.Now,
                        // Reason field is temporary. Need to create entity with reasons
                        // and use it instead of this hard coding
                        Reason = "Перемещение"
                    };

                    try
                    {
                        Repository.FixDeviceMovement(newRecord);
                        Repository.SaveChanges();
                    }
                    catch
                    {
                        Repository.RemoveDeviceMovementNote(newRecord);
                    }

                    Repository.SaveChanges();
                }
                catch (Exception e) { MessageToUser = e.Message; }
            },
                (obj) => (SelectedDevice != null) &&
                (SelectedDevice.Cabinet != SelectedCabinet)
                );

            SubscribeActionOnHousingChanged(
                (h) =>
            {
                if (h != null)
                {
                    SelectedHousingCabinets = GetAllSelectedHousingCabinets();
                    SelectDeviceCabinetIfHousingIsTheSame();
                }
                else
                {
                    SelectedHousingCabinets = null;
                }
            }
                );

            SubscribeActionOnDeviceSelectionChanging(
                (device) =>
            {
                if (device != null)
                {
                    SelectedHousing = device.Cabinet.Housing;
                    SelectedCabinet = device.Cabinet;
                }
            }
                );

            DeviceEvents.OnDeviceSelectionChanged += device =>
            {
                if (device != null)
                {
                    IsDeviceLocationChoosingAvailable = true;
                }
                else
                {
                    SelectedCabinet = null;
                    SelectedHousing = null;

                    IsDeviceLocationChoosingAvailable = false;
                }
            };
        }