-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserverMain.py
More file actions
86 lines (68 loc) · 2.75 KB
/
serverMain.py
File metadata and controls
86 lines (68 loc) · 2.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
from fastapi import FastAPI, HTTPException
from pydantic import BaseModel
import uuid
from eventstore import EventStore
from features.createItem import CreateItem
from features.modifyItem import ModifyItem
from features.itemDecider import ItemDecider
from features.itemReadmodel import ItemReadModel
from features.analytics.analyticsReadmodel import AnalyticsReadModel
from features.history.itemHIstoryReadModel import ItemHistoryReadModel
app = FastAPI()
# Initialize components
event_store = EventStore()
decider = ItemDecider()
read_model = ItemReadModel()
analytics_read_model = AnalyticsReadModel()
history_read_model = ItemHistoryReadModel()
class CreateItemRequest(BaseModel):
name: str
class ModifyItemRequest(BaseModel):
item_id: str
new_name: str
@app.post("/items/")
def create_item(request: CreateItemRequest):
item_id = str(uuid.uuid4())
create_command = CreateItem(item_id=item_id, name=request.name)
create_events = decider.decide(create_command)
aggregate_id = str(uuid.uuid4())
for event in create_events:
event_store.append(aggregate_id, event)
# Project events to read models
events = event_store.get_events(aggregate_id)
read_model.project(events)
analytics_read_model.project(events)
history_read_model.project(events)
return {"item_id": item_id}
@app.put("/items/")
def modify_item(request: ModifyItemRequest):
modify_command = ModifyItem(item_id=request.item_id, new_name=request.new_name)
modify_events = decider.decide(modify_command)
aggregate_id = str(uuid.uuid4()) # Assuming the aggregate ID is related to the item ID
for event in modify_events:
event_store.append(aggregate_id, event)
# Project events to read models
events = event_store.get_events(aggregate_id)
read_model.project(events)
analytics_read_model.project(events)
history_read_model.project(events)
return {"item_id": request.item_id}
@app.get("/items/")
def get_items():
return {"items": read_model.items}
@app.get("/analytics/{item_id}/modification_count")
def get_modification_count(item_id: str):
return {"modification_count": analytics_read_model.get_modification_count(item_id)}
@app.get("/analytics/{item_id}/modification_history")
def get_modification_history(item_id: str):
return {"modification_history": analytics_read_model.get_modification_history(item_id)}
@app.get("/history/{item_id}")
def get_item_history(item_id: str):
return {"history": history_read_model.get_history(item_id)}
@app.get("/history/{item_id}/comparison")
def compare_histories(item_id: str):
return {"comparison": history_read_model.compare_histories(item_id)}
# Run the server using uvicorn
if __name__ == "__main__":
import uvicorn
uvicorn.run(app, host="0.0.0.0", port=8000)