public ValidationResult Update(Domain.Entities.CourseConfiguration entity)
        {
            if (entity == null)
            {
                var result = new ValidationResult();
                result.AddError("Nenhuma configuraçao de curso informada para atualização.");
                return(result);
            }

            if (!entity.IsValid())
            {
                return(entity);
            }

            if (entity.IsCurrentConfiguration)
            {
                try
                {
                    DisableCurrentConfiguration(entity);
                }
                catch (Exception ex)
                {
                    entity.AddError(ex.Message);
                    return(entity);
                }
            }

            entity.UpdatedDate = DateTime.Now;

            _courseDocumentRepository.RemoveByCourseConfiguration(entity.Id.Value);

            _courseConfigurationRepository.Update(entity);

            return(new ValidationResult());
        }
        public IActionResult Put(Guid key, [FromBody] Domain.Entities.CourseConfiguration configuration)
        {
            try
            {
                var relatedItem = _courseConfiguration.GetById(key);

                if (relatedItem == null)
                {
                    return(NotFound(key));
                }

                if (!configuration.Id.HasValue)
                {
                    configuration.Id = key;
                }

                var result = _courseConfiguration.Update(configuration);

                _unitOfWork.Commit();

                if (!result.Errors.Any())
                {
                    return(Ok());
                }
                else
                {
                    return(BadRequest(configuration));
                }
            }
            catch (Exception ex)
            {
                return(StatusCode(500, ex));
            }
        }
        public IActionResult Post([FromBody] Domain.Entities.CourseConfiguration configuration)
        {
            try
            {
                if (configuration == null)
                {
                    return(BadRequest());
                }

                var createdConfiguration = _courseConfiguration.Add(configuration);

                _unitOfWork.Commit();

                if (createdConfiguration.Errors.Any())
                {
                    return(BadRequest(createdConfiguration.Errors));
                }
                else
                {
                    return(Ok(createdConfiguration));
                }
            }
            catch (Exception ex)
            {
                return(StatusCode(500, ex));
            }
        }
        public Domain.Entities.CourseConfiguration Add(Domain.Entities.CourseConfiguration entity)
        {
            if (!entity.IsValid())
            {
                return(entity);
            }

            if (entity.IsCurrentConfiguration)
            {
                try
                {
                    DisableCurrentConfiguration(entity);
                }
                catch (Exception ex)
                {
                    entity.AddError(ex.Message);
                    return(entity);
                }
            }

            return(_courseConfigurationRepository.Add(entity));
        }
        private void DisableCurrentConfiguration(Domain.Entities.CourseConfiguration entity)
        {
            var course = _courseRepository.Filter(new Domain.Entities.Course {
                Id = entity.CourseId
            }).FirstOrDefault();

            if (course != null)
            {
                if (course.RemovedDate != null)
                {
                    throw new Exception("Não é possível adicionar uma configuração a um curso desabilitado");
                }

                var configurations = _courseConfigurationRepository.GetByCourse(entity.CourseId);

                foreach (var configuration in configurations)
                {
                    configuration.IsCurrentConfiguration = false;
                }

                course.CurrentConfiguration = entity;
                _courseRepository.Update(course);
            }
        }