<commit_before>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using MyQuizBackend.Classes;
using MYVote.Models;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

// For more information on enabling Web API for empty projects, visit http://go.microsoft.com/fwlink/?LinkID=397860

namespace MyQuizBackend.Controllers
{
    [Route("api/[controller]")]
    public class GivenAnswerController : Controller
    {
        private readonly IVoteConnector _voteConnector;
        public GivenAnswerController(IVoteConnector connector) {
            _voteConnector = connector;
        }
        #region GET

        // GET api/givenAnswer
        [HttpGet]
        public IActionResult GetAllGivenAnswers([FromQuery] int groupId, [FromQuery] int singleTopicId)
        {
            var givenAnswers = new List<GivenAnswer>();
            using (var db = new EF_DB_Context())
            {
                givenAnswers = (from g in db.GivenAnswer select g).ToList();
            }
            if (!givenAnswers.Any()) return BadRequest("No data present!");

            if (groupId != 0)
            {
                givenAnswers = givenAnswers.Where(x => x.GroupId != groupId).ToList() ;
            }

            if (singleTopicId != 0)
            {
                givenAnswers = givenAnswers.Where(x => x.SingleTopicId != singleTopicId).ToList();
            }
            foreach (var ga in givenAnswers)
            {
                ga.fillValues();
            }
            return Ok(JsonConvert.SerializeObject(givenAnswers));
        }

        // GET api/givenAnswer/:id
        [HttpGet("{id}")]
        public IActionResult GetGivenAnswerById(int id)
        {
            GivenAnswer givenAnswer;
            using (var db = new EF_DB_Context())
            {
                givenAnswer = db.GivenAnswer.FirstOrDefault(ga => ga.Id == id);
                if (givenAnswer == null) return BadRequest("No data present!");
            }
            givenAnswer.fillValues();
            return Ok(JsonConvert.SerializeObject(givenAnswer));
        }

        // GET api/givenAnswer/latest
        [HttpGet("latest")]
        public IActionResult GetLatestQuestionForDevice()
        {
            var deviceID = DeviceAuthentification.getClientIDfromHeader(Request);
            if (deviceID < 0) return BadRequest();

            List<GivenAnswer> givenAnswerListForClient;

            using (var db = new EF_DB_Context())
            {
                var DeviceGroupList = from temp in db.DeviceGroup where temp.DeviceId == deviceID select temp;
                var GroupList = db.Group.Where(temp => DeviceGroupList.Any(temp2 => temp2.GroupId == temp.Id));
                givenAnswerListForClient = db.GivenAnswer.Where(temp => GroupList.Any(temp2 => temp2.Id == temp.GroupId)).ToList();
            }
            return Ok(JsonConvert.SerializeObject(givenAnswerListForClient));
        }

        #endregion GET

        #region POST

        // Client Answer to Supervisor<commit_msg>Comment<commit_after>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using MyQuizBackend.Classes;
using MYVote.Models;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

// For more information on enabling Web API for empty projects, visit http://go.microsoft.com/fwlink/?LinkID=397860

namespace MyQuizBackend.Controllers
{
    [Route("api/[controller]")]
    public class GivenAnswerController : Controller
    {
        private readonly IVoteConnector _voteConnector;
        public GivenAnswerController(IVoteConnector connector) {
            _voteConnector = connector;
        }
        #region GET

        // GET api/givenAnswer
        [HttpGet]
        public IActionResult GetAllGivenAnswers([FromQuery] int groupId, [FromQuery] int singleTopicId)
        {
            var givenAnswers = new List<GivenAnswer>();
            using (var db = new EF_DB_Context())
            {
                givenAnswers = (from g in db.GivenAnswer select g).ToList();
            }
            if (!givenAnswers.Any()) return BadRequest("No data present!");

            if (groupId != 0)
            {
                givenAnswers = givenAnswers.Where(x => x.GroupId != groupId).ToList() ;
            }

            if (singleTopicId != 0)
            {
                givenAnswers = givenAnswers.Where(x => x.SingleTopicId != singleTopicId).ToList();
            }
            foreach (var ga in givenAnswers)
            {
                ga.fillValues();
            }
            return Ok(JsonConvert.SerializeObject(givenAnswers));
        }

        // GET api/givenAnswer/:id
        [HttpGet("{id}")]
        public IActionResult GetGivenAnswerById(int id)
        {
            GivenAnswer givenAnswer;
            using (var db = new EF_DB_Context())
            {
                givenAnswer = db.GivenAnswer.FirstOrDefault(ga => ga.Id == id);
                if (givenAnswer == null) return BadRequest("No data present!");
            }
            givenAnswer.fillValues();
            return Ok(JsonConvert.SerializeObject(givenAnswer));
        }

        // GET api/givenAnswer/latest
        [HttpGet("latest")]
        public IActionResult GetLatestQuestionForDevice()
        {
            var deviceID = DeviceAuthentification.getClientIDfromHeader(Request);
            if (deviceID < 0) return BadRequest();

            List<GivenAnswer> givenAnswerListForClient;

            using (var db = new EF_DB_Context())
            {
                var DeviceGroupList = from temp in db.DeviceGroup where temp.DeviceId == deviceID select temp;
                var GroupList = db.Group.Where(temp => DeviceGroupList.Any(temp2 => temp2.GroupId == temp.Id));
                givenAnswerListForClient = db.GivenAnswer.Where(temp => GroupList.Any(temp2 => temp2.Id == temp.GroupId)).ToList();
            }
            return Ok(JsonConvert.SerializeObject(givenAnswerListForClient));
        }

        #endregion GET

        #region POST

        // Client Answer input -> send to Supervisor
{'fraction_non_alphanumeric': 0.09728, 'fraction_numerical': 0.00416, 'mean_word_length': 2.108403779214321, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 22, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2559161', 'n_tokens_mistral': 1746, 'n_tokens_neox': 1623, 'n_words': 373}