Esempio n. 1
0
        public async Task <IActionResult> GetPonto(int id)
        {
            var modulo = await _context.Modulos.SingleOrDefaultAsync(m => m.Id == id);

            int x;

            string             myJson = "  { 'Acerto' : 's','Ponto': '3', 'y' = '1', 'x' = '2' }";
            RequisicaoResposta t      = new RequisicaoResposta {
                Acerto = true, ponto = 3
            };

            //string myJson = "{'Username': '******','Password':'******'}";
            using (var client = new HttpClient())
            {
                // var response = await client.PostAsync(
                //   "http://localhost:64466/api/values",
                //  new StringContent(myJson, Encoding.UTF8, "application/json"));
                HttpRequestMessage request =
                    new HttpRequestMessage(HttpMethod.Post, "http://localhost:64466/api/values");

                string json = JsonConvert.SerializeObject(t);

                request.Content = new StringContent(json, System.Text.Encoding.UTF8, "application/json");

                HttpClient          http     = new HttpClient();
                HttpResponseMessage response = await http.SendAsync(request);

                var responseString = await response.Content.ReadAsStringAsync();

                var post = JsonConvert.DeserializeObject <RequisicaoResposta>(responseString.ToString());
                x = post.ponto;
            }

            /*var requisicaoWeb = WebRequest.CreateHttp(" http://localhost:64466/api/values/2");
             * requisicaoWeb.Method = "GET";
             *
             * using (var resposta = requisicaoWeb.GetResponse())
             * {
             *  var streamDados = resposta.GetResponseStream();
             *  StreamReader reader = new StreamReader(streamDados);
             *  object objResponse = reader.ReadToEnd();
             *
             *  var post = JsonConvert.DeserializeObject<Teste>(objResponse.ToString());
             *  x = post.Ponto;
             *  streamDados.Close();
             *  resposta.Close();
             * }*/

            var user  = _userManager.GetUserAsync(User);
            var aluno = (Aluno)await user;

            PontuacaoController p = new PontuacaoController(_context);

            //  await p.AddPoint(aluno.Id, modulo.TurmaId, modulo.Id, x);

            return(RedirectToAction(nameof(Index)));
        }
Esempio n. 2
0
        public async Task <IActionResult> RespAlunoOnline(int id, [Bind("Id,Resposta")] Modulo modulo)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    if (id != modulo.Id)
                    {
                        return(NotFound());
                    }

                    var user  = _userManager.GetUserAsync(User);
                    var aluno = (Aluno)await user;
                    var mod   = await _context.Modulos.SingleOrDefaultAsync(m => m.Id == id);

                    int x;

                    //string myJson = "  { 'Acerto' : 's','Ponto': '3', 'y' = '1', 'x' = '2' }";
                    //classe de dados de envio
                    RequisicaoResposta RR = new RequisicaoResposta
                    {
                        aluno    = aluno.Id,
                        modulo   = mod.Id,
                        resposta = modulo.Resposta
                    };
                    RequisicaoResposta resulthttp;


                    using (var client = new HttpClient())
                    {
                        // var response = await client.PostAsync(
                        //   "http://localhost:64466/api/values",
                        //  new StringContent(myJson, Encoding.UTF8, "application/json"));
                        HttpRequestMessage request =
                            //  new HttpRequestMessage(HttpMethod.Post, "http://localhost:64466/api/values");
                            new HttpRequestMessage(HttpMethod.Post, mod.Url);

                        string json = JsonConvert.SerializeObject(RR);
                        request.Content = new StringContent(json, System.Text.Encoding.UTF8, "application/json");

                        HttpClient          http     = new HttpClient();
                        HttpResponseMessage response = await http.SendAsync(request);

                        var responseString = await response.Content.ReadAsStringAsync();

                        var post = JsonConvert.DeserializeObject <RequisicaoResposta>(responseString.ToString());
                        resulthttp = post;
                        x          = post.ponto;
                    }

                    PontuacaoController   pc  = new PontuacaoController(_context);
                    LogUsuariosController log = new LogUsuariosController(_context);

                    //tratar porcentagem de acerto
                    if (resulthttp.Acerto.Equals(true))
                    {
                        mod.Resposta   = resulthttp.resposta;
                        mod.Pontos     = resulthttp.ponto;
                        mod.Respondido = true;
                    }
                    else
                    {
                        mod.Pontos     = 0;
                        mod.Respondido = true;
                    }

                    Pontuacao p = new Pontuacao
                    {
                        AlunoId    = aluno.Id,
                        Data       = DateTime.Now,
                        ModuloId   = mod.Id,
                        Respondido = true,
                        Resposta   = mod.Resposta,
                        TurmaId    = mod.TurmaId,
                        Pontos     = resulthttp.ponto
                    };


                    _context.Update(mod);
                    pc.AddResposta(p);
                    await log.SetLog("Resposta inserida : " + mod.Nome, user.Id);

                    await _context.SaveChangesAsync();

                    TempData["alert"] = $"{mod.Nome} foi respondido";
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!ModuloExists(modulo.Id))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }
            return(View());
        }