Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
B
BookingMyHrManagement
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Registry
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
Chanachai
BookingMyHrManagement
Commits
e7d7b98c
Commit
e7d7b98c
authored
Mar 15, 2025
by
Ooh-Ao
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
borrow
parent
2e116ef0
Show whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
58 additions
and
53 deletions
+58
-53
borrow_controller.cpython-312.pyc
...controllers/__pycache__/borrow_controller.cpython-312.pyc
+0
-0
borrow_controller.py
API-Fast/src/controllers/borrow_controller.py
+32
-31
borrow_transaction.cpython-312.pyc
...src/models/__pycache__/borrow_transaction.cpython-312.pyc
+0
-0
borrow_transaction.py
API-Fast/src/models/borrow_transaction.py
+3
-1
borrow_routes.cpython-312.pyc
...Fast/src/routes/__pycache__/borrow_routes.cpython-312.pyc
+0
-0
borrow_routes.py
API-Fast/src/routes/borrow_routes.py
+17
-16
borrow_schema.py
API-Fast/src/schemas/borrow_schema.py
+6
-5
No files found.
API-Fast/src/controllers/__pycache__/borrow_controller.cpython-312.pyc
View file @
e7d7b98c
No preview for this file type
API-Fast/src/controllers/borrow_controller.py
View file @
e7d7b98c
# myproject/controllers/borrow_controller.py
from
fastapi
import
HTTPException
from
uuid
import
UUID
from
datetime
import
datetime
from
fastapi
import
HTTPException
from
sqlalchemy.ext.asyncio
import
AsyncSession
from
sqlalchemy
import
select
from
..models.borrow_transaction
import
BorrowTransaction
from
..models.project_equipment
import
ProjectEquipment
from
..models.equipments
import
Equipment
from
..schemas.borrow_schema
import
(
BorrowCreate
,
BorrowUpdate
)
from
..models.equipment
import
Equipment
from
..models.member
import
Member
# ถ้าต้องเช็คว่ามี member นี้จริงไหม
from
..schemas.borrow_schema
import
BorrowCreate
,
BorrowUpdate
# CREATE
# 1) ตรวจสอบ ProjectEquipment (pe_id) มีไหม
# 2) ตรวจสอบ quantity_in_project >= quantity_borrowed
# 3) ลดจำนวน => update
# 4) สร้าง BorrowTransaction (status=borrowed)
async
def
create_borrow
(
db
:
AsyncSession
,
borrow_in
:
BorrowCreate
):
# 1) หา projectEquipment จาก pe_id
# - ตรวจสอบ member_id มีจริงไหม (ถ้าต้องการ)
# - ตรวจสอบ project_equipment (pe_id) มีจริง + พอไหม
# - ลดจำนวนใน project_equipment
# - สร้าง record borrow_transaction
async
def
create_borrow_transaction
(
db
:
AsyncSession
,
borrow_in
:
BorrowCreate
):
# (ถ้าต้องการตรวจสอบว่ามี member_id นี้ไหม)
# result_member = await db.execute(select(Member).where(Member.memberId == borrow_in.member_id))
# member_db = result_member.scalar_one_or_none()
# if not member_db:
# raise HTTPException(status_code=404, detail="Member not found")
# 1) หา project_equipments จาก pe_id
result_pe
=
await
db
.
execute
(
select
(
ProjectEquipment
)
.
where
(
ProjectEquipment
.
pe_id
==
borrow_in
.
pe_id
)
)
...
...
@@ -39,6 +42,7 @@ async def create_borrow(db: AsyncSession, borrow_in: BorrowCreate):
# 4) สร้าง BorrowTransaction
new_borrow
=
BorrowTransaction
(
member_id
=
borrow_in
.
member_id
,
pe_id
=
borrow_in
.
pe_id
,
quantity_borrowed
=
borrow_in
.
quantity_borrowed
,
status
=
borrow_in
.
status
or
"borrowed"
...
...
@@ -47,25 +51,23 @@ async def create_borrow(db: AsyncSession, borrow_in: BorrowCreate):
await
db
.
commit
()
await
db
.
refresh
(
new_borrow
)
return
new_borrow
# UPDATE
#
เช่น เปลี่ยน status -> 'returned' => ถ้า is_returnable => +quantity_in_project
คืน
async
def
update_borrow
(
db
:
AsyncSession
,
borrow_id
:
UUID
,
borrow_up
:
BorrowUpdate
):
# 1) หา borrow
result_b
orrow
=
await
db
.
execute
(
#
- ถ้าเปลี่ยน status เป็น "returned" => เช็ค equipment.is_returnable => บวกจำนวน
คืน
async
def
update_borrow
_transaction
(
db
:
AsyncSession
,
borrow_id
:
UUID
,
borrow_up
:
BorrowUpdate
):
# 1) หา borrow
Transaction เดิม
result_b
t
=
await
db
.
execute
(
select
(
BorrowTransaction
)
.
where
(
BorrowTransaction
.
borrow_id
==
borrow_id
)
)
borrowTx
=
result_b
orrow
.
scalar_one_or_none
()
borrowTx
=
result_b
t
.
scalar_one_or_none
()
if
not
borrowTx
:
raise
HTTPException
(
status_code
=
404
,
detail
=
"BorrowTransaction not found"
)
# ถ้าส่ง status มา
if
borrow_up
.
status
:
# 2) ถ้าเปลี่ยนเป็น
returned
# 2) ถ้าเปลี่ยนเป็น
'returned'
if
borrow_up
.
status
==
"returned"
:
# หา
P
rojectEquipment
# หา
p
rojectEquipment
result_pe
=
await
db
.
execute
(
select
(
ProjectEquipment
)
.
where
(
ProjectEquipment
.
pe_id
==
borrowTx
.
pe_id
)
)
...
...
@@ -73,7 +75,7 @@ async def update_borrow(db: AsyncSession, borrow_id: UUID, borrow_up: BorrowUpda
if
not
projectEquipment
:
raise
HTTPException
(
status_code
=
404
,
detail
=
"ProjectEquipment not found"
)
# หา
E
quipment
# หา
e
quipment
result_eq
=
await
db
.
execute
(
select
(
Equipment
)
.
where
(
Equipment
.
equipment_id
==
projectEquipment
.
equipment_id
)
)
...
...
@@ -81,38 +83,37 @@ async def update_borrow(db: AsyncSession, borrow_id: UUID, borrow_up: BorrowUpda
if
not
equipment
:
raise
HTTPException
(
status_code
=
404
,
detail
=
"Equipment not found"
)
# ถ้า is_returnable => บวก
คื
น
# ถ้า is_returnable => บวก
จำนว
น
if
equipment
.
is_returnable
:
projectEquipment
.
quantity_in_project
+=
borrowTx
.
quantity_borrowed
db
.
add
(
projectEquipment
)
# อัปเดต borrowTx: status = returned, returned_date = now
borrowTx
.
status
=
"returned"
borrowTx
.
returned_date
=
datetime
.
utcnow
()
else
:
#
กรณีอัปเดตเป็น status
อื่น
#
เปลี่ยนเป็นสถานะ
อื่น
borrowTx
.
status
=
borrow_up
.
status
db
.
add
(
borrowTx
)
await
db
.
commit
()
await
db
.
refresh
(
borrowTx
)
return
borrowTx
# GET ALL
async
def
get_all_borrows
(
db
:
AsyncSession
):
async
def
get_all_borrow
_transaction
s
(
db
:
AsyncSession
):
result
=
await
db
.
execute
(
select
(
BorrowTransaction
))
return
result
.
scalars
()
.
all
()
# GET
ONE
async
def
get_borrow_by_id
(
db
:
AsyncSession
,
borrow_id
:
UUID
):
# GET
BY ID
async
def
get_borrow_
transaction_
by_id
(
db
:
AsyncSession
,
borrow_id
:
UUID
):
result
=
await
db
.
execute
(
select
(
BorrowTransaction
)
.
where
(
BorrowTransaction
.
borrow_id
==
borrow_id
)
)
return
result
.
scalar_one_or_none
()
# DELETE
async
def
delete_borrow
(
db
:
AsyncSession
,
borrow_id
:
UUID
):
async
def
delete_borrow
_transaction
(
db
:
AsyncSession
,
borrow_id
:
UUID
):
borrowTx
=
await
db
.
get
(
BorrowTransaction
,
borrow_id
)
if
not
borrowTx
:
raise
HTTPException
(
status_code
=
404
,
detail
=
"BorrowTransaction not found"
)
...
...
API-Fast/src/models/__pycache__/borrow_transaction.cpython-312.pyc
View file @
e7d7b98c
No preview for this file type
API-Fast/src/models/borrow_transaction.py
View file @
e7d7b98c
# myproject/models/borrow_transaction.py
import
uuid
from
datetime
import
datetime
from
sqlalchemy
import
Column
,
Integer
,
String
,
DateTime
...
...
@@ -13,7 +14,8 @@ class BorrowTransaction(Base):
primary_key
=
True
,
default
=
uuid
.
uuid4
)
pe_id
=
Column
(
UUID
(
as_uuid
=
True
),
nullable
=
False
)
# FK to project_equipments.pe_id
pe_id
=
Column
(
UUID
(
as_uuid
=
True
),
nullable
=
False
)
# FK -> project_equipments.pe_id
member_id
=
Column
(
UUID
(
as_uuid
=
True
),
nullable
=
False
)
# FK -> member.memberId (ตาม Model Member)
quantity_borrowed
=
Column
(
Integer
,
nullable
=
False
,
default
=
1
)
status
=
Column
(
String
(
50
),
nullable
=
False
,
default
=
"borrowed"
)
returned_date
=
Column
(
DateTime
,
nullable
=
True
)
...
...
API-Fast/src/routes/__pycache__/borrow_routes.cpython-312.pyc
View file @
e7d7b98c
No preview for this file type
API-Fast/src/routes/borrow_routes.py
View file @
e7d7b98c
# myproject/routes/borrow_routes.py
from
fastapi
import
APIRouter
,
Depends
,
HTTPException
,
status
from
typing
import
List
from
uuid
import
UUID
...
...
@@ -6,11 +7,11 @@ from sqlalchemy.ext.asyncio import AsyncSession
from
..config.database
import
get_db
from
..controllers.borrow_controller
import
(
create_borrow
,
update_borrow
,
get_all_borrows
,
get_borrow_by_id
,
delete_borrow
create_borrow
_transaction
,
update_borrow
_transaction
,
get_all_borrow
_transaction
s
,
get_borrow_
transaction_
by_id
,
delete_borrow
_transaction
)
from
..schemas.borrow_schema
import
(
BorrowCreate
,
...
...
@@ -20,45 +21,45 @@ from ..schemas.borrow_schema import (
router
=
APIRouter
()
# CREATE
# CREATE
(พนักงานยืมอุปกรณ์)
@router.post
(
"/"
,
response_model
=
BorrowResponse
,
status_code
=
status
.
HTTP_201_CREATED
)
async
def
create_borrow_
transaction_
endpoint
(
async
def
create_borrow_endpoint
(
borrow_in
:
BorrowCreate
,
db
:
AsyncSession
=
Depends
(
get_db
)
):
new_borrow
=
await
create_borrow
(
db
,
borrow_in
)
new_borrow
=
await
create_borrow
_transaction
(
db
,
borrow_in
)
return
new_borrow
# UPDATE
# UPDATE
(เช่น คืนอุปกรณ์)
@router.put
(
"/{borrow_id}"
,
response_model
=
BorrowResponse
)
async
def
update_borrow_
transaction_
endpoint
(
async
def
update_borrow_endpoint
(
borrow_id
:
UUID
,
borrow_up
:
BorrowUpdate
,
db
:
AsyncSession
=
Depends
(
get_db
)
):
updated_borrow
=
await
update_borrow
(
db
,
borrow_id
,
borrow_up
)
updated_borrow
=
await
update_borrow
_transaction
(
db
,
borrow_id
,
borrow_up
)
return
updated_borrow
# GET ALL
@router.get
(
"/"
,
response_model
=
List
[
BorrowResponse
])
async
def
get_all_borrows_endpoint
(
db
:
AsyncSession
=
Depends
(
get_db
)):
return
await
get_all_borrows
(
db
)
return
await
get_all_borrow
_transaction
s
(
db
)
# GET
ONE
# GET
BY ID
@router.get
(
"/{borrow_id}"
,
response_model
=
BorrowResponse
)
async
def
get_borrow_by_id_endpoint
(
borrow_id
:
UUID
,
db
:
AsyncSession
=
Depends
(
get_db
)
):
tx
=
await
get_borrow_by_id
(
db
,
borrow_id
)
tx
=
await
get_borrow_
transaction_
by_id
(
db
,
borrow_id
)
if
not
tx
:
raise
HTTPException
(
status_code
=
404
,
detail
=
"BorrowTransaction not found"
)
return
tx
# DELETE
@router.delete
(
"/{borrow_id}"
)
async
def
delete_borrow_
transaction_
endpoint
(
async
def
delete_borrow_endpoint
(
borrow_id
:
UUID
,
db
:
AsyncSession
=
Depends
(
get_db
)
):
return
await
delete_borrow
(
db
,
borrow_id
)
return
await
delete_borrow
_transaction
(
db
,
borrow_id
)
API-Fast/src/schemas/borrow_schema.py
View file @
e7d7b98c
# myproject/schemas/borrow_schema.py
from
pydantic
import
BaseModel
from
typing
import
Optional
from
uuid
import
UUID
from
typing
import
Optional
from
datetime
import
datetime
#
สำหรับสร้าง BorrowTransaction
#
CREATE Borrow (ตอน user กดยืม)
class
BorrowCreate
(
BaseModel
):
member_id
:
UUID
pe_id
:
UUID
quantity_borrowed
:
int
status
:
Optional
[
str
]
=
"borrowed"
#
สำหรับอัปเดต BorrowTransaction (เปลี่ยน status
ฯลฯ)
#
UPDATE Borrow (เปลี่ยนสถานะเป็น returned, canceled
ฯลฯ)
class
BorrowUpdate
(
BaseModel
):
status
:
Optional
[
str
]
=
None
#
สำหรับแสดงผลลัพธ์ (Response)
#
RESPONSE
class
BorrowResponse
(
BaseModel
):
borrow_id
:
UUID
pe_id
:
UUID
member_id
:
UUID
quantity_borrowed
:
int
status
:
str
returned_date
:
Optional
[
datetime
]
=
None
...
...
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