Files
FACULTATE-KNOW_YOUR_FOOD/service_authentication/models/RegisterModel.php
T

51 lines
1.4 KiB
PHP
Executable File

<?php
namespace app\models;
use app\core\AuthInterface;
use app\exceptions\GenericErrorException;
use app\exceptions\UserAlreadyExistsException;
use Exception;
use mysqli;
class RegisterModel implements AuthInterface
{
private ?mysqli $conn = NULL;
private array $config;
public function __construct()
{
$this->config = $_SESSION['database_configuration'];
}
/**
* @throws GenericErrorException
*/
public function createConnection(): void
{
try {
$this->conn = new mysqli($this->config['dsn'], $this->config['user'], $this->config['password'], $this->config['dbname']);
} catch (Exception){
throw new GenericErrorException();
}
}
/**
* @throws UserAlreadyExistsException
*/
public function executeReq(array $body): int|string
{
$username = $body["username"];
$email = $body["email"];
$password = $body["password"];
$query = "SELECT * FROM Users WHERE email = '$email'";
$result = $this->conn->query($query);
if($result->num_rows) throw new UserAlreadyExistsException();
$query = "INSERT INTO Users (username, email, password) VALUES ('$username', '$email', '$password')";
$result = $this->conn->query($query);
return "Success";
}
}