<reponame>Group-13-Bachelor/Microservice
from flask import request, jsonify, Response
from UserService import app, db
from UserService.model import User
from flask_bcrypt import Bcrypt


@app.route("/UserRegistered", methods=['POST'])
def register_user():
	user_json = request.get_json()
	user = User(
		username=user_json["username"],
		email=user_json["email"],
		image_file=user_json["image_file"],
		password=user_json["password"])
	db.session.add(user)
	db.session.commit()

	return jsonify(
		id=user.id,
		username=user.username,
		email=user.email,
		image_file=user.image_file,
		password=<PASSWORD>
	)


@app.route("/get_user", methods=['GET'])
def get_user():
	body = request.get_json()
	user = None
	if "username" in body:
		user = User.query.filter_by(username=body["username"]).first()
	elif "id" in body:
		user = User.query.filter_by(id=body["id"]).first()
	elif "email" in body:
		user = User.query.filter_by(email=body["email"]).first()

	if user is not None:
		return jsonify(
			id=user.id,
			username=user.username,
			email=user.email,
			image_file=user.image_file,
			password=<PASSWORD>)
	else:
		return Response(status=404)


@app.route("/UserUpdated", methods=['POST'])
def UserUpdated():
	user_json = request.get_json()
	user = User.query.filter_by(id=user_json["id"]).first()

	user.username = user_json["username"]
	user.email = user_json["email"]

	db.session.commit()

	return Response(status=200)


@app.route("/print")
def print_all_users():
	users = []
	for user in User.query.all():
		users.append(user.__str__())
	return jsonify(users)


def init_db():
	with app.app_context():
		db.drop_all()
		db.create_all()
		db.session.commit()

	bcrypt = Bcrypt()

	users = [
		User(username="user1", email="<EMAIL>", password=bcrypt.generate_password_hash("<PASSWORD>").decode('utf-8')),
		User(username="user2", email="<EMAIL>", password=bcrypt.generate_password_hash("<PASSWORD>").decode('utf-8')),
		User(username="user3", email="<EMAIL>", password=bcrypt.generate_password_hash("<PASSWORD>").decode('utf-8'))
	]
	for user in users:
		db.session.add(user)
	db.session.commit()
	print("DB initialized")
	print(User.query.all())


if __name__ == '__main__':
	init_db()
	app.run(host='0.0.0.0', port=5003, debug=True)

from flask import request, jsonify, Response
from UserService import app, db
from UserService.model import User
from flask_bcrypt import Bcrypt


@app.route("/UserRegistered", methods=['POST'])
def register_user():
	user_json = request.get_json()
	user = User(
		username=user_json["username"],
		email=user_json["email"],
		image_file=user_json["image_file"],
		password=user_json["password"])
	db.session.add(user)
	db.session.commit()

	return jsonify(
		id=user.id,
		username=user.username,
		email=user.email,
		image_file=user.image_file,
		password=<PASSWORD>
	)


@app.route("/get_user", methods=['GET'])
def get_user():
	body = request.get_json()
	user = None
	if "username" in body:
		user = User.query.filter_by(username=body["username"]).first()
	elif "id" in body:
		user = User.query.filter_by(id=body["id"]).first()
	elif "email" in body:
		user = User.query.filter_by(email=body["email"]).first()

	if user is not None:
		return jsonify(
			id=user.id,
			username=user.username,
			email=user.email,
			image_file=user.image_file,
			password=<PASSWORD>)
	else:
		return Response(status=404)


@app.route("/UserUpdated", methods=['POST'])
def UserUpdated():
	user_json = request.get_json()
	user = User.query.filter_by(id=user_json["id"]).first()

	user.username = user_json["username"]
	user.email = user_json["email"]

	db.session.commit()

	return Response(status=200)


@app.route("/print")
def print_all_users():
	users = []
	for user in User.query.all():
		users.append(user.__str__())
	return jsonify(users)


def init_db():
	with app.app_context():
		db.drop_all()
		db.create_all()
		db.session.commit()

	bcrypt = Bcrypt()

	users = [
		User(username="user1", email="<EMAIL>", password=bcrypt.generate_password_hash("<PASSWORD>").decode('utf-8')),
		User(username="user2", email="<EMAIL>", password=bcrypt.generate_password_hash("<PASSWORD>").decode('utf-8')),
		User(username="user3", email="<EMAIL>", password=bcrypt.generate_password_hash("<PASSWORD>").decode('utf-8'))
	]
	for user in users:
		db.session.add(user)
	db.session.commit()
	print("DB initialized")
	print(User.query.all())


if __name__ == '__main__':
	init_db()
	app.run(host='0.0.0.0', port=5003, debug=True)