Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Data;
- using System.Data.Objects.SqlClient;
- using System.Linq;
- using System.Web;
- using System.Web.Mvc;
- using AutoMapper;
- using Questiona2011.Models.Context;
- using Questiona2011.Models.Entities;
- using Questiona2011.ViewModels.Pesquisa;
- using Entrevistado = Questiona2011.Models.Entities.Entrevistado;
- namespace Questiona2011.Controllers
- {
- /// <summary>
- /// Controller de Pesquisa
- /// </summary>
- public class PesquisaController : Controller
- {
- private readonly IQuestiona2011Context _context;
- public PesquisaController(IQuestiona2011Context context)
- {
- this._context = context;
- }
- /// <summary>
- /// Descrição da pesquisa com a Listagem de Perguntas
- /// </summary>
- /// <returns></returns>
- public ActionResult Index()
- {
- IndexViewModel indexViewModel = new IndexViewModel();
- indexViewModel.PerguntasRespostas = GetPerguntasRespostas();
- return View(indexViewModel);
- }
- /// <summary>
- /// Carrega perguntas
- /// </summary>
- /// <returns></returns>
- private IList<PerguntaResposta> GetPerguntasRespostas()
- {
- IList<PerguntaResposta> perguntaRespostas = new List<PerguntaResposta>();
- _context.Pergunta.ToList().ForEach(pergunta => perguntaRespostas.Add(new PerguntaResposta()
- {
- PerguntaId = pergunta.Id,
- DescricaoPergunta = pergunta.Descricao
- }));
- return perguntaRespostas;
- }
- /// <summary>
- /// Post da pesquisa
- /// </summary>
- /// <returns></returns>
- [HttpPost]
- public ActionResult Index(IndexViewModel indexViewModel)
- {
- if (ModelState.IsValid)
- {
- int qtdPerguntasRespostas = indexViewModel.PerguntasRespostas.Count;
- //Entrevistado
- Entrevistado entrevistado = new Entrevistado();
- entrevistado.Matricula = indexViewModel.Entrevistado.Matricula;
- entrevistado.Nome = indexViewModel.Entrevistado.Nome;
- entrevistado.Ramal = indexViewModel.Entrevistado.Ramal;
- entrevistado.Setor = new Setor() { Nome = indexViewModel.Entrevistado.NomeSetor, Secretaria = new Secretaria() { Nome = indexViewModel.Entrevistado.NomeSecretaria } };
- _context.Entrevistado.Add(entrevistado);
- Pergunta pergunta;
- Resposta resposta;
- Pesquisa pesquisa = new Pesquisa();
- //Respostas
- for (int i = 0; i < qtdPerguntasRespostas; i++)
- {
- long perguntaId = indexViewModel.PerguntasRespostas[i].PerguntaId;
- pergunta = _context.Pergunta.Where(x => x.Id == perguntaId).FirstOrDefault();
- resposta = new Resposta();
- resposta.Nota = Convert.ToByte(indexViewModel.PerguntasRespostas[i].NotaResposta);
- resposta.Observacao = indexViewModel.PerguntasRespostas[i].ObservacaoResposta;
- resposta.Pesquisa = pesquisa;
- resposta.Pergunta = pergunta;
- _context.Resposta.Add(resposta);
- pesquisa.Perguntas.Add(pergunta);
- }
- //Apurador
- pesquisa.Apurador = _context.Apurador.Where(x => x.Credencial.Usuario == User.Identity.Name).FirstOrDefault();
- //Pesquisa
- pesquisa.Entrevistado = entrevistado;
- pesquisa.Avaliacao = indexViewModel.Avaliacao;
- pesquisa.CodigoChamado = indexViewModel.CodigoChamado;
- pesquisa.Observacao = indexViewModel.ObservacaoPesquisa;
- _context.Pesquisa.Add(pesquisa);
- _context.SaveChanges();
- return RedirectToAction("Index");
- } else
- {
- indexViewModel.PerguntasRespostas = GetPerguntasRespostas();
- return View(indexViewModel);
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment