Skip to content

Commit

Permalink
Add trading feature and to-watch or to-play position
Browse files Browse the repository at this point in the history
* First commit: add back-end

* some corrections

* trading history screen ok

* deletion ok

* trading history done

* ordering to do and to watch is done

* done

* add new SQL dump
  • Loading branch information
ecourtial authored Sep 12, 2021
1 parent b3d381b commit f15984b
Show file tree
Hide file tree
Showing 23 changed files with 521 additions and 27 deletions.
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,28 +35,42 @@ todo list, watch list, ranking...

## Changelog

## 3.5.0

* Platform is now visible in the history screen.
* Added a trading feature: you can list the games you bought or sell.
* Added a position (rank) in the "to play" and "to watch" sections.
* Restructuration of the menu

### 3.4.0

* Added watched and played badges in the history section.

### 3.3.0

* Added a new feature: "Best Game Forever". Different from the hall of fames. BGF games are still valuable today, while some entries in the HOF were valuable only at time.

### 3.2.0

* Added a blacktheme (the white one is no longer applied).
* Added a "Has the box" badge when we have the original box (not a re-edition).
* Fixed the "To do with help" badge that was not displayed in the header of a game details page.
* Resized the textarea for the comment section when editing a game.

### 3.1.0

* Added a "todo with help" icon in the game list if the game is to be completed with some help.
* The game id is now visible in the game lists.
* Added a History menu to log the games we finish (watched or played).

### 3.0.1

* Fixed a bug when adding a new game while edition was still working.

## Todo

Yet the project is fully working, there are some features or elements to improve:

* Internationalization.

* Add a logical check if the user already exists when creating an account.
Expand Down
8 changes: 8 additions & 0 deletions V4_FEATURES.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Estimated list of changes in the V4

* Front app is no longer included.
* To watch serious and to watch background are merged.
* True REST APIs:
* endpoint are reflecting resources and no longer screens ;
* using HTTP verbs instead of different endpoints for each action.
* Games are no longer duplicated (one entry per platform). We link them to the platforms instead.
28 changes: 25 additions & 3 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from src.controller.games import GameController
from src.controller.user import UserController
from src.controller.history import HistoryController
from src.controller.trading import TradeController
from src.repository.user_repository import UserRepository
from src.connection.mysql_factory import MySQLFactory

Expand Down Expand Up @@ -182,9 +183,30 @@ def add_history():
controller = HistoryController
return controller.add(MySQLFactory.get())

@app.route('/history/delete/<int:game_id>', methods=['DELETE'])
@app.route('/history/delete/<int:entity_id>', methods=['DELETE'])
@login_required
def delete_history(game_id):
def delete_history(entity_id):
"""History deletion"""
controller = HistoryController
return controller.delete(MySQLFactory.get(), game_id)
return controller.delete(MySQLFactory.get(), entity_id)

# Trading management
@app.route('/trading/history', methods=['GET'])
def trading_history():
"""Trading history"""
controller = TradeController()
return controller.get_list(MySQLFactory.get())

@app.route('/trading/add', methods=['GET', 'POST'])
@login_required
def add_trade():
"""Adding a new trading entry"""
controller = TradeController
return controller.add(MySQLFactory.get())

@app.route('/trading/delete/<int:entity_id>', methods=['DELETE'])
@login_required
def delete_trade(entity_id):
"""Trading deletion"""
controller = TradeController
return controller.delete(MySQLFactory.get(), entity_id)
25 changes: 24 additions & 1 deletion games_empty.sql
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#
# Hôte: 0.0.0.0 (MySQL 5.7.29)
# Base de données: games
# Temps de génération: 2021-08-05 12:36:24 +0000
# Temps de génération: 2021-09-12 14:43:35 +0000
# ************************************************************


Expand Down Expand Up @@ -66,6 +66,8 @@ CREATE TABLE `games_meta` (
`todo_with_help` tinyint(3) unsigned NOT NULL DEFAULT '0',
`has_box` tinyint(3) unsigned NOT NULL DEFAULT '0',
`bgf` tinyint(3) unsigned NOT NULL DEFAULT '0',
`to_watch_position` tinyint(3) unsigned NOT NULL DEFAULT '0',
`to_do_position` tinyint(3) unsigned NOT NULL DEFAULT '0',
PRIMARY KEY (`game_id`),
CONSTRAINT `games_meta_ibfk_1` FOREIGN KEY (`game_id`) REFERENCES `games` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
Expand All @@ -82,6 +84,8 @@ CREATE TABLE `history` (
`game_id` smallint(11) unsigned NOT NULL,
`year` smallint(6) unsigned NOT NULL,
`position` smallint(6) unsigned NOT NULL,
`watched` tinyint(3) unsigned NOT NULL DEFAULT '0',
`played` tinyint(3) unsigned NOT NULL DEFAULT '0',
PRIMARY KEY (`id`),
KEY `game_id` (`game_id`),
CONSTRAINT `history_ibfk_1` FOREIGN KEY (`game_id`) REFERENCES `games` (`id`)
Expand All @@ -103,6 +107,25 @@ CREATE TABLE `platforms` (



# Affichage de la table trades
# ------------------------------------------------------------

DROP TABLE IF EXISTS `trades`;

CREATE TABLE `trades` (
`id` int(4) unsigned NOT NULL AUTO_INCREMENT,
`game_id` smallint(5) unsigned NOT NULL,
`year` smallint(1) unsigned NOT NULL,
`month` smallint(2) unsigned NOT NULL,
`day` smallint(2) unsigned NOT NULL,
`type` tinyint(1) unsigned NOT NULL,
PRIMARY KEY (`id`),
KEY `game_id` (`game_id`),
CONSTRAINT `trades_ibfk_1` FOREIGN KEY (`game_id`) REFERENCES `games` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;



# Affichage de la table users
# ------------------------------------------------------------

Expand Down
53 changes: 53 additions & 0 deletions src/controller/trading.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
""" Trading controller for the GMG project """
from flask import jsonify, request, render_template, session
from src.repository.trade_repository import TradeRepository

class TradeController:
""" Trading controller for the GMG project """
@classmethod
def get_list(cls, mysql):
"""Return the whole trading history."""
trade_repo = TradeRepository(mysql)
games_list = trade_repo.get_all()
return jsonify(
games=[game.serialize() for game in games_list]
)

@classmethod
def add(cls, mysql):
"""Add a new trade."""
if request.method == 'GET':
form = render_template(
'general/trading-history-form.html',
token=session['csrfToken']
)

return jsonify(form=form, title="Ajouter une transaction")

if request.form['_token'] != session['csrfToken']:
return jsonify(), 400

game_id = request.form['game_id']
year = request.form['year']
month = request.form['month']
day = request.form['day']
operation = request.form['type']

if game_id == '' or year == '' or month == '' or day == '' or operation == '':
return "Form is incomplete"

trade_repo = TradeRepository(mysql)
trade_repo.insert(game_id, year, month, day, operation)

return jsonify(), 200

@classmethod
def delete(cls, mysql, entry_id):
"""Delete a trading entry."""
if request.form['_token'] != session['csrfToken']:
return jsonify(), 400

history_repo = TradeRepository(mysql)
history_repo.delete(entry_id)

return jsonify(), 204
8 changes: 8 additions & 0 deletions src/entity/history.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ def __init__(
entity_id,
game_id,
title,
platform,
year,
position,
watched,
Expand All @@ -16,6 +17,7 @@ def __init__(
self.entity_id = entity_id
self.game_id = game_id
self.title = title
self.platform = platform
self.year = year
self.position = position
self.watched = watched
Expand All @@ -36,6 +38,11 @@ def get_title(self):

return self.title

def get_platform(self):
"""Platform?"""

return self.platform

def get_year(self):
"""Return the year."""

Expand Down Expand Up @@ -66,6 +73,7 @@ def serialize(self):
'id': self.entity_id,
'game_id': self.game_id,
'title': self.title,
'platform': self.platform,
'year': self.year,
'position': self.position,
'watched': self.watched,
Expand Down
81 changes: 81 additions & 0 deletions src/entity/trade.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
""" Trading entity for the GMG project """
import json

class Trade:
""" This class represent a trading entry, for instance I sell or bought a game """
def __init__(
self,
entity_id,
game_id,
title,
platform,
year,
month,
day,
operation
):
self.entity_id = entity_id
self.game_id = game_id
self.title = title
self.platform = platform
self.year = year
self.month = month
self.day = day
self.type = operation

def get_id(self):
"""Return the id entry."""

return self.entity_id

def get_game_id(self):
"""Return the id of the game, for instance "125"."""

return self.game_id

def get_title(self):
"""Return the title of the game, for instance "Fifa 98"."""

return self.title

def get_platform(self):
"""Platform?"""

return self.platform

def get_year(self):
"""Return the year"""

return self.year

def get_month(self):
"""Return the month."""

return self.month

def get_day(self):
"""Return the day."""

return self.day

def get_type(self):
"""0: sold, 1: bought"""

return self.type

def to_json(self):
"""Jsonify the object"""
return json.dumps(self, default=lambda o: o.__dict__)

def serialize(self):
"""serialize the object"""
return {
'id': self.entity_id,
'game_id': self.game_id,
'title': self.title,
'platform': self.platform,
'year': self.year,
'month': self.month,
'day': self.day,
'type': self.type
}
14 changes: 10 additions & 4 deletions src/repository/game_repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ class GameRepository(AbstractRepository):
'to_buy',
'original',
'ongoing',
'bgf'
'bgf',
'to_watch_position',
'to_do_position'
]

random_cases = [
Expand Down Expand Up @@ -153,9 +155,9 @@ def insert(self, title, platform, form_content):
request += "many, top_game,"
request += "hall_of_fame, hall_of_fame_year,"
request += "hall_of_fame_position, played_it_often, ongoing, comments, todo_with_help"
request += ", has_box, bgf) "
request += ", has_box, bgf, to_watch_position, to_do_position) "
request += "VALUES (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s, %s, %s, %s"
request += ", %s)"
request += ", %s, %s, %s)"

self.write(request, meta)

Expand Down Expand Up @@ -189,7 +191,9 @@ def update(self, game_id, title, platform, form_content):
request += "comments=%s, "
request += "todo_with_help=%s, "
request += "has_box=%s, "
request += "bgf=%s "
request += "bgf=%s, "
request += "to_watch_position=%s, "
request += "to_do_position=%s "
request += "WHERE game_id = %s"

self.write(request, meta)
Expand Down Expand Up @@ -222,6 +226,8 @@ def get_meta_form_request(cls, game_id, form_content, operation):
form_content['todo_with_help'],
form_content['has_box'],
form_content['bgf'],
form_content['to_watch_position'],
form_content['to_do_position'],
)

if operation == 'INSERT':
Expand Down
6 changes: 4 additions & 2 deletions src/repository/history_repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ def get_all(self):
"""Gets all the history."""
request = "SELECT history.id as id, history.game_id as game_id, history.year as year"
request += ", history.position as position, games.title as title, history.watched"
request += ", history.played FROM history, games"
request += " WHERE history.game_id = games.id ORDER BY year, position"
request += ", platforms.name as platform, history.played FROM history, games, platforms"
request += " WHERE history.game_id = games.id AND games.platform = platforms.id"
request += " ORDER BY year, position"

return self.fetch_multiple(request, ())

Expand All @@ -33,6 +34,7 @@ def hydrate(cls, row):
row['id'],
row['game_id'],
row['title'],
row['platform'],
row['year'],
row['position'],
row['watched'],
Expand Down
Loading

0 comments on commit f15984b

Please sign in to comment.