Created
September 12, 2023 03:20
-
-
Save emezac/6bf321c246d9b4357211ef4465ca9d62 to your computer and use it in GitHub Desktop.
messages_controller.rb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Detalles adicionales: | |
| Namespace api/v1 | |
| Rutas RESTful anidadas para bots y messages | |
| Controlador de Messages para las acciones sobre mensajes | |
| Servicio MessagesService para encapsular lógica | |
| Autenticación y autorización | |
| Paginación, búsqueda y manejo de errores mejorados | |
| Respuestas JSON con status codes apropiados | |
| Acciones index, show, create | |
| Queda pendiente: | |
| Tests unitarios y de integración | |
| Documentación de la API | |
| Caching y optimizaciones |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # app/controllers/api/v1/messages_controller.rb | |
| class Api::V1::MessagesController < ApplicationController | |
| before_action :set_bot | |
| before_action :authenticate_user! | |
| before_action :authorize_user | |
| # GET /bots/:bot_id/messages | |
| def index | |
| @messages = MessagesService.new(@bot).paginate(params[:page], params[:per_page]) | |
| render json: @messages | |
| end | |
| # POST /bots/:bot_id/messages | |
| def create | |
| @message = MessagesService.new(@bot).create(message_params) | |
| if @message.persisted? | |
| render json: @message, status: :created | |
| else | |
| render json: { errors: @message.errors.full_messages }, status: :unprocessable_entity | |
| end | |
| end | |
| # GET /bots/:bot_id/messages/:id | |
| def show | |
| @message = Message.find(params[:id]) | |
| if @message | |
| render json: @message | |
| else | |
| render json: { error: 'Message not found' }, status: :not_found | |
| end | |
| end | |
| private | |
| def set_bot | |
| @bot = Bot.find(params[:bot_id]) | |
| end | |
| def authorize_user | |
| # Aquí iría la lógica para autorizar al usuario | |
| # por ejemplo verificar que es owner del bot | |
| end | |
| def message_params | |
| params.require(:message).permit(:content, :role) | |
| end | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # app/services/messages_service.rb | |
| class MessagesService | |
| # pagina los mensajes | |
| def paginate(page, per_page) | |
| Message.where(bot: @bot).page(page).per(per_page) | |
| end | |
| # crea un nuevo mensaje | |
| def create(params) | |
| Message.create!(params.merge(bot: @bot)) | |
| end | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # config/routes.rb | |
| Rails.application.routes.draw do | |
| namespace :api do | |
| namespace :v1 do | |
| resources :bots do | |
| resources :messages | |
| end | |
| end | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment