예제 #1
0
        static void Main()
        {
            connectionStringBuilder.DataSource = "./database.db";

            Route.Add("/", (request, response, args) => {
                response.AsText("Hello, World!");
            }, "GET");

            Route.Add("/items", (request, response, args) => {
                response.AsText($"{style}{getItems()}");
            }, "GET");

            Route.Add("/items", (request, response, args) => {
                request.ParseBody(args);
                RunQuery($@"
                    INSERT into items (name, price, container_id)
                    VALUES ('{args["name"]}', '{args["price"]}', {args["container_id"]});
                ");
                response.AsText($"{style}{getItems()}");
            }, "POST");

            //run the server
            int port = 8000;

            Console.WriteLine($"Running Server On http://127.0.0.1:{port}");
            HttpServer.ListenAsync(port, CancellationToken.None, Route.OnHttpRequestAsync).Wait();
        }
예제 #2
0
        static void Main()
        {
            connectionStringBuilder.DataSource = "./database.db";

            Route.Add("/", (request, response, args) => {
                response.AsText("Hello, World!");
            }, "GET");

            Route.Add("/items", (request, response, args) => {
                response.AsText($"{style}{getItems()}");
            }, "GET");

            Route.Add("/items", (request, response, args) => {
                request.ParseBody(args);
                if (args.ContainsKey("_method") && args["_method"] == "DELETE")
                {
                    RunQuery($@"
                        DELETE FROM items 
                        WHERE id = {args["id"]};
                    ");
                }
                else if (args.ContainsKey("_method") && args["_method"] == "UPDATE")
                {
                    RunQuery($@"
                        UPDATE items
                        SET price = {args["price"]}
                        WHERE id = {args["id"]};
                    ");
                }
                else
                {
                    RunQuery($@"
                        INSERT into items (name, price, container_id)
                        VALUES ('{args["name"]}', '{args["price"]}', {args["container_id"]});
                    ");
                }
                response.AsText($"{style}{getItems()}");
            }, "POST");

            Route.Add("/items/update", (request, response, args) => {
                response.AsText($@"
                    <form method='POST' action='/items'>
                        <input type='hidden' name='id' value='{"id"}'>
                        <input type='hidden' name='_method' value='UPDATE'>
                        <label>New Price
                            <input type='text' name='price'>
                        </label>
                        <input type='submit' value='Update'>
                    </form>
                ");
            }, "POST");

            //run the server
            int port = 8001;

            Console.WriteLine($"Running Server On http://127.0.0.1:{port}");
            HttpServer.ListenAsync(port, CancellationToken.None, Route.OnHttpRequestAsync).Wait();
        }
예제 #3
0
        static void Main()
        {
            connectionStringBuilder.DataSource = "./databass.db";

            // keeping below for testing
            Route.Add("/", (request, response, args) => {
                response.AsText("Hello, World!");
            }, "GET");

            // adding a bit of style
            Route.Add("/tasks", (request, response, args) => {
                response.AsText($"{style}{getItems()}");
            }, "GET");

            // show tasks and their id
            Route.Add("/tasks", (request, response, args) => {
                request.ParseBody(args);
                if (args.ContainsKey("_method") && args["_method"] == "DELETE")
                {
                    RunQuery($@"
                        DELETE FROM todo
                        WHERE task_id = ('{args["task_id"]}');
                    ");
                }
                else
                {
                    RunQuery($@"
                        INSERT into todo (task)
                        VALUES ('{args["task"]}');
                    ");
                }

                response.AsText($"{style}{getItems()}");
            }, "POST");

            //run the server
            int port = 8000;

            Console.WriteLine($"Running Server On http://127.0.0.1:{port}");
            HttpServer.ListenAsync(port, CancellationToken.None, Route.OnHttpRequestAsync).Wait();
        }
예제 #4
0
        static void Main()
        {
            connectionStringBuilder.DataSource = "./database.db";

            Route.Add("/items", (request, response, args) => {
                response.AsText(getItems());
            });

            Route.Add("/items", (request, response, args) => {
                RunQuery($@"
                    INSERT into items (name, price, container_id)
                    VALUES ('milk', '2.99', 1);
                ");
                response.AsText(getItems());
            }, "POST");

            //run the server
            int port = 8000;

            Console.WriteLine($"Running Server On http://127.0.0.1:{port}");
            HttpServer.ListenAsync(port, CancellationToken.None, Route.OnHttpRequestAsync).Wait();
        }
예제 #5
0
        static void Main()
        {
            connectionStringBuilder.DataSource = "./database.db";

            Route.Add("/", (request, response, args) => {
                response.AsText("Hello, World!");
            }, "GET");

            Route.Add("/items", (request, response, args) => {
                response.AsText($"{style}{getItems()}");
            }, "GET");

            Route.Add("/items", (request, response, args) => {
                request.ParseBody(args);
                if (args.ContainsKey("_method") && args["_method"] == "DELETE")
                {
                    RunQuery($@"
                        DELETE FROM items 
                        WHERE id = {args["id"]};
                    ");
                }
                else if (args.ContainsKey("_method") && args["_method"] == "UPDATE")
                {
                    RunQuery($@"
                        UPDATE items
                        SET price = {args["price"]}
                        WHERE id = {args["id"]};
                    ");
                }
                else
                {
                    RunQuery($@"
                        INSERT into items (name, price, container_id)
                        VALUES ('{args["name"]}', '{args["price"]}', {args["container_id"]});
                    ");
                }
                response.AsText($"{style}{getItems()}");
            }, "POST");

            Route.Add("/items/update", (request, response, args) => {
                var results = RunQuery($@"  
                    SELECT *
                    FROM items;
                ");
                string html = $@"
                    {String.Join("", results.Select(item => $@"
                        <form method='POST' action='/items'>
                            <input type='hidden' name='id' value='{item["id"]}'>
                            <input type='hidden' name='_method' value='UPDATE'>
                            <label>{item["name"]}: ${item["price"]}; New price:
                                $<input type='text' name='price'>
                            </label>
                            <input type='submit' value='Update'>
                        </form>
                    "))}
                ";
                response.AsText(html);
            }, "GET");

            // WHY DOES THE BELOW POST ROUTE THROW KEY NOT FOUND IN DICTIONARY ERROR?

            // Route.Add("/items/update", (request, response, args) => {
            //     var results = RunQuery($@"
            //         SELECT *
            //         FROM items
            //         WHERE id = {args["id"]}; <-- THIS VARIABLE IS THE CULPRIT, BUT WHY?
            //     ");
            //     string html = $@"
            //         {String.Join("", results.Select(item => $@"
            //             <form method='POST' action='/items'>
            //                 <input type='hidden' name='id' value='{item["id"]}'>
            //                 <input type='hidden' name='_method' value='UPDATE'>
            //                 <label>{item["name"]}: ${item["price"]}; New price:
            //                     $<input type='text' name='price'>
            //                 </label>
            //                 <input type='submit' value='Update'>
            //             </form>
            //         "))}
            //     ";
            //     response.AsText(html);
            // }, "POST");


            //run the server
            int port = 8000;

            Console.WriteLine($"Running Server On http://127.0.0.1:{port}");
            HttpServer.ListenAsync(port, CancellationToken.None, Route.OnHttpRequestAsync).Wait();
        }