Skip to content

Commit da7d51a

Browse files
committed
feat: Now the HarperDB is replaced with API call for Create Shape
1 parent 4695308 commit da7d51a

4 files changed

Lines changed: 138 additions & 19 deletions

File tree

components/core/CreateShape.js

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,6 @@ import { ShapeForm, ShapePreview } from "..";
1616
// Toast
1717
import toast from "react-hot-toast";
1818

19-
// harperDb fetch call
20-
import { harperFetch } from "../../utils/HarperFetch";
21-
2219
const CreateShape = (props) => {
2320

2421
// Store the default state as a variable so resetting form is easier
@@ -337,30 +334,43 @@ const CreateShape = (props) => {
337334
} else {
338335

339336
// Create the shape in the DB
340-
const insertShape = await harperFetch({
341-
operation: "sql",
342-
sql: `INSERT into tryshape.shapes(backgroundColor, createdAt, createdBy, edges, email, formula, likes, name, notes, private, type, vertices)
343-
values('${shapeInformation.backgroundColor}', null, '${props.user.email}', ${shapeInformation.edges}, null, '${shapeInformation.formula}', 0, '${shapeInformation.name}', '${shapeInformation.notes}', ${shapeInformation.private}, '${shapeInformation.clipPathType}', ${shapeInformation.vertices})`,
337+
const insertShapeResponse = await axios.post('/api/POST/shape', {
338+
name: shapeInformation.name,
339+
formula: shapeInformation.formula,
340+
vertices: shapeInformation.vertices,
341+
visibility: shapeInformation.private,
342+
edges: shapeInformation.edges,
343+
notes: shapeInformation.notes,
344+
type: shapeInformation.clipPathType,
345+
backgroundColor: shapeInformation.backgroundColor,
346+
createdBy: props.user.email,
347+
likes: 0
344348
});
349+
const insertShape = insertShapeResponse.data
345350

346-
console.log(insertShape);
351+
console.log({insertShape});
347352

348353
// Create the user in the db
349-
if (insertShape['inserted_hashes'].length > 0) {
354+
if (insertShape.data['inserted_hashes'].length > 0) {
350355
// First check if the user exist
351-
const result = await harperFetch({
352-
operation: "sql",
353-
sql: `SELECT count(*) from tryshape.users WHERE email='${props.user.email}'`,
354-
});
355-
const count = (result[0]['COUNT(*)']);
356+
const userResponse = await axios.get("/api/GET/user", {
357+
params: {
358+
email: props.user.email
359+
}
360+
});
361+
const result = userResponse.data;
362+
const count = result.length;
356363
console.log({count});
364+
357365
// If doesn't exist, create in db
358366
if (count === 0) {
359-
const insertUser = await harperFetch({
360-
operation: "sql",
361-
sql: `INSERT into tryshape.users(email, name, photoURL)
362-
values('${props.user.email}', '${props.user.displayName}', '${props.user.photoURL}')`,
367+
const insertUserResponse = await axios.post('/api/POST/user', {
368+
displayName: props.user.displayName,
369+
email: props.user.email,
370+
photoURL: props.user.photoURL
363371
});
372+
const insertUser = insertUserResponse.data;
373+
console.log({insertUser});
364374
} else {
365375
console.log(`The user ${props.user.email} present in DB`);
366376
}
@@ -375,7 +385,7 @@ const CreateShape = (props) => {
375385
...props.shapeAction,
376386
"action": "add",
377387
"payload": {
378-
"shape_id": insertShape['inserted_hashes']
388+
"shape_id": insertShape.data['inserted_hashes']
379389
}
380390
});
381391

pages/api/GET/user.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
export default async function handler(req, res) {
2+
const { email } = req.query;
3+
4+
let sql;
5+
6+
if(email) {
7+
sql = `SELECT * from tryshape.users WHERE email='${email}'`;
8+
} else {
9+
sql = `SELECT * from tryshape.users`;
10+
}
11+
12+
const request = await fetch(process.env.NEXT_PUBLIC_DB_URL, {
13+
method: "POST",
14+
headers: {
15+
"Content-Type": "application/json",
16+
Authorization: `Basic ${process.env.NEXT_PUBLIC_DB_AUTHORIZATION}`,
17+
},
18+
body: JSON.stringify({
19+
operation: "sql",
20+
sql: sql,
21+
}),
22+
});
23+
24+
const data = await request.json();
25+
// console.log(data);
26+
27+
res.status(200).json(data);
28+
}

pages/api/POST/shape.js

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
2+
export default async function handler(req, res) {
3+
4+
const {
5+
name,
6+
formula,
7+
vertices,
8+
visibility,
9+
edges,
10+
notes,
11+
type,
12+
backgroundColor,
13+
likes,
14+
createdBy} = req.body;
15+
16+
17+
const record = {};
18+
record['private'] = visibility;
19+
record['name'] = name;
20+
record['formula'] = formula;
21+
record['vertices'] = vertices;
22+
record['edges'] = edges;
23+
record['notes'] = notes;
24+
record['type'] = type;
25+
record['backgroundColor'] = backgroundColor;
26+
record['createdBy'] = createdBy;
27+
record['likes'] = likes;
28+
29+
console.log({record});
30+
31+
const request = await fetch(process.env.NEXT_PUBLIC_DB_URL, {
32+
method: "POST",
33+
headers: {
34+
"Content-Type": "application/json",
35+
Authorization: `Basic ${process.env.NEXT_PUBLIC_DB_AUTHORIZATION}`,
36+
},
37+
body: JSON.stringify({
38+
operation: "insert",
39+
schema: "tryshape",
40+
table: "shapes",
41+
records: [
42+
record
43+
],
44+
}),
45+
});
46+
47+
const data = await request.json();
48+
49+
res.status(200).json({ data });
50+
}
51+

pages/api/POST/user.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
2+
export default async function handler(req, res) {
3+
4+
const { email, displayName, photoURL } = req.body;
5+
6+
const request = await fetch(process.env.NEXT_PUBLIC_DB_URL, {
7+
method: "POST",
8+
headers: {
9+
"Content-Type": "application/json",
10+
Authorization: `Basic ${process.env.NEXT_PUBLIC_DB_AUTHORIZATION}`,
11+
},
12+
body: JSON.stringify({
13+
operation: "insert",
14+
schema: "tryshape",
15+
table: "users",
16+
records: [
17+
{
18+
name: displayName,
19+
email: email,
20+
photoURL: photoURL
21+
},
22+
],
23+
}),
24+
});
25+
26+
const data = await request.json();
27+
28+
res.status(200).json({ data });
29+
}
30+

0 commit comments

Comments
 (0)