-
Notifications
You must be signed in to change notification settings - Fork 35
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add check for conflicting events before creation #129
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
from datetime import timedelta | ||
from http import HTTPStatus | ||
from logging import getLogger | ||
from typing import List | ||
|
@@ -12,6 +13,7 @@ | |
create_event, | ||
read_event_by_id, | ||
read_events, | ||
read_events_by_robot_id_and_time_span, | ||
remove_event, | ||
) | ||
from flotilla.database.db import get_db | ||
|
@@ -21,8 +23,7 @@ | |
|
||
router = APIRouter() | ||
|
||
NOT_FOUND_DESCRIPTION = "Not Found - No event with given id" | ||
INTERNAL_SERVER_ERROR_DESCRIPTION = "Internal Server Error" | ||
DEFAULT_EVENT_DURATION = timedelta(hours=1) | ||
|
||
|
||
@router.get( | ||
|
@@ -62,11 +63,24 @@ async def post_event( | |
) -> Event: | ||
"""Add a new event to the robot schedule""" | ||
try: | ||
end_time = event_request.start_time + DEFAULT_EVENT_DURATION | ||
overlapping_events: List[Event] = read_events_by_robot_id_and_time_span( | ||
db=db, | ||
robot_id=event_request.robot_id, | ||
start_time=event_request.start_time, | ||
end_time=end_time, | ||
) | ||
if overlapping_events: | ||
raise HTTPException( | ||
status_code=HTTPStatus.CONFLICT.value, | ||
detail=f"Conflict with already existing event in the same time period. Events with id: {','.join(str(event.id) for event in overlapping_events)}", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Will all users be authorized to see all events for a given robot? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would say that if the user is authorized to post events (which I believe should have a "higher" access level than just read events) they will be allowed to see all events. |
||
) | ||
event_id: int = create_event( | ||
db, | ||
event_request.robot_id, | ||
event_request.mission_id, | ||
event_request.start_time, | ||
DEFAULT_EVENT_DURATION, | ||
) | ||
db_event: EventDBModel = read_event_by_id(db, event_id) | ||
event: Event = db_event.get_api_event() | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will we always use the default duration? Maybe the user should have an event_duration optional field to this post endpoint so they can define it themselves?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Currently a default value is enough in my opinion. I don't think the user should be able to set the event_duration, but rather be based on the number of tags and/or based on data from previous missions with the same id. Can make a card for setting the event_duration based on number of tags.