public async Task <IHttpActionResult> CreateCamp(CampDto campDto) { try { if (await _repository.GetCampAsync(campDto.Moniker) != null) { ModelState.AddModelError("Moniker", "Moniker is already in use / not unique!"); } if (ModelState.IsValid) /* validate the model, according to the data annotation (e.g. [Required]) defined in the CampDto class. */ { Camp camp = _mapper.Map <Camp>(campDto); _repository.AddCamp(camp); if (await _repository.SaveChangesAsync()) { var newCamp = _mapper.Map <CampDto>(camp); // 'camp' here, after saving, includes an id generated from the DB... //return Created(new Uri(Request.RequestUri + "/" + newCamp.Moniker), newCamp); return(CreatedAtRoute("GetCamp", new { moniker = newCamp.Moniker }, newCamp)); } } } catch (Exception ex) { return(InternalServerError(ex)); } return(BadRequest(ModelState)); }
public async Task <ActionResult <CampDto> > Post(CampDto model) { try { var existing = await _campRepository.GetCampAsync(model.Moniker); if (existing != null) { return(BadRequest("Moniker already in use")); } var location = _linkGenerator.GetPathByAction("GetCamp", "Camps", new { moniker = model.Moniker }); _logger.LogInformation($"{location}"); if (string.IsNullOrWhiteSpace(location)) { return(BadRequest("Could not use current moniker")); } _logger.LogInformation($"model.Moniker:{model.Moniker}, model.Name:{model.Name}"); var camp = _mapper.Map <Camp>(model); _logger.LogInformation("Mapping done"); _campRepository.Add(camp); if (await _campRepository.SaveChangesAsync()) { return(Created(location, _mapper.Map <CampDto>(camp))); } return(BadRequest()); } catch (System.Exception ex) { return(this.StatusCode(StatusCodes.Status500InternalServerError, "Failed to create Camp" + ex.StackTrace)); } }
public async Task <ActionResult <CampDto> > Put(string moniker, CampDto campDto) { try { var oldCamp = await repository.GetCampAsync(moniker); if (oldCamp == null) { return(NotFound($"Could not find camp with moniker of {moniker}")); } mapper.Map(campDto, oldCamp); if (await repository.SaveChangesAsync()) { return(mapper.Map <CampDto>(oldCamp)); } } catch (Exception e) { return(this.StatusCode(StatusCodes.Status500InternalServerError, "Database Failure")); } return(BadRequest()); }
private IList<CampDto> BuildCampList(IList<Camp> camps, string imagesPath) { IList<CampDto> campDtos = new List<CampDto>(); foreach (Camp camp in camps) { CampDto campDto = BuildCamp(camp, imagesPath); campDtos.Add(campDto); } return campDtos; }
private CampDto BuildCamp(Camp camp, string imagesPath) { CampDto campDto = new CampDto(camp.Id, camp.Name, camp.Street, camp.Number, camp.IsEnabled, camp.Location.X, camp.Location.Y); foreach (CampImage campImage in camp.Images) { string fileUrl = string.Format("{0}/{1}", imagesPath, campImage.Name); CampImageDto campImageDto = new CampImageDto(campImage.Id, fileUrl); campDto.Images.Add(campImageDto); } return campDto; }
public IActionResult Create(CreateCampForm form) { try { string email = User.Identity.Name; CampDto createdCampDto = campLogic.Create(email, form.Name, form.Street, form.Number, form.Longitude, form.Latitude, AbcFieldNames, form.FieldCount, imagesPath, spatialReference); return(Ok(createdCampDto)); } catch (ArgumentException ex) { return(NotFound(new { Message = ex.Message })); } catch (Exception ex) { Logger.LogError(ex, "Create method"); return(NotFound(new { Message = "Ocurrió un error" })); } }
public async Task <IHttpActionResult> UpdateCamp(string moniker, CampDto campDto) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (moniker != campDto.Moniker) { return(BadRequest("moniker != Moniker ==> Confusion of da highest order!!! :)")); } try { Camp camp = await _repository.GetCampAsync(moniker); if (camp == null) { return(NotFound()); } _mapper.Map(campDto, camp); if (await _repository.SaveChangesAsync()) { return(Ok(_mapper.Map <CampDto>(camp))); //return StatusCode(HttpStatusCode.NoContent); } else { return(InternalServerError()); } } catch (Exception ex) { return(InternalServerError(ex)); } }
public async Task <ActionResult <CampDto> > Put(String moniker, CampDto model) { try { var existing = await _campRepository.GetCampAsync(model.Moniker); if (existing == null) { return(NotFound($"Could not find Camp with Moniker: {moniker} ")); } _mapper.Map(model, existing); if (await _campRepository.SaveChangesAsync()) { return(_mapper.Map <CampDto>(existing)); } return(BadRequest("There was an issue in updating ")); } catch (System.Exception ex) { return(this.StatusCode(StatusCodes.Status500InternalServerError, "Failed to create Camp" + ex.StackTrace)); } }
public async Task <IHttpActionResult> GetCamp(string moniker, bool includeTalks = false) { Camp result; try { result = await _repository.GetCampAsync(moniker, includeTalks); if (result == null) { return(NotFound()); } } catch (Exception ex) { return(InternalServerError(ex)); } // Mapping (Model/Entity --> Dto) CampDto mappedResult = _mapper.Map <CampDto>(result); return(Ok(new { success = true, camp = mappedResult })); }
public async Task <IHttpActionResult> GetCamp(string moniker) { Camp result; try { result = await _repository.GetCampAsync(moniker, true); if (result == null) { return(NotFound()); } } catch (Exception ex) { return(InternalServerError(ex)); } // Mapping (Model/Entity --> Dto) CampDto mappedResult = _mapper.Map <CampDto>(result); return(Ok(mappedResult)); }
public async Task <ActionResult <CampDto> > Post(CampDto campDto) { try { var existing = await repository.GetCampAsync(campDto.Moniker); if (existing != null) { return(BadRequest("Moniker in Use")); } var location = linkGenerator.GetPathByAction( "Get", "Camps", new { moniker = campDto.Moniker }); if (string.IsNullOrWhiteSpace(location)) { return(BadRequest("Could not use current moniker")); } // Create a new Camp var camp = mapper.Map <Camp>(campDto); repository.Add(camp); if (await repository.SaveChangesAsync()) { return(Created(location, mapper.Map <CampDto>(camp))); } } catch (Exception e) { return(this.StatusCode(StatusCodes.Status500InternalServerError, "Database Failure")); } return(BadRequest()); }