<?php

namespace Lycoreco\Apps\Recipes\Controllers;

use Lycoreco\Apps\Recipes\Models\RecipeModel;
use Lycoreco\Includes\BaseController;
use Lycoreco\Includes\Routing\HttpExceptions;

class SingleRecipeController extends BaseController
{
   protected $template_name = APPS_PATH . '/Recipes/Templates/single.php';

   protected function get_model()
   {
      if (isset($this->__model))
         return $this->__model;

      $this->__model = RecipeModel::get(
         array(
            [
               'name'      => 'obj.id',
               'type'      => '=',
               'value'     => $this->url_context['url_1']
            ]
         )
      );
      if (empty($this->__model))
         throw new HttpExceptions\NotFound404('Recipe not found');

      return $this->__model;
   }

   public function get_context_data() 
   {
      $context = parent::get_context_data();

      $context['recipe'] = $this->get_model();

      return $context;
   }
}
