public IHttpActionResult Put(CarAdvertViewModel advert)
        {
            if (false == ModelState.IsValid)
            {
                return(BadRequest("Not a valid data"));
            }

            List <CarAdvertViewModel> advertLst = DummyAdvertList;

            CarAdvertViewModel existingAdvert = advertLst.Where(x => x.Id == advert.Id).FirstOrDefault();

            if (existingAdvert != null)
            {
                existingAdvert.Title             = advert.Title;
                existingAdvert.Fuel              = advert.Fuel;
                existingAdvert.Price             = advert.Price;
                existingAdvert.New               = advert.New;
                existingAdvert.Mileage           = advert.New ? null : advert.Mileage;
                existingAdvert.FirstRegistration = advert.New ? null : advert.FirstRegistration;
            }
            else
            {
                return(NotFound());
            }

            return(Ok());
        }
        public ActionResult Edit(CarAdvertViewModel advert)
        {
            using (var client = new HttpClient())
            {
                client.BaseAddress = new Uri("http://localhost:63915/api/");

                //HTTP POST
                var putTask = client.PutAsJsonAsync <CarAdvertViewModel>("caradvert", advert);
                putTask.Wait();

                var result = putTask.Result;
                if (result.IsSuccessStatusCode)
                {
                    return(RedirectToAction("Index"));
                }
            }
            return(View(advert));
        }
        // DELETE api/caradvert/5
        public IHttpActionResult DeleteAdvert(int id)
        {
            // This will be used if DB connection failed
            List <CarAdvertViewModel> advertLst = DummyAdvertList;

            CarAdvertViewModel advert = advertLst.Where(a => a.Id == id).FirstOrDefault();

            if (id <= 0 || advert == null)
            {
                return(BadRequest("Not a valid advert id"));
            }

            if (advert != null)
            {
                advertLst.Remove(advert);
            }

            return(Ok());
        }
        public ActionResult create(CarAdvertViewModel advert)
        {
            using (var client = new HttpClient())
            {
                client.BaseAddress = new Uri("http://localhost:63915/api/caradvert/");

                //HTTP POST
                var postTask = client.PostAsJsonAsync <CarAdvertViewModel>("caradvert", advert);
                postTask.Wait();

                var result = postTask.Result;
                if (result.IsSuccessStatusCode)
                {
                    return(RedirectToAction("Index"));
                }
            }

            ModelState.AddModelError(string.Empty, "Server Error. Please contact administrator.");

            return(View(advert));
        }
        // PUT api/caradvert/5
        public IHttpActionResult PutAdvert(int id, [FromBody] CarAdvertViewModel advert)
        {
            // This will be used if DB connection failed
            List <CarAdvertViewModel> advertLst = DummyAdvertList;

            CarAdvertViewModel existingAdvert = advertLst.Where(x => x.Id == advert.Id).FirstOrDefault();

            if (existingAdvert != null)
            {
                existingAdvert.Title             = advert.Title;
                existingAdvert.Fuel              = advert.Fuel;
                existingAdvert.Price             = advert.Price;
                existingAdvert.New               = advert.New;
                existingAdvert.Mileage           = advert.New ? null : advert.Mileage;
                existingAdvert.FirstRegistration = advert.New ? null : advert.FirstRegistration;
            }
            else
            {
                return(NotFound());
            }

            return(Ok());
        }
        // POST api/caradvert
        public IHttpActionResult PostAdvert([FromBody] CarAdvertViewModel advert)
        {
            if (false == ModelState.IsValid)
            {
                return(BadRequest("Not a valid model"));
            }

            // This will be used if DB connection failed
            List <CarAdvertViewModel> advertLst = DummyAdvertList;

            DummyAdvertList.Add(new CarAdvertViewModel()
            {
                Id                = advertLst.Count() + 1,
                Title             = advert.Title,
                Fuel              = advert.Fuel,
                Price             = advert.Price,
                New               = advert.New,
                Mileage           = advert.New ? null : advert.Mileage,
                FirstRegistration = advert.New ? null : advert.FirstRegistration
            });

            return(Ok());
        }
        public ActionResult Edit(int id)
        {
            CarAdvertViewModel advert = null;

            using (var client = new HttpClient())
            {
                client.BaseAddress = new Uri("http://localhost:63915/api/");
                //HTTP GET
                var responseTask = client.GetAsync("caradvert?id=" + id.ToString());
                responseTask.Wait();

                var result = responseTask.Result;
                if (result.IsSuccessStatusCode)
                {
                    var readTask = result.Content.ReadAsAsync <CarAdvertViewModel>();
                    readTask.Wait();

                    advert = readTask.Result;
                }
            }

            return(View(advert));
        }