コード例 #1
0
        public HttpResponseMessage LanguageGrid([FromUri]string id, [FromBody]string requestWord)
        {
            id = HttpUtility.HtmlEncode(id);
            requestWord = HttpUtility.HtmlEncode(requestWord);

            if (string.IsNullOrEmpty(requestWord)) return Request.CreateResponse(HttpStatusCode.BadRequest);

            foreach (Language language in _languages)
            {
                _gridModel.Add(_detector.Detect(requestWord, language));
            }

            if (string.IsNullOrEmpty(id))
                return Request.CreateResponse(HttpStatusCode.NoContent); //TODO: Found out a right status code.

            JavaScriptSerializer serializer = new JavaScriptSerializer();
            string JSonString = serializer.Serialize(_gridModel);

            SQLiteContext context = new SQLiteContext();

            //Parametrized query.
            string queryString = "INSERT INTO Requests(UserId, QueryString, QueryResult) VALUES (?, ?, ?);";

            try
            {
                context.ExecuteQuery(queryString, id, requestWord, JSonString);
                return Request.CreateResponse(HttpStatusCode.Created, _gridModel);
            }
            catch (Exception e)
            {
                return Request.CreateResponse(HttpStatusCode.InternalServerError, _gridModel); //TODO: Found out a right status code.
            }
        }
コード例 #2
0
        // PUT: api/TopRequestsAPI
        public void Put([FromBody]string id)
        {
            if (id == null) return;

            //Xss protect.
            id = HttpUtility.HtmlEncode(id);

            SQLiteContext context = new SQLiteContext();

            string queryString = $@"Update RequestsInfo Set amountOfQueries = amountOfQueries+1, lastLoginDateTime='{DateTime.Now}'
                                    Where UserId=?;";

            context.ExecuteQuery(queryString, id);
        }
コード例 #3
0
ファイル: HomeController.cs プロジェクト: IlyaTsiamchyk/Work
        public ActionResult TopTenRequests() 
         { 
             var context = new SQLiteContext();

            //string path = Server.MapPath("/App_Data/Detector.sqlite");
            //SQLiteContext.CreateDatabase(path);

            StringBuilder str = new StringBuilder();

             //Возвращает первые 10 записей из таблицы RequestsInfo и AspNetUsers, отсортированные по amountOfQueries.
             string queryString = "select UserName, amountOfQueries, registerDateTime, lastLoginDateTime" +
                 " from RequestsInfo inner join AspNetUsers on RequestsInfo.UserId=AspNetUsers.Id" +
                 " order by RequestsInfo.amountOfQueries desc limit 10";

            //context.LoadData(10) возвращает массив из 10-ти записей таблицы RequestsInfo.
             foreach (DataRow row in context.LoadData(queryString)) 
             { 
                 //Формируется таблица для bootstrap
                 str.Append("<div class='row'>"); 

                 str.Append("<div class = 'col-md-3'><p>Name - " 
                         + row["UserName"] + "</p></div>");

                 str.Append("<div class = 'col-md-3'><p>Amount of querys - "
                         + row[1] + "</p></div>"); 

                 str.Append("<div class = 'col-md-3'><p>Last visit " 
                         + row[3] + "</p></div>"); 

                 //Нахождение значения среднего времени между запросами.
                 TimeSpan elapsed = DateTime.Parse(row["lastLoginDateTime"].ToString()) - DateTime.Parse(row["registerDateTime"].ToString());
                 double timeBetweenRequests = elapsed.TotalSeconds/Convert.ToInt32(row["amountOfQueries"]);
                 str.Append("<div class = 'col-md-3'><p>Average time between querys "
                         + timeBetweenRequests + "с</p></div>");

                 str.Append("</div>");
             } 
 
             ViewBag.Grid = str;  

             return View(); 
         }