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

            var response = await httpClient.PostAsync(
                $"api/asset/{model.AssetId}/locations",
                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> Post(int id, [FromBody] Model.AssetLocationForCreation model)
        {
            try
            {
                //Check to see if the model is Valid.  If not return the ModelState
                if (!ModelState.IsValid)
                {
                    return(BadRequest(ModelState));
                }

                _logger.LogInformation("Creating new Asset Location Association");

                //Create new asset location and map properties
                var newItem = new Core.Entities.AssetLocation()
                {
                    AssetId    = id,
                    LocationId = model.LocationId,
                    Note       = model.Note,
                    CreateDt   = DateTime.Now
                };

                if (await _service.Create(newItem))
                {
                    var url = Url.Link("AssetLocationGet",
                                       new { id         = newItem.AssetId,
                                             locationId = newItem.LocationId,
                                             createDt   = newItem.CreateDt });

                    var ret = _service.GetById(newItem.AssetId, newItem.LocationId, newItem.CreateDt);
                    return(Created(url, ret));
                }
            }
            catch (EntityException ex)
            {
                _logger.LogWarning($"Could not save AssetLocation to the database due to following error: {ex.Message}");
                ModelState.AddModelError(ex.Source, ex.Message);
            }
            catch (Exception ex)
            {
                _logger.LogError($"Threw exception while saving AssetLocation: {ex}");
            }

            return(BadRequest(ModelState));
        }