You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
sae_2a_anglais/Project/php/gateway/GroupGateway.php

173 lines
5.6 KiB

<?php
namespace gateway;
use PDO;
use PDOException;
use Exception;
use config\Connection;
use model\Group;
class GroupGateway extends AbsGateway
{
public function add(array $parameters): int
{
try {
$query = "INSERT INTO Group_ values(null, :num,:year,:sec)";
$args = array(':num'=>array($parameters[0] ,PDO::PARAM_INT),
':year'=>array($parameters[1] ,PDO::PARAM_INT),
':sec'=>array($parameters[2] ,PDO::PARAM_STR));
$this->con->ExecuteQuery($query, $args);
return $this->con->lastInsertId();
} catch (PDOException $e) {
throw new Exception($e->getMessage());
}
}
public function remove(int $id): void
{
try {
$query="UPDATE User_ SET groupID=NULL WHERE groupID=:id";
$args = array(':id' => array($id, PDO::PARAM_INT));
$this->con->executeQuery($query, $args);
$query = "DELETE FROM Practice WHERE groupID=:id ";
$args = array(':id' => array($id, PDO::PARAM_INT));
$this->con->executeQuery($query, $args);
$query = "DELETE FROM Group_ WHERE id=:id ";
$args = array(':id' => array($id, PDO::PARAM_INT));
$this->con->executeQuery($query, $args);
} catch (PDOException $e) {
throw new Exception($e->getMessage());
}
}
public function findAll(): array
{
try {
$query = "SELECT * FROM Group_";
$this->con->ExecuteQuery($query);
$res = $this->con->getResults();
$tabGroup=[];
foreach ($res as $r) {
$tabGroup[]=new Group($r['id'], $r['num'], $r['year'], $r['sector']);
}
return $tabGroup;
} catch (PDOException $e) {
throw new Exception($e->getMessage());
}
}
public function findById(int $id):array
{
try {
$query = "SELECT * FROM Group_ WHERE id = :id";
$args = array(':id'=>array($id, PDO::PARAM_INT));
$this->con->executeQuery($query, $args);
return $this->con->getResults();
} catch (PDOException $e) {
throw new Exception($e->getMessage());
}
}
public function findByNum(String $num): array
{
try {
$query = "SELECT * FROM Group_ g WHERE g.num = :num";
$args = array(':num'=>array($num ,PDO::PARAM_INT));
$this->con->ExecuteQuery($query, $args);
$res = $this->con->getResults();
$tabGroup=[];
foreach ($res as $r) {
$tabGroup[]=new Group($r['id'], $r['num'], $r['year'], $r['sector']);
}
return $tabGroup;
} catch (PDOException $e) {
throw new Exception($e->getMessage());
}
}
public function modifGroupById(int $id, int $num, int $year ,String $sector):void
{
try {
$query = "UPDATE Group_ SET num=:num, year=:year, sector=:sector WHERE id=:id";
$args = array(':id'=>array($id ,PDO::PARAM_INT),
':num'=>array($num ,PDO::PARAM_INT),
':year'=>array($year ,PDO::PARAM_INT),
':sector'=>array($sector ,PDO::PARAM_STR));
$this->con->ExecuteQuery($query, $args);
} catch (PDOException $e) {
throw new Exception($e->getMessage());
}
}
public function addVocabToGroup(int $vocab, int $group): void
{
try {
$query = "INSERT INTO Practice VALUES (:vocabID, :groupID)";
$args = array(':vocabID'=>array($vocab ,PDO::PARAM_INT),
':groupID'=>array($group ,PDO::PARAM_INT));
$this->con->ExecuteQuery($query, $args);
} catch (PDOException $e) {
throw new Exception($e->getMessage());
}
}
public function removeVocabFromGroup(int $vocab, int $group): void
{
try {
$query = "DELETE FROM Practice WHERE vocabID=:vocabID and groupID=:groupID";
$args = array(':vocabID'=>array($vocab ,PDO::PARAM_INT),
':groupID'=>array($group ,PDO::PARAM_INT));
$this->con->ExecuteQuery($query, $args);
} catch (PDOException $e) {
throw new Exception($e->getMessage());
}
}
public function findGroupVocab(int $vocab) : array
{
try {
$query = "SELECT g.* FROM Practice p, Group_ g WHERE g.id=p.groupID AND p.vocabID=:vocabID;";
$args = array(':vocabID'=>array($vocab ,PDO::PARAM_INT));
$this->con->ExecuteQuery($query, $args);
$results = $this->con->getResults();
$tab = array();
foreach ($results as $row) {
$tab[] = new Group($row['id'], $row['num'], $row['year'], $row['sector']);
}
return $tab;
} catch (PDOException $e) {
throw new Exception($e->getMessage());
}
}
public function findGroupNoVocab(int $vocab) : array
{
try {
$query = "SELECT * FROM Group_ WHERE id NOT IN (SELECT groupID FROM Practice Where vocabID=:vocabID);";
$args = array(':vocabID'=>array($vocab ,PDO::PARAM_INT));
$this->con->ExecuteQuery($query, $args);
$results = $this->con->getResults();
$tab = array();
foreach ($results as $row) {
$tab[] = new Group($row['id'], $row['num'], $row['year'], $row['sector']);
}
return $tab;
} catch (PDOException $e) {
throw new Exception($e->getMessage());
}
}
}