Add zblock_feed_info_delete()

This commit is contained in:
2024-11-23 16:47:18 -05:00
parent ca6d37a1cf
commit 47143582e0
2 changed files with 40 additions and 1 deletions

View File

@ -3,6 +3,8 @@
#include <assert.h>
#include <stdio.h>
#include <stdint.h>
#include <inttypes.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
@ -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;

View File

@ -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);