Skip to content

Commit

Permalink
Merge pull request #13 from tharindu-dm/developement
Browse files Browse the repository at this point in the history
Developement
  • Loading branch information
tharindu-dm authored Oct 16, 2024
2 parents 04ef580 + 66e23a3 commit 8454ce7
Show file tree
Hide file tree
Showing 29 changed files with 566 additions and 312 deletions.
1 change: 0 additions & 1 deletion app/config/database.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ public function query($query, $data = []) //using sql prepared statement to avoi
{
$con = $this->connect();
$statement = $con->prepare($query);

$check = $statement->execute($data);
if ($check) {
// For SELECT queries, fetch results
Expand Down
71 changes: 55 additions & 16 deletions app/controllers/BookController.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,65 @@ class BookController extends Controller
public function index()
{
$URL = splitURL();

if (count($URL) == 2) {
switch ($URL[1]) {
case 'book':
$this->viewBook();
break;
default:
$this->view('book/bookOverview');
break;
}

} else {
$this->view('book/bookOverview');
//show($URL);
/*Array
(
[0] => book
[1] => Overview
[2] => 4
)*/
switch ($URL[1]) {
case 'Overview':
if ($URL[2] >= 1) {
$this->viewBook($URL[2]);
} else {
$this->view('error');
}

break;

case 'Chapter':
if ($URL[2] >= 1) {
$this->viewChapter($URL[2]);
} else {
$this->view('error');
}

break;

case 'List':
$this->addToList($_POST['List_uid'], $_POST['List_bid'], $_POST['list']);
break;
default:
$this->view('error');
break;
}


}

private function viewBook($bookID)//set as private
{
$book = new Book();

$bookFound = $book->getBookByID($bookID);
$bookChapters = $book->getBookChapters($bookID); //list of chapters related to the specific book

$this->view('book/Overview', ['book' => $bookFound, 'chapters' => $bookChapters]);
}

private function viewBook()//set as private
private function viewChapter($chapterID)
{
//change URL to

$chapter = new Chapter();
//$chapterFound = $chapter->getChapterByID($chapterID);

// $this->view('book/Chapter', ['chapter' => $chapterFound]);
}

private function addToList($uid, $bookID, $status)
{
$list = new BookList(); //get chapter to be added to the list
$list->addToList($uid,$bookID, $status);
$this->viewBook($bookID);
}
}
14 changes: 14 additions & 0 deletions app/controllers/BookListController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

class BookListController extends Controller
{
public function index()
{
//echo "this is the List Controller\n";
$list = new BookList;

$this->view("BookList"); //calling the view function in /includes/Controller.php to view the Listpage
}


}
6 changes: 3 additions & 3 deletions app/controllers/BrowseController.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ public function index()

public function loadBrowsePage(){
$book = new Book();
$books = $book->getBooks();

$this->view('browse', ['books' => $books]);
$FWObooks = $book->getFWOBooks();
$paidBooks = $book->getPaidBooks();
$this->view('browse', ['FWObooks' => $FWObooks, 'paidBooks' => $paidBooks]);
}
public function viewBook()//set as private
{
Expand Down
17 changes: 4 additions & 13 deletions app/controllers/ChapterController.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,15 @@ public function index()
{
$URL = splitURL();

if (count($URL) == 2) {
switch ($URL[1]) {
case 'book':
$this->viewBook();
break;
default:
$this->view('book/bookOverview');
break;
}

if ($URL[2] >=1) {
$this->viewChapter($URL[2]);
} else {
$this->view('book/bookChapter');
$this->view('book/Chapter');
}
}

private function viewBook()//set as private
private function viewChapter($cID)//set as private
{
//change URL to

}

Expand Down
14 changes: 12 additions & 2 deletions app/controllers/HomeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,18 @@ class HomeController extends Controller
public function index()
{
//echo "this is the Home Controller\n";
$user = new User;

//$this->checkLoggedUser();
$this->view("home"); //calling the view function in /includes/Controller.php to view the homepage
}

private function checkLoggedUser()
{
if (session_status() == PHP_SESSION_NONE) {
echo "no session found";
}

if (isset($_SESSION['user_id'])) {
echo "user is already logged in \n";
}
}
}
10 changes: 8 additions & 2 deletions app/controllers/UserController.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,14 @@ public function index()

public function userProfile()
{
$uid = $_SESSION['user_id'];
//echo "inside the userProfile function\n";
$userDetails = new UserDetails();
$this->view('userProfile');
$userDetailsTable = new UserDetails();
$Booklist = new BookList(); //List Table

$userDetails = $userDetailsTable->getUserDetails($uid);
$list = $Booklist->getBookListCount($uid);

$this->view('userProfile', ['user' => $userDetails, 'listCounts' => $list]);
}
}
2 changes: 1 addition & 1 deletion app/includes/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public function where($data, $data_not = []) //return multiple rows
}

$query = rtrim($query, ' && ');
$query .= " order by " . lcfirst($this->table) . "ID" . " $this->orderBy";// offset $this->offset";
//$query .= " order by " . lcfirst($this->table) . "ID" . " $this->orderBy";// offset $this->offset";

$data = array_merge($data, $data_not);

Expand Down
22 changes: 20 additions & 2 deletions app/models/Book.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,30 @@ class Book

protected $table = 'Book'; //when using the Model trait, this table name ise used

public function getBooks()
public function getFWOBooks()
{
$query = "SELECT b.bookID, b.title, CONCAT(u.firstName, ' ', u.lastName) AS author, c.[name] AS cover_image FROM Book b JOIN [UserDetails] u ON b.author = u.[user] JOIN [CoverImage] c ON b.[coverImage] = c.covID WHERE b.accessType = 'public';";
$query = "SELECT TOP(5) b.bookID, b.title, b.price, CONCAT(u.firstName, ' ', u.lastName) AS author, c.[name] AS cover_image FROM Book b JOIN [UserDetails] u ON b.author = u.[user] LEFT JOIN [CoverImage] c ON b.[coverImage] = c.covID WHERE b.accessType = 'public' AND b.[publisher] IS NULL;";

return $this->query($query);
}
public function getPaidBooks()
{
$query = "SELECT TOP(5) b.bookID, b.title, b.price, CONCAT(u.firstName, ' ', u.lastName) AS author, c.[name] AS cover_image FROM Book b JOIN [UserDetails] u ON b.author = u.[user] LEFT JOIN [CoverImage] c ON b.[coverImage] = c.covID WHERE b.accessType = 'public' AND b.[price]>=0;";

return $this->query($query);
}

public function getBookByID($bid)
{
$query = "SELECT b.[bookID], b.[title], b.[Synopsis], b.[accessType], b.[lastUpdateDate], b.[isCompleted], b.[price], CONCAT(u.[firstName], ' ', u.[lastName]) AS author, c.[name] AS cover_image FROM [Book] b JOIN [UserDetails] u ON b.author = u.[user] LEFT JOIN [CoverImage] c ON b.[coverImage] = c.covID WHERE b.[bookID] = $bid;";

return $this->query($query);
}

public function getBookChapters($bid)
{
$query = "SELECT C.[chapterID],C.[title], C.[lastUpdated] FROM [dbo].[BookChapter] BC JOIN [dbo].[Chapter] C ON BC.[chapter] = C.[chapterID] WHERE BC.[book] = $bid ORDER BY C.[chapterID] ASC;";

return $this->query($query);
}
}
31 changes: 31 additions & 0 deletions app/models/BookList.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

class BookList
{
use Model; // Use the Model trait

protected $table = 'BookList'; //when using the Model trait, this table name ise used

public function getBookList($userID)
{

return $this->where(['user' => $userID]);

}
public function getBookListCount($userID)
{
$query = "SELECT
COUNT(CASE WHEN [status] = 'reading' THEN 1 END) AS reading,
COUNT(CASE WHEN [status] = 'planned' THEN 1 END) AS planned,
COUNT(CASE WHEN [status] = 'dropped' THEN 1 END) AS dropped,
COUNT(CASE WHEN [status] = 'hold' THEN 1 END) AS hold,
COUNT(CASE WHEN [status] = 'completed' THEN 1 END) AS completed
FROM [dbo].[BookList]
WHERE [user] = $userID;";
return $this->query($query);
}
public function addToList($userID, $bookID, $AddStatus)
{ //$chapter to be added to the list : edit the query to add the chapter to the list
return $this->insert(['user' => $userID, 'book' => $bookID, 'status' => $AddStatus]);
}
}
2 changes: 1 addition & 1 deletion app/models/Chapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ class Chapter
{
use Model; // Use the Model trait

protected $table = 'Admin'; //when using the Model trait, this table name ise used
protected $table = 'Chapter'; //when using the Model trait, this table name ise used

}
9 changes: 0 additions & 9 deletions app/models/List.php

This file was deleted.

3 changes: 0 additions & 3 deletions app/models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,6 @@ public function createUser($email, $password, $userType, $isPremium, $isActivate

public function getUserByUsername($username)
{
$arr = [
'email' => $username,
];
return $this->first(['email' => $username]);
}

Expand Down
5 changes: 4 additions & 1 deletion app/models/UserDetails.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,11 @@ public function addUserDetails($user, $firstName, $lastName, $regDate, $lastlogi
return $this->insert($arr);
}

public function getUserDetails(){
public function getUserDetails($userID)
{
$query = "SELECT CONCAT(ud.firstName, ' ', ud.lastName) AS fullName, ud.dob, ud.regDate, ud.country, ud.bio, ud.lastLogDate, u.userType, u.isPremium FROM [dbo].[UserDetails] ud INNER JOIN [dbo].[User] u ON ud.[user] = u.[userID] WHERE u.userID = $userID";

return $this->query($query);
}

}
Loading

0 comments on commit 8454ce7

Please sign in to comment.