Esempio n. 1
0
        private static void DispatchGet(HttpListenerContext context)
        {
            var request = context.Request;
            var response = context.Response;

            response.ContentType = "text/html";

            var query = HttpUtility.ParseQueryString(request.Url.Query);

            int postID = 0;
            int editorID = 0;
            int currentPage = 1;

            if (query["page"] != null)
            {
                int.TryParse(query["page"], out currentPage);
            }
            if (query["post"] != null)
            {
                int.TryParse(query["post"], out postID);
                currentPage = 0;
            }
            if (query["editor"] != null)
            {
                int.TryParse(query["editor"], out editorID);
            }

            using (var sw = new StreamWriter(response.OutputStream, Encoding.UTF8))
            {
                var indexPage = new IndexPage(markdown);
                indexPage.Query = query;
                indexPage.CurrentPage = currentPage;
                indexPage.NumberOfPages = (int)Math.Floor((double)GetEntryCount(editorID) / ServerSettings.Default.NumberOfPostsPerPage - 0.0001) + 1;

                if (indexPage.CurrentPage > indexPage.NumberOfPages)
                    indexPage.CurrentPage = indexPage.NumberOfPages;

                int startPost = 0;
                int numPosts = ServerSettings.Default.NumberOfPostsPerPage;

                if (indexPage.CurrentPage > 0)
                {
                    startPost = numPosts * (indexPage.CurrentPage - 1);
                }

                using (var cmd = db.CreateCommand())
                {
                    cmd.CommandText =
                        @"SELECT
                                `Entries`.`ID`, `Entries`.`Text`, `Entries`.`TimeStamp`, `Writers`.`Name`
                            FROM
                                `Entries`,`Writers`
                            WHERE
                                `Entries`.`Editor`=`Writers`.`ID`" +
                        (postID != 0 ? "AND `Entries`.`ID` = @postId \n" : "") +
                        (editorID != 0 ? "AND `Entries`.`Editor` = @editorId \n" : "") +
                        @" ORDER BY `Entries`.`TimeStamp` DESC
                            LIMIT @start, @count";
                    cmd.Parameters.AddWithValue("@start", startPost);
                    cmd.Parameters.AddWithValue("@count", numPosts);
                    cmd.Parameters.AddWithValue("@editorId", editorID);
                    cmd.Parameters.AddWithValue("@postId", postID);
                    using (var reader = cmd.ExecuteReader())
                    {
                        while (reader.Read())
                        {
                            var entry = new BlogEntry();
                            entry.ID = reader.GetInt32(0);
                            entry.Text = reader.GetString(1);
                            entry.CreationDate = DateTime.FromBinary(reader.GetInt64(2));
                            entry.Author = reader.GetString(3);
                            indexPage.Entries.Add(entry);
                        }
                    }
                }

                sw.Write(indexPage.TransformText());
                sw.Flush();
            }
        }