コード例 #1
0
        /// <exception cref="ServiceException"/>
        /// <exception cref="NotFoundException"/>
        public async Task UpdateAsync(Dtos.Student dto)
        {
            if (dto.Id.Equals(Guid.Empty))
            {
                throw new InvalidModelException("Property Id failed validation. Error was: Id is empty");
            }

            var validator           = new Dtos.StudentValidator();
            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 student = await _context.Set <Entities.Student>()
                          .Include(x => x.UserInfo)
                          .FirstOrDefaultAsync(t => t.Id == dto.Id);

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

            if (!await _context.Set <Entities.AcademicGroup>().AnyAsync(d => d.Id == dto.AcademicGroupId))
            {
                throw new InvalidModelException($"Academic group with id: {dto.AcademicGroupId} not found");
            }

            if (dto.EducationProgramId != null && !await _context.Set <Entities.EducationProgram>().AnyAsync(d => d.Id == dto.EducationProgramId))
            {
                throw new InvalidModelException($"Education program with id: {dto.EducationProgramId} 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");
            }

            if (!await _context.Set <Entities.FormOfEducation>().AnyAsync(d => d.Id == dto.FormOfEducationId))
            {
                throw new InvalidModelException($"Form of education with id: {dto.FormOfEducationId} not found");
            }

            if (dto.PrivilegeId != null && !await _context.Set <Entities.Privilege>().AnyAsync(d => d.Id == dto.PrivilegeId))
            {
                throw new InvalidModelException($"Privilege with id: {dto.PrivilegeId} not found");
            }

            student.CreatedAt                  = dto.CreatedAt;
            student.Id                         = dto.Id;
            student.UpdatedAt                  = dto.UpdatedAt;
            student.StudentTicketNumber        = dto.StudentTicketNumber;
            student.Sex                        = dto.Sex;
            student.PrivilegeId                = dto.PrivilegeId;
            student.NumberOfRecordBook         = dto.NumberOfRecordBook;
            student.MilitaryRegistration       = dto.MilitaryRegistration;
            student.ForeignLanguage            = dto.ForeignLanguage;
            student.Financing                  = dto.Financing;
            student.FormOfEducationId          = dto.FormOfEducationId;
            student.EntryDate                  = dto.EntryDate;
            student.AcademicGroupId            = dto.AcademicGroupId;
            student.AcceleratedFormOfEducation = dto.AcceleratedFormOfEducation;
            student.AddressOfResidence         = dto.AddressOfResidence;
            student.Chummery                   = dto.Chummery;
            student.EducationLevelId           = dto.EducationLevelId;
            student.EducationProgramId         = dto.EducationProgramId;
            student.EndDate                    = dto.EndDate;

            student.UserInfo.DateOfBirth  = dto.DateOfBirth;
            student.UserInfo.Email        = dto.Email;
            student.UserInfo.FirstName    = dto.FirstName;
            student.UserInfo.FirstNameEng = dto.FirstNameEng;
            student.UserInfo.LastName     = dto.LastName;
            student.UserInfo.LastNameEng  = dto.LastNameEng;
            student.UserInfo.Patronymic   = dto.Patronymic;
            student.UserInfo.PhoneNumber  = dto.PhoneNumber;
            student.UpdatedAt             = DateTime.UtcNow;

            await _context.SaveChangesAsync();
        }
コード例 #2
0
        /// <exception cref="InvalidModelException"/>
        /// <exception cref="NotFoundException"/>
        public async Task <Guid> AddAsync(Dtos.Student dto)
        {
            var validator           = new Dtos.StudentValidator();
            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.AcademicGroup>().AnyAsync(d => d.Id == dto.AcademicGroupId))
            {
                throw new InvalidModelException($"Academic group with id: {dto.AcademicGroupId} not found");
            }

            if (dto.EducationProgramId != null && !await _context.Set <Entities.EducationProgram>().AnyAsync(d => d.Id == dto.EducationProgramId))
            {
                throw new InvalidModelException($"Education program with id: {dto.EducationProgramId} 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");
            }

            if (!await _context.Set <Entities.FormOfEducation>().AnyAsync(d => d.Id == dto.FormOfEducationId))
            {
                throw new InvalidModelException($"Form of education with id: {dto.FormOfEducationId} not found");
            }

            if (dto.PrivilegeId != null && !await _context.Set <Entities.Privilege>().AnyAsync(d => d.Id == dto.PrivilegeId))
            {
                throw new InvalidModelException($"Privilege with id: {dto.PrivilegeId} 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 student = new Entities.Student
            {
                Id                         = id,
                CreatedAt                  = now,
                UpdatedAt                  = now,
                AcademicGroupId            = dto.AcademicGroupId,
                AcceleratedFormOfEducation = dto.AcceleratedFormOfEducation,
                AddressOfResidence         = dto.AddressOfResidence,
                Chummery                   = dto.Chummery,
                EducationProgramId         = dto.EducationProgramId,
                EducationLevelId           = dto.EducationLevelId,
                EndDate                    = dto.EndDate,
                EntryDate                  = dto.EntryDate,
                FormOfEducationId          = dto.FormOfEducationId,
                Financing                  = dto.Financing,
                ForeignLanguage            = dto.ForeignLanguage,
                MilitaryRegistration       = dto.MilitaryRegistration,
                NumberOfRecordBook         = dto.NumberOfRecordBook,
                PrivilegeId                = dto.PrivilegeId,
                Sex                        = dto.Sex,
                StudentTicketNumber        = dto.StudentTicketNumber,
                UserInfoId                 = uiId
            };

            await _context.AddAsync(student);

            await _context.SaveChangesAsync();

            return(id);
        }