Skip to content

Commit

Permalink
Fixed deletion of notices at expiry
Browse files Browse the repository at this point in the history
  • Loading branch information
Shaswat975 committed Sep 16, 2024
1 parent 6d37987 commit 78796d7
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 16 deletions.
10 changes: 6 additions & 4 deletions resources/lib/UnitySQL.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,14 +126,15 @@ public function deleteRequestsByUser($user)
$stmt->execute();
}

public function addNotice($title, $date, $content, $operator)
public function addNotice($title, $date, $content, $expiry, $operator)
{
$stmt = $this->conn->prepare(
"INSERT INTO " . self::TABLE_NOTICES . " (date, title, message) VALUES (:date, :title, :message)"
"INSERT INTO " . self::TABLE_NOTICES . " (date, title, message, expiry) VALUES (:date, :title, :message, :expiry)"

Check warning on line 132 in resources/lib/UnitySQL.php

View workflow job for this annotation

GitHub Actions / PHP_CodeSniffer

resources/lib/UnitySQL.php#L132

Line exceeds 120 characters; contains 126 characters (Generic.Files.LineLength.TooLong)
);
$stmt->bindParam(":date", $date);
$stmt->bindParam(":title", $title);
$stmt->bindParam(":message", $content);
$stmt->bindParam(":expiry", $expiry);

Check failure on line 137 in resources/lib/UnitySQL.php

View workflow job for this annotation

GitHub Actions / PHP_CodeSniffer

resources/lib/UnitySQL.php#L137

Whitespace found at end of line (Squiz.WhiteSpace.SuperfluousWhitespace.EndLine)

$stmt->execute();

Expand All @@ -147,14 +148,15 @@ public function addNotice($title, $date, $content, $operator)
);
}

public function editNotice($id, $title, $date, $content)
public function editNotice($id, $title, $date, $content, $expiry)
{
$stmt = $this->conn->prepare(
"UPDATE " . self::TABLE_NOTICES . " SET date=:date, title=:title, message=:message WHERE id=:id"
"UPDATE " . self::TABLE_NOTICES . " SET date=:date, title=:title, message=:message, expiry=:expiry WHERE id=:id"

Check warning on line 154 in resources/lib/UnitySQL.php

View workflow job for this annotation

GitHub Actions / PHP_CodeSniffer

resources/lib/UnitySQL.php#L154

Line exceeds 120 characters; contains 124 characters (Generic.Files.LineLength.TooLong)
);
$stmt->bindParam(":date", $date);
$stmt->bindParam(":title", $title);
$stmt->bindParam(":message", $content);
$stmt->bindParam(":expiry", $expiry);
$stmt->bindParam(":id", $id);

$stmt->execute();
Expand Down
8 changes: 4 additions & 4 deletions tools/docker-dev/sql/bootstrap.sql
Original file line number Diff line number Diff line change
Expand Up @@ -148,16 +148,16 @@ CREATE TABLE `notices` (
`date` timestamp NOT NULL DEFAULT current_timestamp(),
`title` varchar(300) NOT NULL,
`message` longtext NOT NULL,
`expiry` timestamp NOT NULL DEFAULT DATE_ADD(CURDATE(), INTERVAL 7 DAY)
`expiry` timestamp NOT NULL DEFAULT DATE_ADD(current_timestamp(), INTERVAL 14 DAY)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;

--
-- Dumping data for table `notices`
--

INSERT INTO `notices` (`id`, `date`, `title`, `message`) VALUES
(9, '2022-09-19 15:49:10', 'Example Notice 1', '<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>'),
(10, '2022-09-14 11:48:39', 'Example Notice 2', '<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>', '2024-05-29');
INSERT INTO `notices` (`id`, `date`, `title`, `message`, `expiry`) VALUES
(9, '2022-09-19 15:49:10', 'Example Notice 1', '<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>', '2025-05-29 01:02:03'),
(10, '2024-03-02 00:00:00', 'Example Notice 2', '<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>', '2024-03-02 00:00:00');

-- --------------------------------------------------------

Expand Down
4 changes: 2 additions & 2 deletions webroot/admin/notices.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@
if ($_SERVER["REQUEST_METHOD"] == "POST") {
switch ($_POST["form_type"]) {
case "newNotice":
$SQL->addNotice($_POST["title"], $_POST["date"], $_POST["content"], $USER);
$SQL->addNotice($_POST["title"], $_POST["date"], $_POST["content"], $_POST["expiry"], $USER);

break;
case "editNotice":
$SQL->editNotice($_POST["id"], $_POST["title"], $_POST["date"], $_POST["content"]);
$SQL->editNotice($_POST["id"], $_POST["title"], $_POST["date"], $_POST["content"], $_POST["expiry"]);

break;
case "delNotice":
Expand Down
4 changes: 4 additions & 0 deletions webroot/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@

$notices = $SQL->getNotices();
foreach ($notices as $notice) {
if ($notice["expiry"] < date('Y-m-d')) {
$SQL->deleteNotice($notice["id"]);

This comment has been minimized.

Copy link
@bryank-cs

bryank-cs Sep 17, 2024

Contributor

Doing this here causes a race condition if two page loads retrieve the list of notices and both try to delete the same old items.

continue;
}
echo "<div class='notice'>";
echo "<span class='noticeTitle'>" . $notice["title"] . "</span>";
echo "<span class='noticeDate'>" . date('m-d-Y', strtotime($notice["date"])) . "</span>";
Expand Down
6 changes: 0 additions & 6 deletions workers/notices-expiry-deletion.php

This file was deleted.

0 comments on commit 78796d7

Please sign in to comment.