public bool PostSkillQuestionBank(SkillQuestionBankDAO skillQuestionBank)
        {
            SkillQuestionBankServiceClient client = new SkillQuestionBankServiceClient();

            try
            {
                bool result = client.CreateSkillQuestionBank(skillQuestionBank);
                return result;
            }
            catch (FaultException<KaskServiceException> e)
            {
                throw new HttpException(e.Message);
            }
        }
Пример #2
0
        public async Task<ActionResult> Create(FormCollection collection)
        {
            try
            {
                if (!string.IsNullOrEmpty(Request.Form["skillName"]))
                {
                    // save application form data back to database through service
                    using (HttpClient httpClient = new HttpClient())
                    {
                        httpClient.BaseAddress = new Uri("http://localhost:51309");
                        httpClient.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
                        HttpResponseMessage result = new HttpResponseMessage();
                        string resultContent = "";

                        // gather SkillOpening form data
                        SkillDAO skill = new SkillDAO();
                        skill.SkillName = Request.Form["skillName"];
                        
                        // post (save) SkillOpening data
                        result = httpClient.PostAsJsonAsync(ServiceURIs.ServiceSkillUri, skill).Result;
                        resultContent = result.Content.ReadAsStringAsync().Result;

                        // get skill id from last insert
                        // get correct skill id of the skill we just saved
                        var skills = await ServerResponse<List<SkillDAO>>.GetResponseAsync(ServiceURIs.ServiceSkillUri);
                        int lastSkillId = skills.Last().SkillID;

                        // gather data for QuestionBank
                        
                        // TODO: change the hardcoded "50" into the length of all available mc options
                        for (int i = 0; i < 50; i++)
                        {
                            for (int j = 0; j < 50; j++)
                            {
                                try
                                {
                                    QuestionBankDAO questionBank = new QuestionBankDAO();
                                    questionBank.MCQuestionID = Convert.ToInt32(Request.Form["MCQuestion_" + j]);

                                    // check if this is a selected multiple choice option
                                    if (Request.Form["MCOptionID_" + j + "_" + i] != null)
                                    {
                                        questionBank.MCOptionID = i;

                                        // check if this is a valid option (a "correct answer")
                                        if (Request.Form["Valid_MCOptionID_" + j + "_" + i] != null)
                                        {
                                            questionBank.MCCorrectOption = 1;
                                        }
                                        else
                                        {
                                            questionBank.MCCorrectOption = 0;
                                        }

                                        // post data for QuestionBank
                                        result = httpClient.PostAsJsonAsync(ServiceURIs.ServiceQuestionBankUri, questionBank).Result;
                                        resultContent = result.Content.ReadAsStringAsync().Result;

                                        // get question bank id from last insert
                                        // get correct question bank id of the question bank we just saved
                                        var questionBanks = await ServerResponse<List<QuestionBankDAO>>.GetResponseAsync(ServiceURIs.ServiceQuestionBankUri);
                                        int lastQuestionBankId = questionBanks.Last().QuestionBankID;

                                        // then connect up QuestionBank data to SkillQuestionBank
                                        // gather skill-question bank data
                                        SkillQuestionBankDAO skillQuestionBank = new SkillQuestionBankDAO();
                                        skillQuestionBank.SkillID = lastSkillId;
                                        skillQuestionBank.QuestionBankID = lastQuestionBankId;

                                        // and then post data for skill-question bank
                                        result = httpClient.PostAsJsonAsync(ServiceURIs.ServiceSkillQuestionBankUri, skillQuestionBank).Result;
                                        resultContent = result.Content.ReadAsStringAsync().Result;
                                    }
                                }
                                catch { }
                            }
                        }
                    }

                    return RedirectToAction("Index", "Skills");
                }
                else
                {
                    // TODO: validation later on...
                    return RedirectToAction("Create");
                }
            }
            catch
            {
                // TODO: validation later on...
                return RedirectToAction("Create");
            }
        }