Skip to content
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
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,6 @@ bin/Release
.idea

# Mac files
.DS_Store
.DS_Store

app/dist
5 changes: 5 additions & 0 deletions app/.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
host="127.0.0.1"
port=3306
user="root"
password="12345678"
database="user"
34 changes: 34 additions & 0 deletions app/common/database/mysql.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import * as mysql from "mysql";

const env = process.env
const { host, port, user, password, database } = env
const mysqlConfig :mysql.PoolConfig = {
host,
user,
password,
database,
port: Number(port)
}

const pool = mysql.createPool(mysqlConfig)

const query = (sql: string) => {
return new Promise<any>((resolve, reject) => {
pool.getConnection((error, connection) => {
if (error) {
reject(error)
} else {
connection.query(sql, (error, results) => {
if (error) {
reject(error)
} else {
resolve(results)
}
connection.release()
})
}
})
})
}

export const mysqlConnect = query
22 changes: 22 additions & 0 deletions app/common/httpCode/httpCode.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
export const httpCode = {
SUCCESS:{
code: 200,
detail: "success"
},
INVALID:{
code: 400,
detail: "Invalid ID supplied"
},
NOTFOUND:{
code: 404,
detail: "Player not found"
},
INVALIDINPUT:{
code: 405,
description: "Invalid input"
},
VALIDATION:{
code: 405,
detail: "Validation exception"
},
}
82 changes: 82 additions & 0 deletions app/controller/player.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import { mysqlConnect } from "../common/database/mysql"
import { httpCode } from "../common/httpCode/httpCode";

// Create a new player
export const createPlayer = async (req, res) => {
const { name, position } = req.body;
if (name == undefined || position == undefined) {
res.status(405).json(httpCode.INVALIDINPUT);
return
}
const sql = `insert into player (name, position) values ("${name}", "${position}")`;
const data = await mysqlConnect(sql);
const { insertId } = data;
const player = {
id: insertId,
name,
position
}
res.json(player);
}

// Update an existing player
export const updatePlayer = async (req, res) => {
const { playerId, name, position } = req.body;
if (playerId == undefined) {
res.status(405).json(httpCode.INVALID);
return
}
if (name == undefined || position == undefined) {
res.status(405).json(httpCode.INVALIDINPUT);
return
}
const sql = `update player set name = "${name}", position = "${position}" where id = "${playerId}"`;
const data = await mysqlConnect(sql);
const { affectedRows } = data
if (affectedRows == 0) {
res.status(404).json(httpCode.NOTFOUND);
return
}
const player = {
id: playerId,
name,
position
}
res.json(player);
}

// Find player by ID
export const getPlayer = async (req, res) => {
const { playerId } = req.params;
if (playerId == undefined) {
res.status(405).json(httpCode.INVALID);
return
}
const sql = `select * from player where id = ${playerId}`;
const data = await mysqlConnect(sql);
if (data.length == 0 ) {
res.status(404).json(httpCode.NOTFOUND);
return
}

res.json(data[0]);
}

export const deletePlayer = async (req, res) => {
const { playerId } = req.params;
if (playerId == undefined) {
res.status(405).json(httpCode.INVALID);
return
}
const sql = `delete from player where id = ${playerId}`;
const data = await mysqlConnect(sql);
const { affectedRows } = data
if (affectedRows == 0) {
res.status(404).json(httpCode.NOTFOUND);
return
}
res.json({
playerId
})
}

9 changes: 6 additions & 3 deletions app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,18 @@
"description": "Building a RESTful CRUD API with Node.js, Express/Koa and MongoDB.",
"main": "server.js",
"scripts": {
"start": "NODE_ENV=development node server.js",
"start": "export $(cat .env | xargs) && NODE_ENV=development && tsc && node ./dist/server.js",
"start:prod": "NODE_ENV=production node server.js",
"test": "echo \"Error: no test specified\" && exit 1"
"test": "mocha ./test/test.js"
},
"dependencies": {
"express": "^4.17.1",
"mongoose": "^5.9.2"
"mysql": "^2.18.1"
},
"devDependencies": {
"@types/express": "^4.17.13",
"@types/mysql": "^2.15.21",
"request": "^2.88.2",
"chai": "^4.2.0"
},
"engines": {
Expand Down
Loading