mirror of
https://github.com/aaronpo97/the-biergarten-app.git
synced 2026-02-16 10:42:08 +00:00
Add github actions, refactor seed and beer p likes
This commit is contained in:
31
requests/sendCheckIfUserLikesBeerPostRequest.ts
Normal file
31
requests/sendCheckIfUserLikesBeerPostRequest.ts
Normal file
@@ -0,0 +1,31 @@
|
||||
import APIResponseValidationSchema from '@/validation/APIResponseValidationSchema';
|
||||
import { z } from 'zod';
|
||||
|
||||
const sendCheckIfUserLikesBeerPostRequest = async (beerPostId: string) => {
|
||||
const response = await fetch(`/api/beers/${beerPostId}/like/is-liked`);
|
||||
const data = await response.json();
|
||||
|
||||
const parsed = APIResponseValidationSchema.safeParse(data);
|
||||
|
||||
if (!parsed.success) {
|
||||
throw new Error('Invalid API response.');
|
||||
}
|
||||
|
||||
const { payload } = parsed.data;
|
||||
|
||||
const parsedPayload = z
|
||||
.object({
|
||||
isLiked: z.boolean(),
|
||||
})
|
||||
.safeParse(payload);
|
||||
|
||||
if (!parsedPayload.success) {
|
||||
throw new Error('Invalid API response.');
|
||||
}
|
||||
|
||||
const { isLiked } = parsedPayload.data;
|
||||
|
||||
return isLiked;
|
||||
};
|
||||
|
||||
export default sendCheckIfUserLikesBeerPostRequest;
|
||||
30
requests/sendLikeRequest.ts
Normal file
30
requests/sendLikeRequest.ts
Normal file
@@ -0,0 +1,30 @@
|
||||
import BeerPostQueryResult from '@/services/BeerPost/schema/BeerPostQueryResult';
|
||||
import APIResponseValidationSchema from '@/validation/APIResponseValidationSchema';
|
||||
|
||||
const sendLikeRequest = async (beerPost: BeerPostQueryResult) => {
|
||||
const response = await fetch(`/api/beers/${beerPost.id}/like`, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
body: '',
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error('Something went wrong.');
|
||||
}
|
||||
|
||||
const data = await response.json();
|
||||
|
||||
const parsed = APIResponseValidationSchema.safeParse(data);
|
||||
|
||||
if (!parsed.success) {
|
||||
throw new Error('Invalid API response.');
|
||||
}
|
||||
|
||||
const { success, message } = parsed.data;
|
||||
|
||||
return { success, message };
|
||||
};
|
||||
|
||||
export default sendLikeRequest;
|
||||
Reference in New Issue
Block a user