Skip to content

Instantly share code, notes, and snippets.

@JamesTheHacker
Last active November 26, 2023 02:17
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JamesTheHacker/0733b97e6399d677ec84a08cf4cc4b18 to your computer and use it in GitHub Desktop.
Save JamesTheHacker/0733b97e6399d677ec84a08cf4cc4b18 to your computer and use it in GitHub Desktop.
<?php
namespace API\Controllers;
use Respect\Validation\Exceptions\NestedValidationException;
class User {
protected $userRepository;
protected $logger;
public function __construct(
\API\Repositories\User $userRepository,
\Monolog\Logger $logger
) {
$this->userRepository = $userRepository;
$this->logger = $logger;
}
/*
* Updates the users profile
*/
public function updateProfile($request, $response, $args) {
return false;
}
/*
*
*/
public function profile($request, $response, $args) {
$user = $this->userRepository->user($args['username']);
if(!$user) {
return $response
->withStatus(200)
->withJson([
'susscess' => false,
'errors' => [ 'User does not exist' ]
]);
}
return $response->withStatus(200)->withJson([
'success' => true,
'user' => [
'username' => $user['snapchat_name'],
'age' => $user['age'],
'gender' => $user['gender'],
'bio' => $user['bio'],
'profile_pic' => $user['profile_pic'],
'country' => $user['country']
]
]);
}
public function add($request, $response, $args) {
$user = $request->getParsedBody();
if(!$user) {
throw new \Exception('Failed to add user: $user is null');
return;
}
$validator = \API\Validators\UserRegistration::validate();
$this->logger->info('User:', [$user]);
try {
$validator->assert($user);
} catch (NestedValidationException $e) {
$this->logger->warning('Validation Failed!', [$e]);
$errors = $e->findMessages([
'snapchat_name' => 'Enter a valid snapchat name',
'age' => 'You must be over 18',
'gender' => 'Are you an alien?',
'password' => 'Enter a secure password',
'country' => 'Select your country of residence'
]);
return $response
->withStatus(200)
->withJson([
"success" => false,
"errors" => $errors
]);
}
if($this->userRepository->user($user['snapchat_name'])) {
return $response
->withStatus(200)
->withJson([
'success' => false,
'errors' => [
'snapchat_name' => 'Username already taken'
]
]);
}
$userID = $this->userRepository->add(
$user['snapchat_name'],
$user['age'],
$user['gender'],
password_hash($user['password'], PASSWORD_DEFAULT),
$user['country'],
'test'
);
return $response
->withStatus(200)
->withJson([
"response" => true
]);
}
}
<?php
namespace API\Repositories;
class User {
protected $db;
public function __construct(\Doctrine\DBAL\Connection $db) {
$this->db = $db;
}
public function user($snapchatName) {
$sql = 'SELECT * FROM users WHERE snapchat_name = :snapname';
$stmt = $this->db->prepare($sql);
$stmt->bindValue('snapname', $snapchatName);
$stmt->execute();
return $stmt->fetch();
}
public function userWithImages($snapchatName) {
return $this
->db
->createQueryBuilder()
->select(
'u.id',
'u.snapchat_name',
'u.age',
'u.gender',
'u.bio',
'u.is_deleted',
'ui.user_id',
'ui.image_id',
'i.filename',
'i.is_visible',
'i.is_adult'
)
->from('users', 'u')
->where('u.snapchat_name = :snapname')
->andWhere('u.role != :role')
->innerJoin('u', 'user_image', 'ui', 'u.id = ui.user_id')
->innerJoin('ui', 'images', 'i', 'ui.image_id = i.id')
->setParameter('snapname', $snapchatName)
->setParameter('role', 'rouge')
->execute()
->fetchAll();
}
/*
* Adds a new user to the database
*/
public function add(
$snapchatName,
$age,
$gender,
$password,
$country,
$profile_pic
) {
return $this
->db
->insert('users', [
'snapchat_name' => $snapchatName,
'age' => $age,
'gender' => $gender,
'password' => $password,
'country' => $country,
'profile_pic' => $profile_pic
]);
}
/*
* Update profile
*/
public function updateProfile($bio, $password, $country) {
return $this
->db
->update('users', [
'bio' => $bio,
'password' => $password,
'country' => $country
]);
}
/*
* Removes a user. The right to be forgot!
*/
public function delete($userID) {
return $this
->db
->delete('user', ['id' => $userID]);
}
public function updateLastActive($userID, $date) {
return $this
->db
->update(
'users',
[ 'last_activity' => $date ],
[ 'id' => $userID ]
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment