Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat blog comment #870

Merged
merged 3 commits into from
Sep 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 0 additions & 73 deletions blog/actions/submitComment.ts

This file was deleted.

75 changes: 75 additions & 0 deletions blog/actions/submitRating.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import { and, eq, like, or } from "https://esm.sh/drizzle-orm@0.30.10";
import { Person } from "../../commerce/types.ts";
import { AppContext } from "../mod.ts";
import { logger } from "@deco/deco/o11y";
import { Rating } from "../types.ts";
import { rating } from "../db/schema.ts";

export interface Props {
itemReviewed: string;
author: Person;
ratingValue: number;
additionalType?: string;
}

export default async function submitRating(
{ itemReviewed, author, ratingValue, additionalType }: Props,
_req: Request,
ctx: AppContext,
): Promise<Rating | null> {
const records = await ctx.invoke.records.loaders.drizzle();

try {
const storedRating = await records.select({
id: rating.id,
itemReviewed: rating.itemReviewed,
author: rating.author,
ratingValue: rating.ratingValue,
additionalType: rating.additionalType,
})
.from(rating).where(
and(
eq(rating.itemReviewed, itemReviewed),
or(
like(rating.author, `%"email":"${author.email}"%`),
like(rating.author, `%"id":"${author["@id"]}"%`),
),
),
) as Rating[] | undefined;

//if has data, then update de table
if (storedRating && storedRating.length > 0 && storedRating?.at(0)?.id) {
const current = storedRating.at(0)!;
await records.update(rating).set({
ratingValue,
additionalType: additionalType ?? current.additionalType,
}).where(
eq(rating.id, current.id!),
);
return {
...current,
ratingValue,
additionalType: additionalType ?? current.additionalType,
};
}

const insertedData = {
itemReviewed,
author: author!,
ratingValue: ratingValue!,
additionalType: additionalType,
};

await records.insert(rating).values({
...insertedData,
});

return {
"@type": "Rating",
...insertedData,
};
} catch (e) {
logger.error(e);
return null;
}
}
80 changes: 0 additions & 80 deletions blog/actions/submitReaction.ts

This file was deleted.

87 changes: 87 additions & 0 deletions blog/actions/submitReview.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import { eq } from "https://esm.sh/drizzle-orm@0.30.10";
import { Person } from "../../commerce/types.ts";
import { AppContext } from "../mod.ts";
import { logger } from "@deco/deco/o11y";
import { Review } from "../types.ts";
import { getReviewById } from "../utils/records.ts";
import { review } from "../db/schema.ts";

export interface Props {
action: "create" | "update";
id?: string;
reviewBody?: string;
reviewHeadline?: string;
itemReviewed?: string;
author?: Person;
/** Review status */
additionalType?: string;
isAnonymous?: boolean;
}

export default async function submitReview(
{
reviewBody,
reviewHeadline,
itemReviewed,
id,
author,
action,
additionalType,
isAnonymous,
}: Props,
_req: Request,
ctx: AppContext,
): Promise<Review | null> {
const isoDate = new Date().toISOString();
const records = await ctx.invoke.records.loaders.drizzle();

try {
if (action != "create") {
const storedReview = await getReviewById({ ctx, id });
if (!storedReview) {
return null;
}
const updateRecord = {
additionalType: additionalType ?? storedReview.additionalType,
reviewHeadline: reviewHeadline ?? storedReview.reviewHeadline,
reviewBody: reviewBody ?? storedReview.reviewBody,
dateModified: isoDate,
};
await records.update(review).set({
...updateRecord,
}).where(
eq(review.id, id!),
);

return {
...updateRecord,
"@type": "Review",
author: author ?? storedReview.author,
datePublished: storedReview.datePublished,
};
}

const insertData = {
itemReviewed,
isAnonymous,
author: author!,
additionalType: additionalType,
reviewHeadline: reviewHeadline,
reviewBody: reviewBody!,
datePublished: isoDate,
dateModified: isoDate,
};

await records.insert(review).values({
...insertData,
});

return {
"@type": "Review",
...insertData,
};
} catch (e) {
logger.error(e);
return null;
}
}
35 changes: 13 additions & 22 deletions blog/db/schema.ts
Original file line number Diff line number Diff line change
@@ -1,34 +1,25 @@
import {
integer,
sqliteTable,
text,
} from "https://esm.sh/drizzle-orm@0.30.10/sqlite-core";
import { ArticleComment, Reaction } from "../types.ts";

export const reactions = sqliteTable("reactions", {
export const rating = sqliteTable("rating", {
id: text("id").primaryKey().$defaultFn(() => crypto.randomUUID()),
postSlug: text("postSlug").notNull(),
person: text("person", { mode: "json" }),
datePublished: (text("datePublished")).notNull(),
dateModified: (text("dateModified")).notNull(),
action: (text("action")).notNull(),
itemReviewed: text("itemReviewed").notNull(),
author: text("author", { mode: "json" }),
ratingValue: (integer("ratingValue")).notNull(),
additionalType: text("additionalType"),
});

export const comments = sqliteTable("comments", {
export const review = sqliteTable("review", {
id: text("id").primaryKey().$defaultFn(() => crypto.randomUUID()),
postSlug: text("postSlug").notNull(),
person: text("person", { mode: "json" }),
itemReviewed: text("itemReviewed").notNull(),
author: text("author", { mode: "json" }),
datePublished: (text("datePublished")).notNull(),
dateModified: (text("dateModified")).notNull(),
comment: (text("comment")).notNull(),
status: (text("status")).notNull(),
reviewHeadline: (text("reviewHeadline")),
reviewBody: (text("reviewBody")).notNull(),
additionalType: (text("additionalType")),
isAnonymous: integer("isAnonymous", { mode: "boolean" }),
});

export interface ReactionSchema extends Reaction {
id: string;
postSlug: string;
}

export interface CommentsSchema extends ArticleComment {
id: string;
postSlug: string;
}
Loading