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

(Todo with Database) and (socket.io) and (final project link in README) #110

Open
wants to merge 3 commits into
base: master
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
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,3 +97,6 @@

- Ayush Poddar
- **[Portfolio](https://mr-magnificent.github.io/)**
- **[Socket.io (Chatisry)](https://chatistry.herokuapp.com/)**
- **[todo (with database)](https://dotodaytodo.herokuapp.com/)**
- **[Lyricist (spotify+lastFm+lyrics API)](https://lyricspot.herokuapp.com/)**
16 changes: 16 additions & 0 deletions Summer2018/Ayush Poddar/Socket.io-chat/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"name": "sockt",
"version": "1.0.0",
"description": "Socket application with implimentation for chat",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "Ayush Poddar",
"license": "ISC",
"dependencies": {
"express": "^4.16.3",
"socket.io": "^2.1.1"
},
"devDependencies": {}
}
47 changes: 47 additions & 0 deletions Summer2018/Ayush Poddar/Socket.io-chat/public/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@


$(document).ready(function () {
let btn = $('#btn');
// let inp = $('#inp').val();
let list = $('#list');
let prmt;

while (!prmt) {
prmt = prompt("Please enter your name");
}

let socket = io();



socket.emit('name', prmt);

socket.on('initConnect', function (data) {
console.log(data);
$('#connected').text('');
for (let id in data) {
if (data.hasOwnProperty(id)) {
console.log(data[id]);
$('#connected').append(`<li>${data[id]}</li>`);
}
}
});

socket.on('chat', function (data) {
data.forEach(function(chatItem) {
list.append(`<li><strong>${chatItem[1]}</strong>:- ${chatItem[0]}</li>`);
})
});

btn.click(function () {
let inp = $('#inp').val();
console.log(inp);
socket.emit('message', [inp, prmt]);
list.append(`<li><strong>${prmt}</strong>:- ${inp}</li>`);
});

socket.on('reciveData', function (data) {
console.log(data);
list.append(`<li><strong>${data[1]}</strong>:- ${data[0]}</li>`);
})
})
26 changes: 26 additions & 0 deletions Summer2018/Ayush Poddar/Socket.io-chat/public/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Sock-t</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" type="text/css" href="style.css">

</head>
<body class="container">
<div class="chat-area">
<ul id="list"></ul>
</div>
<div class="connection-area">
<h3>People Connected</h3>
<ul id="connected"></ul>
</div>
<div class="input-area">
<input id="inp" type="text" autofocus placeholder="Type to send">
<button id="btn" type="button">Send!</button>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="/socket.io/socket.io.js"></script>
<script src="app.js"></script>
</body>
</html>
33 changes: 33 additions & 0 deletions Summer2018/Ayush Poddar/Socket.io-chat/public/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
.container {
display: flex;
flex-wrap: wrap;
width: 100%;
height: 100%;
margin: 0 auto;
font-size: 0;
}

.chat-area {
height: 75vh;
width: 74%;
font-size: initial;
border-right: black dashed;
border-bottom: black dashed;
margin: 0 auto;
overflow-y: scroll;
}

.connection-area {
height: 75vh;
width: 25%;
overflow-y: scroll;
font-size: initial;
}

.input-area {
height: 25vh;
width: 100%;
display: table-cell;
font-size: initial;
vertical-align: middle;
}
10 changes: 10 additions & 0 deletions Summer2018/Ayush Poddar/Socket.io-chat/public/username.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<!DOCTYPE html>
<html>
<head>
<title>Username</title>
</head>
<body>
<input id="inp" type="text" name="uname">
<button id="btn">Enter</button>
</body>
</html>
63 changes: 63 additions & 0 deletions Summer2018/Ayush Poddar/Socket.io-chat/server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
let express = require('express');
let app = express();

let server = require('http').Server(app);

let io = require('socket.io')(server);

let nameObj = {};
let initChat = [];

let PORT = process.env.PORT || 5000;
/*
console.log(app);
console.log();
console.log(server);
console.log();
console.log(io);
console.log();
*/


app.use('/', express.static('./public'));


io.on('connection', function (socket) {

socket.on('name', function (data) {
console.log(data);
nameObj[socket.id] = data;
console.log('name');
console.log(nameObj);
io.sockets.emit('initConnect', nameObj);
});


socket.emit('chat', initChat);

socket.on('message', function (data) {
socket.broadcast.emit('reciveData', data);
console.log(data);
initChat.push(data);
});

socket.on('disconnect', function () {
delete nameObj[socket.id];
console.log('disconnect');
console.log(nameObj);
io.sockets.emit('initConnect', nameObj);
console.log(Object.keys(nameObj).length);
if (Object.keys(nameObj).length === 0) {

initChat = [];
}

})
});




server.listen(PORT, function () {
console.log('Server is listening on port ' + PORT);
});
105 changes: 105 additions & 0 deletions Summer2018/Ayush Poddar/todoList-database/database.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
const mongodb = require('mongodb').MongoClient;
let topUser = require('mongodb').ObjectID("5b637cfde7179a0733452cea");

const uri = 'YOUR_DATABASE_URI';

let database;
let collection;

function connectDb() {
console.log("inside connectDb");
mongodb.connect(uri, function (err, client) {
console.log("inside mongodb.connect");
if (err) {
console.log(err);
throw err;
}
database = client.db('todo');
collection = database.collection('collection');

// console.log(collection);

// global.insertNewUser = insertNewUser;
// global.insertTodo = insertTodo;
// global.retrieveAll = retrieveAll;
// global.deleteElement = deleteElement;
// global.updateElement = updateElement;
// global.deleteMul = deleteMul;
});
}

function insertNewUser (userID) {
collection.insertOne({
user: {
userId: userID
}
});
}

function getUserTop (callback) {
collection.find({"_id" : topUser}).toArray(function (err, result) {
if (err) {
console.log(err);
throw err;
}
console.log(result);
incrementUserTop(result);
callback(result);
})
}

function incrementUserTop(data) {
data = parseInt(data[0]['userTop']);
collection.updateOne({"userTop": data}, {$set :{'userTop' : ++data}});
}



function insertTodo(userID, task, status, callback) {
userID = parseInt(userID);
task = task.toString();
console.log(userID, task);
status = status.toString();
collection.updateOne({'user.userId': userID},{$set: {[task]: status}});
callback(task);
}



function retrieveAll(userID, callback) {
console.log(userID);
collection.find({"user.userId": userID}).toArray(function (err, result) {
if (err) {
console.log(err);
throw err;
}
console.log(result);
callback(result);
});
}

function deleteElement(userID, task) {
collection.updateOne({ "user.userId": userID}, { $unset: { [task]: ""}});
}

function updateElement(userID, task, newTask, callback) {
collection.updateOne({ "user.userId": userID}, { $rename: { [task]: newTask}});
callback(newTask);
}

function deleteMul(userID, taskArr) {
taskArr.forEach(function (task) {
collection.updateOne({ "user.userId": userID}, { $unset: { [task]: ""}});
})
}

module.exports = {
insertNewUser,
insertTodo,
retrieveAll,
deleteElement,
deleteMul,
updateElement,
connectDb,
getUserTop
};
18 changes: 18 additions & 0 deletions Summer2018/Ayush Poddar/todoList-database/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"name": "database-integration",
"version": "1.0.0",
"description": "",
"main": "database.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node server.js"
},
"author": "",
"license": "ISC",
"dependencies": {
"body-parser": "^1.18.3",
"cookie-parser": "^1.4.3",
"express": "^4.16.3",
"mongodb": "^3.1.1"
}
}
Loading