Gets the name of a number, even if it's stupidly big. Supersedes TheoryOfNekomata/number-name.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

adder.controller.ts 1.8 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. import { RouteHandlerMethod } from 'fastify';
  2. import {
  3. AdderService,
  4. AdderServiceImpl,
  5. ArgumentOutOfRangeError,
  6. InvalidArgumentTypeError,
  7. } from '@/modules/adder/adder.service';
  8. import { constants } from 'http2';
  9. export interface AdderController {
  10. addNumbers: RouteHandlerMethod;
  11. subtractNumbers: RouteHandlerMethod;
  12. }
  13. export class AdderControllerImpl implements AdderController {
  14. constructor(
  15. private readonly adderService: AdderService = new AdderServiceImpl(),
  16. ) {
  17. // noop
  18. }
  19. readonly addNumbers: RouteHandlerMethod = async (request, reply) => {
  20. const { a, b } = request.body as { a: number; b: number };
  21. try {
  22. const response = this.adderService.addNumbers({ a, b });
  23. reply.send(response);
  24. } catch (errorRaw) {
  25. if (errorRaw instanceof InvalidArgumentTypeError) {
  26. request.log.info(errorRaw);
  27. reply.status(constants.HTTP_STATUS_BAD_REQUEST).send(errorRaw.message);
  28. return;
  29. }
  30. if (errorRaw instanceof ArgumentOutOfRangeError) {
  31. reply.status(constants.HTTP_STATUS_BAD_REQUEST).send(errorRaw.message);
  32. return;
  33. }
  34. const error = errorRaw as Error;
  35. reply.status(constants.HTTP_STATUS_INTERNAL_SERVER_ERROR).send(error.message);
  36. }
  37. }
  38. readonly subtractNumbers: RouteHandlerMethod = async (request, reply) => {
  39. const { a, b } = request.body as { a: number; b: number };
  40. try {
  41. const response = this.adderService.addNumbers({ a, b: -b });
  42. reply.send(response);
  43. } catch (errorRaw) {
  44. if (errorRaw instanceof InvalidArgumentTypeError) {
  45. reply.status(constants.HTTP_STATUS_BAD_REQUEST).send(errorRaw.message);
  46. return;
  47. }
  48. if (errorRaw instanceof ArgumentOutOfRangeError) {
  49. reply.status(constants.HTTP_STATUS_BAD_REQUEST).send(errorRaw.message);
  50. return;
  51. }
  52. const error = errorRaw as Error;
  53. reply.status(constants.HTTP_STATUS_INTERNAL_SERVER_ERROR).send(error.message);
  54. }
  55. }
  56. }