Diff between a56cbcbf2ed7e5236d0ad0de14079b490a4dc3fc and d51cbc19cf2c60f00a5ec30d6c9d8db6fbda6502

Changed Files

File Additions Deletions Status
info.txt +2 -0 added
src/main.cpp +45 -11 modified
static/images/d8yutup1drfsg7kp5ntk.jpg +0 -0 added
static/style.css +3 -0 modified
templates/index.html +6 -4 modified

Full Patch

diff --git a/info.txt b/info.txt
new file mode 100644
index 0000000..4f2d4c3
--- /dev/null
+++ b/info.txt
@@ -0,0 +1,2 @@
+Olemme Jaakon alaisia
+
diff --git a/src/main.cpp b/src/main.cpp
index 876f325..5139375 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -66,8 +66,8 @@ namespace utils
 }
 
 struct Asunto {
-	std::string osoite;
-	std::string kuvaPolku;
+	const std::string osoite;
+	const std::string kuvaPolku;
 };
 
 int main()
@@ -80,16 +80,18 @@ int main()
 		crow::FileStore{"./sessions"}}};
 	// Asunnot lista
 	std::vector<Asunto> asunnot;
+	// "Tietoa meistä"
+	std::string info;
 
 	CROW_ROUTE(app, "/")
-		.methods("GET"_method, "POST"_method)([&asunnot](){
+		.methods("GET"_method, "POST"_method)([&asunnot, &info](){
 		auto page = crow::mustache::load("index.html");
 		crow::json::wvalue asunnotJson;
 		for (int i = 0; i < asunnot.size(); ++i) {
 			asunnotJson[i]["osoite"] = asunnot.at(i).osoite;
 			asunnotJson[i]["kuva"] = asunnot.at(i).kuvaPolku;
 		}
-		crow::mustache::context ctx({{"asunnot", asunnotJson}});
+		crow::mustache::context ctx({{"asunnot", asunnotJson}, {"tieto", info}});
 		return page.render(ctx);
 	});
 
@@ -131,7 +133,7 @@ int main()
 	});
 
 	CROW_ROUTE(app, "/admin")
-		.methods("GET"_method, "POST"_method)([&app](const crow::request& req, crow::response& res){
+		.methods("GET"_method, "POST"_method)([&app, &info](const crow::request& req, crow::response& res){
 		auto& session = app.get_context<Session>(req);
 
 		if (session.get("name","_NO_") == "_NO_") {
@@ -141,9 +143,30 @@ int main()
 			return;
 		}
 		// Jatka jos kirjautunut sisään
+		if (req.method == "POST"_method) { // Jos POST request
+			// Muuta "Tietoa meistä"
+			info = req.get_body_params().get("info");
+			// Kirjoita tieto info.txt tiedostoon
+			std::ofstream infoTxt("info.txt", std::ios::out | std::ios::trunc);
+			if (not infoTxt.is_open()) {
+				CROW_LOG_ERROR << "Cannot open info.txt";
+				res.redirect("/");
+				res.code = 500;
+				res.end();
+				return;
+			}
+			// Kirjoita
+			infoTxt << info + "\n";
+			infoTxt.close();
+			// Valmista ollaan
+			res.redirect("/");
+			res.end();
+			return;
+		}
 		// Renderöi sivu
 		auto page = crow::mustache::load("admin.html");
-		res.body = page.render().body_;
+		crow::mustache::context ctx({{"info", info}});
+		res.body = page.render(ctx).body_;
 		res.end();
 	});
 
@@ -211,7 +234,7 @@ int main()
 			}
 			// Write file to hard drive
 			std::ofstream file(outfilePath);
-			if (file.fail()) {
+			if (not file.is_open()) {
 				CROW_LOG_ERROR << "Cannot write file";
 				res.code = 500;
 				res.redirect("/");
@@ -273,7 +296,7 @@ int main()
 		return;
 	});
 
-
+	// Lue tietokannasta asunnot
 	try
 	{
 		pqxx::connection cx("dbname = postgres user = postgres password = 1234 \
@@ -293,9 +316,9 @@ int main()
 		tx.commit();
 
 		for (auto s : r) {
-			struct Asunto asunto;
-			asunto.osoite = s[0].as<std::string>();
-			asunto.kuvaPolku = s[2].as<std::string>();
+			const struct Asunto asunto(
+				s[0].as<std::string>(),
+				s[2].as<std::string>());
 			//CROW_LOG_INFO << asunto.osoite << ":" << asunto.kuvaPolku << "\n";
 			asunnot.push_back( asunto );
 		}
@@ -305,6 +328,17 @@ int main()
 		CROW_LOG_ERROR << e.what();
 		return EXIT_FAILURE;
 	}
+	// Lue tiedostosta "Tietoa meistä"
+	std::ifstream infoTxt("info.txt", std::ios::in);
+	if (not infoTxt.is_open()) {
+		CROW_LOG_ERROR << "Unable to open info.txt";
+		return EXIT_FAILURE;
+	}
+	std::string line;
+	while (getline(infoTxt, line)) {
+		info += line += "\n";
+	}
+	infoTxt.close();
 
 	// :D
 	//app.loglevel(crow::LogLevel::Debug);
diff --git a/static/images/d8yutup1drfsg7kp5ntk.jpg b/static/images/d8yutup1drfsg7kp5ntk.jpg
new file mode 100644
index 0000000..98fe140
Binary files /dev/null and b/static/images/d8yutup1drfsg7kp5ntk.jpg differ
diff --git a/static/style.css b/static/style.css
index d8049e1..9ccf9e2 100644
--- a/static/style.css
+++ b/static/style.css
@@ -12,3 +12,6 @@ img {
 	max-width: 100%;
 	max-height: 100%;
 }
+.img-full {
+	width: 100%;
+}
diff --git a/templates/index.html b/templates/index.html
index 4cefa94..e1684b4 100644
--- a/templates/index.html
+++ b/templates/index.html
@@ -29,13 +29,15 @@
 					</ul>
 				</nav>
 			</header>
-			<img class="img-fluid" src="https://wisdurm.fi/static/images/sign.webp">
+			<div class="container-fluid">
+				<img class="img-full" src="https://www.gradia.fi/sites/default/files/styles/image_paragraph_large/public/media/embed/20240610_Jyv%C3%A4skyl%C3%A4n%20asuntola.jpg?itok=wGzrX8_C">
+			</div>
 			<div class="container">
 				<h1 class="pt-5 pb-5">Asunnot</h1>
-				<div class="row">
+				<div class="row w-100">
 					{{#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">
+					<div class="col-md-3 bg h-good p-0 m-0" style="background-image: url('/static/images/{{ kuva }}');">
+							<div class="text-bg-dark w-75">
 								<p>{{osoite}}</p>
 							</div>
 						</div>