Retrieve feed, add to database (#4)

This commit is contained in:
2024-04-09 21:16:34 -04:00
parent c89e6af346
commit 5c92c8c7d6
3 changed files with 104 additions and 0 deletions

View File

@@ -96,4 +96,38 @@ class Data {
$query->bindValue(':password', password_hash($password, PASSWORD_DEFAULT));
$query->execute();
}
/**
* Add an RSS feed
* @param string $url The URL for the RSS feed
* @param string $title The title of the RSS feed
* @param string $updatedOn The date/time the RSS feed was last updated (from the XML, not when we checked)
* @return int The ID of the added feed
*/
public static function addFeed(string $url, string $title, string $updatedOn): int {
$db = self::getConnection();
if ($updatedOn) {
try {
$updated = (new DateTimeImmutable($updatedOn))->format(DateTimeInterface::ATOM);
} catch (Exception $ex) {
$updated = null;
}
} else {
$updated = null;
}
$query = $db->prepare('INSERT INTO feed (user_id, url, title, updated_on, checked_on)'
. ' VALUES (:user, :url, :title, :updated, :checked)');
$query->bindValue(':user', $_REQUEST['FRC_USER_ID']);
$query->bindValue(':url', $url);
$query->bindValue(':title', $title);
$query->bindValue(':updated', $updated);
$query->bindValue(':checked', (new DateTimeImmutable())->format(DateTimeInterface::ATOM));
$result = $query->execute();
if ($result) {
$idQuery = $db->prepare('SELECT last_insert_rowid()');
$idResult = $idQuery->execute();
if ($idResult) return $idResult->fetchArray(SQLITE3_NUM)[0];
}
return -1;
}
}