Skip to content

Commit

Permalink
Implement accounts API
Browse files Browse the repository at this point in the history
  • Loading branch information
S3L1M committed Jul 9, 2023
1 parent c3430c4 commit a66671f
Showing 1 changed file with 58 additions and 0 deletions.
58 changes: 58 additions & 0 deletions accounts-service/app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
from flask import Flask, jsonify, request
from flask_sqlalchemy import SQLAlchemy

app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'postgresql://postgres:password@db1:5432/accounts_db'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db = SQLAlchemy(app)

# User model
class User(db.Model):
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(50), unique=True, nullable=False)
email = db.Column(db.String(100), unique=True, nullable=False)
password = db.Column(db.String(100), unique=False, nullable=False)

def __init__(self, username, email, password):
self.username = username
self.email = email
self.password = password

with app.app_context():
db.create_all()

# Routes
@app.route('/users', methods=['POST'])
def create_user():
data = request.get_json()
username = data.get('username')
email = data.get('email')
password = data.get('password')

if not username or not email:
return jsonify(message='Username and email are required.'), 400

new_user = User(username=username, email=email, password=password)
db.session.add(new_user)
db.session.commit()

return jsonify(message='User created successfully.'), 201


@app.route('/users/<username>', methods=['GET'])
def get_user(username):
user = User.query.filter_by(username=username).first()

if user:
user_data = {
'id': user.id,
'username': user.username,
'email': user.email
}
return jsonify(user_data)
else:
return jsonify({'message': 'User not found'}), 404


if __name__ == '__main__':
app.run(host='0.0.0.0', port=5000)

0 comments on commit a66671f

Please sign in to comment.