Пример #1
0
        public async Task <IActionResult> Update(
            int id,
            [FromBody] ProposalRegistration registration)
        {
            //if(id == Guid.Empty)
            //{
            //    return NotFound();
            //}

            if (registration == null)
            {
                return(BadRequest("Proposal registration not given"));
            }

            if (!ModelState.IsValid)
            {
                return(BadRequest("The given proposal registration is invalid"));
            }

            var result = await _proposalUpdater.Update(id, registration);

            if (result.Successful())
            {
                return(Ok(result.Body()));
            }
            return(BadRequest(result.GetAggregatedErrors()));
        }
Пример #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));
        }
Пример #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));
        }
Пример #4
0
        public async Task <IActionResult> Create([FromBody] ProposalRegistration registration)
        {
            if (registration == null)
            {
                return(BadRequest("Proposal registration not given"));
            }

            if (!ModelState.IsValid)
            {
                return(BadRequest("The given proposal registration is invalid"));
            }

            var result = await _proposalCreator.Create(registration);

            if (result.Successful())
            {
                return(Ok(result.Body()));
            }
            return(BadRequest(result.GetAggregatedErrors()));
        }
Пример #5
0
        public async Task <IServiceResult <bool> > IsStudentGroupValidFor(
            ProposalRegistration registration)
        {
            var indexNumbers = registration.Students;

            if (indexNumbers == null)
            {
                return(ServiceResult <bool> .Success(true));
            }

            var numOfStudents = indexNumbers.Count();

            if (numOfStudents > registration.MaxNumberOfStudents)
            {
                return(ServiceResult <bool> .Error("The number of students exceeds the maximal number"));
            }
            if (numOfStudents != indexNumbers.Distinct().Count())
            {
                return(ServiceResult <bool> .Error("Some of the given student index numbers are the same"));
            }

            foreach (var indexNumber in indexNumbers)
            {
                var student = await _context.Students.AsNoTracking().FirstOrDefaultAsync(s => s.IndexNumber == indexNumber);

                if (student == null)
                {
                    return(ServiceResult <bool> .Error($"The student with index number {indexNumber} does not exist"));
                }
                if (student.StudyLevel != registration.Level)
                {
                    return(ServiceResult <bool> .Error($"The student with index number {indexNumber} does not match to the given study level"));
                }
                if (student.StudyMode != registration.Mode)
                {
                    return(ServiceResult <bool> .Error($"The student with index number {indexNumber} does not match to the given study mode"));
                }
            }
            return(ServiceResult <bool> .Success(true));
        }
Пример #6
0
        private ProposalStatus CalculateProposalStatus(ProposalRegistration registration)
        {
            var students            = registration.Students;
            var maxNumberOfStudents = registration.MaxNumberOfStudents;

            if (students == null || students.Count() == 0)
            {
                return(ProposalStatus.Free);
            }
            else if (students.Count() < maxNumberOfStudents)
            {
                return(ProposalStatus.PartiallyTaken);
            }
            else if (students.Count() == maxNumberOfStudents)
            {
                return(ProposalStatus.Taken);
            }
            else
            {
                return(ProposalStatus.Overloaded);
            }
        }
Пример #7
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));
        }