예제 #1
0
        public override async Task <StudentInfoReply> GetInfo(StudentInfoRequest request, ServerCallContext context)
        {
            _logger.LogInformation("Get student information with {0} id", request.UserId);
            var student =
                await _studentService.GetAsync <StudentDto>(x => x.UserId == request.UserId, new CancellationToken());

            var user = await _userGrpcService.GetAsync(student.UserId);

            var currentSemester = await _semesterService.GetAsync(x => x.ActivatedAt != null);

            var(curriculumSchedule, canTakeCurriculums) = await _curriculumScheduleService.GetCurrentScheduleAsync(
                currentSemester.Id, student.Semester.IntegerTitle,
                student.Field.FieldGroupId);

            student = user.MapTo(student);
            return(new StudentInfoReply
            {
                Code = student.Code,
                Id = student.Id,
                FieldId = student.Field.Id,
                CreateTime = student.CreateTime.ToString(CultureInfo.InvariantCulture),
                FieldTitle = student.Field.Title,
                FullName = student.FullName,
                UserId = student.UserId,
                CurrentSemesterId = currentSemester.Id,
                CurrentSemesterTitle = currentSemester.Title,
                CanTakeCurriculums = canTakeCurriculums,
                EndCurriculumScheduleDateTime = canTakeCurriculums
                    ? curriculumSchedule.End.ToString(CultureInfo.InvariantCulture)
                    : "",
                StartCurriculumScheduleDateTime = canTakeCurriculums
                    ? curriculumSchedule.Start.ToString(CultureInfo.InvariantCulture)
                    : ""
            });
        }
        public async Task Consume(ConsumeContext <ICurriculumAddedRequest> context)
        {
            _logger.LogInformation("process adding curriculum {0} to user {1}", context.Message.CurriculumId,
                                   context.Message.UserId);

            var curriculum =
                await _curriculumService.GetAsync <CurriculumDto>(context.Message.CurriculumId);

            var student = await _studentService.GetAsync <StudentDto>(context.Message.StudentId);

            if (curriculum == null || student == null)
            {
                _logger.LogInformation("student or curriculum was not found");
                return;
            }

            var currentSemester = await _semesterService.GetAsync(x => x.ActivatedAt != null);

            var(curriculumSchedule, canTakeCurriculums) = await _curriculumScheduleService.GetCurrentScheduleAsync(
                currentSemester.Id, student.Semester.IntegerTitle,
                student.Field.FieldGroupId);

            if (!canTakeCurriculums)
            {
                _logger.LogInformation("it's not time of adding curriculums");
                return;
            }

            if (currentSemester.Title != curriculum.Semester.Title)
            {
                _logger.LogInformation("You're not allowed to add curriculums from previous semesters");
                return;
            }


            if (curriculum.IsCapacityCompleted)
            {
                _logger.LogInformation("curriculum {0} is completed. failed to add to user {1}",
                                       context.Message.CurriculumId,
                                       context.Message.UserId);
                await context.Publish <ICurriculumAddedResponse>(new CurriculumAddedResponse
                {
                    CurriculumResponse = curriculum.MapTo <CurriculumResponse>(),
                    StudentResponse    = student.MapTo <StudentResponse>(),
                    Status             = StudentCurriculumStatus.Rejected,
                    StatusDescription  = "Capacity is already completed"
                });

                return;
            }

            var isCompleted = await _curriculumService.ReserveAsync(curriculum.Id);

            //todo: it may increase reversed variable twice if UpdateCurriculum run after CurriculumAdded
            curriculum.ReservedCapacity++;
            if (isCompleted)
            {
                curriculum.IsCapacityCompleted = true;
            }
            await context.Publish <ICurriculumAddedResponse>(new CurriculumAddedResponse
            {
                CurriculumResponse = curriculum.MapTo <CurriculumResponse>(),
                StudentResponse    = student.MapTo <StudentResponse>(),
                Status             = StudentCurriculumStatus.Accepted,
                StatusDescription  = "success"
            });

            _logger.LogInformation("add curriculum process completed");
        }