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

Feat/backend PR #3

Open
wants to merge 5 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
132 changes: 129 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"@emotion/styled": "^11.13.5",
"@mui/material": "^6.1.9",
"@mui/material-nextjs": "^6.1.9",
"@notionhq/client": "^2.2.15",
"next": "^15.1.3",
"react": "^18",
"react-dom": "^18"
Expand Down
15 changes: 15 additions & 0 deletions pages/api/achieve.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { AchieveList } from "@/src/object";
import type { NextApiRequest, NextApiResponse } from "next";

type ResponseData = {
response: any;
};

const achieve = new AchieveList();

export default async function handler(
req: NextApiRequest,
res: NextApiResponse<ResponseData>
) {
res.status(200).json({ response: await achieve.Get() });
}
15 changes: 15 additions & 0 deletions pages/api/member.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { MemberList } from "@/src/object";
import type { NextApiRequest, NextApiResponse } from "next";

type ResponseData = {
response: any;
};

const member = new MemberList();

export default async function handler(
req: NextApiRequest,
res: NextApiResponse<ResponseData>
) {
res.status(200).json({ response: await member.Get() });
}
29 changes: 29 additions & 0 deletions src/notion.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
const { Client } = require("@notionhq/client");

const notion = new Client({ auth: process.env.NOTION });

function parseProp(prop: any): any {
try {
if (prop["type"] == "title") return prop["title"][0]["plain_text"];
if (prop["type"] == "email") return prop["email"];
if (prop["type"] == "number") return prop["number"];
if (prop["type"] == "rich_text") return prop["rich_text"][0]["plain_text"];
if (prop["type"] == "phone_number") return prop["phone_number"];
if (prop["type"] == "checkbox") return prop["checkbox"];
} catch (e) {
return null;
}
return null;
}

async function Query(type: "Member" | "Achievements"): Promise<any> {
let databaseId;
if (type == "Member") databaseId = process.env.MEMBERDB;
if (type == "Achievements") databaseId = process.env.ACHIEVEDB;

return await notion.databases.query({
database_id: databaseId,
});
}

export { Query, parseProp };
98 changes: 98 additions & 0 deletions src/object.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
import { parseProp, Query } from "./notion";

let MemberArray: Array<Member> = [];
let AchieveArray: Array<Achieve> = [];

class Member {
name: String = "";
active: boolean = true;
num: number = 0;
department: String = "";
phone: String = "";
boj: String = "";
email: String = "";

constructor(prop: any) {
this.name = parseProp(prop["이름"]);
this.active = parseProp(prop["Active"]);
this.num = parseProp(prop["학번"]);
this.department = parseProp(prop["학과"]);
this.phone = parseProp(prop["전화번호"]);
this.boj = parseProp(prop["백준 핸들"]);
this.email = parseProp(prop["이메일"]);

return this;
}

Get() {
return {
name: this.name,
department: this.department,
boj: this.boj,
email: this.email,
};
}
}

class Achieve {
contest: String = "";
team: String = "";
member: String = "";
comments: String = "";

constructor(prop: any) {
this.contest = parseProp(prop["대회명"]);
this.team = parseProp(prop["팀명"]);
this.member = parseProp(prop["팀 멤버"]);
this.comments = parseProp(prop["비고"]);
return this;
}

Get() {
return this;
}
}

class MemberList {
constructor() {
this.Update();
}

Update = async () => {
// caching logic if expression. TBD
if (MemberArray.length == 0) {
const query = await Query("Member");
MemberArray = query["results"].map(
(elem: any) => new Member(elem["properties"])
);
}
};

Get = async () => {
await this.Update();
return MemberArray.map((elem) => elem.Get());
};
}

class AchieveList {
constructor() {
this.Update();
}

Update = async () => {
// caching logic if expression. TBD
if (AchieveArray.length == 0) {
const query = await Query("Achievements");
AchieveArray = query["results"].map(
(elem: any) => new Achieve(elem["properties"])
);
}
};

Get = async () => {
await this.Update();
return AchieveArray.map((elem) => elem.Get());
};
}

export { MemberList, AchieveList };