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 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_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 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 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 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 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 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 } )