public async Task <SendRenewContractRequestResponse> SendRenewContract(SendRenewContractRequestRequest request)
        {
            var acceptableMonths = (await _paramService.FindAllByParamType(GlobalParams.ParamTypeContractParam)).Select(p => p.Value).ToList();

            if (!acceptableMonths.Contains(request.Month))
            {
                throw new HttpStatusCodeException(HttpStatusCode.Forbidden, "RenewContractService: Month is invalid");
            }

            var student = await _studentService.FindById(request.StudentId);

            //Check if student has room
            if (student.RoomId == null)
            {
                throw new HttpStatusCodeException(HttpStatusCode.Forbidden, "RenewContractService: Student doesn't have room");
            }

            //Check student's evaluation point is enough
            var contractRenewalEvaluationScoreMargin = (await
                                                        _paramService.FindById(GlobalParams.ParamContractRenewalEvaluationPointMargin))?.Value ?? GlobalParams.DefaultContractRenewalEvaluationPointMargin;

            if (student.EvaluationScore < contractRenewalEvaluationScoreMargin)
            {
                throw new HttpStatusCodeException(HttpStatusCode.Forbidden, "RenewContractService: Student's evaluation is not enough");
            }

            var contracts = (List <Contract>)
                            await _repoWrapper.Contract.FindAllAsyncWithCondition(c => c.StudentId == student.StudentId && c.Status == ContractStatus.Active);

            if (contracts == null || !contracts.Any())
            {
                throw new HttpStatusCodeException(HttpStatusCode.NotFound, "RenewContractService: Student doesn't have any active contract'");
            }

            var renewContract = SendRenewContractRequestRequest.EntityFromRequest(request, contracts[0]);

            renewContract = await _repoWrapper.RenewContract.CreateAsync(renewContract);

            return(SendRenewContractRequestResponse.ResponseFromEntity(renewContract));
        }
예제 #2
0
        public async Task <ActionResult <SendRenewContractRequestResponse> > SendRenewContract(SendRenewContractRequestRequest request)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            return(await _contractService.SendRenewContract(request));
        }