Projeto
CRUD: Estrutura de Gestão Organizacional - DepartamentoCRUD: Estrutura de Gestão Organizacional - Departamento
Fabrício de Medeiros
CRUD (Create, Read, Update, Delete) em MVC para a Estrutura de Gestão Organizacional da Medeiros Corporation Inc., utilizando práticas de sanitização e getters/setters. Aqui está um guia passo a passo para implementar isso:
Estrutura do Projeto
Estrutura de Arquivos:
library-system ├── src │ ├── Controller │ │ ├── DepartmentController.php │ ├── Model │ │ ├── Department.php │ ├── View │ │ ├── DepartmentView.php │ ├── Service │ │ ├── DepartmentService.php ├── public │ └── index.php ├── config │ └── database.php ├── vendor └── composer.json
1. Modelo (Model)
src/Model/Department.php
<?php
namespace LibrarySystem\Model;
class Department {
private $id_department;
private $department;
private $description;
// Getters and Setters
public function getIdDepartment() {
return $this->id_department;
}
public function setIdDepartment($id) {
$this->id_department = (int)$id; // Sanitização
}
public function getDepartment() {
return $this->department;
}
public function setDepartment($department) {
$this->department = htmlspecialchars(strip_tags($department)); // Sanitização
}
public function getDescription() {
return $this->description;
}
public function setDescription($description) {
$this->description = htmlspecialchars(strip_tags($description)); // Sanitização
}
}
2. Controlador (Controller)
src/Controller/DepartmentController.php
<?php
namespace LibrarySystem\Controller;
use LibrarySystem\Model\Department;
use LibrarySystem\Service\DepartmentService;
class DepartmentController {
private $departmentService;
public function __construct($dbConnection) {
$this->departmentService = new DepartmentService($dbConnection);
}
public function create() {
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$department = new Department();
$department->setDepartment($_POST['department']);
$department->setDescription($_POST['description']);
$this->departmentService->createDepartment($department);
header('Location: /departments');
}
}
public function read() {
$departments = $this->departmentService->getAllDepartments();
include '../src/View/DepartmentView.php';
}
public function update($id) {
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$department = new Department();
$department->setIdDepartment($id);
$department->setDepartment($_POST['department']);
$department->setDescription($_POST['description']);
$this->departmentService->updateDepartment($department);
header('Location: /departments');
} else {
$department = $this->departmentService->getDepartment($id);
include '../src/View/DepartmentView.php';
}
}
public function delete($id) {
$this->departmentService->deleteDepartment($id);
header('Location: /departments');
}
}
3. Visualização (View)
src/View/DepartmentView.php
2. Serviço (Service)
src/Service/DepartmentService.php
<?php
namespace LibrarySystem\Service;
use LibrarySystem\Model\Department;
class DepartmentService {
private $db;
public function __construct($dbConnection) {
$this->db = $dbConnection;
}
public function createDepartment(Department $department) {
$stmt = $this->db->prepare("INSERT INTO dep_departments (department, description) VALUES (:department, :description)");
$stmt->bindValue(':department', $department->getDepartment());
$stmt->bindValue(':description', $department->getDescription());
return $stmt->execute();
}
public function getAllDepartments() {
$stmt = $this->db->query("SELECT * FROM dep_departments");
return $stmt->fetchAll(\PDO::FETCH_ASSOC);
}
public function getDepartment($id) {
$stmt = $this->db->prepare("SELECT * FROM dep_departments WHERE id_department = :id");
$stmt->bindValue(':id', (int)$id);
$stmt->execute();
return $stmt->fetch(\PDO::FETCH_ASSOC);
}
public function updateDepartment(Department $department) {
$stmt = $this->db->prepare("UPDATE dep_departments SET department = :department, description = :description WHERE id_department = :id");
$stmt->bindValue(':department', $department->getDepartment());
$stmt->bindValue(':description', $department->getDescription());
$stmt->bindValue(':id', $department->getIdDepartment());
return $stmt->execute();
}
public function deleteDepartment($id) {
$stmt = $this->db->prepare("DELETE FROM dep_departments WHERE id_department = :id");
$stmt->bindValue(':id', (int)$id);
return $stmt->execute();
}
}
Editar
Voltar