-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: added api for request and offers
- Loading branch information
1 parent
246ae0c
commit 811bde1
Showing
11 changed files
with
249 additions
and
103 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import { NextRequest } from 'next/server'; | ||
import { createServerRoleClient } from '@/lib/supabase/server_role'; | ||
|
||
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' }); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,42 @@ | ||
import { createClient } from '@/lib/supabase/server'; | ||
import { NextRequest } from 'next/server'; | ||
import { createServerRoleClient } from '@/lib/supabase/server_role'; | ||
|
||
export async function GET() { | ||
const supabase = await createClient(); | ||
const { data, error } = await supabase.auth.getUser(); | ||
if (error || !data?.user) { | ||
return Response.json({ message: 'Not logged.', error }); | ||
} | ||
const email = data.user.email; | ||
const registeredPost = await supabase | ||
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') | ||
.select('id') | ||
.eq('type', 'ofrece') | ||
.or(`contact_info.ilike.%${email}%,additional_info.cs.${JSON.stringify({ email: email })}`); | ||
return Response.json({ registeredPost }); | ||
.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 }); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { createServerRoleClient } from '@/lib/supabase/server_role'; | ||
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('id', id) | ||
.limit(1) | ||
.select(); | ||
|
||
if (error) { | ||
return Response.json({ error }); | ||
} | ||
return Response.json({ data }); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,48 +1,52 @@ | ||
import { NextRequest } from 'next/server'; | ||
import { createServerRoleClient } from '@/lib/supabase/server_role'; | ||
|
||
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 town_id = searchParams.get('town'); | ||
const urgency = searchParams.get('urgency'); | ||
const currentPage = searchParams.get('page') ?? 1 ; | ||
const itemsPerPage = 10; | ||
const supabase = await createServerRoleClient(); | ||
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') || null; | ||
const town_id = searchParams.get('town') || null; | ||
const urgency = searchParams.get('urgency') || null; | ||
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, urgency, number_of_people, contact_info, additional_info->special_situations, status, resources, help_type, people_needed, other_help', { count: 'exact' }).eq('type', 'necesita'); | ||
|
||
if (help_type !== null) { | ||
query.contains('help_type', [help_type]); | ||
} | ||
|
||
if (town_id !== null) { | ||
query.eq('town_id', town_id); | ||
} | ||
|
||
if (urgency !== null) { | ||
query.eq('urgency', urgency); | ||
} | ||
|
||
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}) | ||
} else { | ||
const countResponse = count ?? 0; | ||
return Response.json({data, count: countResponse }); | ||
} | ||
return Response.json({message: 'Error'}); | ||
} | ||
const query = 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', 'necesita'); | ||
|
||
if (help_type !== null) { | ||
query.contains('help_type', [help_type]); | ||
} | ||
|
||
if (town_id !== null) { | ||
query.eq('town_id', town_id); | ||
} | ||
|
||
if (urgency !== null) { | ||
query.eq('urgency', urgency); | ||
} | ||
|
||
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 }); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.