Skip to content

Commit

Permalink
feat: add brain subscriber count
Browse files Browse the repository at this point in the history
  • Loading branch information
mamadoudicko committed Sep 21, 2023
1 parent 21ed41c commit 0241a9b
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 7 deletions.
1 change: 1 addition & 0 deletions backend/models/brain_entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,4 @@ class PublicBrain(BaseModel):
id: UUID
name: str
description: Optional[str]
number_of_subscribers: int = 0
25 changes: 19 additions & 6 deletions backend/models/databases/supabase/brains.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,13 +84,13 @@ def get_public_brains(self) -> list[PublicBrain]:
)
public_brains: list[PublicBrain] = []
for item in response.data:
public_brains.append(
PublicBrain(
id=item["id"],
name=item["name"],
description=item["description"],
)
brain = PublicBrain(
id=item["id"],
name=item["name"],
description=item["description"],
)
brain.number_of_subscribers = self.get_brain_subscribers_count(brain.id)
public_brains.append(brain)
return public_brains

def get_brain_for_user(self, user_id, brain_id) -> MinimalBrainEntity | None:
Expand Down Expand Up @@ -292,3 +292,16 @@ def get_brain_by_id(self, brain_id: UUID) -> BrainEntity | None:
return None

return BrainEntity(**response[0])

def get_brain_subscribers_count(self, brain_id: UUID) -> int:
response = (
self.db.from_("brains_users")
.select(
"count",
)
.filter("brain_id", "eq", str(brain_id))
.execute()
).data
if len(response) == 0:
raise ValueError(f"Brain with id {brain_id} does not exist.")
return response[0]["count"]
2 changes: 1 addition & 1 deletion backend/repository/brain/create_brain.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from models.databases.supabase.brains import CreateBrainProperties
from models import BrainEntity, get_supabase_db
from models.databases.supabase.brains import CreateBrainProperties


def create_brain(brain: CreateBrainProperties) -> BrainEntity:
Expand Down
1 change: 1 addition & 0 deletions frontend/lib/context/BrainProvider/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export type PublicBrain = {
id: UUID;
name: string;
description?: string;
number_of_subscribers: number;
};

export type BrainContextType = ReturnType<typeof useBrainProvider>;

0 comments on commit 0241a9b

Please sign in to comment.