Guest User

Untitled

a guest
Jan 22nd, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.38 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data;
  4. using System.Data.Objects.SqlClient;
  5. using System.Linq;
  6. using System.Web;
  7. using System.Web.Mvc;
  8. using AutoMapper;
  9. using Questiona2011.Models.Context;
  10. using Questiona2011.Models.Entities;
  11. using Questiona2011.ViewModels.Pesquisa;
  12. using Entrevistado = Questiona2011.Models.Entities.Entrevistado;
  13.  
  14. namespace Questiona2011.Controllers
  15. {
  16. /// <summary>
  17. /// Controller de Pesquisa
  18. /// </summary>
  19. public class PesquisaController : Controller
  20. {
  21. private readonly IQuestiona2011Context _context;
  22.  
  23. public PesquisaController(IQuestiona2011Context context)
  24. {
  25. this._context = context;
  26. }
  27.  
  28. /// <summary>
  29. /// Descrição da pesquisa com a Listagem de Perguntas
  30. /// </summary>
  31. /// <returns></returns>
  32. public ActionResult Index()
  33. {
  34. IndexViewModel indexViewModel = new IndexViewModel();
  35. indexViewModel.PerguntasRespostas = GetPerguntasRespostas();
  36.  
  37. return View(indexViewModel);
  38. }
  39.  
  40. /// <summary>
  41. /// Carrega perguntas
  42. /// </summary>
  43. /// <returns></returns>
  44. private IList<PerguntaResposta> GetPerguntasRespostas()
  45. {
  46. IList<PerguntaResposta> perguntaRespostas = new List<PerguntaResposta>();
  47.  
  48. _context.Pergunta.ToList().ForEach(pergunta => perguntaRespostas.Add(new PerguntaResposta()
  49. {
  50. PerguntaId = pergunta.Id,
  51. DescricaoPergunta = pergunta.Descricao
  52. }));
  53.  
  54. return perguntaRespostas;
  55. }
  56.  
  57. /// <summary>
  58. /// Post da pesquisa
  59. /// </summary>
  60. /// <returns></returns>
  61. [HttpPost]
  62. public ActionResult Index(IndexViewModel indexViewModel)
  63. {
  64. if (ModelState.IsValid)
  65. {
  66. int qtdPerguntasRespostas = indexViewModel.PerguntasRespostas.Count;
  67.  
  68. //Entrevistado
  69. Entrevistado entrevistado = new Entrevistado();
  70.  
  71. entrevistado.Matricula = indexViewModel.Entrevistado.Matricula;
  72. entrevistado.Nome = indexViewModel.Entrevistado.Nome;
  73. entrevistado.Ramal = indexViewModel.Entrevistado.Ramal;
  74. entrevistado.Setor = new Setor() { Nome = indexViewModel.Entrevistado.NomeSetor, Secretaria = new Secretaria() { Nome = indexViewModel.Entrevistado.NomeSecretaria } };
  75.  
  76. _context.Entrevistado.Add(entrevistado);
  77.  
  78. Pergunta pergunta;
  79. Resposta resposta;
  80. Pesquisa pesquisa = new Pesquisa();
  81.  
  82. //Respostas
  83. for (int i = 0; i < qtdPerguntasRespostas; i++)
  84. {
  85. long perguntaId = indexViewModel.PerguntasRespostas[i].PerguntaId;
  86. pergunta = _context.Pergunta.Where(x => x.Id == perguntaId).FirstOrDefault();
  87. resposta = new Resposta();
  88.  
  89. resposta.Nota = Convert.ToByte(indexViewModel.PerguntasRespostas[i].NotaResposta);
  90. resposta.Observacao = indexViewModel.PerguntasRespostas[i].ObservacaoResposta;
  91. resposta.Pesquisa = pesquisa;
  92. resposta.Pergunta = pergunta;
  93.  
  94. _context.Resposta.Add(resposta);
  95.  
  96. pesquisa.Perguntas.Add(pergunta);
  97. }
  98.  
  99. //Apurador
  100. pesquisa.Apurador = _context.Apurador.Where(x => x.Credencial.Usuario == User.Identity.Name).FirstOrDefault();
  101.  
  102. //Pesquisa
  103. pesquisa.Entrevistado = entrevistado;
  104. pesquisa.Avaliacao = indexViewModel.Avaliacao;
  105. pesquisa.CodigoChamado = indexViewModel.CodigoChamado;
  106. pesquisa.Observacao = indexViewModel.ObservacaoPesquisa;
  107.  
  108. _context.Pesquisa.Add(pesquisa);
  109.  
  110. _context.SaveChanges();
  111.  
  112. return RedirectToAction("Index");
  113. } else
  114. {
  115. indexViewModel.PerguntasRespostas = GetPerguntasRespostas();
  116.  
  117. return View(indexViewModel);
  118. }
  119. }
  120. }
  121. }
Add Comment
Please, Sign In to add comment