Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GitAuto: [FEATURE] Implement Brand API #342

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions src/api/brand.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
from flask import Blueprint, request, jsonify
from models import Brand
from auth import requires_auth

brand_api = Blueprint('brand_api', __name__)

@brand_api.route('/brands', methods=['GET'])
@requires_auth
def get_brands():
brands = Brand.query.all()
return jsonify([brand.to_dict() for brand in brands]), 200

@brand_api.route('/brands', methods=['POST'])
@requires_auth
def create_brand():
data = request.json
new_brand = Brand(**data)
new_brand.save()
return jsonify(new_brand.to_dict()), 201

@brand_api.route('/brands/<int:brand_id>', methods=['PUT'])
@requires_auth
def update_brand(brand_id):
data = request.json
brand = Brand.query.get_or_404(brand_id)
brand.update(**data)
return jsonify(brand.to_dict()), 200

@brand_api.route('/brands/<int:brand_id>', methods=['DELETE'])
@requires_auth
def delete_brand(brand_id):
brand = Brand.query.get_or_404(brand_id)
brand.delete()
return '', 204
14 changes: 14 additions & 0 deletions src/auth.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from functools import wraps
from flask import request, jsonify

def requires_auth(f):
@wraps(f)
def decorated(*args, **kwargs):
auth = request.headers.get('Authorization', None)
if not auth:
return jsonify({'message': 'Authorization header is expected'}), 401

# Here you would add your logic to verify the token
return f(*args, **kwargs)

return decorated
9 changes: 9 additions & 0 deletions src/database.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from flask_sqlalchemy import SQLAlchemy

# Initialize the database connection
db = SQLAlchemy()

def init_db(app):
db.init_app(app)
with app.app_context():
db.create_all()
26 changes: 26 additions & 0 deletions src/models/brand.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
from database import db

class Brand(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(80), nullable=False)
description = db.Column(db.String(200))

def to_dict(self):
return {
'id': self.id,
'name': self.name,
'description': self.description
}

def save(self):
db.session.add(self)
db.session.commit()

def update(self, **kwargs):
for key, value in kwargs.items():
setattr(self, key, value)
db.session.commit()

def delete(self):
db.session.delete(self)
db.session.commit()
Loading