public async Task <IActionResult> GetAssetForCreation() { try { var organizationId = User.Claims.FirstOrDefault(c => c.Type == "selectedOrganization").Value; var organization = await _organizationService.GetById(Convert.ToInt32(organizationId)); if (organization == null) { return(NotFound($"Organization with ID:{organizationId} not found in database to get lookup lists from.")); } var assetForCreation = new Model.AssetForCreation( _mapper.Map <IEnumerable <Model.Location> >(organization.Locations), _mapper.Map <IEnumerable <Model.Status> >(organization.Statuses), _mapper.Map <IEnumerable <Model.Type> >(organization.Types)); return(Ok(assetForCreation)); } catch (EntityException ex) { _logger.LogWarning($"Could not create a new Asset to the database due to following error: {ex.Message}"); } catch (Exception ex) { _logger.LogError($"Threw exception while creating Asset: {ex}"); } return(BadRequest()); }
public async Task <IActionResult> Post([FromBody] Model.AssetForCreation 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"); var userId = User.Claims.FirstOrDefault(c => c.Type == "sub").Value; var organizationId = _userService.GetById(Convert.ToInt32(userId)).SelectedOrganizationId; //Create new asset and map properties var newItem = new Core.Entities.Asset { CreateDt = DateTime.Now }; newItem = _mapper.Map <Core.Entities.Asset>(model); //Add Asset Location newItem.AssetLocations.Add(new Core.Entities.AssetLocation() { LocationId = model.LocationId, Asset = newItem, Note = model.Note, CreateDt = new DateTime(2018, 12, 4) }); //Associate Asset with Organization newItem.AssetOrganizations.Add(new Core.Entities.AssetOrganization() { OrganizationId = organizationId }); if (await _service.Create(newItem)) { return(Created(Url.Link("AssetGet", new { id = newItem.Id }), _mapper.Map <Core.Entities.Asset>( _service.GetById(newItem.Id)))); } } catch (EntityException ex) { _logger.LogWarning($"Could not save Asset to the database due to following error: {ex.Message}"); ModelState.AddModelError(ex.Source, ex.Message); } catch (Exception ex) { _logger.LogError($"Threw exception while saving Asset: {ex}"); } return(BadRequest(ModelState)); }
public async Task <IActionResult> Create(Model.AssetForCreation model) { // call the API var httpClient = await _assetTrackerHttpClient.GetClient(); var response = await httpClient.PostAsync( $"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}"); }