From 2d4fb6a59be2d2ba7db6bdc813a094b485d04d38 Mon Sep 17 00:00:00 2001 From: KKlochko Date: Sun, 18 Jun 2023 16:17:35 +0300 Subject: [PATCH] Add the validation for City, User that returns message for requests. --- app/Models/City.php | 21 +++++++++++++++++++++ app/Models/User.php | 21 +++++++++++++++++++++ 2 files changed, 42 insertions(+) diff --git a/app/Models/City.php b/app/Models/City.php index aeaa28a..7239666 100644 --- a/app/Models/City.php +++ b/app/Models/City.php @@ -14,4 +14,25 @@ class City extends Model 'name', 'url', ]; + + public static function validate_with_name(string $name) + { + $name = $name ?? ''; + + if($name == '') + return [ + 'error' => 'The city name is empty, please, write the name!!!' + ]; + + $city = City::where('name', $name)->first(); + + if(!$city) + return [ + 'error' => 'A city with the name does not exist!!!' + ]; + + return [ + 'ok' => 'A city with the name is valid.' + ]; + } } diff --git a/app/Models/User.php b/app/Models/User.php index a3e099b..0076f36 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -22,4 +22,25 @@ class User extends Authenticatable 'matrix_username', 'phone', ]; + + public static function validate_with_matrix_username(string $matrix_username) + { + $matrix_username = $matrix_username ?? ''; + + if($matrix_username == '') + return [ + 'error' => 'The username is empty, please, write username!!!' + ]; + + $user = User::where('matrix_username', $matrix_username)->first(); + + if(!$user) + return [ + 'error' => 'A user with the username does not exist!!!' + ]; + + return [ + 'ok' => 'A user with the username is valid.' + ]; + } }