public List <PerguntaModel> ListarPerguntas(int ID_Quest) { List <PerguntaModel> ListaPerguntas = new List <PerguntaModel>(); connectionStringBuilder.DataSource = "Database/agrotools.db"; using (var connection = new SqliteConnection(connectionStringBuilder.ConnectionString)) { connection.Open(); var cmd = connection.CreateCommand(); cmd.CommandText = "SELECT * FROM Perguntas WHERE ID_Quest = @id_quest;"; cmd.Parameters.AddWithValue("@id_quest", ID_Quest); using (var reader = cmd.ExecuteReader()) { while (reader.Read()) { PerguntaModel pergunta = new PerguntaModel(); pergunta.ID = Convert.ToInt32(reader.GetString(0)); pergunta.Pergunta = reader.GetString(1); pergunta.DataCricao = Convert.ToDateTime(reader.GetString(2)); pergunta.TipoResposta = reader.GetString(3); pergunta.Ordem = Convert.ToInt32(reader.GetString(4)); pergunta.ID_Questionario = Convert.ToInt32(reader.GetString(5)); ListaPerguntas.Add(pergunta); } } connection.Close(); } return(ListaPerguntas); }
// GET: Pergunta/Edit/5 public ActionResult Edit(int id) { string requestUrl = "/api/" + _CONTROLLER + "/" + id; HttpResponseMessage response = client.GetAsync(requestUrl, HttpCompletionOption.ResponseHeadersRead).Result; if (response.IsSuccessStatusCode) { var data = response.Content.ReadAsStringAsync().Result; PerguntaModel model = JsonConvert.DeserializeObject <PerguntaModel>(data); RespostaModel resposta = new RespostaModel(); ViewBag.Pergunta = model.Descricao; resposta.IdAutor = model.IdAutor; resposta.IdPergunta = model.Id; if (model != null) { return(View(resposta)); } else { return(NotFound()); } } else { return(View()); } }
// GET: Pergunta/Delete/5 public ActionResult Delete(int id) { string requestUrl = "/api/" + _CONTROLLER + "/" + id; HttpResponseMessage response = client.GetAsync(requestUrl, HttpCompletionOption.ResponseHeadersRead).Result; if (response.IsSuccessStatusCode) { var data = response.Content.ReadAsStringAsync().Result; PerguntaModel model = JsonConvert.DeserializeObject <PerguntaModel>(data); if (model != null) { return(View(model)); } else { return(NotFound()); } } else { return(NotFound()); } }
public ActionResult Create(PerguntaModel model) { try { model.DataCriacao = DateTime.Now; string requestUrl = "/api/" + _CONTROLLER + "/"; HttpResponseMessage response = client.PostAsJsonAsync <PerguntaModel>(requestUrl, model).Result; if (response.IsSuccessStatusCode) { return(RedirectToAction("Index")); } else { ViewBag.Error = "Erro na criação do registro."; return(View()); } } catch { return(View()); } }
public IActionResult Put([FromBody] PerguntaModel pergunta) { if (_service.Atualizar(pergunta)) { return(Ok(pergunta)); } return(null); }
public IActionResult Post([FromBody] PerguntaModel pergunta) { if (_service.Add(pergunta)) { return(Ok(pergunta)); } return(null); }
public bool Add(PerguntaModel objeto) { if (objeto != null) { _context.Add(ModelToEntity(objeto, new Pergunta())); return(_context.SaveChanges() == 1 ? true : false); } return(false); }
private Pergunta CriarEntidadePergunta(PerguntaModel perguntaModel, Usuario usuario) { var pergunta = new Pergunta(usuario, perguntaModel.Titulo, perguntaModel.Descricao); if (perguntaModel.TagsIds != null) { perguntaModel.TagsIds .ForEach(tag => pergunta.AdicionarTag(tagsRepositorio.ObterPorId(tag))); } return(pergunta); }
public HttpResponseMessage Criar([FromBody] PerguntaModel perguntaModel) { var usuario = usuarioRepositorio.ObterPorEmail(Thread.CurrentPrincipal.Identity.Name); var pergunta = CriarEntidadePergunta(perguntaModel, usuario); if (!pergunta.EhValida()) { return(ResponderErro(pergunta.Mensagens)); } perguntasRepositorio.Criar(pergunta); return(ResponderOK(new { id = pergunta.Id })); }
public bool Atualizar(PerguntaModel objeto) { if (objeto != null) { var antigo = _context.Pergunta.Where(r => r.IdPergunta == objeto.IdPergunta).FirstOrDefault(); if (antigo != null) { _context.Update(ModelToEntity(objeto, antigo)); return(_context.SaveChanges() == 1 ? true : false); } return(false); } return(false); }
private Pergunta ModelToEntity(PerguntaModel model, Pergunta entity) { entity.IdPergunta = model.IdPergunta; entity.Questao = model.Questao; entity.Alternativa1 = model.Alternativa1; entity.Alternativa2 = model.Alternativa2; entity.Alternativa3 = model.Alternativa3; entity.Alternativa4 = model.Alternativa4; entity.OpcaoCorreta = model.OpcaoCorreta; entity.Pontuacao = model.Pontuacao; entity.Tempo = model.Tempo; entity.Dificuldade = model.Dificuldade; entity.TematicaIdTematica = model.TematicaIdTematica; return(entity); }
public HttpResponseMessage Alterar([FromBody] PerguntaModel perguntaModel) { var usuarioLogado = usuarioRepositorio.ObterPorEmail(Thread.CurrentPrincipal.Identity.Name); var perguntaBuscada = perguntasRepositorio.ObterPorId(perguntaModel.Id); try { perguntaBuscada.Editar(perguntaModel.Descricao, perguntaModel.Titulo, usuarioLogado); perguntasRepositorio.Alterar(perguntaBuscada); return(ResponderOK()); } catch (Exception e) { return(ResponderErro(e.Message)); } }
// GET: Pergunta/Create public ActionResult Create() { PerguntaModel pergunta = new PerguntaModel() { IdAutor = ObterAutorLogado().Id }; var listaCategoria = this.ListarCategorias().Where(item => item.Ativo == true).Select(c => new SelectListItem() { Text = c.Titulo, Value = c.Id.ToString() }).ToList(); ViewBag.Categorias = listaCategoria; return(View(pergunta)); }
public void NovaPergunta(PerguntaModel pergunta) { connectionStringBuilder.DataSource = "Database/agrotools.db"; using (var connection = new SqliteConnection(connectionStringBuilder.ConnectionString)) { connection.Open(); var cmd = connection.CreateCommand(); cmd.CommandText = "INSERT INTO Perguntas(Pergunta,DataCricao,TipoResposta,Ordem,ID_Quest) values (@pergunta,@data,@tipo,@ordem,@id_quest)"; cmd.Parameters.AddWithValue("@pergunta", pergunta.Pergunta); cmd.Parameters.AddWithValue("@data", pergunta.DataCricao); cmd.Parameters.AddWithValue("@tipo", pergunta.TipoResposta); cmd.Parameters.AddWithValue("@ordem", pergunta.Ordem); cmd.Parameters.AddWithValue("@id_quest", pergunta.ID_Questionario); cmd.ExecuteNonQuery(); connection.Close(); } }
private PerguntaModel CriarModelPergunta(Pergunta entidadePergunta) { var perguntaModel = new PerguntaModel(); perguntaModel.Id = entidadePergunta.Id; perguntaModel.Titulo = entidadePergunta.Titulo; perguntaModel.Usuario = entidadePergunta.Usuario.converterUsuarioParaUsuarioModel(); perguntaModel.Descricao = entidadePergunta.Descricao; perguntaModel.DataPergunta = entidadePergunta.DataPergunta; perguntaModel.Tags = new List <TagModel>(); perguntaModel.DownVotes = new List <UsuarioBaseModel>(); perguntaModel.UpVotes = new List <UsuarioBaseModel>(); var tags = entidadePergunta.Tags; foreach (var tag in tags) { var tagModel = new TagModel(); tagModel.Id = tag.Id; tagModel.Descricao = tag.Descricao; perguntaModel.Tags.Add(tagModel); } perguntaModel.Comentarios = new List <ComentarioRespostaModel>(); foreach (var each in entidadePergunta.ComentariosPergunta) { var comentario = new ComentarioRespostaModel(); comentario.Usuario = each.Usuario.converterUsuarioParaUsuarioModel(); comentario.Id = each.Id; comentario.DataComentario = each.DataComentario; comentario.Descricao = each.Descricao; perguntaModel.Comentarios.Add(comentario); } perguntaModel.QuantidadeDownVotes = entidadePergunta.DownVotes.Count(); perguntaModel.QuantidadeUpVotes = entidadePergunta.UpVotes.Count(); foreach (var each in entidadePergunta.UpVotes) { perguntaModel.UpVotes.Add(each.Usuario.converterUsuarioParaUsuarioModel()); } foreach (var each in entidadePergunta.DownVotes) { perguntaModel.DownVotes.Add(each.Usuario.converterUsuarioParaUsuarioModel()); } return(perguntaModel); }
public ActionResult Delete(int id, PerguntaModel model) { try { string requestUrl = "/api/" + _CONTROLLER + "/" + id; HttpResponseMessage response = client.DeleteAsync(requestUrl).Result; if (response.IsSuccessStatusCode) { return(RedirectToAction("Index")); } else { ViewBag.Error = "Erro na exclusão do registro."; return(View()); } } catch { return(View()); } }
public IActionResult NovoQuestionario(IFormCollection form) { QuestionarioRepository repository = new QuestionarioRepository(); Questionario quest = new Questionario(); var names = form["todos"]; quest.Titulo = form["titulo"]; quest.Descricao = form["descricao"]; quest.ID_Usuario = Convert.ToInt32(ObterUsuarioIDSession()); int ID = repository.Registrar(quest); string[] nomes = names.ToString().Split(';'); int cont = 1; foreach (var item in nomes) { PerguntaModel perguntaModel = new PerguntaModel(); if (item != "") { var pergunta = form[item]; string[] pergunt = pergunta.ToString().Split('¥'); string str = pergunt[1].Substring(13); int tipo = Convert.ToInt32(str); if (tipo == 1) { perguntaModel.TipoResposta = tipo.ToString(); } else if (tipo == 2) { string got = pergunt[2].Substring(11); int count = Convert.ToInt32(got) + 3; string stro = ""; for (int i = 3; i < count; i++) { stro += pergunt[i].Substring(7) + ";"; } perguntaModel.TipoResposta = $"TipoResposta=select;" + stro; } else { string got = pergunt[2].Substring(11); int count = Convert.ToInt32(got) + 3; string stro = ""; for (int i = 3; i < count; i++) { stro += pergunt[i].Substring(7) + ";"; } perguntaModel.TipoResposta = $"TipoResposta=checkbox;" + stro; } perguntaModel.ID_Questionario = ID; perguntaModel.Ordem = cont; perguntaModel.DataCricao = DateTime.Now; perguntaModel.Pergunta = pergunt[0].Substring(9); repository.NovaPergunta(perguntaModel); cont++; } } return(RedirectToAction("Panel", "Usuario")); }