public async Task <ActionResult> Create(PowerSupplys ps) { try { string jsonString = JsonConvert.SerializeObject(ps); var request = new HttpRequestMessage(HttpMethod.Post, "api/powersupplys"); { // we set what the Content-Type header will be here request.Content = new StringContent(jsonString, Encoding.UTF8, "application/json"); }; var response = await HttpClient.SendAsync(request); if (!response.IsSuccessStatusCode) { return(View("Error")); } return(RedirectToAction(nameof(Index))); } catch { return(View()); } }
public async void GetPowerSupplysFromDatabaseTest() { var options = new DbContextOptionsBuilder <CheapWareContext>() .UseInMemoryDatabase(databaseName: "GetPowerSupplysFromDatabase") .Options; var powersupply = new PowerSupplys { Name = "test name", Modular = false, Price = 129.99M }; List <PowerSupply> listofpowersupplys = null; using (var context = new CheapWareContext(options)) { context.Add(powersupply); context.SaveChanges(); } using (var context = new CheapWareContext(options)) { var service = new ComputerRepo(context); listofpowersupplys = await service.GetPowerSupplys(); } using (var context = new CheapWareContext(options)) { Assert.Single(listofpowersupplys); Assert.Equal("test name", context.PowerSupplys.Single().Name); } }
// GET: Inventorys/Details/5 public async Task <ActionResult> Details(string name) { var request = CreateRequestToService(HttpMethod.Get, "api/powersupplys/" + name); try { var response = await HttpClient.SendAsync(request); if (!response.IsSuccessStatusCode) { return(View("Error")); } string jsonString = await response.Content.ReadAsStringAsync(); PowerSupplys ps = JsonConvert.DeserializeObject <PowerSupplys>(jsonString); return(View(ps)); } catch (HttpRequestException) { return(View("Error")); } }