134 lines
3.9 KiB
Python
134 lines
3.9 KiB
Python
from flask import Blueprint, request, jsonify
|
|
from flask_jwt_extended import jwt_required
|
|
from flasgger.utils import swag_from
|
|
from models.reports import Employee
|
|
from models.user import db,User
|
|
|
|
employee_bp = Blueprint('employee', __name__, url_prefix='/employee')
|
|
|
|
@employee_bp.route('/', methods=['POST'])
|
|
@jwt_required()
|
|
@swag_from('../docs/reports/create.yml')
|
|
def create_employee():
|
|
name = data.get('name')
|
|
group = data.get('group')
|
|
contract = data.get('contract')
|
|
data = request.get_json()
|
|
entry = data.get('entry')
|
|
exit = data.get('exit')
|
|
status = data.get('status')
|
|
|
|
# Cria um novo ServiceRole
|
|
employee = Employee(
|
|
id = 1,
|
|
name = name,
|
|
group = group,
|
|
contract = contract,
|
|
data = data,
|
|
entry = entry,
|
|
exit = exit,
|
|
status = status
|
|
)
|
|
|
|
# Adiciona e comita no banco de dados
|
|
db.session.add(employee)
|
|
db.session.commit()
|
|
|
|
return jsonify({'message': 'employee criado com sucesso', 'employee_id': employee.id}), 201
|
|
|
|
@employee_bp.route('/', methods=['GET'])
|
|
@jwt_required()
|
|
@swag_from('../docs/reports/list.yml')
|
|
def list_employee():
|
|
page = request.args.get('page', 1, type=int)
|
|
per_page = request.args.get('per_page', 10, type=int)
|
|
|
|
pagination = Employee.query.paginate(page=page, per_page=per_page, error_out=False)
|
|
employee = pagination.items
|
|
|
|
return jsonify({
|
|
'employee': [
|
|
{
|
|
'id': employee.service_id,
|
|
'name': employee.name,
|
|
'group': employee.group,
|
|
'contract': employee.contract,
|
|
'data': employee.data,
|
|
'entry': employee.entry,
|
|
'exit': employee.exit,
|
|
'status': employee.status
|
|
|
|
} for employee in employee
|
|
],
|
|
'total': pagination.total,
|
|
'page': pagination.page,
|
|
'per_page': pagination.per_page
|
|
}), 200
|
|
|
|
@employee_bp.route('/<int:employee_id>', methods=['GET'])
|
|
@jwt_required()
|
|
@swag_from('../docs/reports/get.yml')
|
|
def get_employee(employee_id):
|
|
employee = Employee.query.get(employee_id)
|
|
if not employee:
|
|
return jsonify({'error': 'Relatório não encontrada'}), 404
|
|
|
|
return jsonify({
|
|
'id': employee.service_id,
|
|
'name': employee.name,
|
|
'group': employee.group,
|
|
'contract': employee.contract,
|
|
'data': employee.data,
|
|
'entry': employee.entry,
|
|
'exit': employee.exit,
|
|
'status': employee.status
|
|
}), 200
|
|
|
|
@employee_bp.route('/<int:employee_id>', methods=['PUT'])
|
|
@jwt_required()
|
|
@swag_from('../docs/reports/update.yml')
|
|
def update_employee(employee_id):
|
|
name = data.get('name')
|
|
group = data.get('group')
|
|
contract = data.get('contract')
|
|
data = request.get_json()
|
|
entry = data.get('entry')
|
|
exit = data.get('exit')
|
|
status = data.get('status')
|
|
|
|
employee = Employee.query.get(employee_id)
|
|
if not employee:
|
|
return jsonify({'error': 'Relatório não encontrado'}), 404
|
|
|
|
# Atualiza os campos fornecidos
|
|
if name:
|
|
employee.name = name
|
|
if group:
|
|
employee.group = group
|
|
if contract:
|
|
employee.contract = contract
|
|
if data:
|
|
employee.data = data
|
|
if entry:
|
|
employee.entry = entry
|
|
if exit:
|
|
employee.name = exit
|
|
if status:
|
|
employee.name = status
|
|
|
|
db.session.commit()
|
|
|
|
return jsonify({'message': 'Relatório atualizado com sucesso', 'employee_id': employee.id}), 200
|
|
|
|
@employee_bp.route('/<int:employee_id>', methods=['DELETE'])
|
|
@jwt_required()
|
|
@swag_from('../docs/reports/delete.yml')
|
|
def delete_employee(employee_id):
|
|
employee = Employee.query.get(employee_id)
|
|
if not employee:
|
|
return jsonify({'error': 'Relatório não encontrado'}), 404
|
|
|
|
db.session.delete(employee)
|
|
db.session.commit()
|
|
|
|
return jsonify({'message': 'Relatório deletado com sucesso'}), 200 |