Example #1
0
        public async Task <IActionResult> Edit(Model.AssetForUpdate model)
        {
            // call the API
            var httpClient = await _assetTrackerHttpClient.GetClient();

            var response = await httpClient.PutAsync(
                $"api/asset",
                new StringContent(JsonConvert.SerializeObject(model), System.Text.Encoding.Unicode, "application/json"))
                           .ConfigureAwait(false);

            if (response.IsSuccessStatusCode)
            {
                return(RedirectToAction("Index"));
            }

            throw new Exception($"A problem happened while calling the API: {response.ReasonPhrase}");
        }
Example #2
0
        public async Task <IActionResult> Put([FromBody] Model.AssetForUpdate model)
        {
            try
            {
                //Check to see if the model is Valid.  If not return the ModelState
                if (!ModelState.IsValid)
                {
                    return(BadRequest(ModelState));
                }

                _logger.LogInformation($"Updating Asset with ID: {model.Id}");

                //Get the asset to modify and map properties
                var item = await _service.GetById(model.Id);

                if (item == null)
                {
                    return(NotFound($"Asset with ID:{model.Id} not found in database to update"));
                }

                item = _mapper.Map(model, item);

                if (await _service.Update(item))
                {
                    return(Ok(_mapper.Map <Model.Asset>(
                                  _service.GetById(model.Id))));
                }
            }
            catch (EntityException ex)
            {
                _logger.LogWarning($"Could not save Asset to the database due to following error: {ex.Message}");
            }
            catch (Exception ex)
            {
                _logger.LogError($"Threw exception while saving Asset: {ex}");
            }

            return(BadRequest());
        }