public async Task <IActionResult> Edit(int id, IcmpEndpointViewModel vm) { if (id != vm.Id) { return(NotFound()); } if (ModelState.IsValid) { try { var endpoint = vm.ToEndpoint(); var oldEndpoint = _context.Endpoints.AsNoTracking().First(e => e.Id == endpoint.Id); _context.Update(endpoint); await _context.SaveChangesAsync(); NotifyEditEvent(oldEndpoint, endpoint); _stateService.RemoveStatus(endpoint.Id); } catch (DbUpdateConcurrencyException) { if (!EndpointExists(vm.Id)) { return(NotFound()); } else { throw; } } return(RedirectToAction(nameof(List))); } return(View(nameof(Edit), vm)); }
public async Task <IActionResult> Create(IcmpEndpointViewModel vm) { if (ModelState.IsValid) { var endpoint = vm.ToEndpoint(); endpoint.Id = 0; _context.Add(endpoint); await _context.SaveChangesAsync(); NotifyEvent(endpoint, NotificationType.EndpointAdded); return(RedirectToAction(nameof(List))); } return(View(nameof(Create), vm)); }
public void ShouldConvertIcmpViewModelToEndpoint() { var vm1 = new IcmpEndpointViewModel { Id = 5, Enabled = true, Kind = Endpoint.EndpointKind.Icmp, Name = "fdsa", Address = "asdf" }; var ep1 = vm1.ToEndpoint(); Assert.AreEqual(5, ep1.Id); Assert.IsTrue(ep1.Enabled); Assert.AreEqual(Endpoint.EndpointKind.Icmp, ep1.Kind); Assert.AreEqual("fdsa", ep1.Name); Assert.AreEqual("asdf", ep1.GetIcmpAddress()); Assert.IsTrue(DateTimeOffset.UtcNow.AddMinutes(-1) < ep1.LastUpdated); var vm2 = new IcmpEndpointViewModel { Kind = Endpoint.EndpointKind.Icmp }; var ep2 = vm2.ToEndpoint(); Assert.AreEqual(Endpoint.EndpointKind.Icmp, ep2.Kind); Assert.IsTrue(DateTimeOffset.UtcNow.AddMinutes(-1) < ep2.LastUpdated); }