Add article creation

This commit is contained in:
Bruno Rybársky 2024-02-22 10:05:09 +01:00
parent 06feb93095
commit 5be90d8e95
3 changed files with 38 additions and 3 deletions

16
endpoints/newsarticle.php Normal file

@ -0,0 +1,16 @@
<?php
require_once "lib/newsarticle.php";
function endpoint($endpoint_data): array
{
return match ($endpoint_data["action"]) {
"getNewsArticles" => getNewsArticles(),
"addNewsArticle" => addNewsArticle(
$endpoint_data["title"],
$endpoint_data["body"]
),
default => ["Status" => "Fail", "message" => "Invalid action"],
};
}

@ -89,10 +89,11 @@ function verifyPassword($userID, $password): bool
function UpdateSession(): void function UpdateSession(): void
{ {
global $mysqli; global $mysqli;
$stmt = $mysqli->prepare("SELECT FirstName, LastName, Nickname, Email, MinecraftNick, PrivilegeLevel, LastLoginAt, LoginCount, ClassID, FavoriteColor FROM Users WHERE ID = ? AND isActivated = 1"); $stmt = $mysqli->prepare("SELECT ID, FirstName, LastName, Nickname, Email, MinecraftNick, PrivilegeLevel, LastLoginAt, LoginCount, ClassID, FavoriteColor FROM Users WHERE ID = ? AND isActivated = 1");
$stmt->bind_param("i", $_SESSION["ID"]); $stmt->bind_param("i", $_SESSION["ID"]);
$stmt->execute(); $stmt->execute();
$id = 0;
$first_name = ""; $first_name = "";
$last_name = ""; $last_name = "";
$nickname = ""; $nickname = "";
@ -103,10 +104,11 @@ function UpdateSession(): void
$favorite_color = 0; $favorite_color = 0;
$lastLoginAt = null; $lastLoginAt = null;
$loginCount = 0; $loginCount = 0;
$stmt->bind_result($first_name, $last_name, $nickname, $email, $minecraft_nickname, $privilege_level, $lastLoginAt, $loginCount, $class_id, $favorite_color); $stmt->bind_result($id, $first_name, $last_name, $nickname, $email, $minecraft_nickname, $privilege_level, $lastLoginAt, $loginCount, $class_id, $favorite_color);
$stmt->fetch(); $stmt->fetch();
$stmt->close(); $stmt->close();
$_SESSION["id"] = $id;
$_SESSION["first_name"] = $first_name; $_SESSION["first_name"] = $first_name;
$_SESSION["last_name"] = $last_name; $_SESSION["last_name"] = $last_name;
$_SESSION["nickname"] = $nickname; $_SESSION["nickname"] = $nickname;

@ -6,7 +6,7 @@ function getNewsArticles() :array
$articles = []; $articles = [];
if (isLoggedIn()) { if (isLoggedIn()) {
$result = $mysqli->query("SELECT NewsArticles.ID, NewsArticles.WrittenAt, NewsArticles.WrittenBy, NewsArticles.Title, NewsArticles.Body, NewsArticles.FileList Users.Nickname FROM NewsArticles INNER JOIN Users ON NewsArticles.WrittenBy = Users.ID; "); $result = $mysqli->query("SELECT NewsArticles.ID, NewsArticles.WrittenAt, NewsArticles.WrittenBy, NewsArticles.Title, NewsArticles.Body, NewsArticles.FileList, Users.Nickname FROM NewsArticles INNER JOIN Users ON NewsArticles.WrittenBy = Users.ID; ");
// Check if the query executed Successfully // Check if the query executed Successfully
if ($result) { if ($result) {
@ -18,3 +18,20 @@ function getNewsArticles() :array
return $articles; return $articles;
} }
function addNewsArticle($title="Nazov", $body="Obsah") :array
{
global $mysqli;
$output = ["Status" => "Fail"]; // Default Status is "Fail"
if (isLoggedIn()) {
$query = $mysqli->prepare("INSERT INTO NewsArticles (WrittenBy, Title, Body, FileList) VALUES (?, ?, ?, ?);");
$query->bind_params("issi", $_SESSION["id"], htmlspecialchars($title), htmlspecialchars($body), 0);
$query->execute();
if ($query->affected_rows > 0) {
$output["Status"] = "Success";
}
}
$query->close();
return $output;
}