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

43 lines
1.4 KiB
Python

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', ''),
duedate=datetime.strptime(data['duedate'], '%Y-%m-%dT%H:%M:%S')
)
db.session.add(new_event)
db.session.commit()
return new_event
@staticmethod
def get_all_events():
events=Event.query.filter_by(deleted=False).all()
return [event.to_dict() for event in events]
@staticmethod
def get_event_by_id(event_id):
return Event.query.filter_by(id=event_id, deleted=False).first().to_dict()
@staticmethod
def update_event(event_id, data):
event = Event.query.get(event_id)
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