Skip to content

Commit

Permalink
Merge pull request #4 from atlasharry/3-backend-test
Browse files Browse the repository at this point in the history
3 backend test
  • Loading branch information
atlasharry authored Dec 6, 2023
2 parents dceead7 + 6d9800e commit faecffb
Show file tree
Hide file tree
Showing 3,577 changed files with 319,568 additions and 85 deletions.
The diff you're trying to view is too large. We only load the first 3000 changed files.
4 changes: 3 additions & 1 deletion backend/Controller/BaseController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
class BaseController
{

protected function jsonResponse($data)
protected function jsonResponse($data, $statusCode)
{
//make sure react knows the data is in JSON format
header('Content-Type: application/json');
//set up the status code
http_response_code($statusCode);
//convert data into JSON
echo json_encode($data);
}
Expand Down
56 changes: 18 additions & 38 deletions backend/Controller/UserController.php
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@

<?php
// Include necessary models and base controller
require_once __DIR__ . '/../Model/Database.php';
require_once __DIR__ . '/../Model/UserModel.php';
require_once __DIR__ . '/BaseController.php';

// Define the UserController class that extends the BaseController.
class UserController extends BaseController
{
/**
* Handles user login logic.
*/
// Method for user login
public function login()
{
$input = file_get_contents('php://input');
Expand All @@ -21,20 +19,15 @@ public function login()
$db = new Database();
$userModel = new UserModel($db);

// Check if the user credentials are valid
if ($userModel->verifyUserCredentials($username, $password)) {
$this->jsonResponse(['success' => true, 'message' => 'Login successful']);
$this->jsonResponse(['success' => true, 'message' => 'Login successful'], 200);
} else {
$this->jsonResponse(['success' => false, 'message' => 'wrong username or password']);
$this->jsonResponse(['success' => false, 'message' => 'wrong username or password'], 401);
}

// Close the database connection
$db->closeConnection();
}

/**
* Handles user registration logic.
*/
public function register()
{
$input = file_get_contents('php://input');
Expand All @@ -46,33 +39,29 @@ public function register()
$userModel = new UserModel($db);
// Check if the user already exist.
if ($userModel->checkUserExist($username)) {
$this->jsonResponse(['success' => false, 'message' => 'user already exist']);
$this->jsonResponse(['success' => false, 'message' => 'user already exist'], 500);
}
// Register the user.
else {
$userModel->registerUser($username, $password);
$this->jsonResponse(['success' => true, 'message' => 'user registered successfully']);
$this->jsonResponse(['success' => true, 'message' => 'user registered successfully'], 201);
}
$db->closeConnection();
}

/**
* Retrieve all ratings from the database.
*/
// Method to retrieve ratings.
public function getRatings()
{
$db = new Database();
$userModel = new UserModel($db);

// Retrieve all ratings.
$ratings = $userModel->getAllRatings();
$this->jsonResponse($ratings);

$this->jsonResponse($ratings, 200);

$db->closeConnection();
}

/**
* View a specific song's details.
*/
public function viewSong()
{
$input = file_get_contents('php://input');
Expand All @@ -84,16 +73,13 @@ public function viewSong()

$song_info = $userModel->getSongInfo($song_id);
if ($song_info == "wrong") {
$this->jsonResponse(['success' => false, 'song_info' => $song_info]);
$this->jsonResponse(['success' => false, 'song_info' => $song_info], 500);
} else {
$this->jsonResponse(['success' => true, 'song_info' => $song_info]);
$this->jsonResponse(['success' => true, 'song_info' => $song_info], 200);
}
$db->closeConnection();
}

/**
* Update details of a specific song.
*/
public function updateSong()
{
$input = file_get_contents('php://input');
Expand All @@ -110,16 +96,13 @@ public function updateSong()
$check = $userModel->checkValid($song_id, $user_name);
if ($check == "true") {
$userModel->updateInfo($song_id, $artist, $song_name, $song_rating);
$this->jsonResponse(['success' => true, 'message' => 'song updated successfully']);
$this->jsonResponse(['success' => true, 'message' => 'song updated successfully'], 200);
} else {
$this->jsonResponse(['success' => false, 'message' => 'only the user who added the song can update it']);
$this->jsonResponse(['success' => false, 'message' => 'only the user who added the song can update it'], 401);
}
$db->closeconnection();
}

/**
* Delete a specific song.
*/
public function deleteSong()
{
$input = file_get_contents('php://input');
Expand All @@ -132,15 +115,12 @@ public function deleteSong()
$check = $userModel->checkValid($song_id, $user_name);
if ($check == "true") {
$userModel->deleteSong($song_id);
$this->jsonResponse(['success' => true, 'message' => 'song deleted successfully']);
$this->jsonResponse(['success' => true, 'message' => 'song deleted successfully'], 200);
} else {
$this->jsonResponse(['success' => false, 'message' => 'only the user who added the song can delete it']);
$this->jsonResponse(['success' => false, 'message' => 'only the user who added the song can delete it'], 401);
}
}

/**
* Create a new song in the database.
*/
public function createSong()
{
$input = file_get_contents('php://input');
Expand All @@ -155,9 +135,9 @@ public function createSong()

if ($userModel->checkDuplicate($song_name, $user_name, $song_artist)) {
$userModel->createSong($song_name, $user_name, $song_artist, $song_rating);
$this->jsonResponse(['success' => true]);
$this->jsonResponse(['success' => true], 201);
} else {
$this->jsonResponse(['success' => false, 'message' => 'song already exist']);
$this->jsonResponse(['success' => false, 'message' => 'song already exist'], 401);
}
}
}
48 changes: 6 additions & 42 deletions backend/Model/UserModel.php
Original file line number Diff line number Diff line change
@@ -1,17 +1,10 @@
<?php
/**
* UserModel class that interacts with the database to handle
* user and song related functionalities.
*/

class UserModel
{

private $db;

/**
* Constructor to initialize the database instance.
*/
public function __construct($database)
{
$this->db = $database;
Expand All @@ -20,15 +13,14 @@ public function __construct($database)
// Method to verify the user credentials
public function verifyUserCredentials($username, $password)
{
// Check the password associated with the username
$query = $this->db->getConnection()->prepare("SELECT password FROM username WHERE username = ?");
$query->bind_param('s', $username);
$query->execute();
// Fetch the result of the query.
$result = $query->get_result();
$row = $result->fetch_assoc();

// Verify the hashed password

if ($row && password_verify($password, $row['password'])) {
return true;
} else {
Expand All @@ -37,20 +29,13 @@ public function verifyUserCredentials($username, $password)
$query->close();
}


/**
* Checks if a user with the given username exists.
*/
public function checkUserExist($username)
{
$query = $this->db->getConnection()->prepare("SELECT password FROM username WHERE username = ?");
$query->bind_param('s', $username);
$query->execute();
// Fetch the result of the query.
$result = $query->get_result();

// Check if any rows are returned
if ($result->num_rows > 0) {
//user already exist
return true;
Expand All @@ -59,9 +44,6 @@ public function checkUserExist($username)
}
}

/**
* Registers a new user with the given username and password.
*/
public function registerUser($username, $password)
{
$password = password_hash($password, PASSWORD_DEFAULT);
Expand All @@ -71,9 +53,7 @@ public function registerUser($username, $password)
$query->close();
}

/**
* Retrieves all song ratings from the database.
*/
// Method to get the ratings from database
public function getAllRatings()
{
$query = $this->db->getConnection()->prepare("SELECT * FROM ratings");
Expand All @@ -90,9 +70,6 @@ public function getAllRatings()
return $rows;
}

/**
* Retrieves information about a specific song.
*/
public function getSongInfo($song_id)
{
$query = $this->db->getConnection()->prepare("SELECT * FROM ratings WHERE id = ?");
Expand All @@ -109,9 +86,8 @@ public function getSongInfo($song_id)
$query->close();
}

/**
* Updates song information.
*/


public function updateInfo($song_id, $artist, $song_name, $song_rating)
{
$query = $this->db->getConnection()->prepare("UPDATE ratings SET artist = ?, song = ?, rating = ? WHERE id = ?");
Expand All @@ -120,9 +96,6 @@ public function updateInfo($song_id, $artist, $song_name, $song_rating)
$query->close();
}

/**
* Validates if a song belongs to the user.
*/
public function checkValid($song_id, $user_name)
{
$query = $this->db->getConnection()->prepare("SELECT * FROM ratings WHERE id = ? AND username = ?");
Expand All @@ -138,9 +111,6 @@ public function checkValid($song_id, $user_name)
$query->close();
}

/**
* Deletes a song from the database.
*/
public function deleteSong($song_id)
{
$query = $this->db->getConnection()->prepare("DELETE FROM ratings WHERE id = ?");
Expand All @@ -149,9 +119,6 @@ public function deleteSong($song_id)
$query->close();
}

/**
* Checks if a song already exists for the user.
*/
public function checkDuplicate($song_name, $user_name, $song_artist)
{
$query = $this->db->getConnection()->prepare("SELECT * FROM ratings WHERE song = ? AND username = ? AND artist = ?");
Expand All @@ -167,12 +134,9 @@ public function checkDuplicate($song_name, $user_name, $song_artist)
$query->close();
}

/**
* Creates a new song entry in the database.
*/
public function createSong($song_name, $user_name, $song_artist, $song_rating)
{
$query = $this->db->getConnection()->prepare("INSERT INTO ratings (`username`, `artist`, `song`, `rating`) VALUES (?, ?, ?, ?)");
$query = $this->db->getConnection()->prepare("INSERT INTO ratings (`username`, `artist`, `song`, `rating`) VALUES (?, ?, ?, ?, ?)");
$query->bind_param('sssi', $user_name, $song_artist, $song_name, $song_rating);
$query->execute();
$query->close();
Expand Down
20 changes: 20 additions & 0 deletions backend/composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"name": "phpunit/project",
"require-dev": {
"phpunit/phpunit": "^10.4"
},
"autoload": {
"psr-4": {
"Phpunit\\Project\\": "src/"
}
},
"authors": [
{
"name": "Patton",
"email": "[email protected]"
}
],
"require": {
"guzzlehttp/guzzle": "^7.8"
}
}
Loading

0 comments on commit faecffb

Please sign in to comment.