Exemplo n.º 1
0
        public IHttpActionResult Put(int id, [FromBody] CreateRunTypeModel command)
        {
            if (command is null || !ModelState.IsValid)
            {
                return(this.Error().InvalidParameters());
            }

            var runType = _runTypeRepository.Get(id);

            if (runType is null)
            {
                return(this.Error().ResourceNotFound());
            }

            (bool isValid, string message) = RunTypesValidations.ValidateRunTypeName(command.Name);
            if (!isValid)
            {
                return(this.Error().BadRequest(message));
            }

            var runTypeByName = _runTypeRepository.FindByName(command.Name);

            if (runTypeByName != null && runTypeByName.Id != id)
            {
                return(this.Error().BadRequest("Run type name already exists: " + command.Name));
            }

            (bool kpiIsValid, string kpiErrorMessage) = RunTypesValidations.ValidateDefaultKPIName(command.DefaultKPI);
            if (!kpiIsValid)
            {
                return(this.Error().BadRequest(kpiErrorMessage));
            }

            var allAnalysisGroups = _analysisGroupRepository.GetAll();

            (bool analysisGroupsIsValid, string errorMessage) = RunTypesValidations.ValidateRunTypeAnalysisGroupList(command.RunTypeAnalysisGroups, allAnalysisGroups);
            if (!analysisGroupsIsValid)
            {
                return(this.Error().BadRequest(errorMessage));
            }

            _mapper.Map(command, runType);
            runType.ModifiedDate = DateTime.UtcNow;
            _runTypeRepository.Update(runType);
            _runTypeRepository.SaveChanges();

            return(Ok(runType));
        }