Implementing the Story Generator Controller

Introduction

Welcome to this lesson on building a short story generation service. In this lesson, we will focus on creating the Story Generator Controller, a crucial component that manages requests and responses within our application. The controller acts as a bridge between user input and the story generation logic, ensuring smooth communication among different parts of the application. By the end of this lesson, you will understand how to implement and test the controller, completing the core functionality of our service.

Before we dive into the details of the controller, let's briefly recall how the StoryGeneratorService and StoryManager are used in our application. These components are responsible for generating stories and managing them, respectively. The controller will interact with these components to process user requests and return appropriate responses. Remember, the controller is the part of the application that handles incoming requests, calls the necessary services, and returns the results to the client.

Understanding the StoryGeneratorController Class

Let's start by understanding the StoryGeneratorController class. This class is responsible for handling story-related requests. Here's how we define it:

from fastapi.responses import JSONResponse
from services.story_generator_service import StoryGeneratorService

class StoryGeneratorController:
    def __init__(self):
        self.story_generator_service = StoryGeneratorService()
  • We import JSONResponse to format our responses as JSON, which is a common data format for applications.
  • We also import the StoryGeneratorService, which we will use to generate stories.
  • The __init__ method initializes the controller by creating an instance of StoryGeneratorService. This allows the controller to access the story generation logic.

Implementing the generate_story Method

Next, let's implement the generate_story method inside the StoryGeneratorController class, which handles the generation of a new story based on user input.

class StoryGeneratorController:
        
    def generate_story(self, user_input: str, tone: str):
        if not user_input:
            return JSONResponse(content={"error": "Missing input"}, status_code=400)
        try:
            story = self.story_generator_service.generate_story(user_input, tone)
            return {"story": story}
        except Exception as e:
            return JSONResponse(content={"error": str(e)}, status_code=500)
  • The method takes user_input and tone as parameters. These inputs are essential for generating a story.
  • We first check if the user_input is provided. If not, we return an error message with a 400 status code, indicating a bad request.
  • If the input is valid, we call the generate_story method from the StoryGeneratorService to generate a story.
  • We handle any exceptions that might occur during story generation and return an error message with a 500 status code, indicating a server error.
Sign up

Join the 1M+ learners on CodeSignal

Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal