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

Feature db update #22

Merged
merged 6 commits into from
Jan 8, 2018
Merged

Feature db update #22

merged 6 commits into from
Jan 8, 2018

Conversation

buehlefs
Copy link
Collaborator

@buehlefs buehlefs commented Dec 20, 2017

Use the updated db for all api calls.

Fixes:

Needs testing and error handling.

@buehlefs
Copy link
Collaborator Author

Should fix #5 completely now

@buehlefs buehlefs closed this Dec 21, 2017
@buehlefs
Copy link
Collaborator Author

wrong button

@buehlefs buehlefs reopened this Dec 21, 2017
@mee4895 mee4895 added this to the Fachschaftsrelease milestone Jan 8, 2018
@@ -4,6 +4,9 @@
* Author: Sandro Speth
* Author: Tobias Wältken
*/

/* jslint esversion: 6 */
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe create/decide on project wide jslint settings to enforce a uniform coding style

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should be another issue

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is now #23

break;
let stmt = db.prepare("SELECT price, stock FROM Beverages WHERE name = ?;");
stmt.get(beverage, function(err, result) {
if (result == undefined) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Check err first to allow for better error handling as well as catching database errors and other anomalies.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is already a issue (#16, #17). I think a new pull request / issue for additional functionality/ error handling is better than to bloat this PR

console.log('[API] [FAIL] can\'t write /data/beverages.json');
});
break;
let stmt = db.prepare("SELECT price, stock FROM Beverages WHERE name = ?;");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Name it stmt0 for consistency with stmt1, stmt2 and stmt3 ;-)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should be new issue

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#26

src/api.js Outdated
stmt.run(uuidv4(), user, beverage, -cost, new Date().toUTCString());
stmt.finalize();

if (result.stock === 0) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do not check for Zero, although subzero stock makes no sense it is better to have records of all purchases than users beeing unable to purchase the beverage their holding because of some other error.

@@ -19,7 +19,7 @@ var authData = [

db.serialize(function() {
db.run('DROP TABLE IF EXISTS History;');
db.run("CREATE TABLE History (id VARCHAR(255), user VARCHAR(255) NOT NULL, reason VARCHAR(255), amount INTEGER NOT NULL DEFAULT 0, timestamp DATETIME NOT NULL DEFAULT (DATETIME('now', 'localtime')));");
db.run("CREATE TABLE History (id VARCHAR(255), user VARCHAR(255) NOT NULL, reason VARCHAR(255), amount INTEGER NOT NULL DEFAULT 0, beverage VARCHAR(255) NOT NULL DEFAULT '', beverage_count INTEGER NOT NULL DEFAULT 0, timestamp DATETIME NOT NULL DEFAULT (DATETIME('now', 'localtime')));");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

change default value for beverage_count to 1 because it is the commonly used one

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For statements that don't change beverages (user balance changes only) you should be able to leave out the whole beverage part. If default is 1 in sql you would have nasty side effects with such statements.


var stmt3 = db.prepare("INSERT INTO History(id, user, reason, amount, beverage, beverage_count) VALUES (?, ?, ?, ?, ?, ?);");
stmt3.run(uuidv4(), user, beverage, -cost, beverage, 1);
});
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[important] missing the check if the given username exists!
[minor] missing error checking

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

checking if user exists is needed in more than one method and should be as simple as a method call. The problem is, that sql statements work with callbacks. I don't have a good solution yet for this. => should be a new issue

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#24

@@ -199,15 +200,16 @@ api.get('/orders', userAccess(function (req, res) {
api.get('/orders/:userId', userAccess(function (req, res) {
let userId = req.params.userId;
let limit = req.query.limit;
if (userId === undefined || userId === '' || !users.has(userId)) {
if (userId === undefined || userId === '') {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

missing the check if the given username exists!

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#24

src/api.js Outdated
stmt.run(userId);
res.sendStatus(200);
// why return old user?
// res.status(200).send(JSON.stringify(user));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove comment we decided not to anymore

@@ -361,15 +396,15 @@ api.patch('/users/:userId', adminAccess(function (req, res) {
let amount = req.query.amount;
let reason = req.query.reason;
if (userId != undefined && amount != undefined && reason != undefined
&& userId != '' && reason != '' && amount != '' && users.has(userId)) {
&& userId != '' && reason != '' && amount != '') {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add user check

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#24

stmt.run(uuidv4(), userId, reason, amount, new Date().toUTCString());

var stmt = db.prepare("INSERT INTO History(id, user, reason, amount) VALUES (?, ?, ?, ?);");
stmt.run(uuidv4(), userId, reason, amount);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rename stmt to stmt0 to stay consistent

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#26

@mee4895 mee4895 merged commit c0ad9a0 into master Jan 8, 2018
@mee4895 mee4895 deleted the feature-db-update branch January 8, 2018 19:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants