Ejemplo n.º 1
0
        public ActionResult <BaseResponse> UpdateStatus(DetailedStatus status)
        {
            try
            {
                _statusService.AddOrUpdate(status);

                if (status.Id != 0)
                {
                    var oldStatus = _statusService.Get(status.Id);
                    var newStatus = _statusService.Get(status.Status);
                    _deviceService.UpdateStatuses(newStatus, oldStatus);
                }

                return(new BaseResponse()
                {
                    Message = $"Updated/Added status: {status.Status}"
                });
            }
            catch (Exception ex)
            {
                return(new BaseResponse()
                {
                    Error = ErrorCode.InternalError,
                    Message = ex.Message
                });
            }
        }
        private void ReadDevice(IExcelDataReader excel)
        {
            try
            {
                var devices = new List <Device>();

                while (excel.Read() && excel.GetString(3) != null)
                {
                    var device = new Device();

                    if (excel.GetValue(2) == null)
                    {
                        return;
                    }

                    string modelText = excel.GetValue(1)?.ToString();
                    if (string.IsNullOrEmpty(modelText))
                    {
                        //TODO HIGH Add Log
                        continue;
                    }
                    var model = _deviceModelService.Get(modelText);

                    if (model == null)
                    {
                        //TODO HIGH Add Log
                        continue;
                    }
                    device.Model = model;

                    string serialNumberText = excel.GetValue(2)?.ToString();
                    if (string.IsNullOrEmpty(serialNumberText))
                    {
                        //TODO HIGH Add Log
                        continue;
                    }
                    device.SerialNumber = serialNumberText;

                    string ownedByText = excel.GetValue(3)?.ToString();
                    if (string.IsNullOrEmpty(ownedByText))
                    {
                        //TODO HIGH Add Log
                        continue;
                    }
                    device.OwnedBy = _deviceLocationService.Get(ownedByText);

                    if (device.OwnedBy == null)
                    {
                        continue;
                    }

                    string dateText = excel.GetValue(4)?.ToString();
                    if (string.IsNullOrEmpty(dateText) || !DateTime.TryParse(dateText, out var acquisitionDate))
                    {
                        //TODO HIGH Add Log
                        continue;
                    }
                    device.AcquisitionDate = acquisitionDate;

                    string statusText = excel.GetValue(5)?.ToString();
                    if (string.IsNullOrEmpty(statusText))
                    {
                        //TODO HIGH Add Log
                        continue;
                    }
                    var status = _statusService.Get(statusText);

                    if (status == null)
                    {
                        //TODO HIGH Add Log
                        continue;
                    }
                    device.Status = status;

                    string locationText = excel.GetValue(6)?.ToString();
                    if (string.IsNullOrEmpty(locationText))
                    {
                        //TODO HIGH Add Log
                        continue;
                    }
                    var location = _deviceLocationService.Get(locationText);
                    if (location == null)
                    {
                        //TODO HIGH Add Log
                        continue;
                    }
                    device.InitialLocation = location;

                    string groupText = excel.GetValue(7)?.ToString();
                    if (string.IsNullOrEmpty(groupText))
                    {
                        //TODO HIGH Add Log
                        continue;
                    }
                    var group = _groupService.Get(groupText);
                    if (group == null)
                    {
                        //TODO HIGH Add Log
                        continue;
                    }
                    device.Classification = group;

                    string sfDate = excel.GetValue(8)?.ToString();
                    if (!string.IsNullOrEmpty(sfDate) && DateTime.TryParse(sfDate, out var parsedSfDate))
                    {
                        device.SfDate = parsedSfDate;
                    }

                    string sfNumber = excel.GetValue(9)?.ToString();
                    if (!string.IsNullOrEmpty(sfNumber))
                    {
                        device.SfNumber = sfNumber;
                    }

                    string notes = excel.GetValue(10)?.ToString();
                    if (!string.IsNullOrEmpty(notes))
                    {
                        device.AdditionalNotes = notes;
                    }
                    device.DeviceEvents = ReadEvents(excel);
                    devices.Add(device);
                }
                _deviceService.AddOrUpdate(devices);
            }
            catch (Exception ex)
            {
                //TODO: HIGH add logging
                throw new AdministrationException($"Failed to parse the device {excel.GetString(3)}");
            }
        }