public IHttpActionResult Post([FromBody] IEnumerable <CreateClash> clashes) { if (clashes == null || !ModelState.IsValid) { return(InternalServerError()); } var newClashes = _mapper.Map <List <Clash> >(clashes); var allClashes = _clashRepository.GetAll()?.ToList(); try { Validation(newClashes, allClashes); } catch (Exception e) { return(BadRequest(e.Message)); } foreach (var b in newClashes) { b.Uid = Guid.NewGuid(); var validationResult = ValidateClashDifferencesForSave(b, allClashes); if (!validationResult.Successful) { return(this.Error().BadRequest(ApiError.BadRequest(validationResult.Message))); } } _clashRepository.Add(newClashes); return(Ok()); }
public (Car, ApiError?) AddCar(ApiClient current_client, NewCar new_car) { if (new_car.Year < 1886) { return(null, ApiError.BadRequest( $@"The first gas powered vehicle was invented in 1886. Your car's built year { new_car.Year} is earlier than that." )); } if (new_car.Year > DateTime.Now.Year) { return(null, ApiError.BadRequest( $"Your car's built year {new_car.Year} is in the future." )); } var car = new Car { Client = current_client, Id = Guid.NewGuid(), Make = new_car.Make, Model = new_car.Model, Year = new_car.Year, Stock = new_car.Stock, }; if (!_carsRepository.TryAdd(car.Id, car)) { // We don't know what's going on; this shouldn't happen return(null, ApiError.Internal($"Unexpected error: failed to add new car")); } return(car, null); }
public IHttpActionResult Put(int id, [FromBody] UpdateClashException command) { // MUST BE REWORKED! var offsetHours = 6; if (id == 0 || command == null) { return(this.Error().InvalidParameters()); } var clashException = _clashExceptionRepository.Find(id); if (clashException == null) { return(NotFound()); } ClashException.Validation(clashException.StartDate, command.EndDate, command.TimeAndDows); var ValidationPayload = new[] { MapToClashExceptionForValidation(command, clashException) }; var validationResult = ValidateForSave(ValidationPayload); if (!validationResult.Successful) { return(this.Error().BadRequest(ApiError.BadRequest(validationResult.Message))); } MapToClashExceptions(command, ref clashException); _clashExceptionRepository.Add(clashException); _clashExceptionRepository.SaveChanges(); return(Ok(_clashExceptionRepository.GetWithDescriptions(clashException.Id))); }
public IHttpActionResult Post([FromBody] List <CreateClashException> command) { if (!ModelState.IsValid || command == null || !command.Any()) { return(this.Error().InvalidParameters()); } ValidateInput(command); var clashExceptions = _mapper.Map <List <ClashException> >(command); clashExceptions.ForEach(c => { c.StartDate = c.StartDate.Date; c.EndDate = (c.EndDate == null ? (DateTime?)null : c.EndDate.Value.Date); }); var validationResult = ValidateForSave(clashExceptions); if (!validationResult.Successful) { return(this.Error().BadRequest(ApiError.BadRequest(validationResult.Message))); } _clashExceptionRepository.Add(clashExceptions); _clashExceptionRepository.SaveChanges(); return(Ok(_clashExceptionRepository.GetWithDescriptions(clashExceptions.Select(x => x.Id).ToArray()))); }
public IHttpActionResult Post([FromBody] IEnumerable <CreateCampaign> campaigns) { if (campaigns is null || !campaigns.Any() || !ModelState.IsValid) { return(BadRequest(ModelState)); } foreach (var c in campaigns) { try { c.Validate(); } catch (BreakTypeException ex) { ModelState.AddModelError( nameof(CreateCampaign.BreakType), BreakTypeValueErrorMessage(ex) ); } } if (!ModelState.IsValid) { return(BadRequest(ModelState)); } /* ********************************* * Caveat: THIS LOOKS LIKE A BUG! ********************************* * Why is the campaign validation ONLY done if the feature flag is on? */ if (_featureManager.IsEnabled(nameof(ProductFeature.IncludeChannelGroupFileForOptimiser))) { try { ValidateCampaigns(campaigns.ToList()); ValidateTargetRatings(campaigns); } catch (ArgumentException exception) when(exception.ParamName == ReflectionUtilities.PropertyName <CreateCampaign>(c => c.IncludeRightSizer)) { // Case for 400 Bad Request Code for IncludeRightSizer value validation return(this.Error().BadRequest(ApiError.BadRequest(exception.Message))); } } AddCampaign(campaigns); _campaignPassPrioritiesService.AddNewCampaignPassPrioritiesToAllScenariosInLibrary(); return(Ok()); }
public IHttpActionResult Put([FromUri] string externalId, [FromBody] CreateCampaign command) { if (command is null || String.IsNullOrWhiteSpace(externalId) || !ModelState.IsValid) { return(BadRequest(ModelState)); } if (externalId != command.ExternalId) { ModelState.AddModelError(nameof(CreateCampaign.ExternalId), "ExternalId does not match"); } try { command.Validate(); } catch (BreakTypeException ex) { ModelState.AddModelError( nameof(CreateCampaign.BreakType), BreakTypeValueErrorMessage(ex) ); } if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var campaign = _campaignRepository.FindByRef(externalId).FirstOrDefault(); if (_featureManager.IsEnabled(nameof(ProductFeature.IncludeChannelGroupFileForOptimiser))) { try { ValidateCampaigns(new List <CreateCampaign> { command }, campaign == null); } catch (ArgumentException exception) when(exception.ParamName == ReflectionUtilities.PropertyName <CreateCampaign>(c => c.IncludeRightSizer)) // Case for 400 Bad Request Code for IncludeRightSizer value validation { return(this.Error().BadRequest(ApiError.BadRequest(exception.Message))); } catch (Exception ex) { return(BadRequest(ex.Message)); } } if (campaign is null) { AddCampaign(new List <CreateCampaign>() { command }); } else { var newCampaign = _mapper.Map <Campaign>(command); campaign.Update(newCampaign); ApplyDeliveryCurrency(newCampaign); ApplyBookingPositionAndSequencing(newCampaign); _campaignRepository.Update(campaign); } _campaignRepository.SaveChanges(); _campaignPassPrioritiesService.AddNewCampaignPassPrioritiesToAllScenariosInLibrary(); return(Ok(_mapper.Map <CampaignModel>(campaign))); }