/// <exception cref="InvalidModelException"/> /// <exception cref="NotFoundException"/> public async Task UpdateAsync(Dtos.EducationProgram dto) { if (dto.Id.Equals(Guid.Empty)) { throw new InvalidModelException("Property Id failed validation. Error was: Id is empty"); } var validator = new Dtos.EducationProgramValidator(); 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); } if (!await _context.Set <Entities.Specialty>().AnyAsync(d => d.Id == dto.SpecialtyId)) { throw new InvalidModelException($"Specialty with id: {dto.SpecialtyId} not found"); } if (!await _context.Set <Entities.EducationLevel>().AnyAsync(d => d.Id == dto.EducationLevelId)) { throw new InvalidModelException($"Education level with id: {dto.EducationLevelId} not found"); } var educationProgram = await _context.FindAsync <Entities.EducationProgram>(dto.Id); if (educationProgram == null) { throw new NotFoundException($"Entity with id: {dto.Id} not found."); } educationProgram.UpdatedAt = DateTime.UtcNow; educationProgram.ApprovalYear = dto.ApprovalYear; educationProgram.DurationbOfEducation = dto.DurationOfEducation; educationProgram.EducationLevelId = dto.EducationLevelId; educationProgram.Guarantor = dto.Guarantor; educationProgram.Language = dto.Language; educationProgram.SpecialtyId = dto.SpecialtyId; await _context.SaveChangesAsync(); }
/// <exception cref="InvalidModelException"/> /// <exception cref="NotFoundException"/> public async Task <Guid> AddAsync(Dtos.EducationProgram dto) { var validator = new Dtos.EducationProgramValidator(); 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); } if (!await _context.Set <Entities.Specialty>().AnyAsync(d => d.Id == dto.SpecialtyId)) { throw new InvalidModelException($"Specialty with id: {dto.SpecialtyId} not found"); } if (!await _context.Set <Entities.EducationLevel>().AnyAsync(d => d.Id == dto.EducationLevelId)) { throw new InvalidModelException($"Education level with id: {dto.EducationLevelId} not found"); } var id = Guid.NewGuid(); var now = DateTime.UtcNow; var educationProgram = new Entities.EducationProgram { Id = id, CreatedAt = now, UpdatedAt = now, ApprovalYear = dto.ApprovalYear, DurationbOfEducation = dto.DurationOfEducation, EducationLevelId = dto.EducationLevelId, Guarantor = dto.Guarantor, Language = dto.Language, SpecialtyId = dto.SpecialtyId }; await _context.AddAsync(educationProgram); await _context.SaveChangesAsync(); return(id); }