public EncuestaGeneralResponse ObtenerEncuestaParaResponder(int encuestaId) { EncuestaGeneral e = _contextoGeneral.EncuestaGeneral.SingleOrDefault(x => x.Id == encuestaId); EncuestaGeneralResponse encuesta = new EncuestaGeneralResponse() { Id = e.Id, Nombre = e.Titulo, LstPreguntas = new List <PreguntasResponse>() }; List <PreguntasGeneral> preguntas = _contextoGeneral.PreguntasGeneral.Where(x => x.EncuestaGeneralId == encuesta.Id).ToList(); foreach (var p in preguntas) { PreguntasResponse pregunta = new PreguntasResponse() { Id = p.Id, Pregunta = p.Frase, TipoPregunta = p.TipoCheck, LstOpciones = new List <OpcionesResponse>() }; encuesta.LstPreguntas.Add(pregunta); var respuestas = _contextoGeneral.OpcionesGeneral.Where(x => x.PreguntaGeneralId == p.Id).ToList(); foreach (var resp in respuestas) { pregunta.LstOpciones.Add(new OpcionesResponse() { Id = resp.Id, Respuesta = resp.Respuesta }); } } return(encuesta); }
public void AddEncuesta(EncuestaGeneralRequest encuesta) { foreach (var item in _contextoGeneral.Facultad.ToList()) { } var enc = new EncuestaGeneral() { Titulo = encuesta.Titulo, Fecha = DateTime.Now }; _contextoGeneral.EncuestaGeneral.Add(enc); _contextoGeneral.SaveChanges(); foreach (var preg in encuesta.LstPreguntas) { var p = new PreguntasGeneral() { Frase = preg.Pregunta, TipoCheck = preg.Opcion, EncuestaGeneralId = enc.Id }; _contextoGeneral.PreguntasGeneral.Add(p); _contextoGeneral.SaveChanges(); } foreach (var resp in encuesta.LstRespuestas) { var op = new OpcionesGeneral() { Respuesta = resp.Respuesta, PreguntaGeneralId = GetIdPreguntaEncuesta(resp.PreguntaAsociada, enc.Id) }; _contextoGeneral.OpcionesGeneral.Add(op); _contextoGeneral.SaveChanges(); } }