ciyidogan commited on
Commit
98e5e6c
·
verified ·
1 Parent(s): b37555b

Update project_controller.py

Browse files
Files changed (1) hide show
  1. project_controller.py +63 -66
project_controller.py CHANGED
@@ -1,23 +1,21 @@
1
- from fastapi import APIRouter, Request, HTTPException
2
- from service_config import ServiceConfig
3
  import datetime
4
  import json
5
  import copy
6
 
7
  router = APIRouter()
8
- service_config = ServiceConfig()
9
- service_config.load()
10
 
11
  def get_utc_now():
12
  return datetime.datetime.utcnow().isoformat()
13
 
14
  @router.get("/list")
15
- def list_projects():
16
- return service_config.projects
17
 
18
  @router.get("/{project_name}/latest")
19
- def get_latest_project_version(project_name: str):
20
- project = service_config.projects.get(project_name)
21
  if not project:
22
  raise HTTPException(status_code=404, detail="Project not found")
23
 
@@ -35,13 +33,13 @@ def get_latest_project_version(project_name: str):
35
  }
36
 
37
  @router.post("/update")
38
- async def update_project(request: Request):
39
  data = await request.json()
40
  project_name = data.get("project_name")
41
  client_last_updated = data.get("client_last_updated")
42
  new_data = data.get("new_data")
43
 
44
- project = service_config.projects.get(project_name)
45
  if not project:
46
  raise HTTPException(status_code=404, detail="Project not found")
47
 
@@ -55,18 +53,18 @@ async def update_project(request: Request):
55
  latest.update(new_data)
56
  project["last_updated"] = get_utc_now()
57
 
58
- with open(service_config.config_path, "w", encoding="utf-8") as f:
59
- json.dump(service_config, f, indent=2)
60
 
61
  return {"message": f"Project {project_name} updated"}
62
 
63
  @router.post("/publish")
64
- async def publish_project(request: Request):
65
  data = await request.json()
66
  project_name = data.get("project_name")
67
  client_last_updated = data.get("client_last_updated")
68
 
69
- project = service_config.projects.get(project_name)
70
  if not project:
71
  raise HTTPException(status_code=404, detail="Project not found")
72
 
@@ -97,18 +95,18 @@ async def publish_project(request: Request):
97
  project["versions"].append(new_version)
98
  project["last_updated"] = get_utc_now()
99
 
100
- with open(service_config.config_path, "w", encoding="utf-8") as f:
101
- json.dump(service_config, f, indent=2)
102
 
103
  return {"message": f"Project {project_name} version published and new draft version {new_version_number} created"}
104
 
105
  @router.post("/add_intent")
106
- async def add_intent(request: Request):
107
  data = await request.json()
108
  project_name = data.get("project_name")
109
  intent_name = data.get("intent_name")
110
 
111
- project = service_config.projects.get(project_name)
112
  if not project:
113
  raise HTTPException(status_code=404, detail="Project not found")
114
 
@@ -127,18 +125,18 @@ async def add_intent(request: Request):
127
  })
128
  project["last_updated"] = get_utc_now()
129
 
130
- with open(service_config.config_path, "w", encoding="utf-8") as f:
131
- json.dump(service_config, f, indent=2)
132
 
133
  return {"message": f"Intent {intent_name} added to project {project_name}"}
134
 
135
  @router.post("/delete_intent")
136
- async def delete_intent(request: Request):
137
  data = await request.json()
138
  project_name = data.get("project_name")
139
  intent_name = data.get("intent_name")
140
 
141
- project = service_config.projects.get(project_name)
142
  if not project:
143
  raise HTTPException(status_code=404, detail="Project not found")
144
 
@@ -151,14 +149,47 @@ async def delete_intent(request: Request):
151
  latest["intents"] = new_intents
152
  project["last_updated"] = get_utc_now()
153
 
154
- with open(service_config.config_path, "w", encoding="utf-8") as f:
155
- json.dump(service_config, f, indent=2)
156
 
157
  return {"message": f"Intent {intent_name} deleted from project {project_name}"}
158
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
159
  @router.post("/seed/test_data")
160
- def seed_test_data():
161
- service_config.projects = {
162
  "demo-project": {
163
  "enabled": True,
164
  "last_updated": get_utc_now(),
@@ -186,49 +217,15 @@ def seed_test_data():
186
  ]
187
  }
188
  }
189
- with open(service_config.config_path, "w", encoding="utf-8") as f:
190
- json.dump(service_config, f, indent=2)
191
 
192
  return {"message": "Test data seeded"}
193
 
194
  @router.post("/clear/all")
195
- def clear_all():
196
- service_config.projects = {}
197
- with open(service_config.config_path, "w", encoding="utf-8") as f:
198
- json.dump(service_config, f, indent=2)
199
 
200
  return {"message": "All projects cleared"}
201
-
202
- @router.post("/update_intent")
203
- async def update_intent(request: Request):
204
- data = await request.json()
205
- project_name = data.get("project_name")
206
- intent_name = data.get("intent_name")
207
- intent_data = data.get("intent_data")
208
-
209
- if not project_name or not intent_name or not intent_data:
210
- raise HTTPException(status_code=400, detail="project_name, intent_name, and intent_data are required")
211
-
212
- project = service_config.projects.get(project_name)
213
- if not project:
214
- raise HTTPException(status_code=404, detail="Project not found")
215
-
216
- latest = max(project["versions"], key=lambda v: v["version_number"])
217
-
218
- updated = False
219
- for intent in latest.get("intents", []):
220
- if intent.get("name") == intent_name:
221
- intent.update(intent_data)
222
- updated = True
223
- break
224
-
225
- if not updated:
226
- raise HTTPException(status_code=404, detail="Intent not found in project")
227
-
228
- project["last_updated"] = get_utc_now()
229
-
230
- with open(service_config.config_path, "w", encoding="utf-8") as f:
231
- json.dump(service_config, f, indent=2)
232
-
233
- return {"message": f"Intent {intent_name} updated in project {project_name}"}
234
-
 
1
+ from fastapi import APIRouter, Request, HTTPException, Depends
2
+ from app import get_config, ServiceConfig
3
  import datetime
4
  import json
5
  import copy
6
 
7
  router = APIRouter()
 
 
8
 
9
  def get_utc_now():
10
  return datetime.datetime.utcnow().isoformat()
11
 
12
  @router.get("/list")
13
+ def list_projects(config: ServiceConfig = Depends(get_config)):
14
+ return config.projects
15
 
16
  @router.get("/{project_name}/latest")
17
+ def get_latest_project_version(project_name: str, config: ServiceConfig = Depends(get_config)):
18
+ project = config.projects.get(project_name)
19
  if not project:
20
  raise HTTPException(status_code=404, detail="Project not found")
21
 
 
33
  }
34
 
35
  @router.post("/update")
36
+ async def update_project(request: Request, config: ServiceConfig = Depends(get_config)):
37
  data = await request.json()
38
  project_name = data.get("project_name")
39
  client_last_updated = data.get("client_last_updated")
40
  new_data = data.get("new_data")
41
 
42
+ project = config.projects.get(project_name)
43
  if not project:
44
  raise HTTPException(status_code=404, detail="Project not found")
45
 
 
53
  latest.update(new_data)
54
  project["last_updated"] = get_utc_now()
55
 
56
+ with open(config.config_path, "w", encoding="utf-8") as f:
57
+ json.dump(config, f, indent=2)
58
 
59
  return {"message": f"Project {project_name} updated"}
60
 
61
  @router.post("/publish")
62
+ async def publish_project(request: Request, config: ServiceConfig = Depends(get_config)):
63
  data = await request.json()
64
  project_name = data.get("project_name")
65
  client_last_updated = data.get("client_last_updated")
66
 
67
+ project = config.projects.get(project_name)
68
  if not project:
69
  raise HTTPException(status_code=404, detail="Project not found")
70
 
 
95
  project["versions"].append(new_version)
96
  project["last_updated"] = get_utc_now()
97
 
98
+ with open(config.config_path, "w", encoding="utf-8") as f:
99
+ json.dump(config, f, indent=2)
100
 
101
  return {"message": f"Project {project_name} version published and new draft version {new_version_number} created"}
102
 
103
  @router.post("/add_intent")
104
+ async def add_intent(request: Request, config: ServiceConfig = Depends(get_config)):
105
  data = await request.json()
106
  project_name = data.get("project_name")
107
  intent_name = data.get("intent_name")
108
 
109
+ project = config.projects.get(project_name)
110
  if not project:
111
  raise HTTPException(status_code=404, detail="Project not found")
112
 
 
125
  })
126
  project["last_updated"] = get_utc_now()
127
 
128
+ with open(config.config_path, "w", encoding="utf-8") as f:
129
+ json.dump(config, f, indent=2)
130
 
131
  return {"message": f"Intent {intent_name} added to project {project_name}"}
132
 
133
  @router.post("/delete_intent")
134
+ async def delete_intent(request: Request, config: ServiceConfig = Depends(get_config)):
135
  data = await request.json()
136
  project_name = data.get("project_name")
137
  intent_name = data.get("intent_name")
138
 
139
+ project = config.projects.get(project_name)
140
  if not project:
141
  raise HTTPException(status_code=404, detail="Project not found")
142
 
 
149
  latest["intents"] = new_intents
150
  project["last_updated"] = get_utc_now()
151
 
152
+ with open(config.config_path, "w", encoding="utf-8") as f:
153
+ json.dump(config, f, indent=2)
154
 
155
  return {"message": f"Intent {intent_name} deleted from project {project_name}"}
156
 
157
+ @router.post("/update_intent")
158
+ async def update_intent(request: Request, config: ServiceConfig = Depends(get_config)):
159
+ data = await request.json()
160
+ project_name = data.get("project_name")
161
+ intent_name = data.get("intent_name")
162
+ intent_data = data.get("intent_data")
163
+
164
+ if not project_name or not intent_name or not intent_data:
165
+ raise HTTPException(status_code=400, detail="project_name, intent_name, and intent_data are required")
166
+
167
+ project = config.projects.get(project_name)
168
+ if not project:
169
+ raise HTTPException(status_code=404, detail="Project not found")
170
+
171
+ latest = max(project["versions"], key=lambda v: v["version_number"])
172
+
173
+ updated = False
174
+ for intent in latest.get("intents", []):
175
+ if intent.get("name") == intent_name:
176
+ intent.update(intent_data)
177
+ updated = True
178
+ break
179
+
180
+ if not updated:
181
+ raise HTTPException(status_code=404, detail="Intent not found in project")
182
+
183
+ project["last_updated"] = get_utc_now()
184
+
185
+ with open(config.config_path, "w", encoding="utf-8") as f:
186
+ json.dump(config, f, indent=2)
187
+
188
+ return {"message": f"Intent {intent_name} updated in project {project_name}"}
189
+
190
  @router.post("/seed/test_data")
191
+ def seed_test_data(config: ServiceConfig = Depends(get_config)):
192
+ config.projects = {
193
  "demo-project": {
194
  "enabled": True,
195
  "last_updated": get_utc_now(),
 
217
  ]
218
  }
219
  }
220
+ with open(config.config_path, "w", encoding="utf-8") as f:
221
+ json.dump(config, f, indent=2)
222
 
223
  return {"message": "Test data seeded"}
224
 
225
  @router.post("/clear/all")
226
+ def clear_all(config: ServiceConfig = Depends(get_config)):
227
+ config.projects = {}
228
+ with open(config.config_path, "w", encoding="utf-8") as f:
229
+ json.dump(config, f, indent=2)
230
 
231
  return {"message": "All projects cleared"}