예제 #1
0
        public IActionResult Add(DeviceAddViewModel model)
        {
            if (!ModelState.IsValid)
            {
                return(RedirectToAction(nameof(Index), new { dmessage = DeviceMessageId.ValidationFailed }));
            }

            if (_deviceRepo.CheckHostnameExists(model.Hostname, GetCurrentUserAsync().Result.Id))
            {
                return(RedirectToAction(nameof(Index), new { dmessage = DeviceMessageId.HostnameAlreadyExists }));
            }

            try
            {
                var device = new Device
                {
                    Id         = Guid.NewGuid().ToString(),
                    User       = _userRepo.Get(GetCurrentUserAsync().Result.Id),
                    AddedOn    = DateTime.Now,
                    UpdatedOn  = DateTime.Now,
                    DeviceType = _deviceRepo.GetDeviceType(model.DeviceType),
                    Hostname   = model.Hostname
                };

                _deviceRepo.Add(device);
                _logger.LogInformation("User created a new device.");
                return(RedirectToAction(nameof(Index), new { dmessage = DeviceMessageId.AddDeviceSuccess }));
            }
            catch (Exception ex)
            {
                _logger.LogInformation(ex.Message);
                return(RedirectToAction("Index", new { dmessage = DeviceMessageId.AddDeviceFailed }));
            }
        }
예제 #2
0
        public async Task <IDevice> CreateDevice(Device d)
        {
            var result = await deviceRepo.Add(d);

            return(result);
        }