Event-CRUD-Flask-python3-API/services/EventService.py

55 lines
1.6 KiB
Python
Raw Normal View History

from models import db, Event
from datetime import datetime
class EventService:
@staticmethod
def create_event(data):
new_event = Event(
title=data['title'],
description=data.get('description', ''),
location=data.get('location', ''),
2024-01-07 14:21:35 +00:00
duedate=datetime.strptime(data['duedate'], '%Y-%m-%dT%H:%M:%S'),
user_id=data['user_id']
)
db.session.add(new_event)
db.session.commit()
return new_event
@staticmethod
2024-01-07 14:21:35 +00:00
def get_all_user_events(user_id):
events=Event.query.filter_by(user_id=user_id, deleted=False).all()
if(events):
return [event.to_dict() for event in events]
else:
return []
@staticmethod
2024-01-07 14:21:35 +00:00
def get_event_by_id(event_id, user_id):
event = Event.query.filter_by(id=event_id,user_id=user_id, deleted=False).first()
if(event):
return event.to_dict()
else:
return []
2024-01-07 11:28:49 +00:00
@staticmethod
def update_event(event_id, data):
event = Event.query.get(event_id)
2024-01-07 14:32:08 +00:00
if not event:
return None
event.title = data['title']
event.description = data.get('description', '')
event.location = data.get('location', '')
event.time = datetime.strptime(data['duedate'], '%Y-%m-%dT%H:%M:%S')
db.session.commit()
return event
@staticmethod
def delete_event(event_id):
event = Event.query.filter_by(id=event_id, deleted=False).first()
if event:
event.deleted = True
db.session.commit()
else:
return None
return event