Fixed Consistency

This commit is contained in:
Adam Fordsmand 2022-04-16 15:26:54 +02:00
parent cfb89f14bf
commit dfe3466ea5
1 changed files with 45 additions and 44 deletions

View File

@ -58,8 +58,8 @@ int AddPost(char title[], int authorId, char datePosted[], char post[], int tags
{ {
if (con == NULL) con = conn2maria(); if (con == NULL) con = conn2maria();
char* query; char* insertQuery;
if(0 > asprintf(&query, if(0 > asprintf(&insertQuery,
"INSERT INTO blog_posts (title,author_id,date_posted,post,state) VALUES (\"%s\", \"%d\", \"%s\", \"%s\", \"0\")", "INSERT INTO blog_posts (title,author_id,date_posted,post,state) VALUES (\"%s\", \"%d\", \"%s\", \"%s\", \"0\")",
title, title,
authorId, authorId,
@ -67,14 +67,14 @@ int AddPost(char title[], int authorId, char datePosted[], char post[], int tags
post post
)) exit(1); )) exit(1);
if (mysql_query(con, query)) if (mysql_query(con, insertQuery))
{ {
fprintf(stderr, "%s\n", mysql_error(con)); fprintf(stderr, "%s\n", mysql_error(con));
mysql_close(con); mysql_close(con);
exit(1); exit(1);
} }
free(query); free(insertQuery);
if (mysql_query(con, "SELECT LAST_INSERT_ID()")) if (mysql_query(con, "SELECT LAST_INSERT_ID()"))
{ {
@ -83,10 +83,8 @@ int AddPost(char title[], int authorId, char datePosted[], char post[], int tags
exit(1); exit(1);
} }
MYSQL_RES *result = mysql_store_result(con);
int num_fields = mysql_num_fields(result);
MYSQL_ROW row; MYSQL_ROW row;
MYSQL_RES *result = mysql_store_result(con);
row = mysql_fetch_row(result); row = mysql_fetch_row(result);
int postID = atoi(row[0]); int postID = atoi(row[0]);
@ -138,7 +136,8 @@ int EditPost(int postID, char title[], char post[], MYSQL* con)
free(selectQuery); free(selectQuery);
char* updateQuery; char* updateQuery;
if (0 > asprintf(&updateQuery, "UPDATE blog_posts SET title=(\"%s\"), post=(\"%s\") WHERE id=(%d)", if (0 > asprintf(&updateQuery,
"UPDATE blog_posts SET title=(\"%s\"), post=(\"%s\") WHERE id=(%d)",
title, title,
post, post,
postID postID
@ -173,7 +172,6 @@ int AddFilePost(int authorID, char filePath[], MYSQL* con)
char StringEndIndicator[] = "\0"; char StringEndIndicator[] = "\0";
FilePointer = fopen(filePath, "r"); FilePointer = fopen(filePath, "r");
fgets(Buffer, BufferLength, FilePointer); fgets(Buffer, BufferLength, FilePointer);
// Get the substrings startTest = Buffer[0:3] and endTest = Buffer[-5:-1] // Get the substrings startTest = Buffer[0:3] and endTest = Buffer[-5:-1]
@ -188,29 +186,29 @@ int AddFilePost(int authorID, char filePath[], MYSQL* con)
} }
Buffer[LineLength - 6] = StringEndIndicator[0]; Buffer[LineLength - 6] = StringEndIndicator[0];
char* trimBuffer; char* trimmedBufferTitle;
trimBuffer = &Buffer[4]; trimmedBufferTitle = &Buffer[4];
// Add a new post with correct title, author and date but with an empty post. // Add a new post with correct title, author and date but with an empty post.
int postID = AddPost(trimBuffer, authorID, date, "", NULL, 0, con); int postID = AddPost(trimmedBufferTitle, authorID, date, "", NULL, 0, con);
while (fgets(Buffer, BufferLength, FilePointer)) while (fgets(Buffer, BufferLength, FilePointer))
{ {
char* query; char* concatQuery;
if(0 > asprintf(&query, if(0 > asprintf(&concatQuery,
"UPDATE blog_posts SET post = CONCAT(post, \"%s\n\") WHERE id=(%d)", "UPDATE blog_posts SET post = CONCAT(post, \"%s\n\") WHERE id=(%d)",
Buffer, Buffer,
postID postID
)) exit(1); )) exit(1);
if (mysql_query(con, query)) if (mysql_query(con, concatQuery))
{ {
fprintf(stderr, "%s\n", mysql_error(con)); fprintf(stderr, "%s\n", mysql_error(con));
mysql_close(con); mysql_close(con);
exit(1); exit(1);
} }
free(query); free(concatQuery);
} }
return postID; return postID;
@ -227,7 +225,6 @@ int EditFilePost(char filePath[], int postID, MYSQL* con)
char StringEndIndicator[] = "\0"; char StringEndIndicator[] = "\0";
FilePointer = fopen(filePath, "r"); FilePointer = fopen(filePath, "r");
fgets(Buffer, BufferLength, FilePointer); fgets(Buffer, BufferLength, FilePointer);
// Get the substrings startTest = Buffer[0:3] and endTest = Buffer[-6:-1] // Get the substrings startTest = Buffer[0:3] and endTest = Buffer[-6:-1]
@ -243,29 +240,29 @@ int EditFilePost(char filePath[], int postID, MYSQL* con)
Buffer[LineLength - 6] = StringEndIndicator[0]; Buffer[LineLength - 6] = StringEndIndicator[0];
char* Title; char* trimmedBufferTitle;
Title = &Buffer[4]; trimmedBufferTitle = &Buffer[4];
// Edits post with correct title and empties post. // Edits post with correct title and empties post.
EditPost(postID, Title, "", con); EditPost(postID, trimmedBufferTitle, "", con);
while (fgets(Buffer, BufferLength, FilePointer)) while (fgets(Buffer, BufferLength, FilePointer))
{ {
char* query; char* concatQuery;
if(0 > asprintf(&query, if(0 > asprintf(&concatQuery,
"UPDATE blog_posts SET post = CONCAT(post, \"%s\") WHERE id=(%d)", "UPDATE blog_posts SET post = CONCAT(post, \"%s\") WHERE id=(%d)",
Buffer, Buffer,
postID postID
)) exit(1); )) exit(1);
if (mysql_query(con, query)) if (mysql_query(con, concatQuery))
{ {
fprintf(stderr, "%s\n", mysql_error(con)); fprintf(stderr, "%s\n", mysql_error(con));
mysql_close(con); mysql_close(con);
exit(1); exit(1);
} }
free(query); free(concatQuery);
} }
return postID; return postID;
@ -284,7 +281,10 @@ int BlogWrite(MYSQL* con)
fclose(fp); fclose(fp);
char* command; char* command;
if(0 > asprintf(&command, "${EDITOR:-'vi'} %s", tmpName)) exit(1); if(0 > asprintf(&command,
"${EDITOR:-'vi'} %s",
tmpName
)) exit(1);
system(command); system(command);
int postID = AddFilePost(1, tmpName, con); int postID = AddFilePost(1, tmpName, con);
@ -354,10 +354,7 @@ int PullPostData(char* strings[6], int postID, MYSQL* con)
// Pull all coloums of all posts except for the post coloumn // Pull all coloums of all posts except for the post coloumn
int PullPostDescriptions(int** IDs, char* Titles[255], int** authors, int** dates, MYSQL* con) int PullPostDescriptions(int** IDs, char* Titles[255], int** authors, int** dates, MYSQL* con)
{ {
if (con == NULL) if (con == NULL) con = conn2maria();
{
con = conn2maria();
}
if (mysql_query(con, "SELECT COUNT(*) FROM blog_posts")) if (mysql_query(con, "SELECT COUNT(*) FROM blog_posts"))
{ {
@ -463,33 +460,33 @@ int DeleteTag(int tagID, MYSQL* con)
{ {
if (con == NULL) con = conn2maria(); if (con == NULL) con = conn2maria();
char* deleteQuery; char* deleteTagQuery;
if (0 > asprintf(&deleteQuery, "DELETE FROM tags WHERE id=(%d)", if (0 > asprintf(&deleteTagQuery, "DELETE FROM tags WHERE id=(%d)",
tagID tagID
)) exit(1); )) exit(1);
if (mysql_query(con, deleteQuery)) if (mysql_query(con, deleteTagQuery))
{ {
fprintf(stderr, "%s\n", mysql_error(con)); fprintf(stderr, "%s\n", mysql_error(con));
mysql_close(con); mysql_close(con);
exit(1); exit(1);
} }
free(deleteQuery); free(deleteTagQuery);
char* deleteTagsQuery; char* deleteTagAssignmentsQuery;
if (0 > asprintf(&deleteTagsQuery, "DELETE FROM blog_post_tags WHERE tag_id=(%d)", if (0 > asprintf(&deleteTagAssignmentsQuery, "DELETE FROM blog_post_tags WHERE tag_id=(%d)",
tagID tagID
)) exit(1); )) exit(1);
if (mysql_query(con, deleteTagsQuery)) if (mysql_query(con, deleteTagAssignmentsQuery))
{ {
fprintf(stderr, "%s\n", mysql_error(con)); fprintf(stderr, "%s\n", mysql_error(con));
mysql_close(con); mysql_close(con);
exit(1); exit(1);
} }
free(deleteTagsQuery); free(deleteTagAssignmentsQuery);
return tagID; return tagID;
} }
@ -663,17 +660,20 @@ int PullAuthorData(char* strings[4], int authorID, MYSQL* con)
{ {
if (con == NULL) con = conn2maria(); if (con == NULL) con = conn2maria();
char* query; char* selectQuery;
if(0 > asprintf(&query, "SELECT * FROM people WHERE id=(%d)", authorID)) exit(1); if(0 > asprintf(&selectQuery,
"SELECT * FROM people WHERE id=(%d)",
authorID
)) exit(1);
if (mysql_query(con, query)) if (mysql_query(con, selectQuery))
{ {
fprintf(stderr, "%s\n", mysql_error(con)); fprintf(stderr, "%s\n", mysql_error(con));
mysql_close(con); mysql_close(con);
exit(1); exit(1);
} }
free(query); free(selectQuery);
MYSQL_RES *result = mysql_store_result(con); MYSQL_RES *result = mysql_store_result(con);
int num_fields = mysql_num_fields(result); int num_fields = mysql_num_fields(result);
@ -722,8 +722,8 @@ int ChangeAuthor(int postID, int authorID, MYSQL* con)
free(selectQuery); free(selectQuery);
char* updateQuery; char* updateQuery;
if (0 > asprintf(&updateQuery,
if (0 > asprintf(&updateQuery, "UPDATE blog_posts SET author_id=(%d) WHERE id=(%d)", "UPDATE blog_posts SET author_id=(%d) WHERE id=(%d)",
authorID, authorID,
postID postID
)) exit(1); )) exit(1);
@ -770,7 +770,8 @@ int TogglePost(int postID, MYSQL* con)
free(selectQuery); free(selectQuery);
char* updateQuery; char* updateQuery;
if (0 > asprintf(&updateQuery, "UPDATE blog_posts SET state=(%d) WHERE id=(%d)", if (0 > asprintf(&updateQuery,
"UPDATE blog_posts SET state=(%d) WHERE id=(%d)",
!state, !state,
postID postID
)) exit(1); )) exit(1);