Ejemplo n.º 1
0
 public UpdateStatement(SqlBuilder sqlBuilder, Table table)
 {
     SqlBuilder     = sqlBuilder;
     Table          = table;
     Assignment     = new AssignmentRecord();
     WhereCondition = new CompoundSqlCondition(sqlBuilder);
 }
Ejemplo n.º 2
0
        public static DbCommand InsertSelectCommand(
            SelectStatement selectStatement,
            Table table,
            Action <IExpressionRecord> setter
            )
        {
            var sqlBuilder = selectStatement.SqlBuilder;
            var columns    = table.Columns;

            var assignment = new AssignmentRecord();

            foreach (var column in columns)
            {
                assignment.Add(column.UniqueName, null);
            }

            setter(assignment);

            if (assignment.Values.Contains(null))
            {
                var keys = assignment.Where(kv => kv.Value == null).Select(kv => kv.Key);
                throw new Exception($"No expressions assigned to: {keys.Intercalate(',')}");
            }

            Debug.Assert(selectStatement.Fields.IsEmpty());
            selectStatement.Fields.AddRange(columns.Select(c => assignment[c.UniqueName]));

            var tokens =
                new[]
            {
                SqlToken.FromString("insert"),
                SqlToken.FromString("into"),
                SqlToken.FromString(table.QuotedName),
                SqlToken.FromString($"({table.ColumnNameList.Value})")
            }
            .Concat(selectStatement.Tokens);

            return(sqlBuilder.CreateCommand(tokens));
        }
        public async Task <IActionResult> GetNextQuestion([FromBody] GetNextQuestionPostViewModel model)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(new
                {
                    message = "request forbidened"
                }));
            }

            var userID = User.Claims.Where(c => c.Type == ClaimTypes.NameIdentifier).FirstOrDefault().Value;
            var user   = await _userManager.FindByIdAsync(userID);

            if (user == null)
            {
                return(NotFound(new
                {
                    message = "user's token is invalid"
                }));
            }

            // Check if this Exercise has AssignmentRecord for the user exists
            var isSpecificAssignmentHasRecord = _arDbContext.AssignmentRecords.Where(ar => ar.UserID == user.Id && ar.ExerciseID == model.ExerciseID).Any();

            if (isSpecificAssignmentHasRecord)
            {
                var specificAssignment = _arDbContext.AssignmentRecords.Where(ar => ar.UserID == user.Id && ar.ExerciseID == model.ExerciseID).ToList().FirstOrDefault();

                // Check if this AssignmentRecord has been finished
                if (!specificAssignment.IsFinished)
                {
                    var allQuestionsInExercise = (from q in _arDbContext.Questions
                                                  join eqc in _arDbContext.ExerciseQuestionRelationMap on q.ID equals eqc.QuestionID
                                                  where eqc.ExerciseID == model.ExerciseID
                                                  orderby eqc.SerialNumber ascending
                                                  select q).ToList();

                    // Check if the CurrentQuestionIndex has exceeded the upperbound of the list
                    if (specificAssignment.CurrentQuestionIndex > allQuestionsInExercise.Count - 1)
                    {
                        return(NotFound(new
                        {
                            message = "the exercise has already finished"
                        }));
                    }

                    var question = allQuestionsInExercise[specificAssignment.CurrentQuestionIndex];

                    var vm = new GetNextQuestionGetViewModel {
                        AssignmentRecordID = specificAssignment.ID,
                        QuestionID         = question.ID,

                        SentenceEN             = question.SentenceEN,
                        SentenceJP             = question.SentenceJP,
                        Division               = question.Division,
                        StandardAnswerDivision = question.StandardAnswerDivision,

                        CurrentNumber = specificAssignment.CurrentQuestionIndex + 1
                    };
                    return(Ok(vm));
                }
                else
                {
                    // When the user finishes the assignment return No-Content(204) to go to the next scrren
                    return(NoContent());
                }
            }
            else
            {
                // Create a new AssignmentRecord
                var assignmentRecordNew = new AssignmentRecord
                {
                    UserID     = user.Id,
                    ExerciseID = model.ExerciseID,

                    CurrentQuestionIndex = 0,
                    StartDate            = DateTime.Now,

                    IsFinished   = false,
                    AccuracyRate = (float)0.0,
                };
                _arDbContext.Add(assignmentRecordNew);
                _arDbContext.SaveChanges();

                var allQuestionsInExercise = (from q in _arDbContext.Questions
                                              join eqc in _arDbContext.ExerciseQuestionRelationMap on q.ID equals eqc.QuestionID
                                              where eqc.ExerciseID == model.ExerciseID
                                              orderby eqc.SerialNumber ascending
                                              select q).ToList();

                var question = allQuestionsInExercise[assignmentRecordNew.CurrentQuestionIndex];

                var vm = new GetNextQuestionGetViewModel
                {
                    AssignmentRecordID = assignmentRecordNew.ID,
                    QuestionID         = question.ID,

                    SentenceEN             = question.SentenceEN,
                    SentenceJP             = question.SentenceJP,
                    Division               = question.Division,
                    StandardAnswerDivision = question.StandardAnswerDivision,

                    CurrentNumber = assignmentRecordNew.CurrentQuestionIndex + 1
                };
                return(Ok(vm));
            }
        }