post fields improvements
This commit is contained in:
@@ -20,12 +20,13 @@ export const model: { [key: string]: string } = {
|
|||||||
subtitle TEXT,
|
subtitle TEXT,
|
||||||
body TEXT NOT NULL,
|
body TEXT NOT NULL,
|
||||||
banner_photo TEXT,
|
banner_photo TEXT,
|
||||||
date TEXT NOT NULL DEFAULT (datetime('now')),
|
date TEXT,
|
||||||
published INTEGER NOT NULL,
|
published INTEGER NOT NULL,
|
||||||
category TEXT,
|
category TEXT,
|
||||||
author_id TEXT NOT NULL,
|
author_id TEXT NOT NULL,
|
||||||
reads INTEGER NOT NULL DEFAULT 0,
|
reads INTEGER NOT NULL DEFAULT 0,
|
||||||
attachments TEXT
|
attachments TEXT,
|
||||||
|
last_edited_date TEXT
|
||||||
);
|
);
|
||||||
CREATE INDEX IF NOT EXISTS idx_posts_category ON Post (category);
|
CREATE INDEX IF NOT EXISTS idx_posts_category ON Post (category);
|
||||||
`,
|
`,
|
||||||
|
|||||||
@@ -22,11 +22,12 @@ export interface Post {
|
|||||||
subtitle?: string;
|
subtitle?: string;
|
||||||
body: string;
|
body: string;
|
||||||
banner_photo?: string;
|
banner_photo?: string;
|
||||||
date: string;
|
date?: string | null;
|
||||||
published: boolean;
|
published: boolean;
|
||||||
author_id: string;
|
author_id: string;
|
||||||
reads: number;
|
reads: number;
|
||||||
attachments?: string;
|
attachments?: string;
|
||||||
|
last_edited_date?: string | null;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface PostLike {
|
export interface PostLike {
|
||||||
@@ -72,13 +73,14 @@ export interface PostWithCommentsAndLikes {
|
|||||||
subtitle: string;
|
subtitle: string;
|
||||||
body: string;
|
body: string;
|
||||||
banner_photo: string;
|
banner_photo: string;
|
||||||
date: string;
|
date?: string | null;
|
||||||
published: boolean;
|
published: boolean;
|
||||||
author_id: string;
|
author_id: string;
|
||||||
reads: number;
|
reads: number;
|
||||||
attachments: string;
|
attachments: string;
|
||||||
total_likes: number;
|
total_likes: number;
|
||||||
total_comments: number;
|
total_comments: number;
|
||||||
|
last_edited_date?: string | null;
|
||||||
}
|
}
|
||||||
export interface PostWithTags {
|
export interface PostWithTags {
|
||||||
id: number;
|
id: number;
|
||||||
@@ -87,10 +89,11 @@ export interface PostWithTags {
|
|||||||
subtitle: string;
|
subtitle: string;
|
||||||
body: string;
|
body: string;
|
||||||
banner_photo: string;
|
banner_photo: string;
|
||||||
date: string;
|
date?: string | null;
|
||||||
published: boolean;
|
published: boolean;
|
||||||
author_id: string;
|
author_id: string;
|
||||||
reads: number;
|
reads: number;
|
||||||
attachments: string;
|
attachments: string;
|
||||||
tags: Tag[];
|
tags: Tag[];
|
||||||
|
last_edited_date?: string | null;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -382,9 +382,12 @@ export const databaseRouter = createTRPCRouter({
|
|||||||
? env.VITE_AWS_BUCKET_STRING + input.banner_photo
|
? env.VITE_AWS_BUCKET_STRING + input.banner_photo
|
||||||
: null;
|
: null;
|
||||||
|
|
||||||
|
const now = new Date().toISOString();
|
||||||
|
const publishDate = input.published ? now : null;
|
||||||
|
|
||||||
const query = `
|
const query = `
|
||||||
INSERT INTO Post (title, category, subtitle, body, banner_photo, published, author_id)
|
INSERT INTO Post (title, category, subtitle, body, banner_photo, date, published, author_id, last_edited_date)
|
||||||
VALUES (?, ?, ?, ?, ?, ?, ?)
|
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
|
||||||
`;
|
`;
|
||||||
const params = [
|
const params = [
|
||||||
input.title,
|
input.title,
|
||||||
@@ -392,8 +395,10 @@ export const databaseRouter = createTRPCRouter({
|
|||||||
input.subtitle,
|
input.subtitle,
|
||||||
input.body,
|
input.body,
|
||||||
fullURL,
|
fullURL,
|
||||||
|
publishDate,
|
||||||
input.published,
|
input.published,
|
||||||
input.author_id
|
input.author_id,
|
||||||
|
now
|
||||||
];
|
];
|
||||||
|
|
||||||
const results = await conn.execute({ sql: query, args: params });
|
const results = await conn.execute({ sql: query, args: params });
|
||||||
@@ -436,6 +441,26 @@ export const databaseRouter = createTRPCRouter({
|
|||||||
try {
|
try {
|
||||||
const conn = ConnectionFactory();
|
const conn = ConnectionFactory();
|
||||||
|
|
||||||
|
// Check if post is being published for the first time
|
||||||
|
let shouldSetPublishDate = false;
|
||||||
|
if (input.published !== undefined && input.published !== null) {
|
||||||
|
const currentPostQuery = await conn.execute({
|
||||||
|
sql: "SELECT published, date FROM Post WHERE id = ?",
|
||||||
|
args: [input.id]
|
||||||
|
});
|
||||||
|
const currentPost = currentPostQuery.rows[0] as any;
|
||||||
|
|
||||||
|
// Set publish date if transitioning from unpublished to published and date is null
|
||||||
|
if (
|
||||||
|
currentPost &&
|
||||||
|
!currentPost.published &&
|
||||||
|
input.published &&
|
||||||
|
!currentPost.date
|
||||||
|
) {
|
||||||
|
shouldSetPublishDate = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
let query = "UPDATE Post SET ";
|
let query = "UPDATE Post SET ";
|
||||||
let params: any[] = [];
|
let params: any[] = [];
|
||||||
let first = true;
|
let first = true;
|
||||||
@@ -474,6 +499,18 @@ export const databaseRouter = createTRPCRouter({
|
|||||||
first = false;
|
first = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Set date if publishing for the first time
|
||||||
|
if (shouldSetPublishDate) {
|
||||||
|
query += first ? "date = ?" : ", date = ?";
|
||||||
|
params.push(new Date().toISOString());
|
||||||
|
first = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Always update last_edited_date
|
||||||
|
query += first ? "last_edited_date = ?" : ", last_edited_date = ?";
|
||||||
|
params.push(new Date().toISOString());
|
||||||
|
first = false;
|
||||||
|
|
||||||
query += first ? "author_id = ?" : ", author_id = ?";
|
query += first ? "author_id = ?" : ", author_id = ?";
|
||||||
params.push(input.author_id);
|
params.push(input.author_id);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user