Skip to content

Commit

Permalink
v-0.2
Browse files Browse the repository at this point in the history
  • Loading branch information
is-paranoia committed Jul 17, 2022
2 parents e6d4c5a + 31f35d2 commit f4bf28b
Show file tree
Hide file tree
Showing 68 changed files with 3,666 additions and 964 deletions.
2 changes: 2 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
npm-debug.log
39 changes: 39 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
module.exports = {
"env": {
"browser": true,
"node": true,
"es2021": true
},
"extends": [
"eslint:recommended",
"plugin:react/recommended"
],
"parserOptions": {
"ecmaFeatures": {
"jsx": true
},
"ecmaVersion": "latest",
"sourceType": "module"
},
"plugins": [
"react"
],
"rules": {
"indent": [
"error",
"tab"
],
"linebreak-style": [
"error",
"unix"
],
"quotes": [
"error",
"double"
],
"semi": [
"error",
"never"
]
}
}
17 changes: 16 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,16 @@
# 2work.shop
<div align="center">

# 2workshop
Freelance platform, where every client may find talents which realize their dreams. Platform use crypto-payments through smart-contract. Thats meaning only clear and straight interaction, based on trust in blockchain technology, without third parties.

[2workshop.site here! Click!](http://2workshop.site/)

| Part | Tech |
| ------------- |:-------------: |
| Server | ![NodeJS](https://img.shields.io/badge/node.js-6DA55F?style=for-the-badge&logo=node.js&logoColor=white) ![Express.js](https://img.shields.io/badge/express.js-%23404d59.svg?style=for-the-badge&logo=express&logoColor=%2361DAFB) |
| Client | ![React](https://img.shields.io/badge/react-%2320232a.svg?style=for-the-badge&logo=react&logoColor=%2361DAFB) ![React Router](https://img.shields.io/badge/React_Router-CA4245?style=for-the-badge&logo=react-router&logoColor=white) |
| Database | ![Postgres](https://img.shields.io/badge/postgres-%23316192.svg?style=for-the-badge&logo=postgresql&logoColor=white) + knex |
| Smart-contract | ![Solidity](https://img.shields.io/badge/Solidity-%23363636.svg?style=for-the-badge&logo=solidity&logoColor=white) ![Ethereum](https://img.shields.io/badge/Ethereum-3C3C3D?style=for-the-badge&logo=Ethereum&logoColor=white) |
| Security and features | ![Socket.io](https://img.shields.io/badge/Socket.io-black?style=for-the-badge&logo=socket.io&badgeColor=010101) ![Docker](https://img.shields.io/badge/docker-%230db7ed.svg?style=for-the-badge&logo=docker&logoColor=white) ![JWT](https://img.shields.io/badge/JWT-black?style=for-the-badge&logo=JSON%20web%20tokens) + bcrypt |

</div>
76 changes: 30 additions & 46 deletions app.js
Original file line number Diff line number Diff line change
@@ -1,65 +1,49 @@
const express = require('express')
const config = require('config')
const body_parser = require('body-parser')
const {Client} = require('pg')
const knex = require('./knex/knex')
const cors = require('cors')
//const http = require('http')
//const { Server } = require('.io')
const express = require("express")
const config = require("config")
const body_parser = require("body-parser")
const cors = require("cors")


const app = express()
app.use(cors())
app.use(express.json())
app.use(body_parser.json())
app.use(body_parser.urlencoded({ extended: true }))
app.use('/auth', require('./routes/auth'))
app.use('/api', require('./routes/api'))
app.use("/auth", require("./routes/auth"))
app.use("/api", require("./routes/api"))

const PORT = config.get('port') || 8000
const PORT = config.get("port") || 8000

const http = require("http").createServer(app)
const socket = require("socket.io")(http, {
cors: {
origin: `*`,
methods: ["GET", "POST"]}
})

/*const io = new Server(app, {
cors: {
origin: `http://localhost:${PORT}`,
methods: ["GET", "POST"]
}
})*/

cors: {
origin: "*",
methods: ["GET", "POST"]}
})


async function start() {
try {
try {

http.listen(PORT, () => console.log(`App has been started at port ${PORT}...`))
socket.on("connection", (socket) => {
console.log("Socket connected, ID = ", socket.id)
http.listen(PORT, () => console.log(`App has been started at port ${PORT}...`))
socket.on("connection", (socket) => {

socket.on("joinChat", (data) => {
socket.join(data)
console.log(`User ID ${socket.id} joined to chat ID ${data}`)
})

socket.on("sendMessage", (data) => {
console.log("data ", data)
socket.to(data.chatId).emit("receiveMessage", data)
})

socket.on("disconnect", () => {
console.log("User disconnected, ID = ", socket.id)

})
})
} catch (e) {
console.log('Server error', e.message)
process.exit(1)
}
socket.on("joinChat", (data) => {
socket.join(data)
})

socket.on("sendMessage", (data) => {
socket.to(data.chatId).emit("receiveMessage", data)
})

socket.on("disconnect", () => {

})
})
} catch (e) {
console.log("Server error", e.message)
process.exit(1)
}
}

start()
Expand Down
78 changes: 78 additions & 0 deletions controllers/authController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
const jwt = require("jsonwebtoken")
const config = require("config")
const {validationResult} = require("express-validator")
const bcrypt = require("bcrypt")
const knex = require("../knex/knex")

exports.register = async (req, res) => {
try {
console.log("auth")
const errors = validationResult(req)
if (!errors.isEmpty()){
return res.status(400).json({
errors: errors.array(),
message: "Register error",
test: req.body
})
}
const {email, nickname, password} = req.body
console.log(req.body)
const existUser = await knex("Users").where("email", email)
console.log(existUser)
console.log("jjj", existUser.length)
if (existUser.length !== 0) {
return res.status(400).json({message: "User is already exists"})
}
const hashedPassword = await bcrypt.hash(password, 12)
await knex("Users").insert({ email: email, nickname: nickname, password: hashedPassword }).catch(err => console.log("Transaction", err))
res.status(201).json({message: "User has been created"})

} catch (e) {
res.status(500).json({
message: "Server error",
error: e.message
})
}

}

exports.login = async (req, res) => {
try {
console.log("post on login")
const errors = validationResult(req)
if (!errors.isEmpty()){
return res.status(400).json({
errors: errors.array(),
message: "Login error"
})
}

const {email, password} = req.body

const user = await knex("Users").where("email", email).first()
if (user == undefined || user == null) {
return res.status(400).json({
message: "User doesnt exist"
})
}

const isMatch = await bcrypt.compare(password, user.password)

if (!isMatch) {
return res.status(400).json({message: "Error. Check input credentials"})
}

const token = jwt.sign(
{userId: user.id, roleId: user.roleId},
config.get("jwtSecret"),
{expiresIn: "24h"}
)
res.json({token: token, userId: user.id, nickname: user.nickname, roleId: user.roleId})

} catch (e) {
res.status(500).json({
message: "Server error",
error: e.message
})
}
}
29 changes: 29 additions & 0 deletions controllers/commentsController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
const knex = require("../knex/knex")

exports.getCommentsByOrderId = async (req, res) => {
try {
knex.raw(`SELECT * FROM "Comments" WHERE "orderId"=${req.params.order_id} `).then((comments) =>{
res.send(comments.rows)
}).catch(err => console.log("Transaction", err))
} catch (e) {
res.status(500).json({
message: "Server error"
})
}
}

exports.addCommentByOrderId = async (req, res) => {
try {
const {message} = req.body
const comment_dct = { orderId: req.params.order_id, userId: req.user.userId, message: message}
console.log(comment_dct)
await knex("Comments").insert(comment_dct).catch(err => console.log("Transaction", err))
res.status(201).json({message: "Comment has been created"})

} catch (e) {
res.status(500).json({
message: "Server error {api:post:comments}",
error: e.message
})
}
}
28 changes: 28 additions & 0 deletions controllers/messagesController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
const knex = require("../knex/knex")

exports.getMessageByChatId = async (req, res) => {
try {
let query = knex("ChatMessages").select("*").where("chatId", req.params.id)
query.then(response => {
res.send(response)
}).catch(err => console.log("Transaction", err))
} catch (e) {
res.status(500).json({
message: "Server error"
})
}
}

exports.addMessageByChatId = async (req, res) => {
try {
const {chatId, userId, message, timestamp} = req.body
const message_dct = { chatId: chatId, userId: userId, message: message, timestamp: timestamp}
await knex("ChatMessages").insert(message_dct).catch(err => console.log("Transaction", err))
res.status(201).json({message: "ChatMessage has been created"})
} catch (e) {
res.status(500).json({
message: "Server error {api:post:comments}",
error: e.message
})
}
}
Loading

0 comments on commit f4bf28b

Please sign in to comment.