Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Meet, User API 추가 #2

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions src/apis/meet.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { FlatMeet, Meet } from "@/interfaces/Meet";
import { addDocument, getDocument, updateDocument } from "@/utils/firestore";
import { getUser } from "@/apis/user";

export const getMeet = async (meetId: Meet["id"]): Promise<Meet> => {
const { attendeeIds, ...meet } = await getDocument<FlatMeet>("meet", meetId);

return {
...meet,
attendees: await Promise.all(attendeeIds.map((id) => getUser(id))),
};
};

export const createMeet = async (
meet: Omit<FlatMeet, "id">
): Promise<Meet["id"]> => {
return addDocument("meet", meet);
};

export const patchMeet = async (
meetId: Meet["id"],
meet: Partial<Omit<FlatMeet, "id">>
): Promise<void> => {
return updateDocument("meet", meetId, meet);
};
19 changes: 19 additions & 0 deletions src/apis/user.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { User } from "@/interfaces/User";
import { addDocument, getDocument, updateDocument } from "@/utils/firestore";

export const getUser = async (userId: User["id"]): Promise<User> => {
return getDocument("user", userId);
};

export const createUser = async (
user: Omit<User, "id">
): Promise<User["id"]> => {
return addDocument("user", user);
};

export const patchUser = async (
userId: User["id"],
user: Omit<User, "id">
): Promise<void> => {
return updateDocument("user", userId, user);
};
6 changes: 5 additions & 1 deletion src/interfaces/Meet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,9 @@ export interface Meet {
offline: boolean /** 오프라인 가능 여부 */;
candidateSchedules: Schedule[] /** 선택 가능한 일정 */;

attendee: User[] /** 참여자 */;
attendees: User[] /** 참여자 */;
}

export type FlatMeet = Omit<Meet, "attendees"> & {
attendeeIds: User["id"][];
};
71 changes: 61 additions & 10 deletions src/utils/firestore.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
import { initializeApp } from "firebase/app";
import {
DocumentData,
WithFieldValue,
UpdateData,
addDoc,
collection,
doc,
getDoc,
getDocs,
getFirestore,
onSnapshot,
updateDoc,
} from "firebase/firestore";

/**
* Intialization
*/

const app = initializeApp({
apiKey: import.meta.env.VITE_FIREBASE_API_KEY,
authDomain: import.meta.env.VITE_FIREBASE_AUTH_DOMAIN,
Expand All @@ -24,19 +29,65 @@ const db = getFirestore(app);
/**
* Utils
*/
export const addDocument = async <T extends WithFieldValue<DocumentData>>(
collectionName: string,
data: T

type Document = { id: string } & DocumentData;

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

collectionId를 한번만 받아서 이 모든 util들을 묶어서 return 하는 하나의 함수를 만드는건 어떨까용?? 콜렉션이 여러개 생길때마다 이 함수들이 모두 필요할것 같고 그 함수들마다 기본 타입은 같을거 같아서 한번에 만들어낼 수 있으면 더 좋을듯 !!

export const getDocuments = async <T extends Document>(
collectionId: string
) => {
const collectionRef = collection(db, collectionId);
const collectionSnapshot = await getDocs(collectionRef);

return collectionSnapshot.docs.map(
(document) => ({ id: document.id, ...document.data() } as T)
);
};

export const getDocument = async <T extends Document>(
collectionId: string,
documentId: string
) => {
const documentRef = doc(db, collectionId, documentId);
const document = await getDoc(documentRef);

if (!document.exists()) {
throw new Error("No such document!");
}

return { id: document.id, ...document.data() } as T;
};

export const watchDocument = async <T extends Document>(
collectionId: string,
documentId: string,
callback: (data: T) => void
) => {
const ref = await addDoc(collection(db, collectionName), data);
const documentRef = doc(db, collectionId, documentId);

return onSnapshot(documentRef, (document) => {
if (!document.exists()) {
throw new Error("No such document!");
}

return ref.id;
callback({ id: document.id, ...document.data() } as T);
});
};

export const getDocument = async <T extends DocumentData>(
collectionName: string
export const addDocument = async <T extends DocumentData>(
collectionId: string,
data: T
) => {
const snapshot = await getDocs(collection(db, collectionName));
const collectionRef = collection(db, collectionId);
const documentRef = await addDoc(collectionRef, data);

return documentRef.id;
};

return snapshot.docs.map((doc) => doc.data() as T);
export const updateDocument = async <T extends DocumentData>(
collectionId: string,
documentId: string,
data: UpdateData<T>
) => {
const documentRef = doc(db, collectionId, documentId);
await updateDoc(documentRef, data);
};
4 changes: 4 additions & 0 deletions vite.config.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import { defineConfig } from "vite";
import react from "@vitejs/plugin-react";
import path from "path";

// https://vitejs.dev/config/
export default defineConfig({
plugins: [react()],
resolve: {
alias: [{ find: "@", replacement: path.resolve(__dirname, "./src") }],
},
});