Beispiel #1
0
        public async Task <IServiceResult <User> > CreateUser(
            string email,
            string password,
            RoleType[] roles)
        {
            if (await EmailExists(email))
            {
                return(ServiceResult <User> .Error(
                           $"Email {email} is already taken"));
            }

            var roleNames = roles.Select(r => GetRoleName(r));

            foreach (var roleName in roleNames)
            {
                if (!(await _roleManager.RoleExistsAsync(roleName)))
                {
                    return(ServiceResult <User> .Error(
                               $"Role {roleName} does not exist"));
                }
            }

            var user = new User
            {
                //Id = Guid.NewGuid(),
                UserName           = email,
                NormalizedUserName =
                    new UpperInvariantLookupNormalizer()
                    .Normalize(email)
                    .ToUpperInvariant(),
                Email           = email,
                NormalizedEmail =
                    new UpperInvariantLookupNormalizer()
                    .Normalize(email)
                    .ToUpperInvariant(),
                EmailConfirmed = true,
                PasswordHash   = new PasswordHasher <User>().HashPassword(null, password),
            };

            user.SecurityStamp = _tokenGenerator.GenerateTokenFor(user);

            await _userManager.CreateAsync(user);

            await _userManager.AddToRolesAsync(user, roleNames);

            await _context.SaveChangesAsync();

            return(ServiceResult <User> .Success(user));
        }
Beispiel #2
0
        public async Task <IServiceResult <FacultyViewModel> > Update(
            int id,
            FacultyRegistration newData)
        {
            var faculty = _context.Faculties.FirstOrDefault(f => f.Id == id);

            if (faculty == null)
            {
                return(ServiceResult <FacultyViewModel> .Error(
                           $"Faculty with id {id} does not exist"));
            }

            if (FacultyNameTaken(faculty.Id, newData.Name))
            {
                return(ServiceResult <FacultyViewModel> .Error(
                           $"Faculty name {newData.Name} already exists"
                           ));
            }

            faculty = _mapper.Map(newData, faculty);

            _context.Faculties.Update(faculty);
            await _context.SaveChangesAsync();

            var facultyViewModel = _mapper.Map <FacultyViewModel>(faculty);

            return(ServiceResult <FacultyViewModel> .Success(facultyViewModel));
        }
Beispiel #3
0
        public async Task <IServiceResult <CourseViewModel> > Create(CourseRegistration registration)
        {
            var faculty = await _context
                          .Faculties
                          .Include(f => f.Courses)
                          .FirstOrDefaultAsync(f => f.Id == registration.FacultyId);

            if (faculty == null)
            {
                return(ServiceResult <CourseViewModel> .Error(
                           $"Faculty with id {registration.FacultyId} does not exist"
                           ));
            }

            if (IsCourseNameOnFacultyTaken(registration.Name, faculty))
            {
                return(ServiceResult <CourseViewModel> .Error(
                           $"Faculty with id {faculty.Id} already has a course with name {registration.Name}"
                           ));
            }

            var course = _mapper.Map <Course>(registration);

            await _context.Courses.AddAsync(course);

            await _context.SaveChangesAsync();

            var courseViewModel = _mapper.Map <CourseViewModel>(course);

            return(ServiceResult <CourseViewModel> .Success(courseViewModel));
        }
Beispiel #4
0
        public async Task <IServiceResult <PromoterViewModel> > Update(
            int id,
            PromoterUpdate newData)
        {
            var existingPromoter =
                await _context
                .Promoters
                .Include(p => p.ApplicationUser)
                .FirstOrDefaultAsync(p => p.Id == id);

            if (existingPromoter == null)
            {
                return(ServiceResult <PromoterViewModel> .Error(
                           $"Promoter with id {id} does not exist"));
            }

            existingPromoter = _mapper.Map(newData, existingPromoter);

            _context.Promoters.Update(existingPromoter);
            await _context.SaveChangesAsync();

            var promoterViewModel = _mapper.Map <PromoterViewModel>(existingPromoter);

            return(ServiceResult <PromoterViewModel> .Success(promoterViewModel));
        }
Beispiel #5
0
        public async Task <IServiceResult <InstituteViewModel> > Update(
            int id,
            InstituteRegistration newData)
        {
            var institute = _context.Institutes.FirstOrDefault(i => i.Id == id);

            if (institute == null)
            {
                return(ServiceResult <InstituteViewModel> .Error(
                           $"Institute with id {id} does not exist"));
            }

            if (IsNameTakenByAnotherInstitute(institute.Id, newData.Name))
            {
                return(ServiceResult <InstituteViewModel> .Error(
                           $"Institute name {newData.Name} already exists"
                           ));
            }

            institute = _mapper.Map(newData, institute);

            _context.Institutes.Update(institute);
            await _context.SaveChangesAsync();

            var instituteViewModel = _mapper.Map <InstituteViewModel>(institute);

            return(ServiceResult <InstituteViewModel> .Success(instituteViewModel));
        }
Beispiel #6
0
        private async Task <User> CreateUserFrom(EKontoUser eKontoUser, IEnumerable <string> roleNames)
        {
            var user = new User();

            user               = _mapper.Map(eKontoUser, user);
            user.Id            = eKontoUser.id;
            user.SecurityStamp = "";

            _context.Users.Add(user);
            await _context.SaveChangesAsync();

            await _userManager.AddToRolesAsync(user, roleNames);

            await SetSecurityStampAsync(user, roleNames);

            return(user);
        }
Beispiel #7
0
        public async Task <IServiceResult <ProposalViewModel> > Update(
            int id,
            ProposalRegistration inputData)
        {
            var proposal = _context.Proposals.FirstOrDefault(p => p.Id == id);

            if (proposal == null)
            {
                return(ServiceResult <ProposalViewModel> .Error(
                           $"Proposal with id {id} does not exist"
                           ));
            }

            var courseResult = await _courseGetter.Get(inputData.CourseId);

            if (!courseResult.Successful())
            {
                return(ServiceResult <ProposalViewModel> .Error(courseResult.GetAggregatedErrors()));
            }

            var userResult = await _userGetter.GetCurrentUser();

            if (!userResult.Successful())
            {
                var errors = userResult.GetAggregatedErrors();
                return(ServiceResult <ProposalViewModel> .Error(errors));
            }

            var currentUser = userResult.Body();
            var promoter    =
                await _context
                .Promoters
                .FirstOrDefaultAsync(p => p.UserId == currentUser.Id);

            if (promoter == null)
            {
                return(ServiceResult <ProposalViewModel> .Error("The current user has no associated promoter"));
            }

            if (proposal.PromoterId != promoter.Id)
            {
                return(ServiceResult <ProposalViewModel> .Error(
                           $"Promoter with id {promoter.Id} has no proposal with id {id}"
                           ));
            }

            proposal = _mapper.Map(inputData, proposal);
            if (proposal.Status == ProposalStatus.Overloaded)
            {
                return(ServiceResult <ProposalViewModel> .Error("The number of students exceeds the maximal number"));
            }
            _context.Proposals.Update(proposal);
            await _context.SaveChangesAsync();

            var proposalViewModels = _mapper.Map <ProposalViewModel>(proposal);

            return(ServiceResult <ProposalViewModel> .Success(proposalViewModels));
        }
Beispiel #8
0
        public async Task <IServiceResult <ProposalViewModel> > Create(
            ProposalRegistration inputData)
        {
            var courseResult = await _courseGetter.Get(inputData.CourseId);

            if (!courseResult.Successful())
            {
                return(ServiceResult <ProposalViewModel> .Error(courseResult.GetAggregatedErrors()));
            }

            var userResult = await _userGetter.GetCurrentUser();

            if (!userResult.Successful())
            {
                var errors = userResult.GetAggregatedErrors();
                return(ServiceResult <ProposalViewModel> .Error(errors));
            }

            var currentUser = userResult.Body();
            var promoter    =
                await _context
                .Promoters
                .Include(p => p.Proposals)
                .FirstOrDefaultAsync(p => p.UserId == currentUser.Id);

            if (promoter == null)
            {
                return(ServiceResult <ProposalViewModel> .Error("The current user has no associated promoter"));
            }

            if (!HasPermissionToCreateProposal(promoter, inputData.Level))
            {
                return(ServiceResult <ProposalViewModel> .Error("You are not allowed to create this type of proposal"));
            }

            var proposal = _mapper.Map <Proposal>(inputData);

            if (proposal.Status == ProposalStatus.Overloaded)
            {
                return(ServiceResult <ProposalViewModel> .Error("The number of students exceeds the maximal number"));
            }

            SetStartDate(proposal);
            proposal.Promoter = promoter;
            promoter.Proposals.Add(proposal);
            _context.Promoters.Update(promoter);

            await _context.Proposals.AddAsync(proposal);

            await _context.SaveChangesAsync();

            var proposalViewModel = _mapper.Map <ProposalViewModel>(proposal);

            return(ServiceResult <ProposalViewModel> .Success(proposalViewModel));
        }
Beispiel #9
0
        public async Task <IServiceResult <PromoterViewModel> > Update(
            int id,
            PromoterUpdateViewModel newData)
        {
            var existingPromoter =
                await _context
                .Promoters
                .FirstOrDefaultAsync(p => p.Id == id);

            if (existingPromoter == null)
            {
                return(ServiceResult <PromoterViewModel> .Error(
                           $"Promoter with id {id} does not exist"));
            }

            //var instituteResult = await _instituteGetter.Get(newData.InstituteId);
            //if(!instituteResult.Successful())
            //{
            //    return ServiceResult<PromoterViewModel>.Error(instituteResult.GetAggregatedErrors());
            //}

            //var credentials = new UserCredentials
            //{
            //    Email = newData.Email,
            //    Password = newData.Password
            //};

            //var result = await _userUpdater.Update(
            //    existingPromoter.UserId,
            //    credentials,
            //    new RoleType[] {
            //        RoleType.Promoter
            //    });

            //if (!result.Successful())
            //{
            //    var errors = result.GetAggregatedErrors();
            //    return ServiceResult<PromoterViewModel>.Error(errors);
            //}

            existingPromoter = _mapper.Map(newData, existingPromoter);

            _context.Promoters.Update(existingPromoter);
            await _context.SaveChangesAsync();

            var promoterViewModel = _mapper.Map <PromoterViewModel>(existingPromoter);

            return(ServiceResult <PromoterViewModel> .Success(promoterViewModel));
        }
Beispiel #10
0
        public async Task <IServiceResult <User> > Update(
            int id,
            UserCredentials credentials,
            RoleType[] roles)
        {
            var existingUser = await _userManager.FindByIdAsync(id.ToString());

            if (existingUser == null)
            {
                return(ServiceResult <User> .Error(
                           $"User with id {id} does not exist"));
            }

            var isEmailTakenBySomeOneElse = await IsEmailTakenBySomeoneElse(existingUser.Id, credentials.Email);

            if (isEmailTakenBySomeOneElse)
            {
                return(ServiceResult <User> .Error(
                           $"Email {credentials.Email} is already taken by another user"
                           ));
            }

            var roleNames = roles.Select(r => GetRoleName(r));

            foreach (var roleName in roleNames)
            {
                var roleExists = await _roleManager.RoleExistsAsync(roleName);

                if (!roleExists)
                {
                    return(ServiceResult <User> .Error(
                               $"Role {roleName} does not exist"));
                }
            }

            UpdateCredentialsOf(existingUser, credentials);

            var currentRoles = await _userManager.GetRolesAsync(existingUser);

            await _userManager.RemoveFromRolesAsync(existingUser, currentRoles);

            await _userManager.AddToRolesAsync(existingUser, roleNames);

            await _userManager.UpdateAsync(existingUser);

            await _context.SaveChangesAsync();

            return(ServiceResult <User> .Success(existingUser));
        }
Beispiel #11
0
        public async Task <IServiceResult <StudentViewModel> > Create(StudentRegistration registration)
        {
            if (registration.ProposalId != null)
            {
                var proposal = await _context.Proposals.AsNoTracking().FirstOrDefaultAsync(p => p.Id == registration.ProposalId);

                if (proposal == null)
                {
                    return(ServiceResult <StudentViewModel> .Error(
                               $"The proposal with id {registration.ProposalId} does not exist"
                               ));
                }
            }

            if (await IsIndexNumberTaken(registration.IndexNumber))
            {
                return(ServiceResult <StudentViewModel> .Error(
                           $"Index number {registration.IndexNumber} is already taken"
                           ));
            }

            var userResult =
                await _userCreator
                .CreateUserWithId(
                    registration.Email,
                    registration.Password,
                    new RoleType[] {
                RoleType.Student
            });

            if (!userResult.Successful())
            {
                return(ServiceResult <StudentViewModel> .Error(userResult.GetAggregatedErrors()));
            }

            //var user = userResult.Body();
            var student = _mapper.Map <Student>(registration);

            //student.Id = Guid.NewGuid();
            student.UserId = userResult.Body();

            _context.Students.Add(student);
            await _context.SaveChangesAsync();

            var studentViewModel = _mapper.Map <StudentViewModel>(student);

            return(ServiceResult <StudentViewModel> .Success(studentViewModel));
        }
Beispiel #12
0
        public async Task <IServiceResult <CourseViewModel> > Delete(int id)
        {
            var course = await _context.Courses.FirstOrDefaultAsync(c => c.Id == id);

            if (course == null)
            {
                return(ServiceResult <CourseViewModel> .Error(
                           $"Course with id {id} does not exist"));
            }

            var courseViewModel = _mapper.Map <CourseViewModel>(course);

            _context.Courses.Remove(course);
            await _context.SaveChangesAsync();

            return(ServiceResult <CourseViewModel> .Success(courseViewModel));
        }
Beispiel #13
0
        public async Task <IServiceResult <InstituteViewModel> > Delete(int id)
        {
            var institute = await _context.Institutes.FirstOrDefaultAsync(i => i.Id == id);

            if (institute == null)
            {
                return(ServiceResult <InstituteViewModel> .Error(
                           $"Institute with id {id} does not exist"));
            }

            var instituteViewModel = _mapper.Map <InstituteViewModel>(institute);

            _context.Institutes.Remove(institute);
            await _context.SaveChangesAsync();

            return(ServiceResult <InstituteViewModel> .Success(instituteViewModel));
        }
Beispiel #14
0
        public async Task <IServiceResult <FacultyViewModel> > Delete(int id)
        {
            var faculty = await _context.Faculties.FirstOrDefaultAsync(f => f.Id == id);

            if (faculty == null)
            {
                return(ServiceResult <FacultyViewModel> .Error(
                           $"Faculty with id {id} does not exist"));
            }

            var facultyViewModel = _mapper.Map <FacultyViewModel>(faculty);

            _context.Faculties.Remove(faculty);
            await _context.SaveChangesAsync();

            return(ServiceResult <FacultyViewModel> .Success(facultyViewModel));
        }
Beispiel #15
0
        public async Task<IServiceResult<FacultyViewModel>> Create(FacultyRegistration registration)
        {
            if(FacultyExists(registration.Name))
            {
                return ServiceResult<FacultyViewModel>.Error(
                    $"Faculty name {registration.Name} already exists"
                );
            }

            var faculty = _mapper.Map<Faculty>(registration);

            await _context.Faculties.AddAsync(faculty);
            await _context.SaveChangesAsync();

            var facultyViewModel = _mapper.Map<FacultyViewModel>(faculty);
            return ServiceResult<FacultyViewModel>.Success(facultyViewModel);
        }
Beispiel #16
0
        public async Task <IServiceResult <ProposalViewModel> > Delete(int id)
        {
            var proposal = await _context
                           .Proposals
                           .Include(p => p.Students)
                           .FirstOrDefaultAsync(p => p.Id == id);

            if (proposal == null)
            {
                return(ServiceResult <ProposalViewModel> .Error(
                           $"Proposal with id {id} does not exist"
                           ));
            }

            var userResult = await _userGetter.GetCurrentUser();

            if (!userResult.Successful())
            {
                var errors = userResult.GetAggregatedErrors();
                return(ServiceResult <ProposalViewModel> .Error(errors));
            }

            var currentUser = userResult.Body();
            var promoter    =
                await _context
                .Promoters
                .FirstOrDefaultAsync(p => p.UserId == currentUser.Id);

            if (promoter == null)
            {
                return(ServiceResult <ProposalViewModel> .Error("The current user has no associated promoter"));
            }

            if (proposal.PromoterId != promoter.Id)
            {
                return(ServiceResult <ProposalViewModel> .Error(
                           $"Promoter with id {promoter.Id} has no proposal with id {id}"));
            }

            var proposalViewModel = _mapper.Map <ProposalViewModel>(proposal);

            _context.Proposals.Remove(proposal);
            await _context.SaveChangesAsync();

            return(ServiceResult <ProposalViewModel> .Success(proposalViewModel));
        }
Beispiel #17
0
        public async Task <IServiceResult <InstituteViewModel> > Create(InstituteRegistration registration)
        {
            if (InstituteExists(registration.Name))
            {
                return(ServiceResult <InstituteViewModel> .Error(
                           $"Institute name {registration.Name} already exists"
                           ));
            }

            var institute = _mapper.Map <Institute>(registration);

            await _context.Institutes.AddAsync(institute);

            await _context.SaveChangesAsync();

            var instituteViewModel = _mapper.Map <InstituteViewModel>(institute);

            return(ServiceResult <InstituteViewModel> .Success(instituteViewModel));
        }
Beispiel #18
0
        public async Task <IServiceResult <CourseViewModel> > Update(
            int id,
            CourseRegistration newData)
        {
            var course = _context.Courses.FirstOrDefault(c => c.Id == id);

            if (course == null)
            {
                return(ServiceResult <CourseViewModel> .Error(
                           $"Course with id {id} does not exist"));
            }

            var faculty = await _context
                          .Faculties
                          .Include(f => f.Courses)
                          .FirstOrDefaultAsync(f => f.Id == newData.FacultyId);

            if (faculty == null)
            {
                return(ServiceResult <CourseViewModel> .Error(
                           $"Faculty with id {newData.FacultyId} does not exist"
                           ));
            }

            if (IsAnotherSuchCourseNameOnFaculty(course.Id, newData.Name, faculty))
            {
                return(ServiceResult <CourseViewModel> .Error(
                           $"Faculty with id {faculty.Id} already has a course with name {newData.Name}"
                           ));
            }

            course = _mapper.Map(newData, course);

            _context.Courses.Update(course);
            await _context.SaveChangesAsync();

            var courseViewModel = _mapper.Map <CourseViewModel>(course);

            return(ServiceResult <CourseViewModel> .Success(courseViewModel));
        }
Beispiel #19
0
        public async Task <IServiceResult <PromoterViewModel> > Create(
            PromoterRegistration registration)
        {
            //var instituteResult = await _instituteGetter.IsExists(registration.InstituteId);
            if (!(await _instituteGetter.IsExists(registration.InstituteId)))
            {
                return(ServiceResult <PromoterViewModel> .Error($"Institute with id {registration.InstituteId} does not exist"));
            }

            var userResult =
                await _userCreator
                .CreateUserWithId(
                    registration.Email,
                    registration.Password,
                    new RoleType[] {
                RoleType.Promoter
            });

            if (!userResult.Successful())
            {
                return(ServiceResult <PromoterViewModel> .Error(userResult.GetAggregatedErrors()));
            }

            //var user = userResult.Body();
            var promoter = _mapper.Map <Promoter>(registration);

            //promoter.Id = Guid.NewGuid();
            promoter.UserId = userResult.Body();

            _context.Promoters.Add(promoter);
            await _context.SaveChangesAsync();

            var promoterViewModel = _mapper.Map <PromoterViewModel>(promoter);

            return(ServiceResult <PromoterViewModel> .Success(promoterViewModel));
        }
Beispiel #20
0
        public async Task <IServiceResult <StudentViewModel> > Update(
            int id,
            StudentRegistration newData)
        {
            if (newData.ProposalId != null)
            {
                var proposal = await _context.Proposals.FirstOrDefaultAsync(p => p.Id == newData.ProposalId);

                if (proposal == null)
                {
                    return(ServiceResult <StudentViewModel> .Error(
                               $"The proposal with id {newData.ProposalId} does not exist"
                               ));
                }
            }

            var isIndexNumberTaken = await IsIndexNumberTaken(id, newData.IndexNumber);

            if (isIndexNumberTaken)
            {
                return(ServiceResult <StudentViewModel> .Error(
                           $"Index number {newData.IndexNumber} is already taken"
                           ));
            }

            var existingStudent =
                await _context
                .Students
                .FirstOrDefaultAsync(p => p.Id == id);

            if (existingStudent == null)
            {
                return(ServiceResult <StudentViewModel> .Error(
                           $"Student with id {id} does not exist"));
            }

            var credentials = new UserCredentials
            {
                Email    = newData.Email,
                Password = newData.Password
            };

            var result = await _userUpdater.Update(
                existingStudent.UserId,
                credentials,
                new RoleType[] {
                RoleType.Student
            });

            if (!result.Successful())
            {
                var errors = result.GetAggregatedErrors();
                return(ServiceResult <StudentViewModel> .Error(errors));
            }

            existingStudent = _mapper.Map(newData, existingStudent);

            _context.Students.Update(existingStudent);
            await _context.SaveChangesAsync();

            var studentViewModel = _mapper.Map <StudentViewModel>(existingStudent);

            return(ServiceResult <StudentViewModel> .Success(studentViewModel));
        }
Beispiel #21
0
        public async Task <IServiceResult <ProposalViewModel> > Update(
            int id,
            ProposalRegistration inputData)
        {
            var proposal = _context.Proposals.Include(p => p.Students).FirstOrDefault(p => p.Id == id);

            if (proposal == null)
            {
                return(ServiceResult <ProposalViewModel> .Error(
                           $"Proposal with id {id} does not exist"
                           ));
            }

            var courseResult = await _courseGetter.Get(inputData.CourseId);

            if (!courseResult.Successful())
            {
                return(ServiceResult <ProposalViewModel> .Error(courseResult.GetAggregatedErrors()));
            }

            var studentGroupValidatorResult = await _studentGroupValidator.IsStudentGroupValidFor(inputData);

            if (!studentGroupValidatorResult.Successful())
            {
                return(ServiceResult <ProposalViewModel> .Error(studentGroupValidatorResult.GetAggregatedErrors()));
            }

            var isStudentGroupValid = studentGroupValidatorResult.Body();

            if (!isStudentGroupValid)
            {
                return(ServiceResult <ProposalViewModel> .Error("The given students are not valid"));
            }

            var userResult = await _userGetter.GetCurrentUser();

            if (!userResult.Successful())
            {
                var errors = userResult.GetAggregatedErrors();
                return(ServiceResult <ProposalViewModel> .Error(errors));
            }

            var currentUser = userResult.Body();
            var promoter    =
                await _context
                .Promoters
                .FirstOrDefaultAsync(p => p.UserId == currentUser.Id);

            if (promoter == null)
            {
                return(ServiceResult <ProposalViewModel> .Error("The current user has no associated promoter"));
            }

            if (proposal.PromoterId != promoter.Id)
            {
                return(ServiceResult <ProposalViewModel> .Error(
                           $"Promoter with id {promoter.Id} has no proposal with id {id}"
                           ));
            }

            var studentsResult = await _studentGetter.GetMany(inputData.Students);

            if (!studentsResult.Successful())
            {
                return(ServiceResult <ProposalViewModel> .Error(studentsResult.GetAggregatedErrors()));
            }
            var newStudents = studentsResult.Body();

            var proposalStatusResult = _proposalStatusGetter.CalculateProposalStatus(newStudents, inputData.MaxNumberOfStudents);

            if (!proposalStatusResult.Successful())
            {
                return(ServiceResult <ProposalViewModel> .Error(proposalStatusResult.GetAggregatedErrors()));
            }
            var proposalStatus = proposalStatusResult.Body();

            foreach (var student in proposal.Students)
            {
                student.ProposalId = null;
            }
            await _context.SaveChangesAsync();

            proposal          = _mapper.Map(inputData, proposal);
            proposal.Students = newStudents;
            proposal.Status   = proposalStatus;
            _context.Proposals.Update(proposal);
            await _context.SaveChangesAsync();

            var proposalViewModels = _mapper.Map <ProposalViewModel>(proposal);

            return(ServiceResult <ProposalViewModel> .Success(proposalViewModels));
        }