Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
F
fast-food
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Мырзабеков Бекайдар
fast-food
Commits
b36c3b33
Commit
b36c3b33
authored
Nov 28, 2023
by
Мырзабеков Бекайдар
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
added get products
parent
7e14fdc7
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
36 additions
and
5 deletions
+36
-5
ctrl.py
src/app/api/product/ctrl.py
+3
-0
routes.py
src/app/api/product/routes.py
+6
-0
schemas.py
src/app/api/product/schemas.py
+1
-5
services.py
src/app/api/product/services.py
+9
-0
product.py
src/app/db/repositories/product.py
+17
-0
No files found.
src/app/api/product/ctrl.py
View file @
b36c3b33
...
...
@@ -16,3 +16,6 @@ class ProductController(BaseController):
except
ValidationError
as
e
:
raise
http_exc
.
HTTPBadRequestException
(
detail
=
f
"Validation error: {str(e)}"
)
async
def
get_list
(
self
,
**
kwargs
):
return
await
self
.
product_service
.
get_product_list
(
**
kwargs
)
src/app/api/product/routes.py
View file @
b36c3b33
import
fastapi
as
fa
from
.ctrl
import
ProductController
from
.schemas
import
ProductGetSchema
router
=
fa
.
APIRouter
(
prefix
=
'/products'
,
tags
=
[
'Product'
])
...
...
@@ -15,3 +16,8 @@ async def create_product(
price
:
float
=
fa
.
Form
(
...
)
):
return
await
ctrl
.
create
(
name
=
name
,
description
=
description
,
price
=
price
,
image
=
image
)
@
router
.
get
(
''
)
async
def
get_products
(
page
:
int
=
1
,
size
:
int
=
10
,
params
:
ProductGetSchema
=
fa
.
Depends
()):
return
await
ctrl
.
get_list
(
page
=
page
,
size
=
size
,
**
params
.
model_dump
(
exclude_none
=
True
))
src/app/api/product/schemas.py
View file @
b36c3b33
...
...
@@ -8,14 +8,10 @@ class ProductBaseSchema(BaseModel):
class
ProductIdSchema
(
BaseModel
):
id
:
UUID
id
:
UUID
|
None
=
None
model_config
=
ConfigDict
(
from_attributes
=
True
)
class
ProductListSchema
(
ProductBaseSchema
,
ProductIdSchema
):
pass
class
ProductGetSchema
(
ProductBaseSchema
,
ProductIdSchema
):
description
:
str
|
None
=
None
price
:
float
|
None
=
None
...
...
src/app/api/product/services.py
View file @
b36c3b33
...
...
@@ -20,3 +20,12 @@ class ProductService:
)
return
ProductGetSchema
.
model_validate
(
product
)
async
def
get_product_list
(
self
,
**
kwargs
):
products
=
await
self
.
product_repository
.
get_list
(
**
kwargs
)
return
{
"page"
:
products
[
'page'
],
"size"
:
products
[
'size'
],
"total"
:
products
[
'total'
],
"result"
:
[
ProductGetSchema
.
model_validate
(
product
)
for
product
in
products
[
'result'
]]
}
src/app/db/repositories/product.py
View file @
b36c3b33
import
math
from
.base
import
BaseRepository
from
..models
import
Product
class
ProductRepository
(
BaseRepository
):
model
=
Product
async
def
get_list
(
self
,
page
,
size
,
**
kwargs
):
query
=
self
.
model
.
all
()
offset_min
=
(
page
-
1
)
*
size
offset_max
=
page
*
size
data
=
await
query
result
=
{
"page"
:
page
,
"size"
:
size
,
"total"
:
math
.
ceil
(
len
(
data
)
/
size
)
-
1
,
"result"
:
data
[
offset_min
:
offset_max
]
}
return
result
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment