Summary Files Commits Branches @commit @diff @stats
path: root / src / models
diff options
context:
space:
mode:
Commitf8c1df9a9a24be0ff3811dc33e00f92bf51465ab (patch)
Author
 Kamen Mladenov <kamen.d.mladenov@protonmail.com>
2021-04-03 11:54:52 +0300
Committer
 GitHub <noreply@github.com>
2021-04-03 11:54:52 +0300
Files56dfbe9d57e3150b480928da543b3dba30e11692 /src/models
devrix-job-board-f8c1df9a9a24be0ff3811dc33e00f92bf51465ab.tar
devrix-job-board-f8c1df9a9a24be0ff3811dc33e00f92bf51465ab.tar.gz
devrix-job-board-f8c1df9a9a24be0ff3811dc33e00f92bf51465ab.zip
Parentaa2e361c3114b53ac7858e118454b9dfaf51ebe4 (diff)
Parentfff8c08ad7f3845eb32671292392cbf08bb738c9 (diff)
Merge pull request #1 from Syndamia/dev v0.1
Implemented main functionality
4 files changed, 337 insertions, 0 deletions
-rw-r--r--src/models/Company.php47
-rw-r--r--src/models/DB.php176
-rw-r--r--src/models/DBModel.php21
-rw-r--r--src/models/JobOffer.php93
diff --git a/src/models/Company.php b/src/models/Company.php
new file mode 100644
index 0000000..1dfb05e
--- /dev/null
+++ b/src/models/Company.php
@@ -0,0 +1,47 @@
+<?php
+include_once("models/DBModel.php");
+
+class Company extends DBModel {
+ public string $name;
+
+ function __construct(string $name) {
+ $this->name = $name;
+ }
+
+ /*
+ * Everything from here is static functionality
+ */
+
+ public const TABLE_NAME = "companies";
+
+ static function __constructStatic() {
+ $columns = array(
+ "name " . DB::STRING_TYPE,
+ );
+ self::$DB->createTable(self::TABLE_NAME, $columns);
+ }
+
+ /* Create */
+
+ /**
+ * Inserts the giveb JobOffer into the databse. The id is automatically generated in the database.
+ *
+ * @param Company $com
+ */
+ static function insertIntoDB($com) {
+ self::$DB->insertValue(self::TABLE_NAME, $com->name);
+ }
+
+ /* Read */
+
+ /**
+ * Returns all Companies from the database.
+ *
+ * @return Company[]
+ */
+ static function getAllFromDB() {
+ return self::$DB->getAllValues(self::TABLE_NAME);
+ }
+}
+
+Company::__constructStatic();
diff --git a/src/models/DB.php b/src/models/DB.php
new file mode 100644
index 0000000..8174831
--- /dev/null
+++ b/src/models/DB.php
@@ -0,0 +1,176 @@
+<?php
+/**
+ * The model that has the database functionality. DO NOT instantiate this class! If you need an instance, extend the DBModel class!
+ */
+class DB {
+ private static $servername = "localhost";
+ private static $username = "root";
+ private static $password = "password";
+ private static $dbname = "devrix_job_board";
+
+ public const STRING_TYPE = "VARCHAR(100)";
+ public const FLOAT_TYPE = "DOUBLE(16,4)";
+ public const INTEGER_TYPE = "INT";
+
+ private PDO $connection;
+
+ function __construct() {
+ // Try to connect
+ try {
+ $this->connection = new PDO("mysql:host=" . self::$servername, self::$username, self::$password);
+ $this->connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
+ }
+ catch (PDOException $e) {
+ echo "[DB Exception]: " . $e->getMessage();
+ }
+
+ // Make sure the actual db exists
+ $createDBQuery = "CREATE DATABASE IF NOT EXISTS " . self::$dbname . ";";
+
+ $this->connection->exec($createDBQuery);
+
+ // Connect to the actual database
+ $this->connection->exec("USE " . self::$dbname);
+ }
+
+ /**
+ * @return string
+ */
+ static function genFK(string $currentColumn, string $foreignTable, string $foreignColumn) {
+ return "CONSTRAINT fk_{$currentColumn}_{$foreignTable} FOREIGN KEY ({$currentColumn}) REFERENCES {$foreignTable}($foreignColumn)";
+ }
+
+ /* Create */
+
+ /**
+ * Creates a table, with the given name and given column information.
+ * **WARNING**: The method also creates an auto-incremented int id column, **DO NOT** specify it!
+ *
+ * @param string[] $columns
+ * Represents the column information. Each value must be like: "column_name data_type", where the data_type should be taken
+ * from the constant values of DB class.
+ * Example value: array("first_name " . DB::STRING_TYPE, "age " . DB::INTEGER_TYPE, "income " . DB::FLOAT_TYPE)
+ *
+ * @param string[] $foreignKeys - OPTIONAL
+ * Represents the foreign keys. All foreign keys **MUST** be generated the the static DB method genFK.
+ * Example value: array(DB::genFK("jobId", "jobs", "id"), DB::genFK("cityId", "cities", "id"))
+ */
+ function createTable(string $tableName, array $columns, array $foreignKeys = null) {
+ $query = "CREATE TABLE IF NOT EXISTS {$tableName}(
+ id INT NOT NULL AUTO_INCREMENT, ";
+
+ foreach ($columns as $column) {
+ $query .= "{$column}, ";
+ }
+
+ $query .= "PRIMARY KEY (id), ";
+
+ foreach ($foreignKeys as $fk) {
+ $query .= "{$fk}, ";
+ }
+
+ $query = rtrim($query, ", ");
+ $query .= ");";
+
+ try {
+ $this->connection->exec($query);
+ }
+ catch (PDOException $e) {
+ echo "[DB Exception]: " . $e->getMessage();
+ }
+ }
+
+ /**
+ * Inserts a value in a given table.
+ *
+ * @param string[] $values
+ * Represents the data that should be inserted. The values should be ordered the same way the table columns are ordered.
+ * Example: "John", 20, 2000.3
+ * **WARNING**: DO NOT insert the id value
+ */
+ function insertValue(string $tableName, ...$values) {
+ $query = "INSERT INTO {$tableName} VALUES(NULL, ";
+
+ foreach($values as $value) {
+ if (gettype($value) == "string") {
+ $query .= "'{$value}', ";
+ }
+ else {
+ $query .= "{$value}, ";
+ }
+ }
+ $query = rtrim($query, ", ");
+
+ $query .= ");";
+
+ try {
+ $this->connection->exec($query);
+ }
+ catch (PDOException $e) {
+ echo "[DB Exception]: " . $e->getMessage();
+ }
+ }
+
+ /* Read */
+
+ /**
+ * Returns the next value that has the given id.
+ */
+ function getById(string $tableName, int $id) {
+ $sth = $this->connection->prepare("SELECT * FROM `{$tableName}` WHERE id = {$id}");
+ $sth->execute();
+
+ return $sth->fetch(PDO::FETCH_OBJ);
+ }
+
+ /**
+ * Returns all values inside a table.
+ */
+ function getAllValues(string $tableName) {
+ $sth = $this->connection->prepare("SELECT * FROM `{$tableName}`");
+ $sth->execute();
+
+ return $sth->fetchAll(PDO::FETCH_OBJ);
+ }
+
+ /* Update */
+
+ /**
+ * Updates the value with the given id inside the specified table.
+ *
+ * @param string[] $values
+ * Represents the data that should be updated. There should always be an even amount of data, where you alternate the column name and column value.
+ * Example: "first_name", "John", "age", 20, "income", 2000.3
+ */
+ function updateById(string $tableName, int $id, ...$values) {
+ $query = "UPDATE {$tableName} SET ";
+
+ for ($i = 0; $i < sizeof($values); $i += 2) {
+ $query .= "{$values[$i]} = " . (gettype($values[$i+1]) == "string" ? "'{$values[$i+1]}'" : $values[$i+1]) . ", ";
+ }
+ $query = rtrim($query, ", ");
+
+ $query .= " WHERE id = {$id};";
+
+ try {
+ $this->connection->exec($query);
+ }
+ catch (PDOException $e) {
+ echo "[DB Exception]: " . $e->getMessage();
+ }
+ }
+
+ /* Delete */
+
+ /**
+ * Deletes the value with the given id inside the given table.
+ */
+ function deleteById(string $tableName, int $id) {
+ try {
+ $this->connection->exec("DELETE FROM {$tableName} WHERE id = {$id}");
+ }
+ catch (PDOException $e) {
+ echo "[DB Exception]: " . $e->getMessage();
+ }
+ }
+}
diff --git a/src/models/DBModel.php b/src/models/DBModel.php
new file mode 100644
index 0000000..4e10c18
--- /dev/null
+++ b/src/models/DBModel.php
@@ -0,0 +1,21 @@
+<?php
+include_once("DB.php");
+
+/**
+ * A class, mainly for holding the static DB class intance. You can also get your model id from here.
+ */
+abstract class DBModel {
+ /**
+ * Used ONLY for gotten values form the database. Must not be assigned to, since it is AUTO_INCREMENT.
+ * The id column is set by default in the DB class.
+ */
+ public int $id;
+
+ protected static DB $DB;
+
+ public static function __constructStatic() {
+ self::$DB = new DB();
+ }
+}
+
+DBModel::__constructStatic();
diff --git a/src/models/JobOffer.php b/src/models/JobOffer.php
new file mode 100644
index 0000000..b86ff86
--- /dev/null
+++ b/src/models/JobOffer.php
@@ -0,0 +1,93 @@
+<?php
+include_once("models/DBModel.php");
+include_once("models/Company.php");
+
+/**
+ * Model that contains the required JobOffer properties. It also contains the request methods to the job_offers table.
+ */
+class JobOffer extends DBModel {
+ public string $title;
+ public string $description;
+ public int $companyId;
+ public float $salary;
+
+ function __construct(string $title, string $description, int $companyId, float $salary) {
+ $this->title = $title;
+ $this->description = $description;
+ $this->companyId = $companyId;
+ $this->salary = $salary;
+ }
+
+ /*
+ * Everything from here is static functionality
+ */
+
+ public const TABLE_NAME = "job_offers";
+
+ static function __constructStatic() {
+ $columns = array(
+ "title " . DB::STRING_TYPE,
+ "description " . DB::STRING_TYPE,
+ "companyId " . DB::INTEGER_TYPE, // Requires a foreign key
+ "salary " . DB::FLOAT_TYPE,
+ );
+ $foreignKeys = array(
+ DB::genFK("companyId", Company::TABLE_NAME, "id")
+ );
+ self::$DB->createTable(self::TABLE_NAME, $columns, $foreignKeys);
+ }
+
+ /* Create */
+
+ /**
+ * Inserts the giveb JobOffer into the databse. The id is automatically generated in the database.
+ *
+ * @param JobOffer $jb
+ */
+ static function insertIntoDB($jb) {
+ self::$DB->insertValue(self::TABLE_NAME, $jb->title, $jb->description, $jb->companyId, $jb->salary);
+ }
+
+ /* Read */
+
+ /**
+ * Returns the JobOffer with the given id from the database.
+ *
+ * @return JobOffer
+ */
+ static function getFromDBById(int $id) {
+ return self::$DB->getById(self::TABLE_NAME, $id);
+ }
+
+ /**
+ * Returns all JobOffers from the database.
+ *
+ * @return JobOffer[]
+ */
+ static function getAllFromDB() {
+ return self::$DB->getAllValues(self::TABLE_NAME);
+ }
+
+ /* Update */
+
+ /**
+ * Updates the given JobOffer from the database.
+ * **WARNING**: the JobOffer **MUST** have it's database id.
+ *
+ * @param JobOffer $jb
+ */
+ static function updateFromDB($jb) {
+ self::$DB->updateById(self::TABLE_NAME, $jb->id, "title", $jb->title, "description", $jb->description, "companyId", $jb->companyId, "salary", $jb->salary);
+ }
+
+ /* Delete */
+
+ /**
+ * Deletes the JobOffer with the given id from the database.
+ */
+ static function deleteFromDB(int $id) {
+ self::$DB->deleteById(self::TABLE_NAME, $id);
+ }
+}
+
+JobOffer::__constructStatic();