def get(user_uid):
    user = User.get(user_uid)
    if not user:
        return custom_response({'error': 'user not found'}, 404)

    serialized_user = user_schema.dump(user).data
    return custom_response(serialized_user, 200)
def create():
    req_data = request.get_json()
    data, error = user_schema.load(req_data)

    if error:
        return custom_response(error, 400)

    user_in_db = User.get_by_email(data.get('email'))
    if user_in_db:
        message = {'error': 'User already exists'}
        return custom_response(message, 400)

    user = User(
        name=data.get('name'),
        email=data.get('email'),
    )

    user.set_password(password=data.get('password'))
    user.save()
    serialized_data = user_schema.dump(user).data
    token = Auth.generate_token(serialized_data.get('uid'))
    return custom_response(
        {
            'jwt_token': token,
            'uid': serialized_data.get('uid'),
        }, 201)
def update():
    req_data = request.get_json()
    data, error = user_schema.load(req_data, partial=True)
    if error:
        return custom_response(error, 400)

    user = User.get(g.user.get('uid'))
    user.update(data)
    serialized_user = user_schema.dump(user).data
    return custom_response(serialized_user, 200)
def delete(comment_id):
  comment = Comment.get(comment_id)
  if not comment:
    return custom_response({'error': 'comment not found'}, 404)
  data = comment_schema.dump(comment).data
  if data.get('owner_id') != g.user.get('uid'):
    return custom_response({'error': 'permission denied'}, 400)

  comment.delete()
  return custom_response({'message': 'deleted'}, 204)
def update(comment_id):
  req_data = request.get_json()
  comment = Comment.get(comment_id)
  if not comment:
    return custom_response({'error': 'comment not found'}, 404)
  data = comment_schema.dump(comment).data
  if data.get('owner_id') != g.user.get('uid'):
    return custom_response({'error': 'permission denied'}, 400)

  data, error = comment_schema.load(req_data, partial=True)
  if error:
    return custom_response(error, 400)
  comment.update(data)

  data = comment_schema.dump(comment).data
  return custom_response(data, 200)
def get_one(article_external_id):
    article = Article.get_by_external_id(article_external_id)
    if not article:
        article = Article(external_id=article_external_id)
        article.save()

    data = article_schema.dump(article).data
    return custom_response(data, 200)
def login():
    req_data = request.get_json()

    data, error = user_schema.load(req_data, partial=True)
    if error:
        return custom_response(error, 400)
    if not data.get('email') or not data.get('password'):
        return custom_response(
            {'error': 'you need email and password to sign in'}, 400)
    user = User.get_by_email(data.get('email'))
    if not user:
        return custom_response({'error': 'invalid credentials'}, 400)
    if not user.check_password(data.get('password')):
        return custom_response({'error': 'invalid credentials'}, 400)
    serialized_data = user_schema.dump(user).data
    token = Auth.generate_token(serialized_data.get('uid'))
    return custom_response(
        {
            'jwt_token': token,
            'uid': serialized_data.get('uid'),
        }, 200)
def vote(comment_id):
  comment = Comment.get(comment_id)
  if not comment:
    return custom_response({'error': 'comment not found'}, 404)

  voter_id = g.user.get('uid')
  voter = User.get(voter_id)

  vote_type = request.get_json().get('type')
  valid_vote_types = ['like', 'dislike', 'meh']

  if not vote_type or vote_type not in valid_vote_types :
    return custom_response({'error': 'invalid vote'}, 404)

  existing_vote = comment.voters.relationship(voter)
  if existing_vote:
    existing_vote.type = vote_type
    existing_vote.save()
  else:
    comment.voters.connect(voter, {'type': vote_type})

  data = comment_schema.dump(comment).data
  return custom_response(data, 201)
def create():
  req_data = request.get_json()
  data, error = comment_schema.load(req_data)
  if error:
    return custom_response(error, 400)

  # Create and save comment
  comment = Comment(contents=data['contents'])
  comment.save()

  # Connect owner
  owner_id = g.user.get('uid')
  owner = User.get(owner_id)
  comment.owner.connect(owner)

  article_external_id = data['article_external_id']
  article = Article.get_by_external_id(article_external_id)
  if not article:
    article = Article(external_id=article_external_id)
    article.save()
  comment.article.connect(article)

  data = comment_schema.dump(comment).data
  return custom_response(data, 201)
def get_me():
    user = User.get(g.user.get('uid'))
    serialized_user = user_schema.dump(user).data
    return custom_response(serialized_user, 200)
def delete():
    user = User.get(g.user.get('uid'))
    user.delete()
    return custom_response({'message': 'deleted'}, 204)
def get_all():
    users = User.get_all()
    serialized_users = user_schema.dump(users, many=True).data
    return custom_response(serialized_users, 200)
def get_one(comment_id):
  comment = Comment.get(comment_id)
  if not comment:
    return custom_response({'error': 'comment not found'}, 404)
  data = comment_schema.dump(comment).data
  return custom_response(data, 200)
def get_all():
  comments = Comment.get_all()
  data = comment_schema.dump(comments, many=True).data
  return custom_response(data, 200)