Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
T
task_treker
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
Мырзабеков Бекайдар
task_treker
Commits
e5691170
Commit
e5691170
authored
Nov 24, 2023
by
Мырзабеков Бекайдар
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
added CRUD category
parent
775478f1
Show whitespace changes
Inline
Side-by-side
Showing
9 changed files
with
170 additions
and
1 deletion
+170
-1
ctrl.py
src/app/api/category/ctrl.py
+51
-0
routes.py
src/app/api/category/routes.py
+35
-0
schemas.py
src/app/api/category/schemas.py
+17
-0
services.py
src/app/api/category/services.py
+27
-0
router.py
src/app/api/router.py
+2
-0
models.py
src/app/db/models.py
+4
-0
repositories.py
src/app/db/repositories.py
+5
-1
4_20231124061632_update.py
src/migrations/models/4_20231124061632_update.py
+16
-0
5_20231124061903_update.py
src/migrations/models/5_20231124061903_update.py
+13
-0
No files found.
src/app/api/category/ctrl.py
0 → 100644
View file @
e5691170
from
uuid
import
UUID
from
starlette
import
status
import
fastapi
from
tortoise.exceptions
import
ValidationError
from
base_ctrl
import
BaseController
from
exceptions
import
common
as
common_exc
,
http
as
http_exc
from
.services
import
CategoryService
class
CategoryController
(
BaseController
):
category_service
=
CategoryService
()
async
def
get_list
(
self
,
**
kwargs
):
return
await
self
.
category_service
.
get_category_list
(
**
kwargs
)
async
def
get
(
self
,
id
:
UUID
):
try
:
return
await
self
.
category_service
.
get_category
(
id
)
except
common_exc
.
NotFoundException
as
e
:
raise
http_exc
.
HTTPNotFoundException
(
detail
=
str
(
e
))
async
def
create
(
self
,
**
kwargs
):
try
:
return
await
self
.
category_service
.
create_category
(
**
kwargs
)
except
common_exc
.
CreateException
as
e
:
raise
http_exc
.
HTTPBadRequestException
(
detail
=
str
(
e
))
except
ValidationError
as
e
:
raise
http_exc
.
HTTPBadRequestException
(
detail
=
f
"Validation error: {str(e)}"
)
async
def
update
(
self
,
id
:
UUID
,
**
kwargs
):
try
:
return
await
self
.
category_service
.
update_category
(
id
,
**
kwargs
)
except
common_exc
.
UpdateException
as
e
:
raise
http_exc
.
HTTPBadRequestException
(
detail
=
str
(
e
))
async
def
delete
(
self
,
id
:
UUID
):
try
:
await
self
.
category_service
.
delete_category
(
id
)
return
fastapi
.
responses
.
Response
(
status_code
=
status
.
HTTP_204_NO_CONTENT
)
except
common_exc
.
DeleteException
as
e
:
raise
http_exc
.
HTTPBadRequestException
(
detail
=
str
(
e
))
except
common_exc
.
NotFoundException
as
e
:
raise
http_exc
.
HTTPNotFoundException
(
detail
=
str
(
e
))
src/app/api/category/routes.py
0 → 100644
View file @
e5691170
from
uuid
import
UUID
import
fastapi
from
.schemas
import
CategoryBaseSchema
from
.ctrl
import
CategoryController
router
=
fastapi
.
APIRouter
(
prefix
=
'/categories'
,
tags
=
[
'Category'
])
ctrl
=
CategoryController
()
@
router
.
get
(
''
)
async
def
get_categories
(
query
:
CategoryBaseSchema
=
fastapi
.
Depends
()):
return
await
ctrl
.
get_list
(
**
query
.
model_dump
(
exclude_none
=
True
))
@
router
.
get
(
'/{id}'
)
async
def
get_category
(
id
:
UUID
):
return
await
ctrl
.
get
(
id
)
@
router
.
post
(
''
)
async
def
create_category
(
body
:
CategoryBaseSchema
):
return
await
ctrl
.
create
(
**
body
.
model_dump
(
exclude_none
=
True
))
@
router
.
patch
(
'/{id}'
)
async
def
update_category
(
id
:
UUID
,
body
:
CategoryBaseSchema
):
return
await
ctrl
.
update
(
id
,
**
body
.
model_dump
(
exclude_none
=
True
))
@
router
.
delete
(
'/{id}'
)
async
def
delete_category
(
id
:
UUID
):
return
await
ctrl
.
delete
(
id
)
src/app/api/category/schemas.py
0 → 100644
View file @
e5691170
from
uuid
import
UUID
from
pydantic
import
BaseModel
,
ConfigDict
class
CategoryBaseSchema
(
BaseModel
):
name
:
str
|
None
=
None
class
CategoryIdSchema
(
BaseModel
):
id
:
UUID
model_config
=
ConfigDict
(
from_attributes
=
True
)
class
CategorySchema
(
CategoryBaseSchema
,
CategoryIdSchema
):
pass
src/app/api/category/services.py
0 → 100644
View file @
e5691170
from
uuid
import
UUID
from
db.repositories
import
CategoryRepository
from
.schemas
import
CategorySchema
class
CategoryService
:
category_repository
=
CategoryRepository
()
async
def
get_category_list
(
self
,
**
kwargs
):
categories
=
await
self
.
category_repository
.
get_list
(
**
kwargs
)
return
[
CategorySchema
.
model_validate
(
category
)
for
category
in
categories
]
async
def
get_category
(
self
,
id
:
UUID
):
category
=
await
self
.
category_repository
.
get
(
id
)
return
CategorySchema
.
model_validate
(
category
)
async
def
create_category
(
self
,
**
kwargs
):
category
=
await
self
.
category_repository
.
create
(
**
kwargs
)
return
CategorySchema
.
model_validate
(
category
)
async
def
update_category
(
self
,
id
:
UUID
,
**
kwargs
):
category
=
await
self
.
category_repository
.
update
(
id
,
**
kwargs
)
return
CategorySchema
.
model_validate
(
category
)
async
def
delete_category
(
self
,
id
:
UUID
):
return
await
self
.
category_repository
.
delete
(
id
)
src/app/api/router.py
View file @
e5691170
...
...
@@ -4,6 +4,7 @@ from .status.routes import router as status_route
from
.type.routes
import
router
as
type_route
from
.task.routes
import
router
as
task_route
from
.user.routes
import
router
as
user_route
from
.category.routes
import
router
as
category_route
router
=
APIRouter
(
prefix
=
'/api'
)
...
...
@@ -11,3 +12,4 @@ router.include_router(status_route)
router
.
include_router
(
type_route
)
router
.
include_router
(
task_route
)
router
.
include_router
(
user_route
)
router
.
include_router
(
category_route
)
src/app/db/models.py
View file @
e5691170
...
...
@@ -43,6 +43,10 @@ class Task(BaseModel):
through
=
'types_tasks'
)
priority
=
fields
.
CharEnumField
(
enum_type
=
PriorityEnum
,
default
=
PriorityEnum
.
LOW
)
category
=
fields
.
ForeignKeyField
(
'models.Category'
,
related_name
=
'category_tasks'
,
on_delete
=
fields
.
OnDelete
.
SET_NULL
,
null
=
True
)
def
__str__
(
self
):
return
self
.
title
...
...
src/app/db/repositories.py
View file @
e5691170
...
...
@@ -2,7 +2,7 @@ import tortoise
from
tortoise.expressions
import
Q
from
.models
import
(
Status
,
Type
,
Task
,
User
,
Comment
Status
,
Type
,
Task
,
User
,
Comment
,
Category
)
from
exceptions
import
common
as
common_exc
...
...
@@ -98,3 +98,7 @@ class UserRepository(BaseRepository):
class
CommentRepository
(
BaseRepository
):
model
=
Comment
class
CategoryRepository
(
BaseRepository
):
model
=
Category
src/migrations/models/4_20231124061632_update.py
0 → 100644
View file @
e5691170
from
tortoise
import
BaseDBAsyncClient
async
def
upgrade
(
db
:
BaseDBAsyncClient
)
->
str
:
return
"""
CREATE TABLE IF NOT EXISTS "categories" (
"id" UUID NOT NULL PRIMARY KEY,
"created_at" TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updated_at" TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
"name" VARCHAR(255) NOT NULL
);"""
async
def
downgrade
(
db
:
BaseDBAsyncClient
)
->
str
:
return
"""
DROP TABLE IF EXISTS "categories";"""
src/migrations/models/5_20231124061903_update.py
0 → 100644
View file @
e5691170
from
tortoise
import
BaseDBAsyncClient
async
def
upgrade
(
db
:
BaseDBAsyncClient
)
->
str
:
return
"""
ALTER TABLE "tasks" ADD "category_id" UUID;
ALTER TABLE "tasks" ADD CONSTRAINT "fk_tasks_categori_3e76a3ba" FOREIGN KEY ("category_id") REFERENCES "categories" ("id") ON DELETE SET NULL;"""
async
def
downgrade
(
db
:
BaseDBAsyncClient
)
->
str
:
return
"""
ALTER TABLE "tasks" DROP CONSTRAINT "fk_tasks_categori_3e76a3ba";
ALTER TABLE "tasks" DROP COLUMN "category_id";"""
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