CRUD para gerenciar os colaboradores da Medeiros Corporation Inc., seguindo a mesma estrutura de boas práticas. Aqui está o guia passo a passo para implementar isso:
library-system ├── src │ ├── Controller │ │ ├── CollaboratorController.php │ ├── Model │ │ ├── Collaborator.php │ ├── View │ │ ├── CollaboratorView.php │ ├── Service │ │ ├── CollaboratorService.php ├── public │ └── index.php ├── config │ └── database.php ├── vendor └── composer.json
src/Model/Collaborator.php
<?php namespace LibrarySystem\Model; class Collaborator { private $id_collaborator; private $name; private $email; private $position_id; // Getters and Setters public function getIdCollaborator() { return $this->id_collaborator; } public function setIdCollaborator($id) { $this->id_collaborator = (int)$id; // Sanitização } public function getName() { return $this->name; } public function setName($name) { $this->name = htmlspecialchars(strip_tags($name)); // Sanitização } public function getEmail() { return $this->email; } public function setEmail($email) { $this->email = filter_var($email, FILTER_SANITIZE_EMAIL); // Sanitização } public function getPositionId() { return $this->position_id; } public function setPositionId($id) { $this->position_id = (int)$id; // Sanitização } }
src/View/PositionView.php
html /html
src/Controller/CollaboratorController.php
<?php namespace LibrarySystem\Controller; use LibrarySystem\Model\Collaborator; use LibrarySystem\Service\CollaboratorService; class CollaboratorController { private $collaboratorService; public function __construct($dbConnection) { $this->collaboratorService = new CollaboratorService($dbConnection); } public function create() { if ($_SERVER['REQUEST_METHOD'] === 'POST') { $collaborator = new Collaborator(); $collaborator->setName($_POST['name']); $collaborator->setEmail($_POST['email']); $collaborator->setPositionId($_POST['position_id']); $this->collaboratorService->createCollaborator($collaborator); header('Location: /collaborators'); } } public function read() { $collaborators = $this->collaboratorService->getAllCollaborators(); include '../src/View/CollaboratorView.php'; } public function update($id) { if ($_SERVER['REQUEST_METHOD'] === 'POST') { $collaborator = new Collaborator(); $collaborator->setIdCollaborator($id); $collaborator->setName($_POST['name']); $collaborator->setEmail($_POST['email']); $collaborator->setPositionId($_POST['position_id']); $this->collaboratorService->updateCollaborator($collaborator); header('Location: /collaborators'); } else { $collaborator = $this->collaboratorService->getCollaborator($id); include '../src/View/CollaboratorView.php'; } } public function delete($id) { $this->collaboratorService->deleteCollaborator($id); header('Location: /collaborators'); } }
src/Service/CollaboratorService.php
<?php namespace LibrarySystem\Service; use LibrarySystem\Model\Collaborator; class CollaboratorService { private $db; public function __construct($dbConnection) { $this->db = $dbConnection; } public function createCollaborator(Collaborator $collaborator) { $stmt = $this->db->prepare("INSERT INTO dep_collaborators (name, email, position_id) VALUES (:name, :email, :position_id)"); $stmt->bindValue(':name', $collaborator->getName()); $stmt->bindValue(':email', $collaborator->getEmail()); $stmt->bindValue(':position_id', $collaborator->getPositionId()); return $stmt->execute(); } public function getAllCollaborators() { $stmt = $this->db->query("SELECT * FROM dep_collaborators"); return $stmt->fetchAll(\PDO::FETCH_ASSOC); } public function getCollaborator($id) { $stmt = $this->db->prepare("SELECT * FROM dep_collaborators WHERE id_collaborator = :id"); $stmt->bindValue(':id', (int)$id); $stmt->execute(); return $stmt->fetch(\PDO::FETCH_ASSOC); } public function updateCollaborator(Collaborator $collaborator) { $stmt = $this->db->prepare("UPDATE dep_collaborators SET name = :name, email = :email, position_id = :position_id WHERE id_collaborator = :id"); $stmt->bindValue(':name', $collaborator->getName()); $stmt->bindValue(':email', $collaborator->getEmail()); $stmt->bindValue(':position_id', $collaborator->getPositionId()); $stmt->bindValue(':id', $collaborator->getIdCollaborator()); return $stmt->execute(); } public function deleteCollaborator($id) { $stmt = $this->db->prepare("DELETE FROM dep_collaborators WHERE id_collaborator = :id"); $stmt->bindValue(':id', (int)$id); return $stmt->execute(); } }
Verifique se a conexão com o banco de dados está configurada corretamente em config/database.php.
Implemente o roteamento em public/index.php para chamar os métodos corretos do controlador de acordo com as requisições.
Este CRUD para gerenciamento de colaboradores segue as melhores práticas de sanitização, encapsulamento e um design orientado a objetos. Adapte e expanda conforme necessário para atender às necessidades específicas da Medeiros Corporation Inc.