-
Notifications
You must be signed in to change notification settings - Fork 2
Yiran li/feature/get unavailability #178
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
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
2bdddc6
Creating a new endpoint as GET http method to get all the unavailabil…
d6bb446
remove comments
40cc8b9
Creating a new endpoint as GET http method to get all the unavailabil…
5e39923
modified the message
9c6ac09
wrap in EventRepository class and inject it through the constructor o…
a65c6fd
Resolve merge conflict in controllers/v2/unavailability/api.py
5b9c6d4
add delete and create function in EventRepository class
0b7284a
fix some error
021fbe1
modified to pass EventRepository in through the constructor
2a8c53f
Merge branch 'main' of https://github.com/TechlauncherFireApp/backend…
c8a3625
Fix remaining issue by moving event repository instantiation to const…
emilymclean File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,115 @@ | ||
import logging | ||
|
||
from repository.unavailability_repository import * | ||
from flask import jsonify | ||
|
||
from datetime import datetime | ||
|
||
def edit_event(session, userId, eventId, title=None, start=None, end=None, periodicity=None): | ||
try: | ||
event = session.query(UnavailabilityTime).filter(UnavailabilityTime.eventId == eventId, | ||
UnavailabilityTime.userId == userId).first() | ||
if event is None: | ||
from domain import UnavailabilityTime, session_scope | ||
|
||
|
||
class EventRepository: | ||
def __init__(self): | ||
pass | ||
def edit_event(self, userId, eventId, title=None, start=None, end=None, periodicity=None): | ||
with session_scope() as session: | ||
try: | ||
event = session.query(UnavailabilityTime).filter(UnavailabilityTime.eventId == eventId, | ||
UnavailabilityTime.userId == userId).first() | ||
if event is None: | ||
return False | ||
if title is not None: | ||
event.title = title | ||
if start is not None: | ||
event.start = start | ||
if end is not None: | ||
event.end = end | ||
if end is not None: | ||
event.periodicity = periodicity | ||
session.commit() | ||
return True | ||
except Exception as e: | ||
session.rollback() | ||
logging.error(e) | ||
return None | ||
|
||
def get_event(self, userId): | ||
""" | ||
get all the non-availability events of the given user | ||
:param session: session | ||
:param userId: Integer, user id, who want to query the events | ||
""" | ||
now = datetime.now() | ||
with session_scope() as session: | ||
try: | ||
# only show the unavailability time that is end in the future | ||
events = session.query(UnavailabilityTime).filter( | ||
UnavailabilityTime.userId == userId, UnavailabilityTime.status == 1, UnavailabilityTime.end > now).all() | ||
if events: | ||
event_records = [] | ||
for event in events: | ||
# if the start time is earlier than now, then show from now to the end time | ||
start_time = max(event.start, now) | ||
event_record = { | ||
"eventId": event.eventId, | ||
"userId": event.userId, | ||
"title": event.title, | ||
"startTime": start_time.isoformat(), | ||
"endTime": event.end.isoformat(), | ||
"periodicity": event.periodicity | ||
} | ||
event_records.append(event_record) | ||
return jsonify(event_records) | ||
else: | ||
return None | ||
except Exception as e: | ||
logging.error(e) | ||
return None | ||
|
||
# copy from repository.unavailability_repository.py | ||
def create_event(self, userId, title, startTime, endTime, periodicity): | ||
""" | ||
Function to create an event | ||
:param session: session | ||
:param userId: Integer, user id | ||
:param title: String, reason why unavailable | ||
:param startTime: DateTime, from what time is unavailable | ||
:param endTime: DateTime, to what time is unavailable | ||
:param periodicity: Integer, Daily = 1, Weekly = 2, One-Off = 3 | ||
""" | ||
event = UnavailabilityTime(userId=userId, title=title, start=startTime, end=endTime, | ||
periodicity=periodicity) | ||
with session_scope() as session: | ||
session.add(event) | ||
# session.expunge(question) | ||
session.flush() | ||
return event.eventId | ||
|
||
# copy from repository.unavailability_repository.py | ||
def remove_event(self, userId, eventId): | ||
""" | ||
Function to remove an event | ||
:param session: session | ||
:param userId: Integer, user id, who want to remove an event | ||
:param eventId: Integer, event id want to remove | ||
:return: True: remove successful | ||
False: remove failed | ||
""" | ||
with session_scope() as session: | ||
existing = session.query(UnavailabilityTime).filter(UnavailabilityTime.userId == userId, | ||
UnavailabilityTime.eventId == eventId).first() | ||
if existing is not None and existing.status is True: | ||
existing.status = False | ||
return True | ||
return False | ||
if title is not None: | ||
event.title = title | ||
if start is not None: | ||
event.start = start | ||
if end is not None: | ||
event.end = end | ||
if end is not None: | ||
event.periodicity = periodicity | ||
session.commit() | ||
return True | ||
except Exception as e: | ||
session.rollback() | ||
logging.error(e) | ||
return None | ||
|
||
# copy from post function in api.py written by Steven | ||
def check_overlapping_events(self, userId, startTime, endTime, periodicity): | ||
with session_scope() as session: | ||
# checks if new time frame overlaps with any existing in the database for specific userId | ||
overlapping_events = session.query(UnavailabilityTime).filter( | ||
UnavailabilityTime.userId == userId, | ||
UnavailabilityTime.start < endTime, | ||
UnavailabilityTime.end > startTime, | ||
UnavailabilityTime.periodicity == periodicity | ||
).all() | ||
return overlapping_events |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.