Skip to content

Commit

Permalink
fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
dni committed Oct 24, 2024
1 parent e3a5e47 commit aa6f358
Show file tree
Hide file tree
Showing 9 changed files with 327 additions and 335 deletions.
36 changes: 19 additions & 17 deletions crud.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,16 @@ async def get_service_redirect_uri(request, service_id):


async def create_donation(
data: CreateDonation, amount: float, donation_id: Optional[str] = None
data: CreateDonation, wallet: str, amount: float, donation_id: Optional[str] = None
) -> Donation:
"""Create a new Donation"""
donation = Donation(
id=donation_id or urlsafe_short_hash(),
amount=amount,
wallet=wallet,
**data.dict(),
)
await db.insert("streamalerts.Donations", donation)
await db.insert("streamalerts.donations", donation)
return donation


Expand Down Expand Up @@ -62,7 +63,7 @@ async def post_donation(donation_id: str) -> dict:
else:
return {"message": "Unsopported servicename"}
await db.execute(
"UPDATE streamalerts.Donations SET posted = :posted WHERE id = :id",
"UPDATE streamalerts.donations SET posted = :posted WHERE id = :id",
{"id": donation_id, "posted": True},
)
return response.json()
Expand All @@ -72,9 +73,10 @@ async def create_service(data: CreateService) -> Service:
"""Create a new Service"""
service = Service(
id=urlsafe_short_hash(),
state=urlsafe_short_hash(),
**data.dict(),
)
await db.insert("streamalerts.Services", service)
await db.insert("streamalerts.services", service)
return service


Expand All @@ -90,13 +92,13 @@ async def get_service(
assert service_id or by_state, "Must provide either service_id or by_state"
if by_state:
return await db.fetchone(
"SELECT * FROM streamalerts.Services WHERE state = :state",
"SELECT * FROM streamalerts.services WHERE state = :state",
{"state": by_state},
Service,
)
else:
return await db.fetchone(
"SELECT * FROM streamalerts.Services WHERE id = :id",
"SELECT * FROM streamalerts.services WHERE id = :id",
{"id": service_id},
Service,
)
Expand All @@ -105,7 +107,7 @@ async def get_service(
async def get_services(wallet_id: str) -> list[Service]:
"""Return all services belonging assigned to the wallet_id"""
return await db.fetchall(
"SELECT * FROM streamalerts.Services WHERE wallet = :wallet",
"SELECT * FROM streamalerts.services WHERE wallet = :wallet",
{"wallet": wallet_id},
Service,
)
Expand Down Expand Up @@ -148,7 +150,7 @@ async def service_add_token(service_id, token):

await db.execute(
"""
UPDATE streamalerts.Services
UPDATE streamalerts.services
SET authenticated = 1, token = :token WHERE id = :id
""",
{"id": service_id, "token": token},
Expand All @@ -157,9 +159,9 @@ async def service_add_token(service_id, token):


async def delete_service(service_id: str) -> list[str]:
"""Delete a Service and all corresponding Donations"""
"""Delete a Service and all corresponding donations"""
donations = await db.fetchall(
"SELECT * FROM streamalerts.Donations WHERE service = :service",
"SELECT * FROM streamalerts.donations WHERE service = :service",
{"service": service_id},
Donation,
)
Expand All @@ -169,7 +171,7 @@ async def delete_service(service_id: str) -> list[str]:
await delete_donation(donation.id)

await db.execute(
"DELETE FROM streamalerts.Services WHERE id = :id", {"id": service_id}
"DELETE FROM streamalerts.services WHERE id = :id", {"id": service_id}
)

return donation_ids
Expand All @@ -178,16 +180,16 @@ async def delete_service(service_id: str) -> list[str]:
async def get_donation(donation_id: str) -> Optional[Donation]:
"""Return a Donation"""
return await db.fetchone(
"SELECT * FROM streamalerts.Donations WHERE id = :id",
"SELECT * FROM streamalerts.donations WHERE id = :id",
{"id": donation_id},
Donation,
)


async def get_donations(wallet_id: str) -> list[Donation]:
"""Return all streamalerts.Donations assigned to wallet_id"""
"""Return all streamalerts.donations assigned to wallet_id"""
return await db.fetchall(
"SELECT * FROM streamalerts.Donations WHERE wallet = :wallet",
"SELECT * FROM streamalerts.donations WHERE wallet = :wallet",
{"wallet": wallet_id},
Donation,
)
Expand All @@ -196,17 +198,17 @@ async def get_donations(wallet_id: str) -> list[Donation]:
async def delete_donation(donation_id: str) -> None:
"""Delete a Donation and its corresponding statspay charge"""
await db.execute(
"DELETE FROM streamalerts.Donations WHERE id = :id", {"id": donation_id}
"DELETE FROM streamalerts.donations WHERE id = :id", {"id": donation_id}
)


async def update_donation(donation: Donation) -> Donation:
"""Update a Donation"""
await db.update("streamalerts.Donations", donation)
await db.update("streamalerts.donations", donation)
return donation


async def update_service(service: Service) -> Service:
"""Update a service"""
await db.update("streamalerts.Services", service)
await db.update("streamalerts.services", service)
return service
13 changes: 6 additions & 7 deletions migrations.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
async def m001_initial(db):

await db.execute(
f"""
CREATE TABLE IF NOT EXISTS streamalerts.Services (
id {db.serial_primary_key},
"""
CREATE TABLE IF NOT EXISTS streamalerts.services (
id TEXT PRIMARY KEY,
state TEXT NOT NULL,
twitchuser TEXT NOT NULL,
client_id TEXT NOT NULL,
Expand All @@ -19,17 +19,16 @@ async def m001_initial(db):

await db.execute(
f"""
CREATE TABLE IF NOT EXISTS streamalerts.Donations (
CREATE TABLE IF NOT EXISTS streamalerts.donations (
id TEXT PRIMARY KEY,
wallet TEXT NOT NULL,
name TEXT NOT NULL,
message TEXT NOT NULL,
cur_code TEXT NOT NULL,
sats {db.big_int} NOT NULL,
amount FLOAT NOT NULL,
service INTEGER NOT NULL,
posted BOOLEAN NOT NULL,
FOREIGN KEY(service) REFERENCES {db.references_schema}Services(id)
service TEXT NOT NULL,
posted BOOLEAN NOT NULL
);
"""
)
8 changes: 4 additions & 4 deletions models.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class Donation(BaseModel):
sats: int
amount: float # The donation amount after fiat conversion
service: str # The ID of the corresponding Service
posted: bool # Whether the donation has already been posted to a Service
posted: bool = False # Whether the donation has already been posted to a Service


class Service(BaseModel):
Expand All @@ -53,10 +53,10 @@ class Service(BaseModel):
client_id: str # Third party service Client ID
client_secret: str # Secret corresponding to the Client ID
wallet: str
onchain: Optional[str]
servicename: str # Currently, this will just always be "Streamlabs"
authenticated: bool # Whether a token (see below) has been acquired yet
token: Optional[str] # The token with which to authenticate requests
authenticated: bool = False # Whether a token (see below) has been acquired yet
onchain: Optional[str] = None
token: Optional[str] = None # The token with which to authenticate requests


class ChargeStatus(BaseModel):
Expand Down
67 changes: 34 additions & 33 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit aa6f358

Please sign in to comment.