feat(podcast-form): update routes and redirect to podcast page

This commit is contained in:
Yassine Doghri 2020-06-01 21:23:14 +00:00
parent 3234224369
commit 12ce905799
7 changed files with 41 additions and 22 deletions

3
src/.gitignore vendored
View File

@ -125,3 +125,6 @@ nb-configuration.xml
/results/ /results/
/phpunit*.xml /phpunit*.xml
/.phpunit.*.cache /.phpunit.*.cache
# Media files
public/media/*

View File

@ -5,9 +5,8 @@ $routes = Services::routes();
// Load the system's routing file first, so that the app and ENVIRONMENT // Load the system's routing file first, so that the app and ENVIRONMENT
// can override as needed. // can override as needed.
if (file_exists(SYSTEMPATH . 'Config/Routes.php')) if (file_exists(SYSTEMPATH . 'Config/Routes.php')) {
{ require SYSTEMPATH . 'Config/Routes.php';
require SYSTEMPATH . 'Config/Routes.php';
} }
/** /**
@ -20,7 +19,8 @@ $routes->setDefaultController('Home');
$routes->setDefaultMethod('index'); $routes->setDefaultMethod('index');
$routes->setTranslateURIDashes(false); $routes->setTranslateURIDashes(false);
$routes->set404Override(); $routes->set404Override();
$routes->setAutoRoute(true); $routes->setAutoRoute(false);
$routes->addPlaceholder('podcastName', '^@[a-z0-9\_]{1,191}$');
/** /**
* -------------------------------------------------------------------- * --------------------------------------------------------------------
@ -31,6 +31,8 @@ $routes->setAutoRoute(true);
// We get a performance increase by specifying the default // We get a performance increase by specifying the default
// route since we don't have to scan directories. // route since we don't have to scan directories.
$routes->get('/', 'Home::index'); $routes->get('/', 'Home::index');
$routes->add('/podcasts/create', 'Podcasts::create');
$routes->add('/(:podcastName)', 'Podcasts::podcastByHandle/$1');
/** /**
* -------------------------------------------------------------------- * --------------------------------------------------------------------
@ -45,7 +47,6 @@ $routes->get('/', 'Home::index');
* You will have access to the $routes object within that file without * You will have access to the $routes object within that file without
* needing to reload it. * needing to reload it.
*/ */
if (file_exists(APPPATH . 'Config/' . ENVIRONMENT . '/Routes.php')) if (file_exists(APPPATH . 'Config/' . ENVIRONMENT . '/Routes.php')) {
{ require APPPATH . 'Config/' . ENVIRONMENT . '/Routes.php';
require APPPATH . 'Config/' . ENVIRONMENT . '/Routes.php';
} }

View File

@ -5,7 +5,7 @@ use App\Models\LanguageModel;
use App\Models\PodcastModel; use App\Models\PodcastModel;
use RuntimeException; use RuntimeException;
class Podcast extends BaseController class Podcasts extends BaseController
{ {
public function index() public function index()
{ {
@ -15,10 +15,11 @@ class Podcast extends BaseController
public function create() public function create()
{ {
$model = new PodcastModel(); $model = new PodcastModel();
helper(['form', 'url']);
if (!$this->validate([ if (!$this->validate([
'title' => 'required', 'title' => 'required',
'name' => 'required|alpha_dash', 'name' => 'required|regex_match[^[a-z0-9\_]{1,191}$]',
'description' => 'required|max_length[4000]', 'description' => 'required|max_length[4000]',
'image' => 'uploaded[image]|is_image[image]|ext_in[image,jpg,png]|max_dims[image,3000,3000]', 'image' => 'uploaded[image]|is_image[image]|ext_in[image,jpg,png]|max_dims[image,3000,3000]',
'owner_email' => 'required|valid_email|permit_empty', 'owner_email' => 'required|valid_email|permit_empty',
@ -38,20 +39,23 @@ class Podcast extends BaseController
'browser_lang' => $browser_lang, 'browser_lang' => $browser_lang,
]; ];
echo view('podcast/create', $data); echo view('podcasts/create', $data);
} else { } else {
$image = $this->request->getFile('image'); $image = $this->request->getFile('image');
if (!$image->isValid()) { if (!$image->isValid()) {
throw new RuntimeException($image->getErrorString() . '(' . $image->getError() . ')'); throw new RuntimeException($image->getErrorString() . '(' . $image->getError() . ')');
} }
$image_path = $image->store(); $podcast_name = $this->request->getVar('name');
$image_name = 'cover.' . $image->getExtension();
$image_storage_folder = 'media/' . $podcast_name . '/';
$image->move($image_storage_folder, $image_name);
$model->save([ $model->save([
'title' => $this->request->getVar('title'), 'title' => $this->request->getVar('title'),
'name' => $this->request->getVar('name'), 'name' => $podcast_name,
'description' => $this->request->getVar('description'), 'description' => $this->request->getVar('description'),
'episode_description_footer' => $this->request->getVar('episode_description_footer'), 'episode_description_footer' => $this->request->getVar('episode_description_footer'),
'image' => $image_path, 'image' => $image_storage_folder . $image_name,
'language' => $this->request->getVar('language'), 'language' => $this->request->getVar('language'),
'category' => $this->request->getVar('category'), 'category' => $this->request->getVar('category'),
'explicit' => $this->request->getVar('explicit') or false, 'explicit' => $this->request->getVar('explicit') or false,
@ -65,7 +69,18 @@ class Podcast extends BaseController
'custom_html_head' => $this->request->getVar('custom_html_head'), 'custom_html_head' => $this->request->getVar('custom_html_head'),
]); ]);
echo view('podcast/success'); return redirect()->to(base_url('/@' . $podcast_name));
} }
} }
public function podcastByHandle($handle)
{
$model = new PodcastModel();
$podcast_name = substr($handle, 1);
$data['podcast'] = $model->where('name', $podcast_name)->first();
return view('podcasts/view.php', $data);
}
} }

View File

@ -1,5 +0,0 @@
<?=$this->extend('layouts/default')?>
<?=$this->section('content')?>
<h1 class="text-xl">Podcast Page!</h1>
<?=$this->endSection()?>

View File

@ -1 +0,0 @@
Success!

View File

@ -1,4 +1,3 @@
<?=helper('form')?>
<?=$this->extend('layouts/default')?> <?=$this->extend('layouts/default')?>
<?=$this->section('content')?> <?=$this->section('content')?>
@ -9,7 +8,7 @@
<?=\Config\Services::validation()->listErrors()?> <?=\Config\Services::validation()->listErrors()?>
</div> </div>
<?=form_open_multipart('/podcast/create', ["method" => "post", "class" => "flex flex-col max-w-md"])?> <?=form_open_multipart('podcasts/create', ["method" => "post", "class" => "flex flex-col max-w-md"])?>
<?=csrf_field()?> <?=csrf_field()?>
<div class="flex flex-col mb-4"> <div class="flex flex-col mb-4">

View File

@ -0,0 +1,7 @@
<?=$this->extend('layouts/default')?>
<?=$this->section('content')?>
<h1 class="text-xl"><?=$podcast->title?></h1>
<img src="<?=base_url($podcast->image)?>" alt="Podcast cover"/>
<?=$this->endSection()?>