Skip to content

Commit

Permalink
Deploy Final
Browse files Browse the repository at this point in the history
  • Loading branch information
공태현 committed Feb 21, 2024
1 parent 1503a0b commit 69d9a93
Show file tree
Hide file tree
Showing 11 changed files with 153 additions and 69 deletions.
41 changes: 26 additions & 15 deletions src/lib/repository/contactRepository.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import { supabase } from "../supabaseClient";

class ContactRepository {
async fetchContact() {
const { data: contacts } = await supabase
async fetchTotalCount() {
const { count, error } = await supabase
.from("contact")
.select("*, user(*)")
.select("*", { count: "exact" })
.order("createdAt", { ascending: false });

return contacts;
if (error) throw new Error(error);
return { count };
}

async fetchContactById(id) {
Expand All @@ -18,19 +19,29 @@ class ContactRepository {
return contact;
}

async fetchContactByOption(filter) {
async fetchContactByOptionWithPaging({ filter, pageNum }) {
const page = (Number(pageNum) - 1) * 20;

if (filter === "all") {
return await this.fetchContact();
const { data: contacts, error } = await supabase
.from("contact")
.select("*, user(*)")
.order("createdAt", { ascending: false })
.range(page, page + 19);

if (error) throw new Error(error.message);
return { contacts };
} else {
const { data: contacts, error } = await supabase
.from("contact")
.select("*, user(*)")
.eq("status", filter)
.order("createdAt", { ascending: false })
.range(page, page + 19);

if (error) throw new Error(error.message);
return { contacts };
}
const { data: contacts, error } = await supabase
.from("contact")
.select("*, user(*)")
.eq("status", filter)
.order("createdAt", { ascending: false });

if (error) throw new Error(error.message);

return contacts;
}

async updateContact({ contact }) {
Expand Down
15 changes: 13 additions & 2 deletions src/lib/repository/productRepository.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,19 @@
import { supabase } from "../supabaseClient";

class ProductRepository {
async fetchProductList() {
const { data: products, error: err } = await supabase.from("product").select("*");
async fetchTotalCount() {
const { count, error } = await supabase.from("product").select("*", { count: "exact" });
if (error) throw new Error(error);
return { count };
}

async fetchProductsByPaging({ pageNum }) {
const page = (Number(pageNum) - 1) * 20;

const { data: products, error: err } = await supabase
.from("product")
.select("*")
.range(page, page + 19);

if (err) throw new Error(err.message);

Expand Down
5 changes: 4 additions & 1 deletion src/lib/supabaseClient.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import { createClient } from "@supabase/supabase-js";
import { env } from "$env/dynamic/public";

export const supabase = createClient(env.PUBLIC_SUPABASE_URL, env.PUBLIC_SUPABASE_ANON_KEY);
console.log(env.PUBLIC_SUPABASE_ROLE_KEY);
console.log(env);
export const supabase = createClient(env.PUBLIC_SUPABASE_URL, env.PUBLIC_SUPABASE_ANON_KEY, env.SERVICE_ROLE_SECRET);
export const supabaseAdmin = createClient(env.PUBLIC_SUPABASE_URL, env.PUBLIC_SUPABASE_ROLE_KEY);
4 changes: 3 additions & 1 deletion src/routes/admin/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
const deletedAdmins = async () => {
if (selectedAdmins.length === 0) {
alert("삭제할 쿠폰을 선택해주세요");
alert("삭제할 관리자를 선택해주세요");
return;
}
Expand All @@ -66,6 +66,8 @@
if (result) {
alert("삭제되었습니다.");
window.location.reload();
} else {
alert("오류가 발생하였습니다.");
}
};
Expand Down
25 changes: 24 additions & 1 deletion src/routes/admin/+server.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,27 @@
import { json } from "@sveltejs/kit";
import { supabase } from "../../lib/supabaseClient.js";
import { supabase, supabaseAdmin } from "../../lib/supabaseClient.js";

export const DELETE = async ({ request }) => {
const admins = await request.json();
const ids = admins.map((err) => err.id);

try {
// Auth 삭제
admins.forEach(async (test) => {
const { error } = await supabaseAdmin.auth.admin.deleteUser(test.id);

if (error) throw new Error(error.message);
});

const { error: deleteUser } = await supabase.from("admin").delete().in("id", ids);

if (deleteUser) throw new Error(deleteUser.message);
return json(true);
} catch (error) {
console.error(error.message);
return json(false);
}
};

export const POST = async ({ request }) => {
const admin = await request.json();
Expand All @@ -24,6 +46,7 @@ export const POST = async ({ request }) => {
}

const { error: insertErr } = await supabase.from("admin").insert({
id: data.user.id,
email: admin.email,
name: admin.name,
role: admin.role,
Expand Down
32 changes: 0 additions & 32 deletions src/routes/auth/+page.server.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,38 +4,6 @@ import { supabase } from "../../lib/supabaseClient.js";
export const load = async () => {};

export const actions = {
signUp: async ({ request }) => {
try {
const data = await request.formData();

const email = data.get("email");
const password = data.get("password");
const name = data.get("name");

const { data: user, error: err } = await supabase.auth.signUp({
email,
password,
options: {
data: {
role: "ADMIN",
},
},
});

if (err) throw new Error(err.message);

const { error } = await supabase.from("admin").insert({
name,
email,
});
if (error) throw new Error(err.message);
return "success";
} catch (err) {
console.error("Auth Error : ", err);
return "fail";
}
},

/**
* 로그인
*/
Expand Down
11 changes: 1 addition & 10 deletions src/routes/auth/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,8 @@

<div class="flex flex-col items-center justify-center">
<div class=" font-bold">Thome App 관리자 사이트</div>
<form method="post" class="flex flex-col" use:enhance={submitForm}>
<form method="post" class="mt-4 flex flex-col" use:enhance={submitForm}>
<div class="mb-4">
<input
bind:value={name}
placeholder="이름을 입력해주세요"
class="mr-2 rounded-lg border border-gray-200 bg-white px-4 py-2 text-sm text-gray-900 focus:outline-none focus:ring-2 focus:ring-blue-500" />
<input
bind:value={email}
placeholder="ID를 입력해주세요"
Expand All @@ -42,10 +38,5 @@
formaction="?/signIn"
class="mb-2 rounded-lg border border-gray-200 bg-white px-4 py-2 text-sm font-medium text-gray-900 hover:bg-gray-100 hover:text-blue-700 dark:border-gray-600 dark:bg-gray-800 dark:text-gray-400 dark:hover:bg-gray-700 dark:hover:text-white dark:focus:ring-gray-700"
>로그인</button>

<button
formaction="?/signUp"
class="mb-2 rounded-lg border border-gray-200 bg-white px-4 py-2 text-sm font-medium text-gray-900 hover:bg-gray-100 hover:text-blue-700 dark:border-gray-600 dark:bg-gray-800 dark:text-gray-400 dark:hover:bg-gray-700 dark:hover:text-white dark:focus:ring-gray-700"
>회원가입</button>
</form>
</div>
7 changes: 5 additions & 2 deletions src/routes/contact/+page.server.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@ import contactRepository from "../../lib/repository/contactRepository";

export const load = async ({ url }) => {
const filter = url.searchParams.get("filter") || "all";
const pageNum = url.searchParams.get("pageNum") || 1;

const contacts = await contactRepository.fetchContactByOption(filter);
const { contacts } = await contactRepository.fetchContactByOptionWithPaging({ filter, pageNum });

return { contacts };
const { count } = await contactRepository.fetchTotalCount();

return { contacts, count };
};
37 changes: 36 additions & 1 deletion src/routes/contact/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@
export let data;
const { contacts } = data;
const { contacts, count: totalContactCount } = data;
let selectedStatus;
$: pageNum = $page.url.searchParams.get("pageNum") || 1;
onMount(() => {
selectedStatus = $page.url.searchParams.get("filter") || "all";
});
Expand Down Expand Up @@ -52,3 +54,36 @@
{/each}
</tbody>
</table>

{#if totalContactCount > 0 && !isNaN(Number(pageNum))}
<div class="my-3 flex w-full justify-center gap-4">
<button
type="button"
class="mr-4 cursor-pointer"
disabled={Number(pageNum) === 1}
on:click={() => {
window.location.href = `/contact?pageNum=1&filter=${selectedStatus}`;
}}>«</button>
{#if Number(pageNum) !== 1}
<button
type="button"
on:click={() => {
window.location.href = `/contact?pageNum=${Number(pageNum) - 1}&filter=${selectedStatus}`;
}}>{Number(pageNum) - 1}</button>
{/if}
<button class="font-bold text-primary underline">{Number(pageNum)}</button>
{#if Number(pageNum) < Math.ceil(totalContactCount / 20)}
<button
type="button"
on:click={() => {
window.location.href = `/contact?pageNum=${Number(pageNum) + 1}&filter=${selectedStatus}`;
}}>{Number(pageNum) + 1}</button>
{/if}
<button
class="ml-4 cursor-pointer"
disabled={Number(pageNum) >= Math.ceil(totalContactCount / 20)}
on:click={() => {
window.location.href = `/contact?pageNum=${Math.ceil(totalContactCount / 20)}&filter=${selectedStatus}`;
}}>»</button>
</div>
{/if}
8 changes: 5 additions & 3 deletions src/routes/product/+page.server.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import productRepository from "../../lib/repository/productRepository";

export const load = async () => {
const { products } = await productRepository.fetchProductList();
export const load = async ({ url }) => {
const pageNum = url.searchParams.get("pageNum") || 1;
const { products } = await productRepository.fetchProductsByPaging({ pageNum });
const { count } = await productRepository.fetchTotalCount();

return { products };
return { products, count };
};

export const actions = {
Expand Down
37 changes: 36 additions & 1 deletion src/routes/product/+page.svelte
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
<script>
import { enhance } from "$app/forms";
import { goto } from "$app/navigation";
import { page } from "$app/stores";
export let data;
const { products } = data;
const { products, count: totalProductCount } = data;
$: pageNum = $page.url.searchParams.get("pageNum") || 1;
let selectedProducts = [];
let isAllSelected = false;
Expand Down Expand Up @@ -112,6 +114,39 @@
</tbody>
</table>

{#if totalProductCount > 0 && !isNaN(Number(pageNum))}
<div class="my-3 flex w-full justify-center gap-4">
<button
type="button"
class="mr-4 cursor-pointer"
disabled={Number(pageNum) === 1}
on:click={() => {
window.location.href = `/program?pageNum=1`;
}}>«</button>
{#if Number(pageNum) !== 1}
<button
type="button"
on:click={() => {
window.location.href = `/program?pageNum=${Number(pageNum) - 1}`;
}}>{Number(pageNum) - 1}</button>
{/if}
<button class="font-bold text-primary underline">{Number(pageNum)}</button>
{#if Number(pageNum) < Math.ceil(totalProductCount / 20)}
<button
type="button"
on:click={() => {
window.location.href = `/program?pageNum=${Number(pageNum) + 1}`;
}}>{Number(pageNum) + 1}</button>
{/if}
<button
class="ml-4 cursor-pointer"
disabled={Number(pageNum) >= Math.ceil(totalProductCount / 20)}
on:click={() => {
window.location.href = `/program?pageNum=${Math.ceil(totalProductCount / 20)}`;
}}>»</button>
</div>
{/if}

<div class="mt-4 flex items-center justify-center">
<button
on:click={() => goto("/product/new")}
Expand Down

0 comments on commit 69d9a93

Please sign in to comment.