Skip to content

Commit

Permalink
moving backend to supabase
Browse files Browse the repository at this point in the history
  • Loading branch information
Linzo99 committed Mar 6, 2024
1 parent 9211549 commit d1e507f
Show file tree
Hide file tree
Showing 63 changed files with 10,209 additions and 1,292 deletions.
4 changes: 0 additions & 4 deletions .vscode/settings.json

This file was deleted.

262 changes: 262 additions & 0 deletions actions/api/client.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,262 @@
"use server"

import { cookies } from "next/headers"
import { createServerActionClient } from "@supabase/auth-helpers-nextjs"

import {
Audio,
Author,
Database,
Reciter,
TablesInsert,
TablesUpdate,
Verse,
Xassida,
} from "@/types/supabase"

//const supabase = createClientComponentClient<Database>({})
const supabase = createServerActionClient<Database>({
cookies,
})

const signInWithEmail = async (values: { email: string; password: string }) => {
const { error } = await supabase.auth.signInWithPassword(values)
if (error) throw Error

return true
}

const signOut = async () => await supabase.auth.signOut()

const searchXassida = async (search: string): Promise<Xassida[]> => {
const { data, error } = await supabase
.from("xassida")
.select("*, author(name, tariha)")
.like("name", `%${search}%`)
.limit(10)
if (error) throw error

return data as any
}

const searchReciter = async (search: string): Promise<Reciter[]> => {
const { data, error } = await supabase
.from("reciter")
.select("*")
.like("name", `%${search}%`)
.limit(10)
if (error) throw error

return data as any
}

const getXassidas = async (
filters = {},
offset = 0,
page_size = 15
): Promise<Xassida[]> => {
const from = offset * page_size
const to = from + page_size - 1
let query = supabase
.from("xassida")
.select("*, author!inner(*)")
.order("name")
// apply filters
Object.entries(filters).forEach(([k, v]) => {
if (v) query = query.filter(k, "eq", v)
})
const { data, error } = await query.range(from, to)
if (error) throw error

return data as any
}

const getXassidaById = async (id: number): Promise<Xassida> => {
const { data, error } = await supabase
.from("xassida")
.select("*, chapter(id, number), author(*), reciter(id, name)")
.eq("id", id)
.single()
if (error) throw error

return data as any
}

const getVersesByChapterId = async (
id: number,
offset = 1,
page_size = 20
): Promise<Verse[]> => {
const from = offset * page_size
const to = from + page_size - 1
const { data, error } = await supabase
.from("verse")
.select("*, translations:verse_translation(*)")
.eq("chapter_id", id)
.order("number")
.range(from, to)
if (error) throw error

return data as any
}

const getAuthors = async (
filters = {},
offset = 0,
page_size = 15
): Promise<Author[]> => {
const from = offset * page_size
const to = from + page_size - 1
let query = supabase.from("author").select().order("name")
// apply filters
Object.entries(filters).forEach(([k, v]) => {
if (v) query = query.filter(k, "eq", v)
})
const { data, error } = await query.range(from, to)
if (error) throw error

return data
}

const getAuthorById = async (id: number): Promise<Author> => {
const { data, error } = await supabase
.from("author")
.select()
.eq("id", id)
.single()
if (error) throw error

return data
}

const getReciters = async (
filters = {},
offset = 0,
page_size = 15
): Promise<Reciter[]> => {
const from = offset * page_size
const to = from + page_size - 1
let query = supabase.from("reciter").select().order("name")
// apply filters
Object.entries(filters).forEach(([k, v]) => {
if (v) query = query.filter(k, "eq", v)
})
const { data, error } = await query.range(from, to)
if (error) throw error

return data
}

const getReciterById = async (id: number): Promise<Reciter> => {
const { data, error } = await supabase
.from("author")
.select()
.eq("id", id)
.single()
if (error) throw error

return data
}

const createReciter = async (
values: TablesInsert<"reciter">
): Promise<Reciter> => {
const { data, error } = await supabase.from("reciter").insert(values).select()

if (error) throw error

return data as any
}

const updateReciter = async (id: number, values: TablesUpdate<"reciter">) => {
const { data, error } = await supabase
.from("reciter")
.update(values)
.eq("id", id)
.select()

if (error) throw error

return data as any
}

const getAudios = async (
filters = {},
offset = 0,
page_size = 15
): Promise<Audio[]> => {
const from = offset * page_size
const to = from + page_size - 1
let query = supabase
.from("audio")
.select("*, reciter(*), xassida(id, name, reciter(id, name))")
.order("id")
// apply filters
Object.entries(filters).forEach(([k, v]) => {
if (v) query = query.filter(k, "eq", v)
})
const { data, error } = await query.range(from, to)
if (error) throw error

return data as any
}

const getAudioById = async (id: number): Promise<Audio> => {
const { data, error } = await supabase
.from("audio")
.select("*, reciter(*), xassida(id, name, reciter(id, name))")
.eq("id", id)
.single()
if (error) throw error

return data as any
}

const createAudio = async (values: TablesInsert<"audio">): Promise<Audio> => {
const { data, error } = await supabase.from("audio").insert(values).select()

if (error) throw error

return data as any
}

const updateAudio = async (id: number, values: TablesUpdate<"audio">) => {
const { data, error } = await supabase
.from("audio")
.update(values)
.eq("id", id)
.select()

if (error) throw error

return data as any
}

const uploadFile = async (file: File, path: string, name: string) => {
const { error } = await supabase.storage.from(path).upload(name, file, {
upsert: true,
})
if (error) return { success: false, error }
else return { success: true, image: name }
}

export {
searchXassida,
searchReciter,
getXassidas,
getXassidaById,
getVersesByChapterId,
getAuthors,
getAuthorById,
getReciters,
getReciterById,
createReciter,
updateReciter,
getAudios,
getAudioById,
createAudio,
updateAudio,
signInWithEmail,
signOut,
uploadFile,
}
128 changes: 128 additions & 0 deletions actions/api/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
"use server"

import { cookies } from "next/headers"
import { createServerActionClient } from "@supabase/auth-helpers-nextjs"

import { Author, Database, Reciter, Verse, Xassida } from "@/types/supabase"

const supabase = createServerActionClient<Database>({
cookies,
})

const getXassidas = async (
filters = {},
offset = 0,
page_size = 15
): Promise<Xassida[]> => {
const from = offset * page_size
const to = from + page_size - 1
let query = supabase
.from("xassida")
.select("*, author!inner(*)")
.order("name")
// apply filters
Object.entries(filters).forEach(([k, v]) => {
if (v) query = query.filter(k, "eq", v)
})
const { data, error } = await query.range(from, to)
if (error) throw error

return data as any
}

const getXassidaById = async (id: number): Promise<Xassida> => {
const { data, error } = await supabase
.from("xassida")
.select("*, chapter(count), author(*)")
.eq("id", id)
.single()
if (error) throw error

return data as any
}

const getVerseByChapter = async (
id: number,
offset = 1,
page_size = 20
): Promise<Verse[]> => {
const from = offset * page_size
const to = from + page_size
const { data, error } = await supabase
.from("verse")
.select()
.eq("chapter_id", id)
.order("number")
.range(from, to)
if (error) throw error

return data as any
}

const getAuthors = async (
filters = {},
offset = 0,
page_size = 15
): Promise<Author[]> => {
const from = offset * page_size
const to = from + page_size - 1
let query = supabase.from("author").select().order("name")
// apply filters
Object.entries(filters).forEach(([k, v]) => {
if (v) query = query.filter(k, "eq", v)
})
const { data, error } = await query.range(from, to)
if (error) throw error

return data
}

const getAuthorById = async (id: number): Promise<Author> => {
const { data, error } = await supabase
.from("author")
.select()
.eq("id", id)
.single()
if (error) throw error

return data
}

const getReciters = async (
filters = {},
offset = 0,
page_size = 15
): Promise<Reciter[]> => {
const from = offset * page_size
const to = from + page_size - 1
let query = supabase.from("reciter").select().order("name")
// apply filters
Object.entries(filters).forEach(([k, v]) => {
if (v) query = query.filter(k, "eq", v)
})
const { data, error } = await query.range(from, to)
if (error) throw error

return data
}

const getReciterById = async (id: number): Promise<Reciter> => {
const { data, error } = await supabase
.from("author")
.select()
.eq("id", id)
.single()
if (error) throw error

return data
}

export {
getXassidas,
getXassidaById,
getVerseByChapter,
getAuthors,
getAuthorById,
getReciters,
getReciterById,
}
Loading

0 comments on commit d1e507f

Please sign in to comment.