Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/admin sign up #31

Merged
merged 4 commits into from
Nov 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .env
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
DB_DSN = mysql:host=127.0.0.1;port=3306;dbname=fixmedb
DB_DSN = mysql:host=localhost;port=3306;dbname=fixmedb
DB_USER = root
DB_PASSWORD =
23 changes: 23 additions & 0 deletions controllers/AuthController.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use app\core\Controller;
use app\core\Request;
use app\core\Response;
use app\models\Admin;
use app\models\Customer;
use app\models\CustomerLoginForm;

Expand Down Expand Up @@ -163,4 +164,26 @@ public function serviceCenterLogout(Request $request, Response $response)
$response->redirect('/service-centre-landing');
}

/* admin sign up method */
public function adminSignUp(Request $request)
{
$admin = new Admin();
if ($request->isPost()) {

$admin->loadData($request->getBody());
if ($admin->validate() && $admin->save()) {
Application::$app->session->setFlash('success', 'You have been registered successfully!');
Application::$app->response->redirect('/admin-login');
}
$this->setLayout('auth');
return $this->render('/admin/admin-sign-up', [
'model' => $admin
]);
}
$this->setLayout('auth');
return $this->render('/admin/admin-sign-up', [
'model' => $admin
]);
}

}
24 changes: 16 additions & 8 deletions controllers/CommentController.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,22 @@ class CommentController extends Controller
// Create a new comment
public function create(Request $request)
{
// Checks if the HTTP request method is POST
if ($request->isPost()) {
// Create a new instance of the Comment model, used to store and handle data of the new comment
$comment = new Comment();
// The loadData method populates the Comment instance with the data from the POST request
$comment->loadData($request->getBody());

// Set the logged-in user's ID as the comment owner
$comment->cus_id = Application::$app->customer->cus_id;

// Validate the data before saving it
if ($comment->validate() && $comment->save()) {
Application::$app->session->setFlash('success', 'Comment posted successfully');
} else {
Application::$app->session->setFlash('error', 'Failed to post comment');
}

// Redirects the user to the fixmecommunity page after attempting to create the comment
Application::$app->response->redirect('/fixme-community');

}
Expand All @@ -35,7 +38,9 @@ public function create(Request $request)
// Edit an existing comment
public function edit(Request $request)
{
// Fetch the comment ID from the request and find the comment
$comment_id = $request->getBody()['comment_id'];
// Retrives the comment from the database
$comment = (new Comment)->findOne(['comment_id' => $comment_id]);

// Ensure the user is the owner of the comment
Expand All @@ -44,10 +49,11 @@ public function edit(Request $request)
$this->response->redirect("/fixme-community");
return;
}

// Checks if the HTTP request is a POST request
if ($request->isPost()) {
// Updates the Comment object with the new values provided by the user.
$comment->loadData($request->getBody());

// Validate the updated data before saving it
if ($comment->validate() && $comment->update()) {
Application::$app->session->setFlash('success', 'Comment updated successfully');
} else {
Expand All @@ -68,17 +74,19 @@ public function delete(Request $request)
{
// Fetch the comment ID from the request
$commentID = $request->getBody()['comment_id'] ?? null;
// Get the logged-in customer's ID
$cusID = Application::$app->customer->cus_id; // Get the logged-in customer ID

// Check if the comment ID or customer ID is not set (invalid request)
if (!$commentID || !$cusID) {
Application::$app->session->setFlash('error', 'Invalid request.');
Application::$app->response->redirect('/fixme-community');
// Stop the execution of the method
return;
}

// Find the comment by ID
// Find the comment using its ID
$comment = (new Comment)->findOne(['comment_id' => $commentID]);

// Check if the comment does not exist
if (!$comment) {
Application::$app->session->setFlash('error', 'Comment not found.');
Application::$app->response->redirect('/fixme-community');
Expand All @@ -98,7 +106,7 @@ public function delete(Request $request)
} else {
Application::$app->session->setFlash('error', 'Failed to delete the comment.');
}

// Redirect the user to the '/fixmecommunity' page after the operation
Application::$app->response->redirect('/fixme-community');
}

Expand Down
6 changes: 6 additions & 0 deletions controllers/ServiceCentreController.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,4 +92,10 @@ public function marketPlaceHome()
return $this->render('service-centre/market-place/market-place-home');
}

public function serviceCenterMessages()
{
$this->setLayout('auth');
return $this->render('/service-centre/service-center-messages');
}

}
84 changes: 84 additions & 0 deletions models/Admin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<?php

namespace app\models;

use app\core\Application;
use app\core\DbModel;

class Admin extends DbModel
{

public string $fname = '';
public string $lname = '';
public string $email = '';
public string $phone_no = '';
public string $address = '';
public string $password = '';
public string $confirmPassword = '';

public function tableName(): string
{
return 'admin';
}

public function primaryKey(): string
{
return 'admin_id';
}

public function save()
{
$this->password = password_hash($this->password, PASSWORD_DEFAULT);
return parent::save();
}

public function updateAdmin()
{
$sql = "UPDATE admin SET fname = :fname, lname = :lname, phone_no = :phone_no, address = :address WHERE admin_id = :admin_id";
$stmt = self::prepare($sql);
$stmt->bindValue(':fname', $this->fname);
$stmt->bindValue(':lname', $this->lname);
$stmt->bindValue(':phone_no', $this->phone_no);
$stmt->bindValue(':address', $this->address);
$stmt->bindValue(':admin_id', Application::$app->admin->{'admin_id'});
return $stmt->execute();
}

public function rules(): array
{
return [
'fname' => [self::RULE_REQUIRED],
'lname' => [self::RULE_REQUIRED],
'email' => [self::RULE_REQUIRED, self::RULE_EMAIL, [
self::RULE_UNIQUE,
'class' => self::class
]],
'phone_no' => [self::RULE_REQUIRED, [self::RULE_MIN, 'min' => 10], [self::RULE_MAX, 'max' => 10]],
'address' => [self::RULE_REQUIRED],
'password' => [self::RULE_REQUIRED, [self::RULE_MIN, 'min' => 8]],
'confirmPassword' => [self::RULE_REQUIRED, [self::RULE_MATCH, 'match' => 'password']],
];
}

public function updateRules(): array
{
return [
'fname' => [self::RULE_REQUIRED],
'lname' => [self::RULE_REQUIRED],
'phone_no' => [self::RULE_REQUIRED, [self::RULE_MIN, 'min' => 10], [self::RULE_MAX, 'max' => 10]],
'address' => [self::RULE_REQUIRED],
];
}

public function attributes(): array
{
return [
'fname',
'lname',
'email',
'phone_no',
'address',
'password',
];
}
}
2 changes: 1 addition & 1 deletion models/Post.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public function save()
// return $statement->fetchAll(\PDO::FETCH_ASSOC);
// }

public static function getAllPostsWithLikes(int $userId)
public static function getAllPostsWithLikes(?int $userId)
{
$sql = "SELECT p.*, t.fname, t.lname, t.profile_picture,
(SELECT COUNT(*) FROM post_like WHERE post_id = p.post_id) AS like_count,
Expand Down
Binary file added public/assets/products/OIP.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/assets/uploads/HomeImage3.webp
Binary file not shown.
Loading