Skip to content

Commit

Permalink
feat: new endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
patrickwebsdev committed Nov 9, 2024
1 parent d9cd9f1 commit bc9dfb1
Show file tree
Hide file tree
Showing 15 changed files with 1,101 additions and 930 deletions.
33 changes: 33 additions & 0 deletions src/app/api/mapa/puntos/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { NextRequest } from 'next/server';
import { createServerRoleClient } from '@/lib/supabase/serverrole';

export async function GET(req: NextRequest) {
const url = new URL(req.url);
const searchParams: any = url.searchParams;

const accepted_items = searchParams.get('accepted') || null;

const supabase = await createServerRoleClient();
// const { data: dataUser, error: errorUser } = await supabase.auth.getUser();
// if (errorUser || !dataUser?.user) {
// return Response.json({ message: 'Not logged.', errorUser });
// }

const query = supabase
.from('collection_points')
.select('*')

if (accepted_items !== null) {
query.contains('accepted_items', [accepted_items]);
}

const { data, error } = await query.order('created_at', { ascending: false });


if (error) {
return Response.json({ error });
} else {
return Response.json({ data });
}
return Response.json({ message: 'Error' });
}
40 changes: 1 addition & 39 deletions src/app/api/mapa/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,43 +2,5 @@ import { NextRequest } from 'next/server';
import { createServerRoleClient } from '@/lib/supabase/serverrole';

export async function GET(req: NextRequest) {
const url = new URL(req.url);
const searchParams: any = url.searchParams;

const help_type = searchParams.get('type') || null;
const urgency = searchParams.get('urgency') || null;
const acepta = searchParams.get('acepta') || null;

const supabase = await createServerRoleClient();
// const { data: dataUser, error: errorUser } = await supabase.auth.getUser();
// if (errorUser || !dataUser?.user) {
// return Response.json({ message: 'Not logged.', errorUser });
// }

const query = supabase
.from('help_requests')
.select('id, user_id, latitude, longitude, urgency')
.eq('type', 'necesita');

if (help_type !== null) {
query.contains('help_type', [help_type]);
}
if (urgency !== null) {
query.eq('urgency', urgency);
}

query.neq('status', 'finished');

const { data, error } = await query.order('created_at', { ascending: false });

if (acepta !== 'todos') {
query.contains('accepted_items', [acepta]);
}

if (error) {
return Response.json({ error });
} else {
return Response.json({ data });
}
return Response.json({ message: 'Error' });
return Response.json({ message: '' });
}
39 changes: 39 additions & 0 deletions src/app/api/mapa/solicitudes/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { NextRequest } from 'next/server';
import { createServerRoleClient } from '@/lib/supabase/serverrole';

export async function GET(req: NextRequest) {
const url = new URL(req.url);
const searchParams: any = url.searchParams;

const help_type = searchParams.get('type') || null;
const urgency = searchParams.get('urgency') || null;

const supabase = await createServerRoleClient();
// const { data: dataUser, error: errorUser } = await supabase.auth.getUser();
// if (errorUser || !dataUser?.user) {
// return Response.json({ message: 'Not logged.', errorUser });
// }

const query = supabase
.from('help_requests')
.select('id, user_id, latitude, created_at, longitude, resources, help_type, urgency, name,location, description, people_needed, additional_info->special_situations, status,other_help')
.eq('type', 'necesita');

if (help_type !== null) {
query.contains('help_type', [help_type]);
}
if (urgency !== null) {
query.eq('urgency', urgency);
}

query.neq('status', 'finished');

const { data, error } = await query.order('created_at', { ascending: false });

if (error) {
return Response.json({ error });
} else {
return Response.json({ data });
}
return Response.json({ message: 'Error' });
}
28 changes: 28 additions & 0 deletions src/app/api/ofertas/[id]/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { createServerRoleClient } from '@/lib/supabase/serverrole';
import { NextRequest } from 'next/server';

export async function GET(request: NextRequest, { params }: { params: { id: string } }) {
const id = params.id;

const supabase = await createServerRoleClient();
// const { data: dataUser, error: errorUser } = await supabase.auth.getUser();
// if (errorUser || !dataUser?.user) {
// return Response.json({ message: 'Not logged.', errorUser });
// }

const { data, error } = await supabase
.from('help_requests')
.select(
'id, created_at, name, location, description, urgency, number_of_people, contact_info, additional_info->special_situations, status, resources, latitude, longitude, coordinates, help_type, people_needed, other_help,town_id',
{ count: 'exact' },
)
.eq('type', 'ofrece')
.eq('id', id)
.limit(1)
.select();

if (error) {
return Response.json({ error });
}
return Response.json({ data });
}
72 changes: 36 additions & 36 deletions src/app/api/ofertas/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,41 +2,41 @@ import { NextRequest } from 'next/server';
import { createServerRoleClient } from '@/lib/supabase/serverrole';

export async function GET(req: NextRequest) {
// Acceder a los parámetros de búsqueda
const url = new URL(req.url);
const searchParams: any = url.searchParams;

const help_type = searchParams.get('type');
const currentPage = searchParams.get('page') ?? 1;
const itemsPerPage = 10;

const supabase = await createServerRoleClient();
// const { data: dataUser, error: errorUser } = await supabase.auth.getUser();
// if (errorUser || !dataUser?.user) {
// return Response.json({ message: 'Not logged.', errorUser });
// }

const query = supabase
.from('help_requests')
// Acceder a los parámetros de búsqueda
const url = new URL(req.url);
const searchParams: any = url.searchParams;

const help_type = searchParams.get('type');
const currentPage = searchParams.get('page') ?? 1;
const itemsPerPage = 10;

const supabase = await createServerRoleClient();
// const { data: dataUser, error: errorUser } = await supabase.auth.getUser();
// if (errorUser || !dataUser?.user) {
// return Response.json({ message: 'Not logged.', errorUser });
// }

const query = supabase
.from('help_requests')
.select(
'id, created_at,name,location,description,contact_info,additional_info->experience,status,resources,help_type,town_id,other_help',
{ count: 'exact' },
)
.eq('type', 'ofrece');

if (help_type !== null) {
query.contains('help_type', [help_type]);
}

query.neq('status', 'finished');

const { data, count, error } = await query
.range((currentPage - 1) * itemsPerPage, currentPage * itemsPerPage - 1)
.order('created_at', { ascending: false });

if (error) {
return Response.json({ error });
}
const countResponse = count ?? 0;
return Response.json({ data, count: countResponse });
'id, created_at,name,location,description,contact_info,additional_info->experience,status,resources,help_type,town_id,other_help',
{ count: 'exact' },
)
.eq('type', 'ofrece');

if (help_type !== null) {
query.contains('help_type', [help_type]);
}

query.neq('status', 'finished');

const { data, count, error } = await query
.range((currentPage - 1) * itemsPerPage, currentPage * itemsPerPage - 1)
.order('created_at', { ascending: false });

if (error) {
return Response.json({ error });
}
const countResponse = count ?? 0;
return Response.json({ data, count: countResponse });
}
25 changes: 25 additions & 0 deletions src/app/api/ofertas/usuario/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

import { createServerRoleClient } from '@/lib/supabase/serverrole';
import { NextRequest } from 'next/server';

export async function GET(request: NextRequest) {
const supabase = await createServerRoleClient();
const { data: dataUser, error: errorUser } = await supabase.auth.getUser();
if (errorUser || !dataUser?.user) {
return Response.json({ message: 'Not logged.', errorUser });
}

const { data, count, error } = await supabase
.from('help_requests')
.select('',
{ count: 'exact' },
)
.eq('type', 'ofrece')
.eq('user_id', dataUser?.user.id)
.select();

if (error) {
return Response.json({ error });
}
return Response.json({ count });
}
39 changes: 20 additions & 19 deletions src/app/api/solicitudes/[id]/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,27 @@ import { createServerRoleClient } from '@/lib/supabase/serverrole';
import { NextRequest } from 'next/server';

export async function GET(request: NextRequest, { params }: { params: { id: string } }) {
const id = params.id;
const id = params.id;

const supabase = await createServerRoleClient();
// const { data: dataUser, error: errorUser } = await supabase.auth.getUser();
// if (errorUser || !dataUser?.user) {
// return Response.json({ message: 'Not logged.', errorUser });
// }
const supabase = await createServerRoleClient();
// const { data: dataUser, error: errorUser } = await supabase.auth.getUser();
// if (errorUser || !dataUser?.user) {
// return Response.json({ message: 'Not logged.', errorUser });
// }

const { data, error } = await supabase
.from('help_requests')
.select(
'id, created_at, name, location, description, urgency, number_of_people, contact_info, additional_info->special_situations, status, resources, latitude, longitude, coordinates, help_type, people_needed, other_help,town_id',
{ count: 'exact' },
)
.eq('id', id)
.limit(1)
.select();
const { data, error } = await supabase
.from('help_requests')
.select(
'id, created_at, name, location, description, urgency, number_of_people, contact_info, additional_info->special_situations, status, resources, latitude, longitude, coordinates, help_type, people_needed, other_help,town_id',
{ count: 'exact' },
)
.eq('id', id)
.eq('type', 'necesita')
.limit(1)
.select();

if (error) {
return Response.json({ error });
}
return Response.json({ data });
if (error) {
return Response.json({ error });
}
return Response.json({ data });
}
Loading

0 comments on commit bc9dfb1

Please sign in to comment.