Beispiel #1
0
        /// <exception cref="InvalidModelException"/>
        /// <exception cref="NotFoundException"/>
        public async Task <Guid> AddAsync(Dtos.FormOfEducation dto)
        {
            var validator           = new Dtos.FormOfEducationValidator();
            ValidationResult result = validator.Validate(dto);

            if (!result.IsValid)
            {
                string errMess = string.Empty;

                foreach (var failure in result.Errors)
                {
                    errMess += $"Property { failure.PropertyName } failed validation. Error was: { failure.ErrorMessage }\n";
                }

                throw new InvalidModelException(errMess);
            }

            var id  = Guid.NewGuid();
            var now = DateTime.UtcNow;

            var formOfEducation = new Entities.FormOfEducation
            {
                Id        = id,
                CreatedAt = now,
                UpdatedAt = now,
                Name      = dto.Name
            };

            await _context.AddAsync(formOfEducation);

            await _context.SaveChangesAsync();

            return(id);
        }
Beispiel #2
0
        /// <exception cref="InvalidModelException"/>
        /// <exception cref="NotFoundException"/>
        public async Task UpdateAsync(Dtos.FormOfEducation dto)
        {
            if (dto.Id.Equals(Guid.Empty))
            {
                throw new InvalidModelException("Property Id failed validation. Error was: Id is empty");
            }

            var validator           = new Dtos.FormOfEducationValidator();
            ValidationResult result = validator.Validate(dto);

            if (!result.IsValid)
            {
                string errMess = string.Empty;

                foreach (var failure in result.Errors)
                {
                    errMess += $"Property { failure.PropertyName } failed validation. Error was: { failure.ErrorMessage }\n";
                }

                throw new InvalidModelException(errMess);
            }

            var formOfEducation = await _context.FindAsync <Entities.FormOfEducation>(dto.Id);

            if (formOfEducation == null)
            {
                throw new NotFoundException($"Entity with id: {dto.Id} not found.");
            }

            formOfEducation.UpdatedAt = DateTime.UtcNow;
            formOfEducation.Name      = dto.Name;

            await _context.SaveChangesAsync();
        }