public IHttpActionResult Post([FromBody] CreatePassModel command) { // Check empty name if (string.IsNullOrWhiteSpace(command.Name)) { return(this.Error().InvalidParameters("Pass name must be set")); } if (!ModelState.IsValid) { return(this.Error().InvalidParameters("Invalid pass parameters")); } // Check that pass doesn't exist Pass pass = _passRepository.Get(command.Id); if (pass != null) { return(this.Error().InvalidParameters("Pass already exists")); } // Check that pass with such name doesn't exist pass = _passRepository.FindByName(command.Name, command.IsLibraried); if (pass != null) { return(this.Error().InvalidParameters("Pass with such name already exists")); } if (_passInspectorService.InspectPassSalesAreaPriorities( command.PassSalesAreaPriorities, out string errorMessage)) { return(this.Error().InvalidParameters(errorMessage)); } List <string> breakExclusionsErrorMessages = new List <string>(); if (!BreakExclusionsValidations.DateTimeRangeIsValid(command.BreakExclusions, _salesAreaRepository.GetAll(), out breakExclusionsErrorMessages)) { return(this.Error().InvalidParameters(breakExclusionsErrorMessages)); } pass = _mapper.Map <Pass>(command); IdUpdater.SetIds(pass, _identityGeneratorResolver); // Validate ValidateForSave(pass); // Save _passRepository.Add(pass); _passRepository.SaveChanges(); // Do not remove this, need to persist changes now so that we can return PassModel return(Ok(Mappings.MapToPassModel(pass, _passRepository, _mapper))); }