Calendar SQL and CRUD
This commit is contained in:
39
routes/api/calendar/add.tsx
Normal file
39
routes/api/calendar/add.tsx
Normal file
@@ -0,0 +1,39 @@
|
||||
import { Handlers } from 'fresh/server.ts';
|
||||
|
||||
import { Calendar, FreshContextState } from '/lib/types.ts';
|
||||
import { createCalendar, getCalendars } from '/lib/data/calendar.ts';
|
||||
|
||||
interface Data {}
|
||||
|
||||
export interface RequestBody {
|
||||
name: string;
|
||||
}
|
||||
|
||||
export interface ResponseBody {
|
||||
success: boolean;
|
||||
newCalendars: Calendar[];
|
||||
}
|
||||
|
||||
export const handler: Handlers<Data, FreshContextState> = {
|
||||
async POST(request, context) {
|
||||
if (!context.state.user) {
|
||||
return new Response('Unauthorized', { status: 401 });
|
||||
}
|
||||
|
||||
const requestBody = await request.clone().json() as RequestBody;
|
||||
|
||||
if (requestBody.name) {
|
||||
const newCalendar = await createCalendar(context.state.user.id, requestBody.name);
|
||||
|
||||
if (!newCalendar) {
|
||||
return new Response('Not found', { status: 404 });
|
||||
}
|
||||
}
|
||||
|
||||
const newCalendars = await getCalendars(context.state.user.id);
|
||||
|
||||
const responseBody: ResponseBody = { success: true, newCalendars };
|
||||
|
||||
return new Response(JSON.stringify(responseBody));
|
||||
},
|
||||
};
|
||||
41
routes/api/calendar/delete.tsx
Normal file
41
routes/api/calendar/delete.tsx
Normal file
@@ -0,0 +1,41 @@
|
||||
import { Handlers } from 'fresh/server.ts';
|
||||
|
||||
import { Calendar, FreshContextState } from '/lib/types.ts';
|
||||
import { deleteCalendar, getCalendar, getCalendars } from '/lib/data/calendar.ts';
|
||||
|
||||
interface Data {}
|
||||
|
||||
export interface RequestBody {
|
||||
calendarId: string;
|
||||
}
|
||||
|
||||
export interface ResponseBody {
|
||||
success: boolean;
|
||||
newCalendars: Calendar[];
|
||||
}
|
||||
|
||||
export const handler: Handlers<Data, FreshContextState> = {
|
||||
async POST(request, context) {
|
||||
if (!context.state.user) {
|
||||
return new Response('Unauthorized', { status: 401 });
|
||||
}
|
||||
|
||||
const requestBody = await request.clone().json() as RequestBody;
|
||||
|
||||
if (requestBody.calendarId) {
|
||||
const calendar = await getCalendar(requestBody.calendarId, context.state.user.id);
|
||||
|
||||
if (!calendar) {
|
||||
return new Response('Not found', { status: 404 });
|
||||
}
|
||||
|
||||
await deleteCalendar(requestBody.calendarId, context.state.user.id);
|
||||
}
|
||||
|
||||
const newCalendars = await getCalendars(context.state.user.id);
|
||||
|
||||
const responseBody: ResponseBody = { success: true, newCalendars };
|
||||
|
||||
return new Response(JSON.stringify(responseBody));
|
||||
},
|
||||
};
|
||||
48
routes/api/calendar/update.tsx
Normal file
48
routes/api/calendar/update.tsx
Normal file
@@ -0,0 +1,48 @@
|
||||
import { Handlers } from 'fresh/server.ts';
|
||||
|
||||
import { Calendar, FreshContextState } from '/lib/types.ts';
|
||||
import { getCalendar, getCalendars, updateCalendar } from '/lib/data/calendar.ts';
|
||||
|
||||
interface Data {}
|
||||
|
||||
export interface RequestBody {
|
||||
id: string;
|
||||
name: string;
|
||||
color: string;
|
||||
is_visible: boolean;
|
||||
}
|
||||
|
||||
export interface ResponseBody {
|
||||
success: boolean;
|
||||
newCalendars: Calendar[];
|
||||
}
|
||||
|
||||
export const handler: Handlers<Data, FreshContextState> = {
|
||||
async POST(request, context) {
|
||||
if (!context.state.user) {
|
||||
return new Response('Unauthorized', { status: 401 });
|
||||
}
|
||||
|
||||
const requestBody = await request.clone().json() as RequestBody;
|
||||
|
||||
if (requestBody.id) {
|
||||
const calendar = await getCalendar(requestBody.id, context.state.user.id);
|
||||
|
||||
if (!calendar) {
|
||||
return new Response('Not found', { status: 404 });
|
||||
}
|
||||
|
||||
calendar.name = requestBody.name;
|
||||
calendar.color = requestBody.color;
|
||||
calendar.is_visible = requestBody.is_visible;
|
||||
|
||||
await updateCalendar(calendar);
|
||||
}
|
||||
|
||||
const newCalendars = await getCalendars(context.state.user.id);
|
||||
|
||||
const responseBody: ResponseBody = { success: true, newCalendars };
|
||||
|
||||
return new Response(JSON.stringify(responseBody));
|
||||
},
|
||||
};
|
||||
@@ -29,7 +29,7 @@ export const handler: Handlers<Data, FreshContextState> = {
|
||||
return new Response('Not found', { status: 404 });
|
||||
}
|
||||
|
||||
await deleteNewsFeed(requestBody.feedId);
|
||||
await deleteNewsFeed(requestBody.feedId, context.state.user.id);
|
||||
}
|
||||
|
||||
const newFeeds = await getNewsFeeds(context.state.user.id);
|
||||
|
||||
29
routes/calendars.tsx
Normal file
29
routes/calendars.tsx
Normal file
@@ -0,0 +1,29 @@
|
||||
import { Handlers, PageProps } from 'fresh/server.ts';
|
||||
|
||||
import { Calendar, FreshContextState } from '/lib/types.ts';
|
||||
import { getCalendars } from '/lib/data/calendar.ts';
|
||||
import Calendars from '/islands/calendar/Calendars.tsx';
|
||||
|
||||
interface Data {
|
||||
userCalendars: Calendar[];
|
||||
}
|
||||
|
||||
export const handler: Handlers<Data, FreshContextState> = {
|
||||
async GET(request, context) {
|
||||
if (!context.state.user) {
|
||||
return new Response('Redirect', { status: 303, headers: { 'Location': `/login` } });
|
||||
}
|
||||
|
||||
const userCalendars = await getCalendars(context.state.user.id);
|
||||
|
||||
return await context.render({ userCalendars });
|
||||
},
|
||||
};
|
||||
|
||||
export default function CalendarsPage({ data }: PageProps<Data, FreshContextState>) {
|
||||
return (
|
||||
<main>
|
||||
<Calendars initialCalendars={data.userCalendars || []} />
|
||||
</main>
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user