Skip to content

Commit cf7354a

Browse files
authored
Fix: history response (#118)
* history에 unit을 추가했습니다. * key가 중복될 경우 기존의 내용을 수정합니다.
1 parent 7adaa79 commit cf7354a

File tree

8 files changed

+103
-33
lines changed

8 files changed

+103
-33
lines changed

wacruit/src/admin/views.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -505,6 +505,7 @@ class HistoryAdmin(ModelView, model=History):
505505
History.id,
506506
History.history_key,
507507
History.history_value,
508+
History.history_unit,
508509
]
509510

510511
column_searchable_list = [
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,6 @@
11
from wacruit.src.apps.common.exceptions import WacruitException
2+
3+
4+
class HistoryNotFoundException(WacruitException):
5+
def __init__(self):
6+
super().__init__(status_code=404, detail="History key not found")

wacruit/src/apps/history/models.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,4 @@ class History(DeclarativeBase):
1111
id: Mapped[intpk]
1212
history_key: Mapped[str50]
1313
history_value: Mapped[str50]
14+
history_unit: Mapped[str50 | None]

wacruit/src/apps/history/repositories.py

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
from datetime import datetime
2+
from typing import List
23

34
from fastapi import Depends
5+
from sqlalchemy import delete
46
from sqlalchemy import select
57
from sqlalchemy.orm import Session
68

@@ -22,20 +24,24 @@ def get_history(self) -> list[History]:
2224
query = select(History)
2325
return list(self.session.execute(query).scalars().all())
2426

25-
def update_history(self, history: History) -> History:
26-
history_key = history.history_key
27-
history_value = history.history_value
28-
29-
with self.transaction:
30-
query = select(History).where(History.history_key == history_key)
31-
result = self.session.execute(query).scalar_one_or_none()
32-
if result:
33-
result.history_value = history_value
27+
def update_history(self, history_list: List[History]) -> List[History]:
28+
keys = [h.history_key for h in history_list]
29+
existing_key = (
30+
self.session.query(History).filter(History.history_key.in_(keys)).all()
31+
)
32+
existing_map = {r.history_key: r for r in existing_key}
33+
34+
for item in history_list:
35+
if item.history_key in existing_map:
36+
# if existed, update
37+
existing = existing_map[item.history_key]
38+
existing.history_value = item.history_value
39+
existing.history_unit = item.history_unit
3440
else:
35-
result = History(history_key=history_key, history_value=history_value)
36-
self.session.add(result)
41+
# if not existed, insert
42+
self.session.add(item)
3743

38-
return history
44+
return history_list
3945

4046
def get_start_date(self) -> datetime | None:
4147
query = select(History).where(History.history_key == "start_date")
@@ -44,3 +50,10 @@ def get_start_date(self) -> datetime | None:
4450
if result is None:
4551
return None
4652
return datetime.strptime(result.history_value, "%Y-%m-%d")
53+
54+
def delete_history(self, history_key: str) -> bool:
55+
history = self.session.query(History).filter_by(history_key=history_key).first()
56+
if not history:
57+
return False
58+
self.session.delete(history)
59+
return True
Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,26 @@
1+
from typing import List
2+
13
from pydantic import BaseModel
24

35
from wacruit.src.apps.common.schemas import OrmModel
46

57

8+
class HistoryItemRequest(BaseModel):
9+
history_key: str
10+
history_value: str
11+
history_unit: str | None = None
12+
13+
614
class UpdateHistoryRequest(BaseModel):
7-
class Config:
8-
extra = "allow"
15+
items: List[HistoryItemRequest]
16+
17+
18+
class DeleteHistoryRequest(BaseModel):
19+
history_key: str
920

1021

1122
class HistoryResponse(OrmModel):
1223
id: int
1324
history_key: str
1425
history_value: str
26+
history_unit: str | None = None

wacruit/src/apps/history/services.py

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22

33
from fastapi import Depends
44

5+
from wacruit.src.apps.history.exceptions import HistoryNotFoundException
56
from wacruit.src.apps.history.models import History
67
from wacruit.src.apps.history.repositories import HistoryRepository
8+
from wacruit.src.apps.history.schemas import DeleteHistoryRequest
79
from wacruit.src.apps.history.schemas import UpdateHistoryRequest
810

911

@@ -13,30 +15,25 @@ def __init__(self, history_repository: HistoryRepository = Depends()):
1315

1416
def get_history(self) -> list[History]:
1517
history_list = self.history_repository.get_history()
16-
operation_period = self.calculate_operation_period()
17-
18-
if operation_period is not None:
19-
history_list.append(
20-
History(
21-
history_key="operation_period", history_value=str(operation_period)
22-
)
23-
)
2418

2519
return history_list
2620

2721
def update_history(self, update_request: UpdateHistoryRequest):
28-
req = update_request.dict()
22+
to_update_list = []
2923

30-
for k, v in req.items():
31-
to_update = History(history_key=k, history_value=v)
32-
self.history_repository.update_history(to_update)
24+
for item in update_request.items:
25+
to_update = History(
26+
history_key=item.history_key,
27+
history_value=item.history_value,
28+
history_unit=item.history_unit,
29+
)
30+
to_update_list.append(to_update)
3331

34-
return self.get_history()
32+
self.history_repository.update_history(to_update_list)
3533

36-
def calculate_operation_period(self) -> int | None:
37-
start_date = self.history_repository.get_start_date()
38-
now = datetime.now()
34+
return self.get_history()
3935

40-
if start_date is None:
41-
return None
42-
return now.year - start_date.year
36+
def delete_history(self, delete_request: DeleteHistoryRequest):
37+
result = self.history_repository.delete_history(delete_request.history_key)
38+
if not result:
39+
raise HistoryNotFoundException()

wacruit/src/apps/history/views.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1+
from http import HTTPStatus
12
from typing import Annotated
23

34
from fastapi import APIRouter
45
from fastapi import Depends
56

7+
from wacruit.src.apps.history.schemas import DeleteHistoryRequest
68
from wacruit.src.apps.history.schemas import HistoryResponse
79
from wacruit.src.apps.history.schemas import UpdateHistoryRequest
810
from wacruit.src.apps.history.services import HistoryService
@@ -30,3 +32,12 @@ def get_history(history_service: Annotated[HistoryService, Depends()]):
3032
history_list = history_service.get_history()
3133

3234
return [HistoryResponse.from_orm(history) for history in history_list]
35+
36+
37+
@v3_router.delete("", status_code=HTTPStatus.NO_CONTENT)
38+
def delete_history(
39+
admin_user: AdminUser,
40+
history_service: Annotated[HistoryService, Depends()],
41+
delete_request: DeleteHistoryRequest,
42+
):
43+
history_service.delete_history(delete_request)
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
"""add history_unit
2+
3+
Revision ID: 60e3aaf301ce
4+
Revises: 564952e2cd49
5+
Create Date: 2025-11-27 21:52:29.877472
6+
7+
"""
8+
from alembic import op
9+
import sqlalchemy as sa
10+
from sqlalchemy.dialects import mysql
11+
12+
# revision identifiers, used by Alembic.
13+
revision = "60e3aaf301ce"
14+
down_revision = "564952e2cd49"
15+
branch_labels = None
16+
depends_on = None
17+
18+
19+
def upgrade() -> None:
20+
# ### commands auto generated by Alembic - please adjust! ###
21+
op.add_column(
22+
"history", sa.Column("history_unit", sa.String(length=50), nullable=True)
23+
)
24+
# ### end Alembic commands ###
25+
26+
27+
def downgrade() -> None:
28+
# ### commands auto generated by Alembic - please adjust! ###
29+
op.drop_column("history", "history_unit")
30+
# ### end Alembic commands ###

0 commit comments

Comments
 (0)