// src/controllers/courseController.ts

import { Request, Response } from 'express'

export const coursesController = {
  // GET /courses/:id
  show: async (req: Request, res: Response) => {
    const { id } = req.params

  }
}
// src/services/courseService.ts

import { Course } from "../models"

export const courseService = {
  findByIdWithEpisodes: async (id: string) => {
    const courseWithEpisodes = await Course.findByPk(id, {
      attributes: ['id', 'name', 'synopsis', ['thumbnail_url', 'thumbnailUrl']],
      include: {
        association: 'episodes',
        attributes: [
          'id',
          'name',
          'synopsis',
          'order',
          ['video_url', 'videoUrl'],
          ['seconds_long', 'secondsLong']
        ],
        order: [['order', 'ASC']],
        separate: true
      }
    })

    return courseWithEpisodes
  },
}
// src/models/index.ts

// ...

Course.hasMany(Episode, { as: 'episodes' })

// ...
// src/controllers/courseController.ts

import { Request, Response } from 'express'
import { courseService } from '../services/courseService'

export const coursesController = {
  // GET /courses/:id
  show: async (req: Request, res: Response) => {
    const { id } = req.params

    try {
      const course = await courseService.findByIdWithEpisodes(id)
      return res.json(course)
    } catch (err) {
      if (err instanceof Error) {
        return res.status(400).json({ message: err.message })
      }
    }
  }
}
// src/routes.ts

import express from 'express'
import { categoriesController } from './controllers/categoriesController'
import { coursesController } from './controllers/coursesController'

const router = express.Router()

// ...

router.get('/courses/:id', coursesController.show)

export { router }