Skip to content

Commit

Permalink
update for feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
Lendemor committed Oct 11, 2023
1 parent 1fbdec4 commit 750b2eb
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 16 deletions.
2 changes: 0 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
**/node_modules/**
**/package.json
**/package-lock.json
**/alembic
**/alembic.ini
bun.lockb
poetry.lock
frontend.zip
Expand Down
1 change: 1 addition & 0 deletions basic_crud/basic_crud/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"""Basic CRUD example main package."""
19 changes: 15 additions & 4 deletions basic_crud/basic_crud/api.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,34 @@
from datetime import datetime
"""API methods to handle products."""
import json
from datetime import datetime, timezone

from fastapi import APIRouter, Request
from fastapi.exceptions import HTTPException

import reflex as rx

from .model import Product


async def get_product(spec_id: int):
"""Get the product associated with spec_id."""
with rx.session() as session:
spec = session.query(Product).get(spec_id)
return spec if spec else HTTPException(status_code=404)


async def list_product(req: Request):
"""Get a list of all the products."""
with rx.session() as session:
specs = session.query(Product).all()
return specs


async def add_product(req: Request):
"""Add a new product."""
data = json.loads(await req.body())
with rx.session() as session:
now = datetime.now()
now = datetime.now(timezone.utc)
code = data["code"]
if not code:
return HTTPException(status_code=402, detail="Invalid `code`")
Expand All @@ -43,17 +50,21 @@ async def add_product(req: Request):


async def update_product(spec_id: int, req: Request):
"""Update the product associated with spec_id."""
data = json.loads(await req.body())
with rx.session() as session:
spec = session.query(Product).get(spec_id)
for k, v in data.items():
spec.__setattr__(k, v)
spec.__setattr__("updated", datetime.now())
setattr(spec, k, v)
spec.updated = datetime.now(timezone.utc)
# spec.__setattr__(k, v)
# spec.__setattr__("updated", datetime.now())
session.add(spec)
session.commit()


async def delete_product(spec_id):
"""Delete the product associated with spec_id."""
with rx.session() as session:
spec = session.query(Product).get(spec_id)
session.delete(spec)
Expand Down
11 changes: 4 additions & 7 deletions basic_crud/basic_crud/basic_crud.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
"""Welcome to Reflex! This file outlines the steps to create a basic app."""
import asyncio
import json
from basic_crud.api import product_router
from basic_crud.model import Product
from rxconfig import config

import httpx

import reflex as rx

docs_url = "https://reflex.dev/docs/getting-started/introduction"
filename = f"{config.app_name}/{config.app_name}.py"

from .api import product_router
from .model import Product

DEFAULT_BODY = """{
"code":"",
Expand Down Expand Up @@ -121,7 +118,7 @@ def data_display():
def render_product(product: Product):
return rx.hstack(
rx.image(src=product.image, height="100%", width="3vw"),
rx.text("(", product.code, ") ", product.label, width="10vw"),
rx.text(f"({product.code}) {product.label}", width="10vw"),
rx.vstack(
rx.text("Stock:", product.quantity),
rx.text("Category:", product.category),
Expand Down
9 changes: 7 additions & 2 deletions basic_crud/basic_crud/model.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
"""Models used by the app."""
from datetime import datetime, timezone
from typing import Literal

from sqlmodel import Column, DateTime, Field, func

import reflex as rx
from sqlmodel import Field, Column, DateTime, func


class Product(rx.Model, table=True):
"""Product model."""

code: str = Field(unique=True)
created: datetime = Field(
datetime.now(timezone.utc),
Expand All @@ -24,6 +28,7 @@ class Product(rx.Model, table=True):
sender: str

def dict(self, *args, **kwargs) -> dict:
"""Serialize method."""
d = super().dict(*args, **kwargs)
d["created"] = self.created.replace(microsecond=0).isoformat()
d["updated"] = self.updated.replace(microsecond=0).isoformat()
Expand Down
2 changes: 1 addition & 1 deletion basic_crud/rxconfig.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@

config = rx.Config(
app_name="basic_crud",
)
)

0 comments on commit 750b2eb

Please sign in to comment.