public async Task <IHttpActionResult> PutCodeCamp(string moniker, CodeCampModel codeCampModel) { try { if (await _codeCampService.GetCodeCamp(moniker) == null) { return(NotFound()); } // if moniker changed then new moniker unique check if (!string.Equals(moniker, codeCampModel.Moniker) && await _codeCampService.GetCodeCamp(codeCampModel.Moniker) != null) { ModelState.AddModelError("Moniker", "Moniker should be unique"); } if (ModelState.IsValid) { var createdModel = await _codeCampService.UpdateCodeCamp(moniker, codeCampModel); return(CreatedAtRoute("GetCodeCamp", new { moniker = createdModel.Moniker }, createdModel)); } } catch (Exception exception) { Logger.Error(exception); return(InternalServerError(exception)); } return(BadRequest(ModelState)); }
public async Task AddNewCodeCamp() { // Arrange var codeCampModel = new CodeCampModel() { Moniker = "99xCodeCampThree", Sessions = null, Name = "99x Code Camp Three", EventDate = DateTime.Today.AddDays(1), Length = 4, }; // Act var codeCampDb = await _codeCampService.AddNewCodeCamp(codeCampModel); // Assert Assert.Equal(codeCampModel.Moniker, codeCampDb.Moniker); }
/// <summary> /// Adds the new code camp. /// </summary> /// <param name="codeCampModel">The code camp model.</param> /// <returns>created code camp model</returns> /// <exception cref="ArgumentNullException">codeCampModel</exception> /// <exception cref="InvalidOperationException">Moniker should be unique</exception> public async Task <CodeCampModel> AddNewCodeCamp(CodeCampModel codeCampModel) { if (codeCampModel == null) { throw new ArgumentNullException(nameof(codeCampModel)); } if (await this.CodeCampExists(codeCampModel.Moniker)) { throw new InvalidOperationException("Moniker should be unique"); } var codeCamp = _mapper.Map <CodeCamp>(codeCampModel); _repository.Insert(codeCamp); await _repository.SaveAsync(); return(await GetCodeCamp(codeCampModel.Moniker)); }
/// <summary> Updates the code camp.</summary> /// <param name="moniker">The moniker.</param> /// <param name="codeCampModel">The code camp model.</param> /// <returns>updated document camp model</returns> /// <exception cref="ArgumentNullException">codeCampModel</exception> /// <exception cref="InvalidOperationException">Cannot find a code camp by given moniker</exception> public async Task <CodeCampModel> UpdateCodeCamp(string moniker, CodeCampModel codeCampModel) { if (codeCampModel == null) { throw new ArgumentNullException(nameof(codeCampModel)); } var camp = await this.CodeCampIfExists(moniker); if (camp == null) { throw new InvalidOperationException("Cannot find a code camp by given moniker"); } _mapper.Map(codeCampModel, camp); await _repository.SaveAsync(); return(_mapper.Map <CodeCampModel>(camp)); }
// POST: api/CodeCamps /// <summary> /// Posts the code camp. /// </summary> /// <param name="codeCampModel">The code camp model.</param> /// <returns>created code camp model</returns> public async Task <IHttpActionResult> PostCodeCamp(CodeCampModel codeCampModel) { try { if (await _codeCampService.GetCodeCamp(codeCampModel.Moniker) != null) { ModelState.AddModelError("Moniker", "Moniker should be unique"); } if (ModelState.IsValid) { var createdModel = await _codeCampService.AddNewCodeCamp(codeCampModel); return(CreatedAtRoute("GetCodeCamp", new { moniker = createdModel.Moniker }, createdModel)); } } catch (Exception exception) { Logger.Error(exception); return(InternalServerError(exception)); } return(BadRequest(ModelState)); }