コード例 #1
0
ファイル: Arquivo.cs プロジェクト: src68/Poll
        public bool Vote(int idPoll, int idOption)
        {
            try
            {
                Models.Poll poll = GetPoll(idPoll);

                if (poll == null)
                {
                    throw new Exception();
                }

                var item = poll.options.Where(w => w.option_id == idOption).FirstOrDefault();

                if (item == null)
                {
                    throw new Exception();
                }

                string path = filePath;

                string novoArquivo = path + @"\" + String.Format("vote-{0}-{1}-{2:ddMMyyyy HHmmss}.txt", idPoll, idOption, DateTime.Now);


                StreamWriter sw = new StreamWriter(novoArquivo, false, System.Text.Encoding.Default);
                sw.WriteLine(String.Format("{0};{1}", idPoll, idOption));
                sw.Flush();
                sw.Close();

                return(true);
            }
            catch
            {
                return(false);
            }
        }
コード例 #2
0
        public ActionResult InsertResponses(PollsViewModel p)
        {
            Models.Poll       pq = new Models.Poll();
            Models.PollOption po = new Models.PollOption();

            ViewBag.Polllist1 = (from pol in db.Polls
                                 orderby pol.C_id descending
                                 select new question {
                pollsubject = pol.pollsubject, PollCount = pol.PollCount ?? 0
            }).Take(1).ToList();

            for (int i = 0; i < @ViewBag.Polllist1[0].PollCount; i++)
            {
                try
                {
                    po.optiontext = p.optiontext[i];
                    po.pollid     = Convert.ToInt32(TempData["pollidtemp"]);
                    db.PollOptions.Add(po);
                    db.SaveChanges();
                }
                catch (Exception)
                {
                    ViewBag.msg = "لم يتم حفظ الجواب !!";
                }
            }
            return(RedirectToAction("Responds"));
        }
コード例 #3
0
ファイル: EnqueteController.cs プロジェクト: src68/Poll
        public HttpResponseMessage PostPoll([FromBody] Models.Poll poll)
        {
            Arquivo arquivo = new Arquivo();
            var     retorno = arquivo.AddPoll(poll);

            object obj = new
            {
                poll_id = retorno
            };

            return(new HttpResponseMessage()
            {
                Content = new StringContent(JsonConvert.SerializeObject(obj))
            });
        }
コード例 #4
0
ファイル: Arquivo.cs プロジェクト: src68/Poll
        public Models.ModelView GetView(int idPoll)
        {
            try
            {
                Models.Poll poll = GetPoll(idPoll);

                if (poll == null)
                {
                    return(null);
                }

                string path        = filePath;
                string novoArquivo = path + @"\" + String.Format("view-{0}-{1:ddMMyyyy HHmmss}.txt", idPoll, DateTime.Now);


                StreamWriter sw = new StreamWriter(novoArquivo, false, System.Text.Encoding.Default);
                sw.WriteLine(String.Format("{0}", idPoll));
                sw.Flush();
                sw.Close();


                Models.ModelView view = new Models.ModelView();

                foreach (var item in poll.options)
                {
                    view.views = GetViews(idPoll);
                    view.votes.Add(new Models.ModelViewItem()
                    {
                        option_id = item.option_id,
                        qty       = GetVotes(idPoll, item.option_id)
                    });
                }


                return(view);
            }
            catch
            {
                throw;
            }
        }
コード例 #5
0
        public ActionResult AddPollandOptions(PollsViewModel p)
        {
            Models.Poll pq = new Models.Poll();

            try
            {
                pq.pollsubject = p.pollsubject;
                pq.PollCount   = p.PollCount;
                //pq.pollsubject = p.Poll.pollsubject;
                //pq.PollCount = p.Poll.PollCount;
                db.Polls.Add(pq);
                db.SaveChanges();
            }
            catch (Exception)
            {
                // throw;
                ViewBag.msg = "لم يتم حفظ السؤال !!";
            }


            return(RedirectToAction("InsertResponses"));
        }
コード例 #6
0
ファイル: Arquivo.cs プロジェクト: src68/Poll
        public int AddPoll(Models.Poll poll)
        {
            try
            {
                int nextId = GetLastPollId() + 1;
                poll.poll_id = nextId;

                string path = filePath;

                string novoArquivo = path + @"\" + String.Format("poll-{0}.txt", poll.poll_id);


                StreamWriter sw = new StreamWriter(novoArquivo, false, System.Text.Encoding.Default);
                sw.WriteLine(poll.poll_description);

                int index = 1;

                foreach (var item in poll.options)
                {
                    item.option_id = index;

                    sw.WriteLine(String.Format("{0};{1}", item.option_id, item.option_description));

                    index++;
                }

                sw.Flush();
                sw.Close();

                return(poll.poll_id);
            }
            catch
            {
                return(0);
            }
        }
コード例 #7
0
ファイル: Arquivo.cs プロジェクト: src68/Poll
        public Models.Poll GetPoll(int id)
        {
            try
            {
                string path = filePath;

                FileInfo fi = new FileInfo(path);

                Models.Poll retorno = null;


                DirectoryInfo di    = new DirectoryInfo(path);
                FileInfo[]    files = di.GetFiles();

                foreach (var file in files)
                {
                    //pegar o id no nome do arquivo
                    int idTemp = Convert.ToInt32(file.Name.Split('-')[1].Split('.')[0]);

                    if (idTemp == id)
                    {
                        StreamReader sr = new StreamReader(file.FullName);

                        retorno = new Models.Poll();

                        string   linha   = "";
                        int      idLinha = 0;
                        string[] linhaSplit;
                        bool     primeiraLinha = true;

                        while ((linha = sr.ReadLine()) != null)
                        {
                            if (primeiraLinha) //primeira linha do arquivo corresponde ao próprio poll
                            {
                                retorno.poll_id          = id;
                                retorno.poll_description = linha.ToString();
                                primeiraLinha            = false;
                            }
                            else //da segunda em diante são os itens do poll
                            {
                                linhaSplit = linha.Split(';');
                                idLinha    = Convert.ToInt32(linhaSplit[0]);

                                retorno.options.Add(new Models.PollOption()
                                {
                                    option_id          = Convert.ToInt32(linhaSplit[0].ToString()),
                                    poll_id            = id,
                                    option_description = linhaSplit[1].ToString()
                                });
                            }
                        }

                        break;
                    }
                }

                return(retorno);
            }
            catch
            {
                throw;
            }
        }