Ejemplo n.º 1
0
        public async Task <IServiceResult <IEnumerable <ProposalViewModel> > > GetMyProposals()
        {
            var userResult = await _userGetter.GetCurrentUser();

            if (!userResult.Successful())
            {
                var errors = userResult.GetAggregatedErrors();
                return(ServiceResult <IEnumerable <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 <IEnumerable <ProposalViewModel> > .Error("The current user has no associated promoter"));
            }

            var proposalViewModels = promoter
                                     .Proposals
                                     .Select(p => _mapper.Map <ProposalViewModel>(p));

            return(ServiceResult <IEnumerable <ProposalViewModel> > .Success(proposalViewModels));
        }
Ejemplo n.º 2
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));
        }
Ejemplo n.º 3
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));
        }
Ejemplo n.º 4
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));
        }
Ejemplo n.º 5
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));
        }