Diff between 2d9c9a72970f6f56aff747ced5d0800633d8e947 and a6da4f5d08ca752d6d3c2a1a25d84a819b60f2c7

Changed Files

File Additions Deletions Status
.gitignore +1 -0 modified
src/main.cpp +109 -11 modified
static/images/image.jpg +0 -0 added
static/images/image.png +0 -0 added
static/style.css +14 -0 added
templates/admin.html +27 -0 added
templates/index.html +59 -3 modified
templates/login.html +21 -0 added

Full Patch

diff --git a/.gitignore b/.gitignore
index 567609b..a4fb4fb 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1 +1,2 @@
 build/
+.cache/
diff --git a/src/main.cpp b/src/main.cpp
index ba7cd0f..ddd3ac1 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -1,34 +1,130 @@
 #include "crow.h"
+#include "crow/http_response.h"
+#include "crow/middlewares/cookie_parser.h"
+#include "crow/middlewares/session.h"
+#include "crow/mustache.h"
 #include <pqxx/pqxx>
 #include <vector>
 
+struct Asunto {
+	std::string osoite;
+	std::string kuvaPolku;
+};
+
+bool logged_in(const std::string& name, const std::string& password) {
+	try
+    {
+		pqxx::connection cx("dbname = postgres user = postgres password = 1234 \
+      hostaddr = 127.0.0.1 port = 5432");
+        if (not cx.is_open())
+        {
+			CROW_LOG_CRITICAL << "Can't open database";
+            return false;
+        }
+		// Connection established
+		CROW_LOG_INFO << "Opened database successfully: " << cx.dbname();
+		// Etsi kayttajan tiedot
+		CROW_LOG_INFO << name;
+		pqxx::work tx(cx);
+		pqxx::row r = tx.exec(
+			"SELECT * FROM kayttajat_table WHERE nimi='$1'",
+			pqxx::params{name}
+			).one_row();
+		tx.commit();
+		// Tarkista tiedot
+		return r[2].as<std::string>() == password;
+    }
+    catch (const std::exception &e)
+    {
+		CROW_LOG_ERROR << e.what();
+        return false;
+    }
+	return true;
+}
+
 int main()
 {
-    crow::SimpleApp app; //define your crow application
-	std::vector<std::string> asunnot;
+	// Typedef
+	using Session = crow::SessionMiddleware<crow::InMemoryStore>;
+
+	// Crow App
+    crow::App<crow::CookieParser, Session> app{Session{
+		crow::CookieParser::Cookie("session").max_age(24 * 60 * 60).path("/"),
+		AUTA MUA,
+		crow::InMemoryStore{}}};
+	// Asunnot lista
+	std::vector<Asunto> asunnot;
 
-    //define your endpoint at the root directory
     CROW_ROUTE(app, "/")([&asunnot](){
 		auto page = crow::mustache::load("index.html");
 		crow::json::wvalue asunnotJson;
 		for (int i = 0; i < asunnot.size(); ++i) {
-			asunnotJson[i] = asunnot.at(i);
+			asunnotJson[i]["osoite"] = asunnot.at(i).osoite;
+			asunnotJson[i]["kuva"] = asunnot.at(i).kuvaPolku;
 		}
 		crow::mustache::context ctx({{"asunnot", asunnotJson}});
         return page.render(ctx);
     });
 
+	CROW_ROUTE(app, "/login")
+		.methods("GET"_method, "POST"_method)([&app](const crow::request& req, crow::response& res){
+		auto& session = app.get_context<Session>(req);
+
+		if (req.method == "POST"_method) { // Jos POST request
+			if (req.get_body_params().get("name") == nullptr or req.get_body_params().get("pass") == nullptr) {
+				CROW_LOG_ERROR << "Incorrect parameters supplied to post request";
+				res.redirect("/");
+				res.end();
+			}
+			// Hanki params
+			std::string reqName = req.get_body_params().get("name");
+			std::string reqPass = req.get_body_params().get("pass");
+			// Aseta session muuttujat
+			session.set("name", reqName);
+			session.set("pass", reqPass);
+			// Kun POST request ohi, redirect admin sivulle
+
+		}
+		crow::mustache::context ctx;
+		auto page = crow::mustache::load("login.html");
+		if (session.contains("name") and session.contains("pass")) { // Näytä kirjautumistiedot jos kirjautunut
+			ctx["name"] = session.get("name", "_HELP_");
+			ctx["pass"] = session.get("pass", "_HELP_");
+		}
+		res.body = page.render(ctx).body_;
+		res.end();
+	});
+
+	CROW_ROUTE(app, "/admin")
+		.methods("GET"_method, "POST"_method)([&app](const crow::request& req, crow::response& res){
+		auto& session = app.get_context<Session>(req);
+
+		if (not logged_in(session.get("name","_NO_"), session.get("pass","_NO_"))) {
+			// Jos ei kirjauduttu sisään
+			res.redirect("/");
+			res.end();
+		}
+
+		if (req.method == "POST"_method) { // Jos POST request
+			
+		}
+
+		auto page = crow::mustache::load("admin.html");
+		res.body = page.render().body_;
+		res.end();
+	});
+
 	try
     {
 		pqxx::connection cx("dbname = postgres user = postgres password = 1234 \
       hostaddr = 127.0.0.1 port = 5432");
         if (not cx.is_open())
         {
-			CROW_LOG_ERROR << "Can't open database" << "\n";
+			CROW_LOG_CRITICAL << "Can't open database";
             return EXIT_FAILURE;
         }
 		// Connection established
-		CROW_LOG_INFO << "Opened database successfully: " << cx.dbname() << "\n";
+		CROW_LOG_INFO << "Opened database successfully: " << cx.dbname();
 		// Do work
 		pqxx::work tx(cx);
 
@@ -37,18 +133,20 @@ int main()
 		tx.commit();
 
 		for (auto s : r) {
-			const std::string str = s[0].as<std::string>();
-			CROW_LOG_INFO << str << "\n";
-			asunnot.push_back( str );
+			struct Asunto asunto;
+			asunto.osoite = s[0].as<std::string>();
+			asunto.kuvaPolku = s[2].as<std::string>();
+			//CROW_LOG_INFO << asunto.osoite << ":" << asunto.kuvaPolku << "\n";
+			asunnot.push_back( asunto );
 		}
     }
     catch (const std::exception &e)
     {
-		CROW_LOG_CRITICAL << e.what() << "\n";
+		CROW_LOG_ERROR << e.what();
         return EXIT_FAILURE;
     }
 
-    //set the port, set the app to run on multiple threads, and run the app
+    // :D
     app.port(18080).multithreaded().run();
 }
 
diff --git a/static/images/image.jpg b/static/images/image.jpg
new file mode 100644
index 0000000..93b55f3
Binary files /dev/null and b/static/images/image.jpg differ
diff --git a/static/images/image.png b/static/images/image.png
new file mode 100644
index 0000000..309d5ae
Binary files /dev/null and b/static/images/image.png differ
diff --git a/static/style.css b/static/style.css
new file mode 100644
index 0000000..d8049e1
--- /dev/null
+++ b/static/style.css
@@ -0,0 +1,14 @@
+.bg {
+	background-size: cover;
+}
+.h-good {
+	height: 12em;
+}
+nav {
+	width: 100%;
+}
+
+img {
+	max-width: 100%;
+	max-height: 100%;
+}
diff --git a/templates/admin.html b/templates/admin.html
new file mode 100644
index 0000000..015b650
--- /dev/null
+++ b/templates/admin.html
@@ -0,0 +1,27 @@
+<!DOCTYPE html>
+<html lang="fi">
+	<head>
+		<meta charset="UTF-8" />
+		<meta name="viewport" content="width=device-width, initial-scale=1" />
+		<title>Admin</title>
+	</head>
+	<body>
+		<div>
+			Tietoa meistä:
+			<form action="/admin" method="POST" accept-charset="utf-8">
+				<textarea name="info" id="info">{{ info }}</textarea>
+				<input type="submit" value="Muokkaa" />
+			</form>
+		</div>
+		<div>
+			Lisää asuntola
+			<form action="/admin" method="POST" accept-charset="utf-8">
+				<label for="name">Asunnon nimi</label> <br>
+				<input type="text" name="name" id="name" value=""> <br>
+				<label for="image">Kuva asunnosta</label> <br>
+				<input type="file" name="image" id="image" value=""> <br>
+				<input type="submit" value="Lisää asunto" />
+			</form>
+		</div>
+	</body>
+</html>
diff --git a/templates/index.html b/templates/index.html
index e5d3111..4cefa94 100644
--- a/templates/index.html
+++ b/templates/index.html
@@ -3,11 +3,67 @@
 	<head>
 		<meta charset="UTF-8" />
 		<meta name="viewport" content="width=device-width, initial-scale=1" />
+		<link rel="stylesheet" href="/static/style.css" type="text/css" media="all">
+		<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.8/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-sRIl4kxILFvY47J16cr9ZwB07vP4J8+LH7qKQnuqkuIAvNWLzeN8tE5YBujZqJLB" crossorigin="anonymous">
+		<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.8/dist/js/bootstrap.bundle.min.js" integrity="sha384-FKyoEForCGlyvwx9Hj09JcYn3nv7wiPVlz7YYwJrWVcXK/BmnVDxM+D2scQbITxI" crossorigin="anonymous"></script>
 		<title>Index</title>
 	</head>
 	<body>
-		{{#asunnot}}
-			<p>{{.}}</p>
-		{{/asunnot}}
+		<div style="container-fluid">
+			<header class="navbar navbar-expand-md bd-navbar fixed-top bg-white">
+				<nav class="navbar flex-wrap flex-md-nowrap container">
+					<div class="navbar-nav flex-row flex-wrap bd-navbar-nav">
+						<h1>Koivuhaka asuntolat</h1>
+					</div>
+					<ul class="navbar-nav flex-row flex-wrap bd-navbar ms-md-auto">
+						<li class="nav-item">
+							<a class="nav-link p-2">Asunnot</a>
+						</li>
+						<li class="nav-item">
+							<a class="nav-link p-2">Tietoa meistä</a>
+						</li>
+						<li class="nav-item">
+							<a class="nav-link p-2">Ota yhteyttä</a>
+						</li>
+
+					</ul>
+				</nav>
+			</header>
+			<img class="img-fluid" src="https://wisdurm.fi/static/images/sign.webp">
+			<div class="container">
+				<h1 class="pt-5 pb-5">Asunnot</h1>
+				<div class="row">
+					{{#asunnot}}
+					<div class="col bg h-good p-0 m-1" style="background-image: url('/static/images/{{ kuva }}');">
+							<div class="text-bg-dark w-50">
+								<p>{{osoite}}</p>
+							</div>
+						</div>
+					{{/asunnot}}
+				</div>
+				<h2 class="pt-5 pb-3">Tietoa meistä</h2>
+				<p>{{ tieto }}</p>
+				<h2 class="pt-3 pb-4 text-center">Meidän tiimi</h2>
+				<div class="row pb-5">
+					<div class="col">
+						<img src="https://wisdurm.fi/static/images/sign.webp" alt="" class="rounded-circle" />	
+						<p class="text-center pt-2">Jaakko Pekka</p>
+					</div>
+					<div class="col">
+						<img src="https://wisdurm.fi/static/images/sign.webp" alt="" class="rounded-circle" />
+						<p class="text-center pt-2">Jaakko Pekka</p>
+					</div>
+					<div class="col">
+						<img src="https://wisdurm.fi/static/images/sign.webp" alt="" class="rounded-circle" />
+						<p class="text-center pt-2">Jaakko Pekka</p>
+					</div>
+					<div class="col">
+						<img src="https://wisdurm.fi/static/images/sign.webp" alt="" class="rounded-circle" />
+						<p class="text-center pt-2">Jaakko Pekka</p>
+					</div>
+				</div>
+			</div>
+			</div>
+		</div>
 	</body>
 </html>
diff --git a/templates/login.html b/templates/login.html
new file mode 100644
index 0000000..03de722
--- /dev/null
+++ b/templates/login.html
@@ -0,0 +1,21 @@
+<!DOCTYPE html>
+<html lang="fi">
+	<head>
+		<meta charset="utf-8">
+		<meta name="viewport" content="width=device-width">
+		<title></title>
+	</head>
+	<body>
+		{{ #name }}
+		<p>Kirjauduttu sisään, tervetuloa {{ name }}!</p>
+		{{ /name }}
+		<form action="/login" method="POST" accept-charset="utf-8">
+			<label for="name">Nimi</label><br>
+			<input type="text" name="name" id="name" value=""><br>
+			<label for="pass">Salasana</label><br>
+			<input type="password" name="pass" id="pass" value=""><br>
+			<button type="submit">Log in</button>
+		</form>
+	</body>
+</html>
+