Skip to content
Snippets Groups Projects
Commit 64dc9996 authored by Niklas Thielbörger's avatar Niklas Thielbörger
Browse files

Merge branch '23-backend-put-project-parameter-liste-statt-project-object' into 'main'

Resolve "Backend: put("/project") Parameter-Liste statt Project Object"

Closes #23

See merge request !18
parents 752e736e 8e2328b6
No related branches found
No related tags found
1 merge request!18Resolve "Backend: put("/project") Parameter-Liste statt Project Object"
from fastapi import FastAPI, HTTPException from fastapi import FastAPI, HTTPException
from pydantic import BaseModel, field_validator from pydantic import BaseModel, field_validator
from typing import List, Optional from typing import List, Optional
import os,json,re import os, json, re
from datetime import datetime from datetime import datetime
app = FastAPI() app = FastAPI()
...@@ -58,9 +58,11 @@ def load_projects(): ...@@ -58,9 +58,11 @@ def load_projects():
with open(json_file_path, "r") as file: with open(json_file_path, "r") as file:
return json.load(file) return json.load(file)
def id_exists(id: int) -> bool: def id_exists(id: int) -> bool:
return any(project["id"] == id for project in projects) return any(project["id"] == id for project in projects)
# Projekte in die JSON-Datei speichern # Projekte in die JSON-Datei speichern
def save_projects_to_json(): def save_projects_to_json():
with open(json_file_path, "w") as file: with open(json_file_path, "w") as file:
...@@ -119,28 +121,55 @@ async def delete_project(id: int): ...@@ -119,28 +121,55 @@ async def delete_project(id: int):
Error.NOT_FOUND.status_code:{"description":Error.NOT_FOUND.detail}, Error.NOT_FOUND.status_code:{"description":Error.NOT_FOUND.detail},
Error.ID_EXISTS.status_code:{"description":Error.ID_EXISTS.detail} Error.ID_EXISTS.status_code:{"description":Error.ID_EXISTS.detail}
}) })
async def update_project(id: int, updated_project: Project): async def update_project(
id: int,
cost: float,
KPI1: float,
KPI2: float,
KPI3: float,
KPI4: float,
KPI5: int,
KPI6: float,
KPI7: int,
KPI8: int,
start_date: str,
duration: int,
):
global projects global projects
project_index = next( project_index = next(
(index for index, proj in enumerate(projects) if proj["id"] == id), None (index for index, proj in enumerate(projects) if proj["id"] == id), None
) )
if project_index is None: if project_index is None:
raise Error.NOT_FOUND raise Error.NOT_FOUND
# Prüfen, ob die neue ID zu einem anderen Projekt gehört
if updated_project.id is not None and updated_project.id != id and id_exists(updated_project.id):
raise Error.ID_EXISTS
updated_project_dict = updated_project.model_dump() if id is not None and id_exists(id):
updated_project_dict.pop("id", None) raise HTTPException(
status_code=409,
detail="Ein anderes Projekt mit dieser ID existiert bereits.",
)
updated_project = Project(
id=id,
cost=cost,
KPI1=KPI1,
KPI2=KPI2,
KPI3=KPI3,
KPI4=KPI4,
KPI5=KPI5,
KPI6=KPI6,
KPI7=KPI7,
KPI8=KPI8,
start_date=start_date,
duration=duration,
)
projects[project_index].update(updated_project_dict) projects[project_index] = updated_project.model_dump()
save_projects_to_json() save_projects_to_json()
return projects[project_index] return updated_project
# PUT /project - Neues Projekt erstellen (ID wird automatisch vergeben) # PUT /project - Neues Projekt erstellen (ID wird automatisch vergeben)
...@@ -153,7 +182,6 @@ async def update_project(id: int, updated_project: Project): ...@@ -153,7 +182,6 @@ async def update_project(id: int, updated_project: Project):
async def create_project(new_project: Project): async def create_project(new_project: Project):
global projects global projects
# prüfen Prüfen, ob die angegebene ID bereits existiert # prüfen Prüfen, ob die angegebene ID bereits existiert
if new_project.id is not None and id_exists(new_project.id): if new_project.id is not None and id_exists(new_project.id):
raise Error.ID_EXISTS raise Error.ID_EXISTS
...@@ -167,4 +195,3 @@ async def create_project(new_project: Project): ...@@ -167,4 +195,3 @@ async def create_project(new_project: Project):
save_projects_to_json() save_projects_to_json()
return new_project return new_project
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment