<?php
namespace PortalPeople\DisabledDates\Controller\Adminhtml\Date;

use Magento\Backend\App\Action;
use PortalPeople\DisabledDates\Model\DisabledDateFactory;

class Delete extends Action
{
    protected $disabledDateFactory;

    public function __construct(
        Action\Context $context,
        DisabledDateFactory $disabledDateFactory
    ) {
        $this->disabledDateFactory = $disabledDateFactory;
        parent::__construct($context);
    }

    public function execute()
    {
        $id = $this->getRequest()->getParam('entity_id');
        if ($id) {
            $model = $this->disabledDateFactory->create()->load($id);
            if ($model->getId()) {
                try {
                    $model->delete();
                    $this->messageManager->addSuccessMessage(__('Date deleted.'));
                } catch (\Exception $e) {
                    $this->messageManager->addErrorMessage($e->getMessage());
                }
            }
        }
        return $this->_redirect('*/*/');
    }
}
