Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
P
PyFund
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
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
陈涛
PyFund
Commits
85673748
Commit
85673748
authored
Mar 23, 2023
by
Confusion-ymc
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
添加mongodb 集合获取依赖
parent
55cc38bf
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
16 additions
and
14 deletions
+16
-14
fund.py
api/fund.py
+9
-13
dependencies.py
dependencies.py
+7
-1
No files found.
api/fund.py
View file @
85673748
...
@@ -2,13 +2,13 @@ import datetime
...
@@ -2,13 +2,13 @@ import datetime
from
typing
import
Union
from
typing
import
Union
from
fastapi
import
APIRouter
,
Depends
from
fastapi
import
APIRouter
,
Depends
from
motor.core
import
AgnosticCollection
from
pymongo
import
ReturnDocument
from
pymongo
import
ReturnDocument
from
db.mongodb_helper
import
AioMongodbManager
from
exception.db
import
NotFundError
from
exception.db
import
NotFundError
from
model
import
Response
from
model
import
Response
from
model.fund
import
FundType
,
CreateFund
,
StakingFund
,
NormalFund
,
UpdateFund
from
model.fund
import
FundType
,
CreateFund
,
StakingFund
,
NormalFund
,
UpdateFund
from
dependencies
import
get_current_user
,
get_
mongodb_manager
from
dependencies
import
get_current_user
,
get_
fund_collect
router
=
APIRouter
()
router
=
APIRouter
()
...
@@ -20,7 +20,7 @@ router = APIRouter()
...
@@ -20,7 +20,7 @@ router = APIRouter()
async
def
create
(
async
def
create
(
create_fund
:
CreateFund
,
create_fund
:
CreateFund
,
user
:
dict
=
Depends
(
get_current_user
),
user
:
dict
=
Depends
(
get_current_user
),
mongodb_manger
:
AioMongodbManager
=
Depends
(
get_mongodb_manager
)
fund_collect
:
AgnosticCollection
=
Depends
(
get_fund_collect
)
):
):
if
create_fund
.
fund_type
==
FundType
.
staking
:
if
create_fund
.
fund_type
==
FundType
.
staking
:
create_model
=
StakingFund
(
**
create_fund
.
dict
(),
**
user
)
create_model
=
StakingFund
(
**
create_fund
.
dict
(),
**
user
)
...
@@ -28,10 +28,9 @@ async def create(
...
@@ -28,10 +28,9 @@ async def create(
else
:
else
:
create_model
=
NormalFund
(
**
create_fund
.
dict
(),
**
user
)
create_model
=
NormalFund
(
**
create_fund
.
dict
(),
**
user
)
response
=
Response
[
NormalFund
](
data
=
create_model
.
dict
())
response
=
Response
[
NormalFund
](
data
=
create_model
.
dict
())
client
=
mongodb_manger
.
get_client
(
name
=
'pyfund'
,
db
=
'pyfund'
,
collect
=
'fund'
)
insert_data
=
create_model
.
dict
()
insert_data
=
create_model
.
dict
()
await
clien
t
.
insert_one
(
insert_data
)
await
fund_collec
t
.
insert_one
(
insert_data
)
return
response
return
response
...
@@ -41,16 +40,14 @@ async def update(
...
@@ -41,16 +40,14 @@ async def update(
fund_id
:
str
,
fund_id
:
str
,
update_fund
:
UpdateFund
,
update_fund
:
UpdateFund
,
user
:
dict
=
Depends
(
get_current_user
),
user
:
dict
=
Depends
(
get_current_user
),
mongodb_manger
:
AioMongodbManager
=
Depends
(
get_mongodb_manager
)
fund_collect
:
AgnosticCollection
=
Depends
(
get_fund_collect
)
):
):
client
=
mongodb_manger
.
get_client
(
name
=
'pyfund'
,
db
=
'pyfund'
,
collect
=
'fund'
)
db_update_data
=
update_fund
.
dict
(
exclude_unset
=
True
)
db_update_data
=
update_fund
.
dict
(
exclude_unset
=
True
)
db_update_data
.
update
({
db_update_data
.
update
({
"update_time"
:
int
(
datetime
.
datetime
.
utcnow
()
.
timestamp
())
"update_time"
:
int
(
datetime
.
datetime
.
utcnow
()
.
timestamp
())
})
})
data
=
await
clien
t
.
find_one_and_update
({
'id'
:
fund_id
,
'user_id'
:
user
[
'user_id'
]},
{
'$set'
:
db_update_data
},
data
=
await
fund_collec
t
.
find_one_and_update
({
'id'
:
fund_id
,
'user_id'
:
user
[
'user_id'
]},
{
'$set'
:
db_update_data
},
return_document
=
ReturnDocument
.
AFTER
)
return_document
=
ReturnDocument
.
AFTER
)
if
data
[
'fund_type'
]
==
FundType
.
staking
:
if
data
[
'fund_type'
]
==
FundType
.
staking
:
return
Response
[
StakingFund
](
data
=
StakingFund
(
**
data
))
return
Response
[
StakingFund
](
data
=
StakingFund
(
**
data
))
else
:
else
:
...
@@ -62,10 +59,9 @@ async def update(
...
@@ -62,10 +59,9 @@ async def update(
async
def
get
(
async
def
get
(
fund_id
:
str
,
fund_id
:
str
,
user
:
dict
=
Depends
(
get_current_user
),
user
:
dict
=
Depends
(
get_current_user
),
mongodb_manger
:
AioMongodbManager
=
Depends
(
get_mongodb_manager
)
fund_collect
:
AgnosticCollection
=
Depends
(
get_fund_collect
)
):
):
client
=
mongodb_manger
.
get_client
(
name
=
'pyfund'
,
db
=
'pyfund'
,
collect
=
'fund'
)
data
=
await
fund_collect
.
find_one
({
'id'
:
fund_id
,
'user_id'
:
user
[
'user_id'
]})
data
=
await
client
.
find_one
({
'id'
:
fund_id
,
'user_id'
:
user
[
'user_id'
]})
if
not
data
:
if
not
data
:
raise
NotFundError
()
raise
NotFundError
()
if
data
[
'fund_type'
]
==
FundType
.
staking
:
if
data
[
'fund_type'
]
==
FundType
.
staking
:
...
...
dependencies.py
View file @
85673748
from
apscheduler.schedulers.asyncio
import
AsyncIOScheduler
from
apscheduler.schedulers.asyncio
import
AsyncIOScheduler
from
fastapi
import
Security
from
fastapi
import
Security
,
Depends
from
fastapi.security
import
HTTPAuthorizationCredentials
from
fastapi.security
import
HTTPAuthorizationCredentials
from
motor.core
import
AgnosticCollection
from
configs
import
settings
from
configs
import
settings
from
db.mongodb_helper
import
AioMongodbManager
from
db.mongodb_helper
import
AioMongodbManager
...
@@ -20,3 +21,8 @@ def get_mongodb_manager(request: Request) -> AioMongodbManager:
...
@@ -20,3 +21,8 @@ def get_mongodb_manager(request: Request) -> AioMongodbManager:
def
get_schedular
(
request
:
Request
)
->
AsyncIOScheduler
:
def
get_schedular
(
request
:
Request
)
->
AsyncIOScheduler
:
return
request
.
app
.
state
.
schedular
return
request
.
app
.
state
.
schedular
# 获取特定的mongodb集合
def
get_fund_collect
(
mongodb_manger
:
AioMongodbManager
=
Depends
(
get_mongodb_manager
))
->
AgnosticCollection
:
return
mongodb_manger
.
get_client
(
name
=
'pyfund'
,
db
=
'pyfund'
,
collect
=
'fund'
)
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