/// <exception cref="ServiceException"/> /// <exception cref="NotFoundException"/> public async Task <Guid> AddAsync(Dtos.Teacher dto) { var validator = new Dtos.TeacherValidator(); 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 ServiceException(errMess); } if (!await _context.Set <Entities.Department>().AnyAsync(d => d.Id == dto.DepartmentId)) { throw new InvalidModelException($"Department with id: {dto.DepartmentId} not found"); } var id = Guid.NewGuid(); var uiId = Guid.NewGuid(); var now = DateTime.UtcNow; var userInfo = new Entities.UserInfo { DateOfBirth = dto.DateOfBirth, PhoneNumber = dto.PhoneNumber, Email = dto.Email, FirstName = dto.FirstName, FirstNameEng = dto.FirstNameEng, LastName = dto.LastName, LastNameEng = dto.LastNameEng, Patronymic = dto.Patronymic, Id = uiId }; await _context.AddAsync(userInfo); var teacher = new Entities.Teacher { Id = id, CreatedAt = now, UpdatedAt = now, AcademicRank = dto.AcademicRank, DepartmentId = dto.DepartmentId, Position = dto.Position, ScientificDegree = dto.ScientificDegree, TypeOfEmployment = dto.TypeOfEmployment, UserInfoId = uiId }; await _context.AddAsync(teacher); await _context.SaveChangesAsync(); return(id); }
/// <exception cref="ServiceException"/> /// <exception cref="NotFoundException"/> public async Task UpdateAsync(Dtos.Teacher dto) { if (dto.Id.Equals(Guid.Empty)) { throw new InvalidModelException("Property Id failed validation. Error was: Id is empty"); } var validator = new Dtos.TeacherValidator(); 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 ServiceException(errMess); } if (!await _context.Set <Entities.Department>().AnyAsync(d => d.Id == dto.DepartmentId)) { throw new InvalidModelException($"Department with id: {dto.DepartmentId} not found"); } var teacher = await _context.Set <Entities.Teacher>() .Include(x => x.UserInfo) .FirstOrDefaultAsync(t => t.Id == dto.Id); if (teacher == null) { throw new NotFoundException($"Entity with id: {dto.Id} not found."); } teacher.UpdatedAt = DateTime.UtcNow; teacher.AcademicRank = dto.AcademicRank; teacher.DepartmentId = dto.DepartmentId; teacher.Position = dto.Position; teacher.ScientificDegree = dto.ScientificDegree; teacher.TypeOfEmployment = dto.TypeOfEmployment; teacher.UserInfo.DateOfBirth = dto.DateOfBirth; teacher.UserInfo.Email = dto.Email; teacher.UserInfo.FirstName = dto.FirstName; teacher.UserInfo.FirstNameEng = dto.FirstNameEng; teacher.UserInfo.LastName = dto.LastName; teacher.UserInfo.LastNameEng = dto.LastNameEng; teacher.UserInfo.Patronymic = dto.Patronymic; teacher.UserInfo.PhoneNumber = dto.PhoneNumber; await _context.SaveChangesAsync(); }