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

Create Delete Rekomendasi Review 86eqqc6kh #59

Closed
wants to merge 5 commits into from
Closed
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
92 changes: 91 additions & 1 deletion src/controllers/recommendation.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,20 @@
import {
createCoWorkingSpaceRecommendation,
createVoucherRecommendation,
postVoucherReview,
deleteVoucherReview,
postCoWorkingSpaceReview,
deleteCoWorkingSpaceReview,
} from '~/repositories/recommendation.repo';
import {
postRecommendationCoWorkingSpaceRoute,
postRecommendationVoucherRoute,
postVoucherReviewRoute,
postCoWorkingSpaceReviewRoute,
deleteVoucherReviewRoute,
deleteCoWorkingSpaceReviewRoute,
} from '~/routes/recommendation.route';

import { PostVoucherReviewParamsSchema } from '~/types/recommendations.types';

Check warning on line 19 in src/controllers/recommendation.controller.ts

View workflow job for this annotation

GitHub Actions / Run eslint and typescript check

'PostVoucherReviewParamsSchema' is defined but never used
export const recommendationRoute = createAuthRouter();

recommendationRoute.openapi(postRecommendationVoucherRoute, async (c) => {
Expand All @@ -35,3 +43,85 @@
return c.json(recommendation, 201);
},
);

recommendationRoute.openapi(postVoucherReviewRoute, async (c) => {
const { id } = c.var.user;
const { voucherId } = c.req.valid('param');
const { rating, review } = c.req.valid('json');

const data = {
userId: id,
voucherId,
rating,
review,
};
console.log('Created data:', data);

const voucher = await postVoucherReview(db, data);

console.log('Created voucher review:', voucher);

return c.json(voucher, 201);
});

recommendationRoute.openapi(postCoWorkingSpaceReviewRoute, async (c) => {
const { id } = c.var.user;
const { coWorkingSpaceId } = c.req.valid('param');
const { rating, review } = c.req.valid('json');

const data = {
coWorkingSpaceId,
userId: id,
rating,
review,
};
console.log('Created data:', data);

const voucher = await postCoWorkingSpaceReview(db, data);

console.log('Created voucher review:', voucher);

return c.json(voucher, 201);
});

recommendationRoute.openapi(deleteVoucherReviewRoute, async (c) => {
const { id } = c.var.user;
const { userId, voucherId } = c.req.valid('param');

if (id === userId) {
await deleteVoucherReview(db, voucherId, userId);

const response = {
success: true,
};

return c.json(response, 201);
} else {
const errorResponse = {
error: 'Unauthorized',
};

return c.json(errorResponse, 404);
}
});

recommendationRoute.openapi(deleteCoWorkingSpaceReviewRoute, async (c) => {
const { id } = c.var.user;
const { coWorkingSpaceId, userId } = c.req.valid('param');

if (id === userId) {
await deleteCoWorkingSpaceReview(db, coWorkingSpaceId, userId);

const response = {
success: true,
};

return c.json(response, 201);
} else {
const errorResponse = {
error: 'Unauthorized',
};

return c.json(errorResponse, 404);
}
});
57 changes: 57 additions & 0 deletions src/db/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -932,3 +932,60 @@ export const coWorkingSpaceRecommendationsRelation = relations(
}),
}),
);

export const voucherReviews = pgTable(
'voucher_reviews',
{
userId: text('user_id')
.references(() => users.id, { onDelete: 'cascade' })
.notNull(),
voucherId: text('voucher_id')
.references(() => voucherRecommendations.id, { onDelete: 'cascade' })
.notNull(),
rating: integer('rating').notNull(),
review: text('review').notNull(),
},
(t) => ({ pk: primaryKey({ columns: [t.voucherId, t.userId] }) }),
);

export const coWorkingSpaceReviews = pgTable(
'co_working_space_reviews',
{
coWorkingSpaceId: text('coWorkingSpace_id')
.references(() => coWorkingSpaceRecommendations.id, {
onDelete: 'cascade',
})
.notNull(),
userId: text('user_id')
.references(() => users.id, { onDelete: 'cascade' })
.notNull(),
rating: integer('rating').notNull(),
review: text('review').notNull(),
},
(t) => ({ pk: primaryKey({ columns: [t.coWorkingSpaceId, t.userId] }) }),
);

export const voucherReviewsRelation = relations(voucherReviews, ({ one }) => ({
recommendation: one(voucherRecommendations, {
fields: [voucherReviews.voucherId],
references: [voucherRecommendations.id],
}),
user: one(users, {
fields: [voucherReviews.userId],
references: [users.id],
}),
}));

export const coWorkingSpaceReviewsRelation = relations(
coWorkingSpaceReviews,
({ one }) => ({
recommendation: one(coWorkingSpaceRecommendations, {
fields: [coWorkingSpaceReviews.coWorkingSpaceId],
references: [coWorkingSpaceRecommendations.id],
}),
user: one(users, {
fields: [coWorkingSpaceReviews.userId],
references: [users.id],
}),
}),
);
85 changes: 85 additions & 0 deletions src/repositories/recommendation.repo.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
import type { z } from 'zod';
import type { Database } from '~/db/drizzle';
import { firstSure } from '~/db/helper';
import { InferInsertModel, and, eq } from 'drizzle-orm';
import {
coWorkingSpaceRecommendations,
voucherRecommendations,
voucherReviews,
coWorkingSpaceReviews,
} from '~/db/schema';
import type { JWTPayloadSchema } from '~/types/login.types';
import type {
CoWorkingSpaceRecommendationSchema,
VoucherRecommendationSchema,
VoucherReviewSchema,
} from '~/types/recommendations.types';

export const createVoucherRecommendation = async (
Expand Down Expand Up @@ -46,3 +50,84 @@ export const createCoWorkingSpaceRecommendation = async (

return recommendation;
};

export const postVoucherReview = async (
db: Database,
data: z.infer<typeof VoucherReviewSchema>,
) => {
const review = await db
.insert(voucherReviews)
.values({
...data,
})
.returning()
.then(firstSure);

return review;
};

/**
* Insert a new coworking space review
* @param db Database instance
* @param review Review object to insert
* @returns Inserted review or undefined if insertion fails
*/
export async function postCoWorkingSpaceReview(
db: Database,
data: InferInsertModel<typeof coWorkingSpaceReviews>,
) {
return await db
.insert(coWorkingSpaceReviews)
.values(data)
.onConflictDoNothing()
.returning()
.then(firstSure);
}

/**
* Delete a voucher review
* @param db Database instance
* @param voucherId ID of the voucher recommendation
* @param userId ID of the user who made the review
* @returns Deleted review or undefined if no review was deleted
*/
export async function deleteVoucherReview(
db: Database,
voucherId: string,
userId: string,
) {
return await db
.delete(voucherReviews)
.where(
and(
eq(voucherReviews.voucherId, voucherId),
eq(voucherReviews.userId, userId),
),
)
.returning()
.then(firstSure);
}

/**
* Delete a coworking space review
* @param db Database instance
* @param coWorkingSpaceId ID of the coworking space recommendation
* @param userId ID of the user who made the review
* @returns Deleted review or undefined if no review was deleted
*/
export async function deleteCoWorkingSpaceReview(
db: Database,
coWorkingSpaceId: string,
userId: string,
) {
return await db
.delete(coWorkingSpaceReviews)
.where(
and(
eq(coWorkingSpaceReviews.coWorkingSpaceId, coWorkingSpaceId),
eq(coWorkingSpaceReviews.userId, userId),
),
)
.returning()
.then(firstSure);
}
Loading