예제 #1
0
        public IActionResult OnPost(int id)
        {
            Forms     = _context.Forms.ToList();
            Form_ID   = Forms.FirstOrDefault(f => f.Form_ID == id).Form_ID;
            Questions = _context.Questions.Where(q => q.Form_ID == id).ToList();
            string refNo  = GenerateID();
            string status = string.Empty;

            if (q1 == "20" || q1 == "40" || q1 == "60" ||
                q2 == "20" || q2 == "40" || q2 == "60" ||
                q3 == "20" || q3 == "40" || q3 == "60" ||
                q4 == "20" || q4 == "40" || q4 == "60" ||
                q5 == "20" || q5 == "40" || q5 == "60" ||
                q6 == "20" || q6 == "40" || q6 == "60" ||
                q7 == "20" || q7 == "40" || q7 == "60" ||
                q8 == "20" || q8 == "40" || q8 == "60" ||
                q9 == "20" || q9 == "40" || q9 == "60" ||
                q10 == "20" || q10 == "40" || q10 == "60")
            {
                status = "NEGATIVE";
            }
            else
            {
                status = "POSITIVE";
            }
            string imagePath = string.Empty;

            if (UploadImage != null)
            {
                var fileName = Path.Combine(_hostingEnvironment.WebRootPath, "uploadedImages", UploadImage.FileName);
                using (var fileStream = new FileStream(fileName, FileMode.Create))
                {
                    UploadImage.CopyTo(fileStream);
                }
                imagePath = "\\uploadedImages\\" + UploadImage.FileName; // Set the file name
            }

            Customer = _context.Customers.ToList();
            Customer cust = new Customer();

            cust = _context.Customers.FirstOrDefault(c => c.Email == Email.ToLower());
            if (cust == null)
            {
                Customer customer = new Customer
                {
                    Name  = Name,
                    Email = Email.ToLower(),
                    Phone = Phone
                };
                _context.Customers.Add(customer);
                _context.SaveChanges();
            }
            List <string> answers = new List <string>();

            answers.Add(q1);
            answers.Add(q2);
            answers.Add(q3);
            answers.Add(q4);
            answers.Add(q5);
            answers.Add(q6);
            answers.Add(q7);
            answers.Add(q8);
            answers.Add(q9);
            answers.Add(q10);
            double answerPerc = 0;

            for (int a = 0; a < Questions.Count; a++)
            {
                if (answers[a] != "")
                {
                    answerPerc += Convert.ToDouble(answers[a]);
                }
                else
                {
                    answerPerc += 0;
                }
                Answer answer = new Answer
                {
                    Question_ID = Questions[a].Question_ID,
                    QAnswer     = answers[a],
                    Form_ID     = Form_ID,
                    Feedback_ID = refNo
                };
                _context.Answers.Add(answer);
                _context.SaveChanges();
            }
            answerPerc = answerPerc / Questions.Count;
            answerPerc = Math.Round(answerPerc);
            cust       = new Customer();
            cust       = _context.Customers.FirstOrDefault(c => c.Email == Email.ToLower());
            Feedback feedback = new Feedback
            {
                RefNo           = refNo,
                Customer_ID     = cust.Id,
                Comments        = FormComments,
                Servant         = Waitron,
                Form_ID         = Form_ID,
                SubmittedDate   = DateTime.Now,
                Notes           = "",
                Status          = status,
                RefScore        = answerPerc.ToString(),
                Photo           = imagePath,
                ContactCustomer = ContactedString
            };

            _context.Feedbacks.Add(feedback);
            _context.SaveChanges();

            SendEmail(status, feedback, cust);
            SendMailTo(status, feedback, cust);

            return(RedirectToPage("PostForm", new { refno = feedback.RefNo }));
        }