Jums jāizstrādā kāda lielāk projekta prototips. Izstrādājot prototipu, paturiet prātā, ka projektam attīstoties, šo prototipu varētu vajadzēt pilnveidot.
<?php
require_once("Models/ApplicationItem.php");
class ApplicationManager
{
private $dbService;
public function __construct()
{
$this->dbService = new DatabaseService();
}
public function createApplication($email, $amount)
{
$db = new Database();
$mail_service = new MailService;
$db_connect = $db->getConn();
$sql = "INSERT INTO applications (email, amount) VALUES ('".Helper::sanitizeInput($email)."', ".$amount.")";
$this->dbService->execute($sql);
$last_app_id = $db_connect->insert_id;
$mail_body = "Customer ".$email." created new loan application with amount ".$amount;
// values hardcoded only for demonstration
if ($amount <= 5000)
{
$db->executeSql("INSERT INTO deals (app_id, partner, status) VALUES ('".$last_app_id."', 'partner.a@dreamloan.com', 'ask')");
$mail_service->sendMail("partner.a@dreamloan.com", "New loan application", $mail_body);
}
if ($amount > 5000)
{
$db->executeSql("INSERT INTO deals (app_id, partner, status) VALUES ('".$last_app_id."', 'partner.b@crazyloans.com', 'ask')");
$mail_service->sendMail("partner.b@crazyloans.com", "New loan application", $mail_body);
}
}
public function updateApplication($id, $email, $amount, $status)
{
//$db = new Database();
$this->dbService->execute("UPDATE applications SET email = '".Helper::sanitizeInput($email)."', amount = ".$amount.", date_modified = SYSDATE() WHERE id = ".$id);
$this->dbService->execute("UPDATE deals SET status = '".$status."', date_modified = SYSDATE() WHERE app_id = ".$id);
}
public function getApplicationById($id)
{
$item = null;
$db = new Database();
$db_connect = $db->getConn();
$sql = "SELECT a.*, d.partner, d.status, d.date_created, d.date_modified FROM applications a LEFT JOIN deals d ON d.app_id = a.id WHERE a.id = ".$id;
$data = $db_connect->query($sql);
if ($data->num_rows > 0)
{
$row = $data->fetch_assoc();
$item = new ApplicationItem($row["id"], $row["email"], $row["amount"], $row["partner"], $row["status"], $row["date_created"], $row["date_modified"]);
$data->free_result();
}
$db_connect->close();
return $item;
}
public function getApplicationsList()
{
$items = array();
$db = new Database();
$db_connect = $db->getConn();
$sql = "SELECT a.*, d.partner, d.status, d.date_created, d.date_modified FROM applications a LEFT JOIN deals d ON d.app_id = a.id ORDER BY a.id DESC";
$data = $db_connect->query($sql);
if ($data->num_rows > 0)
{
while ($row = $data->fetch_assoc())
{
$items[] = new ApplicationItem($row["id"], $row["email"], $row["amount"], $row["partner"], $row["status"], $row["date_created"], $row["date_modified"]);
}
$data->free_result();
}
$db_connect->close();
return $items;
}
public function isApplicationExists($id)
{
$app = $this->getApplicationById($id);
return $app != null;
}
}
?>