0

I'm new with NestJS so I'm having a hard time understanding the basics of putting together a proper application structure. I understand that everything in Nest is built on a modular system. But I do not know how to implement it correctly and maybe one of you will tell me how to do it correctly.

I am writing a Calculator application. On the client side, the user composes the "expression" string. Further, I send this expression as a request to the server, and on the server the expression must be calculated and the result returned.

@Controller('/calculator')
export class CalculatorController {
  @Post()
  getResult(@Body() expressionDto: ExpressionDto): CalculationResultDto {
    const { expression } = expressionDto;
    const result = getResult(expression); //here is the problem
    return { expression, result };
  }
}

The problem is that the getResult function is a bunch of big functions. All these functions are located in the utils folder. But this is the wrong approach in Nest, because all functionality should be presented in the form of modules. Can you please tell me how to make these functions correct? Where should they be stored and in what form?

1 Answer 1

1

It's not necessarily the wrong approach, having functions in utils. It's not common, but that doesn't mean it's "wrong".

What I would do is create a CalculatorService class that has this getResult method and all of the other methods that you end up calling as methods of the class itself, possibly private methods depending on if you need to call them from outside the class or not. Then you would have your controller inject the service and inside the route handler call this.calculatorService.getResult(expression).

The class could look something like

@Injectable()
export class CalculatorService {

  getResult(expression: string): number {
    let result = 0;
    const subExpressions = this.splitToSubExpressions(expression);
    ...
    // call all of the class methods here
    return result;
  }
}

And then your controller would look like this:

@Controller('/calculator')
export class CalculatorController {
  constructor(private readonly calculatorService: CalculatorService) {}
  @Post()
  getResult(@Body() expressionDto: ExpressionDto): CalculationResultDto {
    const { expression } = expressionDto;
    const result = this.calculatorService.getResult(expression);problem
    return { expression, result };
  }
}

And you'd have a module class that looks like this:

@Module({
  controllers: [CalculatorController],
  providers: [CalculatorService],
})
export class CalculatorModule {}
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you for help, Jay! Now the task seems clearer to me.Thank you for your time and the code example you provided.
what if getResult should call 4 to 5 big functions to get a result of calculation. Is it ok that CalculatorService will be a very big code file? Or maybe there is any way to keep it somehow so the CalculatorService looked not so over coded?
No one says you have to keep all the code in one class. Break it up logically if you need to. The CalculatorService injects the AdderService, SubtractorService, ParserService, MultipleService and DivisionService and delegates to them as necessary if you need

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.