public ActionResult Edit(int id, FormCollection collection) { if (ModelState.IsValid) { try { BeerTypeViewModel beerType = GetBeerType(id); beerType.BeerTypeName = collection["BeerTypeName"]; using (HttpClient client = new HttpClient()) { client.BaseAddress = new Uri(ServiceController.serviceUri.ToString() + "api/BeerTypes"); var putTask = client.PutAsJsonAsync <BeerTypeViewModel>($"BeerTypes/{id}", beerType); putTask.Wait(); if (putTask.Result.IsSuccessStatusCode) { return(RedirectToAction("Index")); } else { return(View("Beer type failed to update")); } } } catch { return(View("Exception caught")); } } else { return(View("Invalid Model State")); } }
private BeerTypeViewModel GetBeerType(int id) { BeerTypeViewModel beerType = null; using (var client = new HttpClient()) { client.BaseAddress = new Uri(ServiceController.serviceUri.ToString() + "/api/BeerTypes"); var responseTask = client.GetAsync($"BeerTypes/{id}"); responseTask.Wait(); var result = responseTask.Result; if (result.IsSuccessStatusCode) { var readTask = result.Content.ReadAsAsync <BeerTypeViewModel>(); readTask.Wait(); beerType = readTask.Result; } else { ModelState.AddModelError(string.Empty, "Server error, no beer type found."); } } return(beerType); }
// GET: Beers public async Task <IActionResult> Index(string beerTypeQuery, string searchString) { // Use LINQ to get list of genres. IQueryable <string> beerQuery = from b in _context.Beer orderby b.BeerType select b.BeerType; var beers = from b in _context.Beer select b; if (!String.IsNullOrEmpty(searchString)) { beers = beers.Where(s => s.BeerName.Contains(searchString)); } if (!String.IsNullOrEmpty(beerTypeQuery)) { beers = beers.Where(x => x.BeerType == beerTypeQuery); } var beerTypeVM = new BeerTypeViewModel(); beerTypeVM.beerTypes = new SelectList(await beerQuery.Distinct().ToListAsync()); beerTypeVM.beers = await beers.ToListAsync(); return(View(beerTypeVM)); }
public IActionResult Get(int id) { var beerDTO = _beerTypeServices.GetBeerType(id); var model = new BeerTypeViewModel(beerDTO); return(Ok(model)); }
public ActionResult Edit(Guid id) { var model = new BeerTypeViewModel(); model = _beerTypeService.GetById(id).ToBeerTypeViewModel(); return(View("Details", model)); }
public async Task <IActionResult> UpdateBeerType(int id, [FromBody] BeerTypeViewModel model) { var type = model.Type; var descr = model.Description; await this.beerTypeServices.UpdateBeerTypeAsync(id, type, descr); return(Ok()); }
public IActionResult Put(int id, [FromBody] BeerTypeViewModel beerTypeApiViewModel) { if (beerTypeApiViewModel == null) { return(BadRequest()); } var beerType = this._beerTypeServices.UpdateBeerType(id, beerTypeApiViewModel.Name); return(Ok(beerTypeApiViewModel)); }
// GET: BeerType/Create public ActionResult Create(BeerTypeViewModel beerType) { ViewBag.LogIn = CurrentUser.UserLoggedIn(); if (beerType == null) { return(View()); } else { return(View(beerType)); } }
public async Task <IActionResult> CreateBeerType([FromBody] BeerTypeViewModel model) { try { var beerTypeDto = new BeerTypeDTO(model.Type, model.Description); var beerType = await this.beerTypeServices.CreateBeerTypeAsync(beerTypeDto); return(Created("Post", beerType)); } catch (Exception) { throw; } }
public async Task <IActionResult> Get(int id) { try { var beerTypeDTO = await this.beerTypeServices.GetBeerTypeAsync(id); var model = new BeerTypeViewModel(beerTypeDTO.Id, beerTypeDTO.Type, beerTypeDTO.Description); return(Ok(model)); } catch (Exception) { return(NotFound()); } }
public IActionResult Post([FromBody] BeerTypeViewModel beerTypeViewModel) { if (beerTypeViewModel == null) { return(BadRequest()); } if (BeerTypeExists(beerTypeViewModel.Name)) { return(ValidationProblem($"Beer Type with name {beerTypeViewModel.Name} already exists.")); } var beerTypeDTO = new BeerTypeDTO { Id = beerTypeViewModel.Id, Name = beerTypeViewModel.Name, }; var beerType = _beerTypeServices.CreateBeerType(beerTypeDTO); return(Created("Post", beerTypeViewModel)); }
public ActionResult Create(FormCollection collection) { if (ModelState.IsValid) { try { BeerTypeViewModel beerType = new BeerTypeViewModel { BeerTypeName = collection["BeerTypeName"] }; using (HttpClient client = new HttpClient()) { client.BaseAddress = new Uri(ServiceController.serviceUri.ToString() + "api/BeerTypes"); var postTask = client.PostAsJsonAsync <BeerTypeViewModel>("BeerTypes", beerType); postTask.Wait(); var result = postTask.Result; if (result.IsSuccessStatusCode) { return(RedirectToAction("Index")); } else { return(View("Beer type failed to create")); } } } catch { return(View("Caught Exception")); } } else { return(View("Invalid Model State")); } }
public ActionResult Update(BeerTypeViewModel details) { _beerTypeService.UpdateBeerType(details.ToBeerTypeDto()); return(RedirectToAction("Index")); }