| @@ 51-64 (lines=14) @@ | ||
| 48 | return $statement->fetchAll(); |
|
| 49 | } |
|
| 50 | ||
| 51 | public function search(string $usersName): array |
|
| 52 | { |
|
| 53 | $query = 'SELECT `id`, `name`, `email` FROM `users` WHERE `name` LIKE :name ORDER BY `id`'; |
|
| 54 | $name = '%' . $usersName . '%'; |
|
| 55 | $statement = $this->database->prepare($query); |
|
| 56 | $statement->bindParam('name', $name); |
|
| 57 | $statement->execute(); |
|
| 58 | $users = $statement->fetchAll(); |
|
| 59 | if (!$users) { |
|
| 60 | throw new UserException('User name not found.', 404); |
|
| 61 | } |
|
| 62 | ||
| 63 | return $users; |
|
| 64 | } |
|
| 65 | ||
| 66 | public function loginUser(string $email, string $password) |
|
| 67 | { |
|
| @@ 66-79 (lines=14) @@ | ||
| 63 | return $users; |
|
| 64 | } |
|
| 65 | ||
| 66 | public function loginUser(string $email, string $password) |
|
| 67 | { |
|
| 68 | $query = 'SELECT * FROM `users` WHERE `email` = :email AND `password` = :password ORDER BY `id`'; |
|
| 69 | $statement = $this->database->prepare($query); |
|
| 70 | $statement->bindParam('email', $email); |
|
| 71 | $statement->bindParam('password', $password); |
|
| 72 | $statement->execute(); |
|
| 73 | $user = $statement->fetchObject(); |
|
| 74 | if (empty($user)) { |
|
| 75 | throw new UserException('Login failed: Email or password incorrect.', 400); |
|
| 76 | } |
|
| 77 | ||
| 78 | return $user; |
|
| 79 | } |
|
| 80 | ||
| 81 | public function create($user) |
|
| 82 | { |
|