Commit 8ddae785 authored by Caleb Connolly's avatar Caleb Connolly

Yay for DI

parent 6386565f
......@@ -10,7 +10,7 @@ def authenticate(f):
}
else:
key = request.args['key']
print("Key was:", apikeys[[k['key'] for k in apikeys].index(key)]['description'])
print(f)
#print("Key was:", apikeys[[k['key'] for k in apikeys].index(key)]['description'])
#print(f)
return f()
return wrapper
\ No newline at end of file
No preview for this file type
import sqlite3
from models import *
from models import House, User
# Database abstraction layer, simplifies db lookups
class HouseDbContext():
......@@ -21,4 +21,13 @@ class HouseDbContext():
def update_housepoints(self, house: House) -> bool:
self.cursor.execute("UPDATE Houses SET HousePoints=? WHERE HouseId=?", (house.housePoints, house.houseId))
self.conn.commit()
\ No newline at end of file
self.conn.commit()
def get_user(self, userId: int) -> User:
return User(self.cursor.execute("SELECT * FROM Users WHERE UserId={}".format(userId)).fetchall()[0])
def set_discordId(self, userId: int, discordId: int) -> bool:
ret = self.cursor.execute("UPDATE Users SET DiscordId=? WHERE UserId=?", (discordId, userId))
self.conn.commit()
return ret
\ No newline at end of file
......@@ -28,9 +28,17 @@ class House():
return self.__dict__
class User():
def __init__(self, userId: int, discordId: int, houseId: int, memberRolesId: int):
def __init__(self, userId: int, discordId: int, houseId: int, memberRolesId: int, housePoints: int):
self.userId = userId
self.discordId = discordId
self.houseId = houseId
self.memberRolesId = memberRolesId
self.housePoints = housePoints
def __init__(self, dbRow: tuple):
self.userId = int(dbRow[0])
self.discordId = int(dbRow[1] if dbRow[1] is not None else 0)
self.houseId = int(dbRow[2] if dbRow[2] is not None else 0)
self.memberRolesId = int(dbRow[3] if dbRow[3] is not None else 0)
self.housePoints = int(dbRow[4] if dbRow[4] is not None else 0)
\ No newline at end of file
from flask_restful import Resource
from flask_restful import Resource, reqparse
from flask import request
from injector import Module
from flask_injector import FlaskInjector
from injector import inject
from database import HouseDbContext
......@@ -13,21 +13,30 @@ from models import *
# @singleton
# def provide_ext(self, app: Flask) -> HouseDbContext:
# return HouseDbContext("data/data.db")
class GivePoints(Resource):
method_decorators = [authenticate]
@inject
def __init__(self, *args, dbContext: HouseDbContext, **kwargs):
self._ctx = dbContext
self._ctx.connect()
super().__init__(*args, **kwargs)
def post(self):
if not 'user' in request.form:
return {
"message": "Must specify a user to receive points"
}
userid = request.form['user']
user = User.getUserByStuId(userid)
user = self._ctx.get_user(userid)
return user.__dict__
class GetPoints(Resource):
# def __init__(self, db: HouseDbContext):
# print("Called!!!!!!!!")
# self.db = db
@inject
def __init__(self, *args, dbContext: HouseDbContext, **kwargs):
self._ctx = dbContext
self._ctx.connect()
super().__init__(*args, **kwargs)
def get(self):
print(request.args)
......@@ -35,6 +44,22 @@ class GetPoints(Resource):
return {
"message": "Must specify a house or user to get points"
}
return {
"message": "WIP"
}
if 'user' in request.args:
user = self._ctx.get_user(request.args['user'])
return {
"userId": request.args['user'],
"discordId": user.discordId,
"housePoints": user.housePoints
}
return request.args
class UpdateUser(Resource):
@inject
def __init__(self, *args, dbContext: HouseDbContext, **kwargs):
self._ctx = dbContext
self._ctx.connect()
super().__init__(*args, **kwargs)
def post(self):
if 'discordId' in request.form:
self._ctx.set_discordId(request.form['user'], request.form['discordId'])
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment