front_ponto_eletronico/routes/time_schedules.py
2025-04-16 19:48:09 -03:00

113 lines
4.0 KiB
Python

from flask import Blueprint, request, jsonify
from extensions import db
from flasgger.utils import swag_from
from models.time_schedule import TimeSchedule
# Blueprint para TimeSchedule
time_schedule_bp = Blueprint('time_schedule', __name__, url_prefix='/time_schedules')
# Criar um novo time schedule
@time_schedule_bp.route('/', methods=['POST'])
@swag_from('../docs/time_schedule/create.yml')
def create_time_schedule():
data = request.json
# Validar dados obrigatórios
if not all(key in data for key in ['shift_id', 'start_time', 'end_time']):
return jsonify({"error": "Os campos 'shift_id', 'start_time' e 'end_time' são obrigatórios"}), 400
try:
new_time_schedule = TimeSchedule(
shift_id=data['shift_id'],
start_time=data['start_time'],
end_time=data['end_time'],
description=data.get('description') # Opcional
)
db.session.add(new_time_schedule)
db.session.commit()
return jsonify({"message": "Time schedule criado com sucesso", "data": new_time_schedule.id}), 201
except Exception as e:
db.session.rollback()
return jsonify({"error": str(e)}), 500
@time_schedule_bp.route('/', methods=['GET'])
@swag_from('../docs/time_schedule/list.yml')
def list_time_schedules():
# Obter o shift_id da query string, se fornecido
shift_id = request.args.get('shift_id', type=int)
if shift_id:
# Filtrar pelo shift_id
time_schedules = TimeSchedule.query.filter_by(shift_id=shift_id).all()
else:
# Caso shift_id não seja fornecido, retorna todos os time schedules
time_schedules = TimeSchedule.query.all()
result = [{
"id": ts.id,
"shift_id": ts.shift_id,
"start_time": str(ts.start_time),
"end_time": str(ts.end_time),
"description": ts.description
} for ts in time_schedules]
return jsonify(result), 200
# Obter um time schedule específico
@time_schedule_bp.route('/<int:id>', methods=['GET'])
@swag_from('../docs/time_schedule/retrieve.yml')
def get_time_schedule(id):
time_schedule = TimeSchedule.query.get(id)
if not time_schedule:
return jsonify({"error": "Time schedule não encontrado"}), 404
return jsonify({
"id": time_schedule.id,
"shift_id": time_schedule.shift_id,
"start_time": str(time_schedule.start_time),
"end_time": str(time_schedule.end_time),
"description": time_schedule.description
}), 200
# Atualizar um time schedule existente
@time_schedule_bp.route('/<int:id>', methods=['PUT'])
@swag_from('../docs/time_schedule/update.yml')
def update_time_schedule(id):
time_schedule = TimeSchedule.query.get(id)
if not time_schedule:
return jsonify({"error": "Time schedule não encontrado"}), 404
data = request.json
try:
if 'shift_id' in data:
time_schedule.shift_id = data['shift_id']
if 'start_time' in data:
time_schedule.start_time = data['start_time']
if 'end_time' in data:
time_schedule.end_time = data['end_time']
if 'description' in data:
time_schedule.description = data['description']
db.session.commit()
return jsonify({"message": "Time schedule atualizado com sucesso"}), 200
except Exception as e:
db.session.rollback()
return jsonify({"error": str(e)}), 500
# Deletar um time schedule
@time_schedule_bp.route('/<int:id>', methods=['DELETE'])
@swag_from('../docs/time_schedule/delete.yml')
def delete_time_schedule(id):
time_schedule = TimeSchedule.query.get(id)
if not time_schedule:
return jsonify({"error": "Time schedule não encontrado"}), 404
try:
db.session.delete(time_schedule)
db.session.commit()
return jsonify({"message": "Time schedule deletado com sucesso"}), 200
except Exception as e:
db.session.rollback()
return jsonify({"error": str(e)}), 500