public static EndpointViewModel ToEndpointViewModel(this Endpoint endpoint) { EndpointViewModel vm; if (endpoint.Kind == Endpoint.EndpointKind.Http) { vm = new HttpEndpointViewModel { Location = endpoint.GetHttpLocation() ?? "", ResponseMatch = endpoint.GetHttpResponseMatch() ?? "", StatusCodes = string.Join(',', endpoint.GetHttpStatusCodes()?.Select(c => c.ToString()) ?? new string[0]), IgnoreCerts = endpoint.GetHttpIgnoreTlsCerts(), CustomCert = endpoint.GetHttpCustomTlsCert() ?? "" }; } else if (endpoint.Kind == Endpoint.EndpointKind.Icmp) { vm = new IcmpEndpointViewModel { Address = endpoint.GetIcmpAddress() }; } else { throw new NotImplementedException(); } vm.Id = endpoint.Id; vm.Kind = endpoint.Kind; vm.Enabled = endpoint.Enabled; vm.Name = endpoint.Name; return(vm); }
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); }