rnegron/cc-api

View on GitHub
src/controllers/theatre.ts

Summary

Maintainability
B
5 hrs
Test Coverage
import * as Hapi from '@hapi/hapi';
import * as Boom from '@hapi/boom';
import Theatre from '../models/theatre';

export default async (request: Hapi.Request) => {
  const theatreSlug = request.params.theatreSlug;

  if (!theatreSlug) {
    const theatres = await Theatre.find({}).lean().exec();

    return Theatre.serialize(theatres);
  }

  const theatre = await Theatre.findOne({
    slug: theatreSlug,
  })
    .lean()
    .exec();

  if (!theatre) {
    return Boom.notFound();
  }

  return Theatre.serialize(theatre);
};