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);
        }
Exemplo n.º 3
0
        // 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));
        }
Exemplo n.º 4
0
        public IActionResult Get(int id)
        {
            var beerDTO = _beerTypeServices.GetBeerType(id);
            var model   = new BeerTypeViewModel(beerDTO);

            return(Ok(model));
        }
Exemplo n.º 5
0
        public ActionResult Edit(Guid id)
        {
            var model = new BeerTypeViewModel();

            model = _beerTypeService.GetById(id).ToBeerTypeViewModel();

            return(View("Details", model));
        }
Exemplo n.º 6
0
        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());
        }
Exemplo n.º 7
0
        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));
     }
 }
Exemplo n.º 9
0
        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;
            }
        }
Exemplo n.º 10
0
        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());
            }
        }
Exemplo n.º 11
0
        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"));
            }
        }
Exemplo n.º 13
0
        public ActionResult Update(BeerTypeViewModel details)
        {
            _beerTypeService.UpdateBeerType(details.ToBeerTypeDto());

            return(RedirectToAction("Index"));
        }