-
Notifications
You must be signed in to change notification settings - Fork 947
Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8"> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||
<meta http-equiv="X-UA-Compatible" content="ie=edge"> | ||
<title>Document</title> | ||
</head> | ||
<body> | ||
<h1>MY BEST TODO APP EVER</h1> | ||
<ol id = 'olTodo'> | ||
|
||
</ol> | ||
<button id = 'btnCreate'>Add ToDO!</button> | ||
<script> | ||
|
||
const btnCreate = document.getElementById("btnCreate") | ||
btnCreate.addEventListener("click", async e=> { | ||
const jsonRequest = {} | ||
jsonRequest.todo = prompt("Enter your todo item!") | ||
const result = await fetch("http://localhost:8080/todos", {method: "POST", | ||
headers: {"content-type": "application/json"}, body: JSON.stringify(jsonRequest) }) | ||
const success = await result.json(); | ||
readTodos() | ||
alert("CREATED! ") | ||
|
||
}) | ||
readTodos(); | ||
async function readTodos() { | ||
|
||
try{ | ||
const olTodo = document.getElementById("olTodo") | ||
while(olTodo.firstChild) olTodo.removeChild(olTodo.firstChild) | ||
|
||
const result = await fetch("http://localhost:8080/todos", {method:"GET"}) | ||
const todos = await result.json(); | ||
todos.forEach(t=>{ | ||
|
||
const li = document.createElement("li") | ||
li.textContent = t.text; | ||
li.id = t.id; | ||
li.addEventListener("click", async e => { | ||
const jsonRequest = {} | ||
jsonRequest.id = e.target.id; | ||
const result = await fetch("http://localhost:8080/todos", {method: "DELETE", | ||
headers: {"content-type": "application/json"}, body: JSON.stringify(jsonRequest) }) | ||
const success = await result.json(); | ||
readTodos(); | ||
alert("Deleted! ") | ||
}) | ||
olTodo.appendChild(li) | ||
}) | ||
} | ||
catch (e) { | ||
console.log("Error reading the todos.") | ||
} | ||
|
||
} | ||
</script> | ||
</body> | ||
</html> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
{ | ||
"name": "express-postgres", | ||
"version": "1.0.0", | ||
"description": "", | ||
"main": "server.js", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1", | ||
"start": "node server.js" | ||
}, | ||
"author": "", | ||
"license": "ISC" | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
const {Client} = require("pg") | ||
const express = require ("express") | ||
const app = express(); | ||
app.use(express.json()) | ||
|
||
const client = new Client({ | ||
"user": "postgres", | ||
"password" : "postgres", | ||
"host" : "husseinmac", | ||
"port" : 5432, | ||
"database" : "todo" | ||
}) | ||
|
||
app.get("/", (req, res) => res.sendFile(`${__dirname}/index.html`)) | ||
|
||
app.get("/todos", async (req, res) => { | ||
const rows = await readTodos(); | ||
res.setHeader("content-type", "application/json") | ||
res.send(JSON.stringify(rows)) | ||
}) | ||
|
||
|
||
|
||
app.post("/todos", async (req, res) => { | ||
let result = {} | ||
try{ | ||
|
||
const reqJson = req.body; | ||
await createTodo(reqJson.todo) | ||
result.success= true; | ||
} | ||
catch(e){ | ||
result.success=false; | ||
} | ||
finally{ | ||
res.setHeader("content-type", "application/json") | ||
res.send(JSON.stringify(result)) | ||
} | ||
|
||
}) | ||
|
||
|
||
|
||
|
||
|
||
app.delete("/todos", async (req, res) => { | ||
let result = {} | ||
try{ | ||
|
||
const reqJson = req.body; | ||
await deleteTodo(reqJson.id) | ||
result.success= true; | ||
} | ||
catch(e){ | ||
result.success=false; | ||
} | ||
finally{ | ||
res.setHeader("content-type", "application/json") | ||
res.send(JSON.stringify(result)) | ||
} | ||
|
||
}) | ||
|
||
|
||
app.listen(8080, () => console.log("Web server is listening.. on port 8080")) | ||
|
||
start() | ||
|
||
async function start() { | ||
await connect(); | ||
/* | ||
const todos = await readTodos(); | ||
console.log(todos) | ||
const successCreate = await createTodo("Go to trader joes") | ||
console.log(`Creating was ${successCreate}`) | ||
const successDelete = await deleteTodo(1) | ||
console.log(`Deleting was ${successDelete}`) | ||
*/ | ||
} | ||
|
||
async function connect() { | ||
try { | ||
await client.connect(); | ||
} | ||
catch(e) { | ||
console.error(`Failed to connect ${e}`) | ||
} | ||
} | ||
|
||
async function readTodos() { | ||
try { | ||
const results = await client.query("select id, text from todos"); | ||
return results.rows; | ||
} | ||
catch(e){ | ||
return []; | ||
} | ||
} | ||
|
||
async function createTodo(todoText){ | ||
|
||
try { | ||
await client.query("insert into todos (text) values ($1)", [todoText]); | ||
return true | ||
} | ||
catch(e){ | ||
return false; | ||
} | ||
} | ||
|
||
|
||
|
||
async function deleteTodo(id){ | ||
|
||
try { | ||
await client.query("delete from todos where id = $1", [id]); | ||
return true | ||
} | ||
catch(e){ | ||
return false; | ||
} | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8"> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||
<meta http-equiv="X-UA-Compatible" content="ie=edge"> | ||
<title>Document</title> | ||
</head> | ||
<body> | ||
<canvas id = 'myCanvas'></canvas> | ||
<script> | ||
const pi = Math.PI; | ||
const c = document.getElementById("myCanvas"); | ||
|
||
c.width = window.innerWidth; | ||
c.height= window.innerHeight; | ||
|
||
const ctx = c.getContext("2d"); | ||
c.style.background = "gray" | ||
const x = window.innerWidth/2; | ||
const y = window.innerHeight/2; | ||
|
||
|
||
drawSlice(100, 200, pi, 1.59*pi, "green", "Black"); | ||
drawSlice(100, 200, 1.6*pi, 1.7*pi, "rgba(255, 0, 0, 0.1)"); | ||
drawSlice(100, 200, 1.7*pi, 1.99*pi, "rgba(0, 255, 0, 0.1)" ); | ||
|
||
function drawSlice(innerRadius, outerRadious, fromAngle, toAngle, color, strikeColor=color) { | ||
|
||
ctx.beginPath(); | ||
ctx.arc(x, y, innerRadius, fromAngle, toAngle); | ||
ctx.arc(x, y, outerRadious, toAngle, fromAngle, 1 ); | ||
ctx.arc(x, y, innerRadius, fromAngle, fromAngle); | ||
|
||
ctx.strokeStyle = strikeColor | ||
ctx.fillStyle = color | ||
ctx.fill(); | ||
|
||
ctx.stroke(); | ||
ctx.closePath(); | ||
|
||
const x2 = outerRadious * Math.cos(fromAngle) + x | ||
const y2 = outerRadious * Math.sin(fromAngle) + y | ||
|
||
const x3 = outerRadious * Math.cos(toAngle) + x | ||
const y3 = outerRadious * Math.sin(toAngle) + y | ||
|
||
const centerX = (x2+x3)/2 | ||
const centerY = (y2+y3)/2 | ||
|
||
//draw text | ||
ctx.fillStyle = "black" | ||
ctx.save(); | ||
ctx.translate(centerX, centerY); | ||
ctx.rotate(-(1/4)*pi); | ||
ctx.textAlign = "center"; | ||
ctx.fillText("Your Label Here", 0, 0); | ||
ctx.restore(); | ||
|
||
|
||
} | ||
|
||
|
||
</script> | ||
</body> | ||
</html> |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.