<?php

namespace Blog\Controller;

use Blog\Model\NewsModel;

class NewsController {

    private $model;
    private $view;

    public function __construct($view) {
        $this->model = new NewsModel();
        $this->view = $view;
    }

    public function showNews() {
        $news = $this->model->getNews();
        $this->view->setVars(['news' => $news]);
    }

    public function createNews() {
        $data = [
            'name' => $_POST['name'],
            'beschreibung' => $_POST['beschreibung'],
            'datum' => $_POST['datum'],
        ];
        $erg = $this->model->createNews($data);
        $this->view->setVars(['news' => $erg]);
        exit;
    }

    public function editNewsForm() {
        $id = $_GET['newsid'];
        $news = $this->model->getNewsById($id);
        $this->view->setVars(['news' => $news]);
    }

    public function updateNews() {
        $id = $_POST['newsid'] ?? null;
        $data = [
            'name' => $_POST['name'],
            'beschreibung' => $_POST['beschreibung'],
            'datum' => $_POST['datum'],
        ];
        $this->model->updateNews($id, $data);
    }

    public function deleteNews() {
        $id = $_GET['newsid'] ?? null;
        $this->model->deleteNews($id);
    }
}