2024-01-08 10:56:45 +00:00
|
|
|
from sqlalchemy.sql import exists
|
2024-01-08 19:12:29 +00:00
|
|
|
from models import db, Event, UserEventAssociation, User
|
2024-01-08 10:56:45 +00:00
|
|
|
from services.UserService import UserService
|
2024-01-04 12:21:37 +00:00
|
|
|
from datetime import datetime
|
2024-01-08 10:56:45 +00:00
|
|
|
from flask import g
|
2024-01-08 13:28:00 +00:00
|
|
|
from sqlalchemy import func
|
2024-01-04 12:21:37 +00:00
|
|
|
|
|
|
|
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']
|
2024-01-04 12:21:37 +00:00
|
|
|
)
|
|
|
|
db.session.add(new_event)
|
|
|
|
db.session.commit()
|
|
|
|
return new_event
|
|
|
|
|
|
|
|
@staticmethod
|
2024-01-08 13:28:00 +00:00
|
|
|
def get_upcoming_events(location=None, sort_by=None):
|
|
|
|
query = Event.query.filter(
|
|
|
|
Event.deleted == False,
|
|
|
|
Event.duedate > datetime.now()
|
|
|
|
)
|
|
|
|
if location:
|
|
|
|
query = query.filter(Event.location.ilike(f"%{location}%"))
|
|
|
|
|
|
|
|
if sort_by == 'date':
|
|
|
|
query = query.order_by(Event.duedate)
|
|
|
|
elif sort_by == 'popularity':
|
2024-01-08 19:12:29 +00:00
|
|
|
query = query.join(UserEventAssociation, Event.id == UserEventAssociation.event_id)\
|
|
|
|
.group_by(Event.id)\
|
|
|
|
.order_by(func.count(UserEventAssociation.user_id).desc())
|
2024-01-08 13:28:00 +00:00
|
|
|
elif sort_by == 'creation':
|
|
|
|
query = query.order_by(Event.created_at)
|
|
|
|
|
|
|
|
events = query.all()
|
|
|
|
return [event.to_dict() for event in events]
|
2024-01-04 12:21:37 +00:00
|
|
|
|
|
|
|
@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
|
|
|
|
2024-01-04 12:21:37 +00:00
|
|
|
@staticmethod
|
|
|
|
def update_event(event_id, data):
|
2024-01-08 18:15:49 +00:00
|
|
|
event = Event.query.filter_by(user_id=g.user_id, id=event_id).first()
|
2024-01-07 14:32:08 +00:00
|
|
|
if not event:
|
|
|
|
return None
|
2024-01-04 12:21:37 +00:00
|
|
|
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):
|
2024-01-08 18:15:49 +00:00
|
|
|
event = Event.query.filter_by(id=event_id, user_id=g.user_id, deleted=False).first()
|
2024-01-04 12:21:37 +00:00
|
|
|
if event:
|
|
|
|
event.deleted = True
|
|
|
|
db.session.commit()
|
|
|
|
else:
|
|
|
|
return None
|
|
|
|
return event
|
2024-01-08 10:56:45 +00:00
|
|
|
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def attend_event(event_id):
|
2024-01-08 18:15:49 +00:00
|
|
|
user_id = g.user_id
|
2024-01-08 10:56:45 +00:00
|
|
|
event = Event.query.filter(
|
|
|
|
Event.id == event_id,
|
|
|
|
Event.duedate > datetime.now(),
|
|
|
|
Event.deleted == False
|
|
|
|
).first()
|
|
|
|
|
|
|
|
if not event:
|
|
|
|
return {'error': 'Event not found or already passed'}
|
|
|
|
|
|
|
|
# Check if the user is already associated with the event
|
2024-01-08 19:12:29 +00:00
|
|
|
is_already_attending = UserEventAssociation.query.filter_by(
|
|
|
|
user_id=user_id,
|
|
|
|
event_id=event_id
|
|
|
|
).first()
|
2024-01-08 10:56:45 +00:00
|
|
|
|
|
|
|
if is_already_attending:
|
|
|
|
return {'error': 'User already attending this event'}
|
|
|
|
|
|
|
|
# Add the user to the event
|
2024-01-08 19:12:29 +00:00
|
|
|
user_event_association = UserEventAssociation(
|
|
|
|
user_id=user_id,
|
|
|
|
event_id=event_id
|
|
|
|
)
|
|
|
|
db.session.add(user_event_association)
|
2024-01-08 10:56:45 +00:00
|
|
|
db.session.commit()
|
|
|
|
|
|
|
|
return {'message': 'User successfully added to the event'}
|
|
|
|
|
|
|
|
|
2024-01-08 19:12:29 +00:00
|
|
|
|
2024-01-08 10:56:45 +00:00
|
|
|
@staticmethod
|
|
|
|
def unattend_event(event_id):
|
|
|
|
user_id = g.user_id # Assuming user_id is stored in Flask's global g object
|
|
|
|
# Check if the event is valid and in the future
|
|
|
|
event = Event.query.filter(
|
|
|
|
Event.id == event_id,
|
|
|
|
Event.duedate > datetime.now(),
|
|
|
|
Event.deleted == False
|
|
|
|
).first()
|
|
|
|
|
|
|
|
if not event:
|
|
|
|
return {'error': 'Event not found or already passed'}
|
|
|
|
|
2024-01-08 19:12:29 +00:00
|
|
|
user_event_association = UserEventAssociation.query.filter_by(
|
|
|
|
user_id=user_id,
|
|
|
|
event_id=event_id
|
|
|
|
).first()
|
|
|
|
|
2024-01-08 10:56:45 +00:00
|
|
|
|
2024-01-08 19:12:29 +00:00
|
|
|
if not user_event_association:
|
2024-01-08 10:56:45 +00:00
|
|
|
return {'error': 'User not attending this event'}
|
|
|
|
|
|
|
|
user = UserService.get_user_by_id(user_id)
|
|
|
|
if not user:
|
|
|
|
return {'error': 'User not found'}
|
|
|
|
|
2024-01-08 19:12:29 +00:00
|
|
|
|
|
|
|
db.session.delete(user_event_association)
|
2024-01-08 10:56:45 +00:00
|
|
|
db.session.commit()
|
|
|
|
|
|
|
|
return {'message': 'User successfully removed from the event'}
|