Skip to content
This repository has been archived by the owner on Dec 11, 2019. It is now read-only.

Commit

Permalink
Modeling relationship between games, players and achievements (Issue #6)
Browse files Browse the repository at this point in the history
  • Loading branch information
oxisto committed Apr 10, 2018
1 parent 0e44272 commit 9330b79
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 30 deletions.
1 change: 1 addition & 0 deletions api/common/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
__all__ = ['model']
23 changes: 0 additions & 23 deletions api/common/achievement.py

This file was deleted.

37 changes: 37 additions & 0 deletions api/common/model.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
from py2neo.ogm import Property, GraphObject, RelatedTo, RelatedFrom


class BaseObject(GraphObject):
def to_dict(self):
return self.__ogm__.node


class Game(BaseObject):
__primarykey__ = "id"

id = Property()
name = Property()

achievements = RelatedTo("Achievement", "hasAchievement")
owned_by = RelatedFrom("Player", "ownedBy")


class Achievement(BaseObject):
__primarykey__ = "id"

id = Property()
name = Property()
description = Property()
image_url = Property(key="imageUrl")
source = Property()

achieved_in = RelatedFrom("Game", "achievedIn")


class Player(BaseObject):
# TODO: use a global unique identifier
__primarykey__ = "name"

name = Property()

games = RelatedTo("Game", "owns")
34 changes: 27 additions & 7 deletions api/main.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#!/usr/bin/env python

from flask import Flask, jsonify
from flask import Flask, jsonify, make_response
from py2neo import Graph, Database

from common.achievement import Achievement, Game
from common.model import Achievement, Game, Player

import unlocked
import data
Expand All @@ -28,17 +28,22 @@ def init_game_data():
graph.push(a)

g = Game()
g.id = "4"
g.name = "Some game"
g.achievements.add(a, {
"platform": "Steam"
})
g.achievements.add(a)

graph.push(g)

p = Player()
p.name = "ipec"
p.games.add(g)

graph.push(p)


@app.route("/achievements")
def get_achievements() -> str:
return jsonify([a.serialize() for a in Achievement.select(graph)])
return jsonify([achievement.serialize() for achievement in Achievement.select(graph)])


@app.route("/achievements/unlocked")
Expand Down Expand Up @@ -67,7 +72,22 @@ def test() -> str:

@app.route("/games")
def get_games() -> str:
return jsonify({1: "quake", 2: "DotA"})
return jsonify([game.to_dict() for game in Game.select(graph)])


@app.route("/games/<id>/achievements")
def get_achievement_for_game(id) -> str:
game: Game = Game.select(graph, id).first()

if game is not None:
return jsonify([achievement.to_dict() for achievement in game.achievements])
else:
return make_response('Game not found', 404)


@app.route("/players")
def get_players() -> str:
return jsonify([player.to_dict() for player in Player.select(graph)])


if __name__ == "__main__":
Expand Down

0 comments on commit 9330b79

Please sign in to comment.