Add ValidationByName and CompanyValidationByName.
continuous-integration/drone/push Build is passing

This commit is contained in:
2023-08-17 19:22:10 +03:00
parent 135f4c0c93
commit f437d777a5
10 changed files with 226 additions and 57 deletions
+12 -19
View File
@@ -7,7 +7,9 @@ use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
class Company extends Model
use App\Models\Validation\ValidationByNameInterface;
class Company extends Model implements ValidationByNameInterface
{
use HasFactory;
@@ -28,31 +30,22 @@ class Company extends Model
return $this->hasMany(Category::class);
}
public static function isExist(int $company_id): bool
public static function isExist(int $id): bool
{
$count = Company::where('id', $company_id)->count();
$count = Company::where('id', $id)->count();
return $count != 0;
}
public static function validateWithName(string $name)
public static function isExistByName(string $name): bool
{
$name = $name ?? '';
$count = Company::where('name', $name)->count();
if($name == '')
return [
'error' => 'The company name is empty, please, write the name!!!'
];
return $count != 0;
}
$company = Company::where('name', $name)->first();
if(!$company)
return [
'error' => 'A company with the name does not exist!!!'
];
return [
'ok' => 'A company with the name is valid.'
];
public static function isNameValid(string $name): bool
{
return $name != '';
}
}