From 24922b7418e07df997c760b35d33879055dc6602 Mon Sep 17 00:00:00 2001 From: aslan Date: Fri, 12 Jul 2024 12:45:23 +0300 Subject: [PATCH] add new fields to User --- app/Enums/UserFields.php | 21 +++++ app/Helpers/AllfieldsUserHelper.php | 16 ++++ app/Http/Controllers/ProfileController.php | 80 ++++++++++++++++++- app/Models/User.php | 19 +++++ .../0001_01_01_000000_create_users_table.php | 2 +- ..._06_20_071628_add_fields_to_user_table.php | 44 ++++++++++ 6 files changed, 178 insertions(+), 4 deletions(-) create mode 100644 app/Enums/UserFields.php create mode 100644 app/Helpers/AllfieldsUserHelper.php create mode 100644 database/migrations/2024_06_20_071628_add_fields_to_user_table.php diff --git a/app/Enums/UserFields.php b/app/Enums/UserFields.php new file mode 100644 index 0000000..734bf2e --- /dev/null +++ b/app/Enums/UserFields.php @@ -0,0 +1,21 @@ +name] = $item->value; + return $carry; + }, []); + } +} diff --git a/app/Http/Controllers/ProfileController.php b/app/Http/Controllers/ProfileController.php index a48eb8d..59b5ccf 100644 --- a/app/Http/Controllers/ProfileController.php +++ b/app/Http/Controllers/ProfileController.php @@ -2,18 +2,92 @@ namespace App\Http\Controllers; +use App\Helpers\HashHelper; use App\Http\Requests\ProfileUpdateRequest; +use App\Models\HashRecord; +use App\Services\GetInfoOfStudent; +use App\Services\TestConnect; use Illuminate\Http\RedirectResponse; use Illuminate\Http\Request; use Illuminate\Support\Facades\Auth; +use Illuminate\Support\Facades\Cache; use Illuminate\Support\Facades\Redirect; use Illuminate\View\View; class ProfileController extends Controller { - /** - * Display the user's profile form. - */ + public function show(): View + { + $currentWeek = json_decode(file_get_contents('https://local.mkgtu.ru/raspisnew/api.php?des=raspis_week_cur'), 1); + $user = Auth::user(); + $ch = curl_init(); + $groupName = 'ПИ'; + $groupNumber = (string) 11; + $educationForm = 'ОФО'; + $api = "https://local.mkgtu.ru/raspisnew/api.php?des=raspis_grupp&name={$groupName}&number={$groupNumber}&fo={$educationForm}"; + curl_setopt($ch, CURLOPT_URL, $api); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); + $response = json_decode(curl_exec($ch), 1); + curl_close($ch); + + $timetable = ['err' => 0, 'msg' => '']; + if ($response['err'] !== 0) { + $timetable = ['err' => $response['err'], 'msg' => $response['msg']]; + } + if (array_key_exists('raspis', $response)) { + foreach($response['raspis'] as $subject) { + $timetable[$subject['week1']][$subject['day1']][$subject['time1']] = $subject; + } + foreach($timetable as $week => $day) { + if (!is_array($day)) { + continue; + } + foreach($day as $dayNumber => $subject) { + $forSortSubjects = $timetable[$week][$dayNumber]; + ksort($forSortSubjects); + $timetable[$week][$dayNumber] = $forSortSubjects; + } + } + } + + +// dd($timetable); + + $getInfoOfStudent = new GetInfoOfStudent(new TestConnect()); + $infoOfStudent = $getInfoOfStudent->getInfo($user); + + $apiHash = HashHelper::getApiHash($infoOfStudent); + $recordHash = HashHelper::getRecordHash($user); + + if ($apiHash === $recordHash) { + return view('dashboard', compact('user', 'currentWeek', 'timetable')); + } + + $user->guid = $infoOfStudent['guid']; + $user->guid_faculty = $infoOfStudent['guid_faculty']; + $user->guid_group = $infoOfStudent['guid_group']; + $user->name = $infoOfStudent['name']; + $user->first_name = $infoOfStudent['first_name']; + $user->last_name = $infoOfStudent['last_name']; + $user->middle_name = $infoOfStudent['middle_name']; + $user->number_record_book = $infoOfStudent['number_record_book']; + $user->email = $infoOfStudent['email']; + $user->faculty = $infoOfStudent['faculty']; + $user->course = $infoOfStudent['course']; + $user->group = $infoOfStudent['group']; + $user->code = $infoOfStudent['code']; + $user->form_education = $infoOfStudent['form_education']; + $user->save(); + + + $hashRecord = HashRecord::firstOrCreate(['user_id' => $user->id]); + $hashRecord->hash = $apiHash; + $hashRecord->save(); + + Cache::set($user->guid, json_encode($infoOfStudent)); + return view('dashboard', compact('user', 'currentWeek', 'timetable')); + } + public function edit(Request $request): View { return view('profile.edit', [ diff --git a/app/Models/User.php b/app/Models/User.php index 90155c7..75f6307 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -4,6 +4,7 @@ namespace App\Models; // use Illuminate\Contracts\Auth\MustVerifyEmail; use Illuminate\Database\Eloquent\Factories\HasFactory; +use Illuminate\Database\Eloquent\Relations\HasOne; use Illuminate\Foundation\Auth\User as Authenticatable; use Illuminate\Notifications\Notifiable; @@ -18,9 +19,22 @@ class User extends Authenticatable * @var array */ protected $fillable = [ + 'id', 'name', 'email', 'password', + 'guid', + 'guid_faculty', + 'guid_group', + 'first_name', + 'last_name', + 'middle_name', + 'number_record_book', + 'faculty', + 'course', + 'group', + 'code', + 'form_education', ]; /** @@ -45,4 +59,9 @@ class User extends Authenticatable 'password' => 'hashed', ]; } + + public function hashRecord(): HasOne + { + return $this->hasOne(HashRecord::class); + } } diff --git a/database/migrations/0001_01_01_000000_create_users_table.php b/database/migrations/0001_01_01_000000_create_users_table.php index f66b4d2..bc3561b 100644 --- a/database/migrations/0001_01_01_000000_create_users_table.php +++ b/database/migrations/0001_01_01_000000_create_users_table.php @@ -13,7 +13,7 @@ return new class () extends Migration { Schema::create('users', function (Blueprint $table) { $table->id(); $table->string('name'); - $table->string('email')->unique(); + $table->string('email')->nullable()->unique(); $table->timestamp('email_verified_at')->nullable(); $table->string('password'); $table->rememberToken(); diff --git a/database/migrations/2024_06_20_071628_add_fields_to_user_table.php b/database/migrations/2024_06_20_071628_add_fields_to_user_table.php new file mode 100644 index 0000000..26ad3e3 --- /dev/null +++ b/database/migrations/2024_06_20_071628_add_fields_to_user_table.php @@ -0,0 +1,44 @@ +string('guid', 255)->nullable(); + $table->string('guid_faculty', 255)->nullable(); + $table->string('guid_group', 255)->nullable(); + $table->string('first_name', 255)->nullable(); + $table->string('last_name', 255)->nullable(); + $table->string('middle_name', 255)->nullable(); + $table->string('number_record_book', 255)->nullable(); + $table->string('faculty', 255)->nullable(); + $table->string('course', 255)->nullable(); + $table->string('group', 255)->nullable(); + $table->string('code', 255)->nullable(); + $table->string('form_education', 255)->nullable(); + }); + } + public function down(): void + { + Schema::table('users', function (Blueprint $table) { + $table->dropColumn( + 'guid', + 'first_name', + 'last_name', + 'middle_name', + 'number_record_book', + 'faculty', + 'guid_faculty', + 'course', + 'group', + 'guid_group', + 'code', + 'form_education', + ); + }); + } +};