Skip to content

Commit

Permalink
feat: build client
Browse files Browse the repository at this point in the history
  • Loading branch information
celaraze committed Jun 19, 2024
1 parent 873d3b3 commit 6b67e71
Show file tree
Hide file tree
Showing 20 changed files with 363 additions and 125 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ Start back-end service with checking `docker-compose.yml` and run the following:

`docker-compose up -d`

And then you need to `GET` [http://localhost:8000/auth/init](http://localhost:8000/auth/init) to initialize the admin.

Now you can visit [http://localhost:8000/docs](http://localhost:8000/docs) to view the API documentation.

Tips: The image cannot be used now, because it still needs to be developed.
Expand Down
2 changes: 1 addition & 1 deletion app/config/env.yml.example
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ jwt:
# please replace it with a secure secret
secret: "153cd8047a05b55021980850bda0c564b9469f4aef5cff59b4256521e979a6d4"
algorithm: "HS256"
ttl_minutes: 30
ttl_minutes: 525600
2 changes: 1 addition & 1 deletion app/controllers/auth_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ async def init(
)
raise HTTPException(
status_code=status.HTTP_200_OK,
detail="Cela inited successfully, please login with username 'admin' and password 'admin'.",
detail="Cela inited successfully, please login with username 'admin' and password 'admin' by default.",
)


Expand Down
11 changes: 6 additions & 5 deletions app/controllers/brand_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@


# Get all brands.
@router.get("/")
@router.get("/", response_model=list[schemas.Brand])
async def get_brands(
db: databaseSession,
skip: int = 0,
Expand All @@ -30,7 +30,7 @@ async def get_brands(


# Get brand by id.
@router.get("/{brand_id}")
@router.get("/{brand_id}", response_model=schemas.Brand)
async def get_brand(
db: databaseSession,
brand_id: int,
Expand All @@ -42,11 +42,12 @@ async def get_brand(
status_code=status.HTTP_404_NOT_FOUND,
detail="Brand not exists",
)
brand.creator = crud.select_id(db, tables.User, brand.creator_id)
return brand


# Create brand.
@router.post("/")
@router.post("/", response_model=schemas.Brand)
async def create_brand(
db: databaseSession,
form_data: schemas.BrandCreateForm,
Expand All @@ -58,7 +59,7 @@ async def create_brand(


# Update brand.
@router.put("/{brand_id}")
@router.put("/{brand_id}", response_model=schemas.Brand)
async def update_brand(
db: databaseSession,
brand_id: int,
Expand All @@ -76,7 +77,7 @@ async def update_brand(


# Delete brand.
@router.delete("/{brand_id}")
@router.delete("/{brand_id}", response_model=schemas.Brand)
async def delete_brand(
db: databaseSession,
brand_id: int,
Expand Down
11 changes: 6 additions & 5 deletions app/controllers/device_category_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
# APIs for device category.

# Get all device categories.
@router.get("/")
@router.get("/", response_model=list[schemas.DeviceCategory])
async def get_device_categories(
db: databaseSession,
skip: int = 0,
Expand All @@ -29,7 +29,7 @@ async def get_device_categories(


# Get device category by id.
@router.get("/{device_category_id}")
@router.get("/{device_category_id}", response_model=schemas.DeviceCategory)
async def get_device_category(
db: databaseSession,
device_category_id: int,
Expand All @@ -41,11 +41,12 @@ async def get_device_category(
status_code=status.HTTP_404_NOT_FOUND,
detail="Device Category not exists",
)
device_category.creator = crud.select_id(db, tables.User, device_category.creator_id)
return device_category


# Create device category.
@router.post("/")
@router.post("/", response_model=schemas.DeviceCategory)
async def create_device_category(
db: databaseSession,
form_data: schemas.DeviceCategoryCreateForm,
Expand All @@ -57,7 +58,7 @@ async def create_device_category(


# Update device category.
@router.put("/{device_category_id}")
@router.put("/{device_category_id}", response_model=schemas.DeviceCategory)
async def update_device_category(
db: databaseSession,
device_category_id: int,
Expand All @@ -75,7 +76,7 @@ async def update_device_category(


# Delete device category.
@router.delete("/{device_category_id}")
@router.delete("/{device_category_id}", response_model=schemas.DeviceCategory)
async def delete_device_category(
db: databaseSession,
device_category_id: int,
Expand Down
17 changes: 9 additions & 8 deletions app/controllers/device_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
# APIs for device.

# Get all devices.
@router.get("/")
@router.get("/", response_model=list[schemas.Device])
async def get_devices(
db: databaseSession,
skip: int = 0,
Expand All @@ -27,12 +27,12 @@ async def get_devices(
):
devices = crud.select_all(db, tables.Device, skip=skip, limit=limit)
if asset_number:
devices = crud.select_asset_number(db, tables.Device, asset_number)
devices = [crud.select_asset_number(db, tables.Device, asset_number)]
return devices


# Get device by id.
@router.get("/{device_id}")
@router.get("/{device_id}", response_model=schemas.Device)
async def get_device(
db: databaseSession,
device_id: int,
Expand All @@ -44,13 +44,14 @@ async def get_device(
status_code=status.HTTP_404_NOT_FOUND,
detail="Device not exists",
)
device.creator = crud.select_id(db, tables.User, device.creator_id)
user = get_user(db, device_id)
device.user = user
return device


# Create device.
@router.post("/")
@router.post("/", response_model=schemas.Device)
async def create_device(
db: databaseSession,
form_data: schemas.DeviceCreateForm,
Expand Down Expand Up @@ -80,7 +81,7 @@ async def create_device(


# Update device.
@router.put("/{device_id}")
@router.put("/{device_id}", response_model=schemas.Device)
async def update_device(
db: databaseSession,
device_id: int,
Expand Down Expand Up @@ -120,7 +121,7 @@ async def update_device(


# Delete device.
@router.delete("/{device_id}")
@router.delete("/{device_id}", response_model=schemas.Device)
async def delete_device(
db: databaseSession,
device_id: int,
Expand All @@ -138,5 +139,5 @@ async def delete_device(
status_code=status.HTTP_409_CONFLICT,
detail="Device has users, please update them first.",
)
db_brand = crud.delete(db, tables.Device, device_id)
return db_brand
db_device = crud.delete(db, tables.Device, device_id)
return db_device
16 changes: 11 additions & 5 deletions app/controllers/role_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@


# Get all roles.
@router.get("/")
@router.get("/", response_model=list[schemas.Role])
async def get_roles(
db: databaseSession,
skip: int = 0,
Expand All @@ -30,7 +30,7 @@ async def get_roles(


# Get role by id.
@router.get("/{role_id}")
@router.get("/{role_id}", response_model=schemas.Role)
async def get_role(
db: databaseSession,
role_id: int,
Expand All @@ -42,11 +42,12 @@ async def get_role(
status_code=status.HTTP_404_NOT_FOUND,
detail="Role not exists",
)
role.creator = crud.select_id(db, tables.User, role.creator_id)
return role


# Create role.
@router.post("/")
@router.post("/", response_model=schemas.Role)
async def create_role(
db: databaseSession,
form_data: schemas.RoleCreateForm,
Expand All @@ -63,7 +64,7 @@ async def create_role(


# Update role.
@router.put("/{role_id}")
@router.put("/{role_id}", response_model=schemas.Role)
async def update_role(
db: databaseSession,
role_id: int,
Expand All @@ -76,12 +77,17 @@ async def update_role(
status_code=status.HTTP_404_NOT_FOUND,
detail="Role not exists",
)
if db_role.name == "superuser":
raise HTTPException(
status_code=status.HTTP_409_CONFLICT,
detail="Role name 'superuser' is reserved.",
)
db_role = crud.update(db, tables.Role, db_role.id, form_data)
return db_role


# Delete role.
@router.delete("/{role_id}")
@router.delete("/{role_id}", response_model=schemas.Role)
async def delete_role(
db: databaseSession,
role_id: int,
Expand Down
Loading

0 comments on commit 6b67e71

Please sign in to comment.