From 47143582e06acbc0776998fb5d4d678579123165 Mon Sep 17 00:00:00 2001 From: Will Brown Date: Sat, 23 Nov 2024 16:47:18 -0500 Subject: [PATCH] Add zblock_feed_info_delete() --- feed_info.c | 38 +++++++++++++++++++++++++++++++++++++- feed_info.h | 3 +++ 2 files changed, 40 insertions(+), 1 deletion(-) diff --git a/feed_info.c b/feed_info.c index 97befac..5000ff4 100644 --- a/feed_info.c +++ b/feed_info.c @@ -3,6 +3,8 @@ #include #include +#include +#include #include #include #include @@ -66,7 +68,7 @@ zblock_feed_info_err zblock_feed_info_insert(PGconn *conn, zblock_feed_info *fee // check if the feed already exists { - int feed_exists = 0; + int feed_exists; zblock_feed_info_err exists_error = zblock_feed_info_exists(conn, feed->url, feed->channel_id, &feed_exists); if (exists_error) { return exists_error; @@ -96,6 +98,40 @@ zblock_feed_info_err zblock_feed_info_insert(PGconn *conn, zblock_feed_info *fee return result; } +// deletes feed from the database +zblock_feed_info_err zblock_feed_info_delete(PGconn *conn, const char *url, u64snowflake channel_id) { + if (!conn) return ZBLOCK_FEED_INFO_INVALID_ARGS; + + // check if the feed already exists + { + int feed_exists; + zblock_feed_info_err exists_error = zblock_feed_info_exists(conn, url, channel_id, &feed_exists); + if (exists_error) { + return exists_error; + } else if (!feed_exists) { + return ZBLOCK_FEED_INFO_NOT_EXIST; + } + } + + // I don't want to deal with the extra fuss that is sending these in binary format + char channel_id_str[21]; + snprintf(channel_id_str, sizeof(channel_id_str), "%" PRIu64, channel_id); + const char *const params[] = {url, channel_id_str}; + PGresult *res = PQexecParams(conn, + "DELETE FROM feeds WHERE url = $1 AND channel_id = $2", + 2, NULL, params, NULL, NULL, 0 + ); + + zblock_feed_info_err result = ZBLOCK_FEED_INFO_OK; + if (PQresultStatus(res) != PGRES_COMMAND_OK) { + log_error(PQresultErrorMessage(res)); + result = ZBLOCK_FEED_INFO_DBERROR; + } + + PQclear(res); + return result; +} + // updates the last_pubDate field of a given feed in the database zblock_feed_info_err zblock_feed_info_update(PGconn *conn, zblock_feed_info_minimal *feed) { if (!conn || !feed) return ZBLOCK_FEED_INFO_INVALID_ARGS; diff --git a/feed_info.h b/feed_info.h index 7167e48..be57fa7 100644 --- a/feed_info.h +++ b/feed_info.h @@ -42,6 +42,9 @@ zblock_feed_info_err zblock_feed_info_exists(PGconn *conn, const char *url, u64s // Insert new feed into the database zblock_feed_info_err zblock_feed_info_insert(PGconn *conn, zblock_feed_info *feed); +// deletes feed from the database +zblock_feed_info_err zblock_feed_info_delete(PGconn *conn, const char *url, u64snowflake channel_id); + // updates the last_pubDate field of a given feed in the database zblock_feed_info_err zblock_feed_info_update(PGconn *conn, zblock_feed_info_minimal *feed);