def update_tickets(ticket_id): event_id = request.json.get("event_id", None) price = request.json.get("price", None) quantity = request.json.get("quantity", None) try: ticket = Tickets.query.filter_by(id=ticket_id).first() except Exception as e: print(e) db.session.rollback() raise InternalServerError("Failed to fetch ticket") if not ticket: raise NotFound(f"Ticket with id {ticket_id} not found") try: if price: ticket.price = price if quantity: ticket.available = ticket.available + quantity ticket.updated_at = datetime.utcnow() Tickets.update(ticket) except Exception as e: print(e) db.session.rollback() raise InternalServerError("Problem updating tickets") return jsonify({ "success": True, "data": ticket.serialize })
def update_event_type(type_id): name = request.json.get("name") description = request.json.get("description") image = request.json.get("image") if not name or not description: raise BadRequest("Invalid body provided") try: event_type = EventType.query.filter_by(id=type_id).first() except Exception as e: print(e) raise InternalServerError( "Internal Server Error! Could not retrieve events.") if not event_type: raise NotFound(f"Event with Id = {type_id} not found") else: event_type.name = name event_type.description = description event_type.image = image EventType.update(event_type) return jsonify( { "success": True, "data": event_type.serialize })
def login(): email = request.json.get("email", None) password = request.json.get("password", None) if not login or not password: raise BadRequest("Missing login or password") try: user = Users.query.filter_by(email=email).first() except Exception as e: print(e) raise InternalServerError("Problem retrieving user") if not user: raise NotFound(f"User with email {email} not found") if not check_password_hash(user.password_hash, password): raise UnAuthorized("invalid password", 401) else: user_id = user.serialize.get("id") access_token = create_access_token(identity=user_id, expires_delta=timedelta(hours=24)) return jsonify({ "success": True, "data": { "access_token": access_token, "user_id": user_id } })
def new_event(): title = request.json.get("title", None) description = request.json.get("description", None) event_datetime = request.json.get("start_datetime", None) event_location = request.json.get("location", None) image_url = request.json.get("image_url", None) event_type_id = request.json.get("event_type_id", None) organizer_id = request.json.get("organizer_id", None) if not title or not description or not event_datetime \ or not event_location or not event_type_id: raise BadRequest("Invalid body parameters") try: new_event = Events(title=title, description=description, start_date_time=event_datetime, event_location=event_location, image=image_url, organizer_id=organizer_id, event_type_id=event_type_id) Events.insert(new_event) return jsonify({"success": True, "data": new_event.serialize}) except Exception as e: print(e) db.session.rollback() raise InternalServerError(f"Something went wrong on server")
def update_user_info(user_id): firstname = request.json.get("firstname", None) lastname = request.json.get("lastname", None) email = request.json.get("email", None) phone = request.json.get("phone", None) password = request.json.get("password", None) user = Users.query.filter_by(id=user_id).first() if not user: raise NotFound(f"Could not find user with id {user_id}") if firstname: user.first_name = firstname if lastname: user.last_name = lastname if email: user.email = email if phone: user.phone = phone if password: hashed_password = generate_password_hash(password).decode('utf-8') user.password_hash = hashed_password user.updated_at = datetime.utcnow() try: Users.update(user) except Exception as e: print(e) raise InternalServerError("Problem retrieving user") return jsonify({ "success": True, "data": user.serialize })
def update_event_info(event_id): title = request.json.get("title") description = request.json.get("description") event_datetime = request.json.get("start_datetime") event_location = request.json.get("location") attendance_price = request.json.get("price") event_type_id = request.json.get("event_type_id") image_url = request.json.get("image_url") if not title or not description or not event_datetime \ or not event_location or not event_type_id: raise BadRequest("Invalid body parameters") try: event = Events.query.filter_by(id=event_id).first() except Exception as e: print(e) raise InternalServerError( "Internal Server Error! Could not retrieve events.") if not event: raise NotFound(f"Event with Id = {event_id} not found") else: event.title = title event.description = description event.start_date_time = event_datetime event.location = event_location event.price = attendance_price event.event_type_id = event_type_id event.image = image_url Events.update(event) return jsonify({"success": True, "data": event.serialize})
def get_tickets(): try: tickets = Tickets.query.all() return jsonify({ "success": True, "data": [ticket.serialize for ticket in tickets] }) except Exception as e: raise InternalServerError("Failed to fetch tickets")
def get_all_users(): try: users = Users.query.all() except Exception as e: print(e) raise InternalServerError({"error": "Server Error! Could not retrieve users."}) return jsonify({"success": True, "data": [user.serialize for user in users]})
def retrieve_single_event(event_id): try: event = Events.query.filter_by(id=event_id).first() except Exception as e: print(e) raise InternalServerError( "Internal Server Error! Could not retrieve events.") if not event: raise NotFound(f"Event with Id = {event_id} not found") else: return jsonify({"success": True, "data": event.serialize})
def retrieve_all_events(): try: events = Events.query.all() return jsonify({ "success": True, "data": [event.serialize for event in events] }) except Exception as e: print(e) raise InternalServerError( "Internal Server Error! Could not retrieve events.")
def get_ticket(ticket_id): try: ticket = Tickets.query.filter_by(id=ticket_id).first() except Exception as e: print(e) db.session.rollback() raise InternalServerError("Failed to fetch ticket") if not ticket: raise NotFound(f"Ticket with id {ticket_id} not found") else: return jsonify(ticket.serialize)
def get_user(user_id): try: user = Users.query.filter_by(id=user_id).first() except Exception as e: print(e) raise InternalServerError("Problem retrieving user") if not user: raise NotFound(f"Could not find user with id {user_id}") return jsonify({ "success": True, "data": user.serialize })
def retrieve_all_events_types(): try: event_types = EventType.query.all() except Exception as e: print(e) raise InternalServerError( f"Internal Server Error! Could not retrieve users.") return jsonify( { "success": True, "data": [event_type.serialize for event_type in event_types] } )
def delete_event(event_id): try: event = Events.query.filter_by(id=event_id).first() except Exception as e: print(e) raise InternalServerError( "Internal Server Error! Could not retrieve events.") if not event: raise NotFound(f"Event with Id {event_id} not found") else: Events.delete(event) return jsonify({ "success": True, "deleted": event_id, }), 200
def get_event_type(type_id): try: event_type = EventType.query.filter_by(id=type_id).first() except Exception as e: print(e) raise InternalServerError( "Internal Server Error! Could not retrieve events types.") if not event_type: raise NotFound(f"Event with Id {type_id} not found") else: return jsonify( { "success": True, "data": event_type.serialize } )
def new_event_type(): name = request.json.get("name") description = request.json.get("description") image = request.json.get("image") if not name or not description: raise BadRequest("Invalid body provided") try: new_event_type = EventType(name=name, description=description, image=image) EventType.insert(new_event_type) return jsonify(new_event_type.serialize) except Exception as e: print(e) db.session.rollback() raise InternalServerError("Could not create event type")
def new_user(): if request.method != 'POST': return jsonify({"error": "Method not allowed!"}) firstname = request.json.get("firstname", None) lastname = request.json.get("lastname", None) email = request.json.get("email", None) phone = request.json.get("phone", None) password = request.json.get("password", None) if not email or not password: raise BadRequest("Provide email and password") user_exist = Users.query.filter_by(email=email).first() if user_exist: raise ExistingResource({"error": f"""User with email {email} and number {phone} exist!"""}) else: hashed_password = generate_password_hash(password).decode('utf-8') user = Users(first_name=firstname, last_name=lastname, email=email, phone=phone, password_hash=hashed_password) try: Users.insert(user) except Exception as e: print(e) db.session.rollback() raise InternalServerError({"error": "Database commit error.\ Could not process your request!"}) access_token = create_access_token(identity=user.id, expires_delta=timedelta(hours=24)) return jsonify({"success": True, "data": { "user": user.serialize, "access_token": access_token } }), 201
def new_ticket(): event_id = request.json.get("event_id", None) price = request.json.get("price", None) quantity = request.json.get("quantity", None) if not event_id or not quantity or not price: raise BadRequest("Provide valid request body") try: new_ticket = Tickets(event_id=event_id, available=quantity, price=price) Tickets.insert(new_ticket) return jsonify({ "success": True, "data": new_ticket.serialize }) except Exception as e: print(e) db.session.rollback() raise InternalServerError("Could not create new event ticket")