コード例 #1
0
        public async Task <PredictionHouseDB.Questions> AddQuestion(PredictionHouseDB.Questions newQuestion)
        {
            PredictionHouseDB.Questions addedQ = null;

            try
            {
                addedQ = await _dbContext
                         .Questions
                         .SingleOrDefaultAsync(x => x.Question == newQuestion.Question && x.Year == newQuestion.Year);

                if (addedQ == null)
                {
                    var newQ          = new PredictionHouseDB.Questions(newQuestion.Question, newQuestion.Year, newQuestion.Answer);
                    var addedResponse = await _dbContext.AddAsync(newQ);

                    await _dbContext.SaveChangesAsync();

                    addedQ = addedResponse.Entity;
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error saving new question to DB: {0}", ex.Message);
                addedQ = null;
            }

            return(addedQ);
        }
コード例 #2
0
        public async Task <PredictionHouseDB.Responses> AddResponseAsync(PredictionHouseDB.Responses newResponse)
        {
            PredictionHouseDB.Responses addedResponse = null;

            try
            {
                addedResponse = await _dbContext
                                .Responses
                                .SingleOrDefaultAsync(x => x.RespondentId == newResponse.RespondentId &&
                                                      x.QuestionId == newResponse.QuestionId);

                if (addedResponse == null)
                {
                    var newR = new PredictionHouseDB.Responses(newResponse.ResponseId, newResponse.QuestionId,
                                                               newResponse.RespondentId, newResponse.Response, newResponse.Correct);
                    var addedR = await _dbContext.AddAsync(newR);

                    await _dbContext.SaveChangesAsync();

                    addedResponse = addedR.Entity;
                }
                else
                {
                    addedResponse = null;
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error saving new response to DB: {0}", ex.Message);
                addedResponse = null;
            }

            return(addedResponse);
        }