Exemplo n.º 1
0
        public async Task <ActionServiceResult> SubmitExam(Exam exam)
        {
            StringValues userHeader;

            Request.Headers.TryGetValue("UserID", out userHeader);
            var userID = userHeader.FirstOrDefault().ToString();
            var result = new ActionServiceResult();

            if (userID == null || string.IsNullOrEmpty(userID) || exam == null)
            {
                result.Success = false;
                result.Code    = ApplicationCore.Enums.Enumration.Code.NotFound;
            }
            else
            {
                if (exam.Status == 0)
                {
                    exam.ModifiedDate = DateTime.Now;
                    var message = JsonConvert.SerializeObject(exam);
                    using (var producer = new ProducerWrapper <Null, string>(_producerConfig, "autosubmit"))
                    {
                        await producer.SendMessage(message);
                    }
                    return(new ActionServiceResult()
                    {
                        Success = true,
                        Code = Code.Success,
                        Message = Resources.Success,
                        Data = exam.ExamId
                    });
                }
                else
                {
                    var contest = await _contestRepo.GetEntityByIdAsync(exam.ContestId);

                    if (DateTime.Compare(DateTime.Now, contest.FinishTime) <= 0)
                    {
                        //Todo tinh diem
                        exam.Point   = 10;
                        exam.IsDoing = 0;
                        exam.Status  = 1;
                        await _baseEntityService.Update(exam);

                        result.Data = exam.ExamId;
                    }
                    else
                    {
                        return(new ActionServiceResult
                        {
                            Code = Code.NotFound,
                            Data = null,
                            Message = Resources.NotFound,
                            Success = false
                        });
                    }
                }
            }
            return(result);
        }
Exemplo n.º 2
0
        public async Task <ActionServiceResult> SubmitExam(Examination exam)
        {
            StringValues userHeader;

            Request.Headers.TryGetValue("UserID", out userHeader);
            var userID = userHeader.FirstOrDefault().ToString();
            var result = new ActionServiceResult();

            if (userID == null || string.IsNullOrEmpty(userID) || exam == null)
            {
                result.Success = false;
                result.Code    = Code.NotFound;
                return(result);
            }

            var contest = await _contestRepo.GetEntityByIdAsync(exam.ContestId);

            var oldExam = await _baseEntityService.GetEntityById(exam.ExamId);

            if (contest == null || oldExam == null)
            {
                return(new ActionServiceResult
                {
                    Code = Code.Exception,
                    Data = null,
                    Message = "Entity null",
                    Success = false
                });
            }
            exam.Question     = oldExam.Question;
            exam.Answer       = oldExam.Answer;
            exam.CreatedDate  = oldExam.CreatedDate;
            exam.ModifiedDate = DateTime.Now;
            exam.StartAgain   = oldExam.StartAgain;


            if (oldExam != null && oldExam.Status == 1)
            {
                return(new ActionServiceResult()
                {
                    Success = false,
                    Code = Code.SubmitDone,
                    Message = "Hệ thống đã ghi nhận bài làm trước đó",
                    Data = new
                    {
                        Point = oldExam.Point,
                        ExamID = exam.ExamId
                    }
                });
            }
            if (DateTime.Compare(Utils.GetNistTime(), contest.FinishTime) <= 0)
            {
                var totaltimes = oldExam.TimeUsing;

                totaltimes += ((DateTime.Now - oldExam.ModifiedDate.Value).TotalSeconds / 60.0);

                exam.TimeUsing = totaltimes;


                //Con thoi gian lam bai
                if (totaltimes < contest.TimeToDo)
                {
                    if (exam.Status == 0)
                    {
                        exam.Point = Utils.TinhDiem(exam.Result, oldExam.Answer);
                        //tinh diem
                        var message = JsonConvert.SerializeObject(exam);
                        using (var producer = new ProducerWrapper <Null, string>(_producerConfig, "autosubmit"))
                        {
                            await producer.SendMessage(message);
                        }

                        return(new ActionServiceResult()
                        {
                            Success = true,
                            Code = Code.Success,
                            Message = Resources.Success,
                            Data = new
                            {
                                ExamID = exam.ExamId,
                                Point = exam.Point
                            }
                        });
                    }
                    else
                    {
                        exam.Point = Utils.TinhDiem(exam.Result, oldExam.Answer);
                        //tinh diem
                        await _baseEntityService.Update(exam);

                        return(new ActionServiceResult()
                        {
                            Success = true,
                            Code = Code.Success,
                            Message = Resources.Success,
                            Data = new
                            {
                                ExamID = exam.ExamId,
                                Point = exam.Point
                            }
                        });
                    }
                }
                //Vua het thoio gian lam bai
                else if (totaltimes == contest.TimeToDo)
                {
                    exam.Status = 1;
                    //tinh diem
                    exam.Point = Utils.TinhDiem(exam.Result, oldExam.Answer);
                    await _baseEntityService.Update(exam);

                    return(new ActionServiceResult()
                    {
                        Success = true,
                        Code = Code.Success,
                        Message = Resources.Success,
                        Data = new
                        {
                            ExamID = exam.ExamId,
                            Point = exam.Point
                        }
                    });
                }
                //Het thoi gian lam bai
                else
                {
                    exam.Status = 1;
                    //exam.Point = oldExam.Point;
                    exam.Point = Utils.TinhDiem(exam.Result, oldExam.Answer);
                    await _baseEntityService.Update(exam);

                    return(new ActionServiceResult()
                    {
                        Success = true,
                        Code = Code.Success,
                        Message = Resources.Success,
                        Data = new
                        {
                            ExamID = exam.ExamId,
                            Point = exam.Point
                        }
                    });
                }
            }
            exam.Point = Utils.TinhDiem(exam.Result, oldExam.Answer);;
            //Tinh diem oldexam
            return(new ActionServiceResult
            {
                Code = Code.TimeOut,
                Data = new
                {
                    ExamID = exam.ExamId,
                    Point = oldExam.Point
                },
                Message = "Hết thời gian làm bài",
                Success = false
            });
        }