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

345 - NHT: Users shall be able to create stats. #357

Open
wants to merge 1 commit 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
13 changes: 13 additions & 0 deletions migrations/20190627103100-add-stats-creator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
var async = require('async');

exports.up = function(db, callback) {
async.series([
db.addColumn.bind(db, "stats","creatorId",{type: 'int'})
], callback);
};

exports.down = function(db, callback) {
async.series([
db.removeColumn.bind(db, "stats","creatorId")
], callback);
};
10 changes: 10 additions & 0 deletions models/stat.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,16 @@ var Stat = bookshelf.Model.extend(
player: function(){
var Player = require("./player");
return this.belongsTo(Player,"playerId");
},
/**
* @function
*
* Informs Bookshelf.js that the `creator` property will be a [models/user]
* model with an `id` that matches the `creatorId` specified in the query.
**/
creator: function() {
var User = require("./user");
return this.belongsTo(User, "creatorId");
}
});

Expand Down
8 changes: 6 additions & 2 deletions public/components/game/details/details.js
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ export const ViewModel = DefineMap.extend('GameDetailsVM',
* ```
*/
showStatMenuFor: function(player, element, event){
if(!this.session || !this.session.isAdmin()) {
if(!this.session.user) {
Copy link
Author

Choose a reason for hiding this comment

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

restore check for session existence

return;
}
var youtubePlayer = this.youtubePlayer;
Expand All @@ -236,7 +236,8 @@ export const ViewModel = DefineMap.extend('GameDetailsVM',
time: time,
playerId: player.id,
gameId: this.game.id,
player: player
player: player,
creatorId: this.session.user.id
});
},
/**
Expand Down Expand Up @@ -549,6 +550,9 @@ export const ViewModel = DefineMap.extend('GameDetailsVM',
top: $(containers[index]).offset().top - first.top + (height/2)
};
},
createdBy: function(stat) {
return this.session && (this.session.isAdmin() || stat.creatorId === this.session.user.id);
},

connectedCallback(el) {
this.element = el;
Expand Down
2 changes: 1 addition & 1 deletion public/components/game/details/details.stache
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
<span on:click="scope.vm.gotoTimeMinus5(time, scope.event)"
class="stat-point stat-{{ type }}" style="left: {{ scope.vm.statPercent(time) }}%">
{{ type }}
{{# if(scope.vm.session.isAdmin()) }}
{{# if(scope.vm.createdBy(this)) }}
<span class="destroy-btn glyphicon glyphicon-trash"
on:click="scope.vm.deleteStat(this, scope.event)"></span>
{{/ if }}
Expand Down
18 changes: 18 additions & 0 deletions public/models/stat.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import { DefineMap, DefineList, QueryLogic, superModel } from "can";
import bookshelfService from "./bookshelf-service";
import Player from "bitballs/models/player";
import User from "bitballs/models/user";

var Stat = DefineMap.extend('Stat',
{
Expand Down Expand Up @@ -96,6 +97,23 @@ var Stat = DefineMap.extend('Stat',
return Math.round(newVal);
}
},
/**
* @property {bitballs/models/user} bitballs/models/stat.properties.creator creator
* @parent bitballs/models/user.properties
*
* User related to the stats
*/
creator: {
Type: User,
serialize: false
},
/**
* @property {Number} bitballs/models/stat.properties.creatorId creatorId
* @parent bitballs/models/stat.properties
*
* The user that created the stat.
*/
creatorId: 'number',

default: 'any'
});
Expand Down
15 changes: 15 additions & 0 deletions services/loggedIn.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module.exports = function( respObj, status ) {
if ( typeof respObj === "string" ) {
respObj = {
message: respObj
};
}

return function ( req, res, next ) {
if ( req.user ) {
Copy link
Author

Choose a reason for hiding this comment

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

only for verified user's

next();
} else {
res.status( status || 404 ).end();
}
}
}
24 changes: 19 additions & 5 deletions services/stats.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@
var app = require("./app");
var Stat = require("../models/stat");
var adminOnly = require( "./adminOnly" );
var loggedIn = require( "./loggedIn" );
var separateQuery = require("./separate-query");

app.get('/services/stats', function(req, res){
Expand All @@ -124,13 +125,26 @@ app.put('/services/stats/:id', adminOnly( "Must be an admin to update stats" ),
});
});

app.delete('/services/stats/:id', adminOnly( "Must be an admin to delete stats" ), function(req, res){
new Stat({id: req.params.id}).destroy().then(function(stat){
res.send({});
app.delete('/services/stats/:id', function(req, res){
new Stat({id: req.params.id})
.fetch()
.then(stat => {
return Promise.all([stat, stat.get("creatorId")]);
})
.then(([stat, creatorId]) => {
debugger;
Copy link
Author

Choose a reason for hiding this comment

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

remove

if ( req.isAdmin || req.user.attributes.id === creatorId ) {
stat.destroy()
.then(() => {
res.send({});
});
} else {
res.status( status || 404 ).end();
}
});
});
});

app.post('/services/stats', adminOnly( "Must be an admin to create stats" ), function(req, res) {
app.post('/services/stats', loggedIn( "Must be logged in to create stats" ), function(req, res) {
new Stat(clean(req.body)).save().then(function(stat){
res.send({id: stat.get('id')});
}, function(e){
Expand Down