/// <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);
        }
Ejemplo n.º 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);
        }
Ejemplo n.º 3
0
        public async Task <Guid> SignUpAsync(Dtos.User userDto, string password)
        {
            if (string.IsNullOrWhiteSpace(password))
            {
                throw new RegistrationException("Password is required");
            }

            var validator           = new Dtos.UserValidator();
            ValidationResult result = validator.Validate(userDto);

            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);
            }

            bool isUserExists = _context.Set <Entities.User>().Include(x => x.UserInfo)
                                .Any(x => x.UserInfo != null && x.UserInfo.Email == userDto.Email);

            if (isUserExists)
            {
                throw new ServiceException(System.Net.HttpStatusCode.Conflict, $"Email \"{userDto.Email}\" is already taken");
            }

            CreatePasswordHash(password, out byte[] passwordHash, out byte[] passwordSalt);

            var userInfo = new Entities.UserInfo
            {
                Id           = Guid.NewGuid(),
                DateOfBirth  = userDto.DateOfBirth,
                Email        = userDto.Email,
                FirstName    = userDto.FirstName,
                FirstNameEng = userDto.FirstNameEng,
                LastName     = userDto.LastName,
                LastNameEng  = userDto.LastNameEng,
                Patronymic   = userDto.Patronymic,
                PhoneNumber  = userDto.PhoneNumber
            };
            await _context.AddAsync(userInfo);

            var user = new Entities.User
            {
                PasswordHash = passwordHash,
                PasswordSalt = passwordSalt,
                CreatedAt    = DateTime.UtcNow,
                UpdatedAt    = DateTime.UtcNow,
                Id           = Guid.NewGuid(),
                UserInfo     = userInfo
            };

            var role = await _context.Set <Entities.Role>()
                       .FirstOrDefaultAsync(x => x.Name == AppRoles.User);

            if (role == null)
            {
                role = new Entities.Role
                {
                    Id        = Guid.NewGuid(),
                    Name      = AppRoles.User,
                    CreatedAt = DateTime.UtcNow,
                    UpdatedAt = DateTime.UtcNow
                };

                await _context.Set <Entities.Role>().AddAsync(role);
            }

            var userRole = new Entities.UserRoles
            {
                RoleId = role.Id,
                UserId = user.Id
            };

            await _context.Set <Entities.User>().AddAsync(user);

            await _context.Set <Entities.UserRoles>().AddAsync(userRole);

            await _context.SaveChangesAsync();

            return(user.Id);
        }