commit 3b100fcd651a2baa259b2765e260aa09ce3af236
parent 551f981f93c8b045b5708f803d241f12dc5cda13
Author: Sophie <sophie@aest.me>
Date: Thu, 17 Oct 2024 17:12:16 +0200
feat: Made label property editable for posts
Diffstat:
3 files changed, 46 insertions(+), 13 deletions(-)
diff --git a/src/edit.c b/src/edit.c
@@ -41,14 +41,18 @@ int edit_read(char *filename, post_t **post) {
line[line_end - line_start] = '\0';
switch (line_nr) {
case 0: {
- (*post)->title = line;
+ (*post)->label = line;
break;
}
case 1: {
- (*post)->summary = line;
+ (*post)->title = line;
break;
}
case 2: {
+ (*post)->summary = line;
+ break;
+ }
+ case 3: {
if (strcmp(line, "true") == 0) {
(*post)->is_public = malloc(sizeof(bool));
*(*post)->is_public = true;
@@ -75,6 +79,9 @@ int edit_read(char *filename, post_t **post) {
int edit_write(char *filename, post_t *post) {
FILE *fp = fopen(filename, "aw+");
+ char label[500];
+ snprintf(label, 500, "%s\r\n", post->label);
+ fwrite(label, sizeof(char), strlen(label), fp);
char title[500];
snprintf(title, 500, "%s\r\n", post->title);
fwrite(title, sizeof(char), strlen(title), fp);
diff --git a/src/post.c b/src/post.c
@@ -8,14 +8,18 @@
#include "post.h"
void post_init(post_t *post) {
+ post->label = NULL;
post->title = NULL;
- post->summary= NULL;
- post->content= NULL;
+ post->summary = NULL;
+ post->content = NULL;
post->date = NULL;
post->is_public = NULL;
}
void post_free(post_t *post) {
+ if (post->label != NULL) {
+ free(post->label);
+ }
if (post->title != NULL) {
free(post->title);
}
@@ -44,6 +48,9 @@ void post_result_free(post_result_t *post_result) {
}
bool post_changed(post_t *post, post_t *changes) {
+ if (changes->label != NULL && strcmp(post->label, changes->label) != 0) {
+ return true;
+ }
if (changes->title != NULL && strcmp(post->title, changes->title) != 0) {
return true;
}
@@ -69,7 +76,7 @@ post_result_t *db_post_search(char *db, char *title) {
PQfinish(conn);
return NULL;
}
- char *query = "SELECT id, title, summary, created_at, is_public FROM posts WHERE lower(title) LIKE lower($1) ORDER BY created_at DESC;";
+ char *query = "SELECT id, label, title, summary, created_at, is_public FROM posts WHERE lower(title) LIKE lower($1) ORDER BY created_at DESC;";
char id_param[10];
snprintf(id_param, 10, "%%%s%%", title);
const char *values[1] = { id_param };
@@ -98,21 +105,26 @@ post_result_t *db_post_search(char *db, char *title) {
post->id = id;
}
if (j == 1) {
+ post->label = malloc(sizeof(char) * (value_len + 1));
+ memcpy(post->label, value, value_len);
+ post->label[value_len] = '\0';
+ }
+ if (j == 2) {
post->title = malloc(sizeof(char) * (value_len + 1));
memcpy(post->title, value, value_len);
post->title[value_len] = '\0';
}
- if (j == 2) {
+ if (j == 3) {
post->summary = malloc(sizeof(char) * (value_len + 1));
memcpy(post->summary, value, value_len);
post->summary[value_len] = '\0';
}
- if (j == 3) {
+ if (j == 4) {
post->date = malloc(sizeof(char) * (value_len + 1));
memcpy(post->date, value, value_len);
post->date[value_len] = '\0';
}
- if (j == 4) {
+ if (j == 5) {
if (strcmp(value, "t") == 0) {
post->is_public = malloc(sizeof(bool));
*post->is_public = true;
@@ -140,7 +152,7 @@ post_t *db_post_get(char *db, int id) {
PQfinish(conn);
return NULL;
}
- char *query = "SELECT title, summary, content, created_at, is_public FROM posts WHERE id = $1;";
+ char *query = "SELECT label, title, summary, content, created_at, is_public FROM posts WHERE id = $1;";
char id_param[10];
snprintf(id_param, 10, "%d", id);
const char *values[1] = { id_param };
@@ -161,26 +173,31 @@ post_t *db_post_get(char *db, int id) {
char *value = PQgetvalue(result, 0, j);
int value_len = strlen(value);
if (j == 0) {
+ post->label = malloc(sizeof(char) * (value_len + 1));
+ memcpy(post->label, value, value_len);
+ post->label[value_len] = '\0';
+ }
+ if (j == 1) {
post->title = malloc(sizeof(char) * (value_len + 1));
memcpy(post->title, value, value_len);
post->title[value_len] = '\0';
}
- if (j == 1) {
+ if (j == 2) {
post->summary = malloc(sizeof(char) * (value_len + 1));
memcpy(post->summary, value, value_len);
post->summary[value_len] = '\0';
}
- if (j == 2) {
+ if (j == 3) {
post->content = malloc(sizeof(char) * (value_len + 1));
memcpy(post->content, value, value_len);
post->content[value_len] = '\0';
}
- if (j == 3) {
+ if (j == 4) {
post->date = malloc(sizeof(char) * (value_len + 1));
memcpy(post->date, value, value_len);
post->date[value_len] = '\0';
}
- if (j == 4) {
+ if (j == 5) {
if (strcmp(value, "t") == 0) {
post->is_public = malloc(sizeof(bool));
*post->is_public = true;
@@ -211,6 +228,14 @@ bool db_post_update(char *db, int id, post_t *post, post_t *changes) {
const char *params[10];
int count = 0;
str_append(&query, "UPDATE posts SET");
+ if (changes->label != NULL && strcmp(post->label, changes->label) != 0) {
+ if (count > 0) {
+ str_append(&query, ",");
+ }
+ str_appendf(&query, " label = $%d", count + 1);
+ params[count] = changes->label;
+ count++;
+ }
if (changes->title != NULL && strcmp(post->title, changes->title) != 0) {
if (count > 0) {
str_append(&query, ",");
diff --git a/src/post.h b/src/post.h
@@ -4,6 +4,7 @@
typedef struct {
int id;
+ char *label;
char *title;
char *summary;
char *content;