Skip to content

Feature db update - #22

Merged
mee4895 merged 6 commits into
masterfrom
feature-db-update
Jan 8, 2018
Merged

Feature db update#22
mee4895 merged 6 commits into
masterfrom
feature-db-update

Conversation

@buehlefs

@buehlefs buehlefs commented Dec 20, 2017

Copy link
Copy Markdown
Collaborator

Use the updated db for all api calls.

Fixes:

Needs testing and error handling.

@buehlefs

Copy link
Copy Markdown
Collaborator Author

Should fix #5 completely now

@buehlefs buehlefs closed this Dec 21, 2017
@buehlefs

Copy link
Copy Markdown
Collaborator Author

wrong button

@buehlefs buehlefs reopened this Dec 21, 2017
@mee4895 mee4895 added this to the Fachschaftsrelease milestone Jan 8, 2018
Comment thread src/api.js
* Author: Tobias Wältken
*/

/* jslint esversion: 6 */

Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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

Comment thread src/api.js
break;
let stmt = db.prepare("SELECT price, stock FROM Beverages WHERE name = ?;");
stmt.get(beverage, function(err, result) {
if (result == undefined) {

Copy link
Copy Markdown
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
Copy Markdown
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

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

Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown
Member

Choose a reason for hiding this comment

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

#26

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

if (result.stock === 0) {

Copy link
Copy Markdown
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.

Comment thread src/install.js
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
Copy Markdown
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
Copy Markdown
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.

Comment thread src/api.js

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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
Member

Choose a reason for hiding this comment

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

#24

Comment thread src/api.js
let userId = req.params.userId;
let limit = req.query.limit;
if (userId === undefined || userId === '' || !users.has(userId)) {
if (userId === undefined || userId === '') {

Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown
Member

Choose a reason for hiding this comment

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

#24

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

Copy link
Copy Markdown
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

Comment thread src/api.js
let reason = req.query.reason;
if (userId != undefined && amount != undefined && reason != undefined
&& userId != '' && reason != '' && amount != '' && users.has(userId)) {
&& userId != '' && reason != '' && amount != '') {

Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown
Member

Choose a reason for hiding this comment

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

#24

Comment thread src/api.js
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
Copy Markdown
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
Copy Markdown
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