Пример #1
0
        public async Task <bool> UpdateAsync(BizCoverCar car)
        {
            var request = new HttpRequestMessage(HttpMethod.Put, _repoUrl + $"/{car.Id}");

            var keyValues = new List <KeyValuePair <string, string> >
            {
                new KeyValuePair <string, string>("Make", car.Make),
                new KeyValuePair <string, string>("Model", car.Model),
                new KeyValuePair <string, string>("Year", car.Year.ToString()),
                new KeyValuePair <string, string>("CountryManufactured", car.CountryManufactured),
                new KeyValuePair <string, string>("Colour", car.Colour),
                new KeyValuePair <string, string>("Price", car.Price.ToString()),
            };

            request.Content = new FormUrlEncodedContent(keyValues);

            HttpResponseMessage response = await _client.SendAsync(request);

            if (response.IsSuccessStatusCode)
            {
                return(true);
            }
            else
            {
                throw new InvalidOperationException($"Unable update a new car, {response.StatusCode}");
            }
        }
Пример #2
0
        public async Task <int> AddAsync(BizCoverCar car)
        {
            var request = new HttpRequestMessage(HttpMethod.Post, _repoUrl);

            var keyValues = new List <KeyValuePair <string, string> >
            {
                new KeyValuePair <string, string>("Make", car.Make),
                new KeyValuePair <string, string>("Model", car.Model),
                new KeyValuePair <string, string>("Year", car.Year.ToString()),
                new KeyValuePair <string, string>("CountryManufactured", car.CountryManufactured),
                new KeyValuePair <string, string>("Colour", car.Colour),
                new KeyValuePair <string, string>("Price", car.Price.ToString()),
            };

            request.Content = new FormUrlEncodedContent(keyValues);

            HttpResponseMessage response = await _client.SendAsync(request);

            if (response.IsSuccessStatusCode)
            {
                int.TryParse(await response.Content.ReadAsStringAsync(), out int id);
                return(id);
            }
            else
            {
                throw new InvalidOperationException($"Unable add a new car, {response.StatusCode}");
            }
        }
Пример #3
0
 public async Task <ActionResult> Post([FromForm] BizCoverCar value)
 {
     try
     {
         value.Id = 0;
         return(Ok(await _cachedRepo.AddAsync(value)));
     }
     catch (InvalidOperationException ioe)
     {
         return(BadRequest(ioe.Message));
     }
     catch (Exception ex)
     {
         return(StatusCode(500, ex.Message));
     }
 }
Пример #4
0
 public async Task <ActionResult> Put(int id, [FromForm] BizCoverCar value)
 {
     try
     {
         value.Id = id;
         if (await _cachedRepo.UpdateAsync(value))
         {
             return(Ok());
         }
         else
         {
             return(BadRequest("Update failed"));
         }
     }
     catch (InvalidOperationException ioe)
     {
         return(BadRequest(ioe.Message));
     }
     catch (Exception ex)
     {
         return(StatusCode(500, ex.Message));
     }
 }