Event-CRUD-Flask-python3-API/routes/eventRoutes.py

95 lines
3.2 KiB
Python
Raw Normal View History

2024-01-07 14:21:35 +00:00
from flask import Blueprint, jsonify, request, g
2024-01-07 11:28:49 +00:00
from services.EventService import EventService
2024-01-07 14:30:55 +00:00
from middlewares.userMiddlewares import authenticate_user
from middlewares.eventMiddlewares import validate_event_post_request
2024-01-07 11:28:49 +00:00
eventRoutes = Blueprint('eventRoutes', __name__)
# Create new event
2024-01-07 11:28:49 +00:00
@eventRoutes.route('/', methods=['POST'])
@eventRoutes.route('', methods=['POST'])
@validate_event_post_request
2024-01-07 11:42:48 +00:00
@authenticate_user
def create_event():
try:
data = request.json
2024-01-07 14:21:35 +00:00
data['user_id'] = g.user_id
print(data)
new_event = EventService.create_event(data)
if new_event:
return jsonify(new_event.to_dict()), 201
else:
return jsonify({'error': 'Failed to create event'}), 400
except Exception as e:
return jsonify({'error': str(e)}), 500
# Get All Events
2024-01-07 11:28:49 +00:00
@eventRoutes.route('/', methods=['GET'])
@eventRoutes.route('', methods=['GET'])
2024-01-07 11:42:48 +00:00
@authenticate_user
def get_events():
try:
user_events = EventService.get_upcoming_events(g.user_id)
2024-01-07 14:21:35 +00:00
return {"events": user_events, "count": len(user_events)}, 200
except Exception as e:
return {"error": str(e)}, 500
# Get Event by ID
2024-01-07 11:28:49 +00:00
@eventRoutes.route('/<int:event_id>', methods=['GET'])
2024-01-07 11:42:48 +00:00
@authenticate_user
def get_event(event_id):
try:
2024-01-07 14:21:35 +00:00
return {"event": EventService.get_event_by_id(event_id, g.user_id)}, 200
except Exception as e:
return {"error": str(e)}, 500
# Update Event
2024-01-07 11:28:49 +00:00
@eventRoutes.route('/<int:event_id>', methods=['PUT'])
@validate_event_post_request
2024-01-07 11:42:48 +00:00
@authenticate_user
def update_event(event_id):
try:
data = request.json
updated_event = EventService.update_event(event_id, data)
if updated_event:
return jsonify(updated_event.to_dict()), 200
else:
2024-01-07 14:32:08 +00:00
return jsonify({'error': 'Event not found'}), 404
except Exception as e:
return jsonify({'error': str(e)}), 500
# DELETE Event
2024-01-07 11:28:49 +00:00
@eventRoutes.route('/<int:event_id>', methods=['DELETE'])
2024-01-07 11:42:48 +00:00
@authenticate_user
def delete_event(event_id):
try:
deleted_event = EventService.delete_event(event_id)
if deleted_event:
return jsonify(deleted_event.to_dict()), 200
else:
2024-01-07 14:32:08 +00:00
return jsonify({'error': 'Event not found'}), 404
except Exception as e:
return jsonify({'error': str(e)}), 500
# User atended to event with id event_id and user must be loggedin
@eventRoutes.route('/<int:event_id>/attend', methods=['POST'])
@authenticate_user
def attend_event(event_id):
response = EventService.attend_event(event_id)
# Check if there's an error in the response
if 'error' in response:
return jsonify({'error': response['error']}), 400 # Or another appropriate status code
# If no error, return success response
return jsonify({'message': response['message']}), 200
@eventRoutes.route('/<int:event_id>/unattend', methods=['POST'])
@authenticate_user
def unattend_event(event_id):
response = EventService.unattend_event(event_id)
# Check if there's an error in the response
if 'error' in response:
return jsonify({'error': response['error']}), 400 # Or another appropriate status code
return jsonify({'message': response['message']}), 200