add new fields to User
Tests & Lint & Deploy to Railway / build (2.6.6, 20.x, 8.3) (push) Failing after 59s Details
Tests & Lint & Deploy to Railway / deploy (push) Has been skipped Details

This commit is contained in:
aslan 2024-07-12 12:45:23 +03:00
parent 1b684b9d6f
commit 24922b7418
6 changed files with 178 additions and 4 deletions

21
app/Enums/UserFields.php Normal file
View File

@ -0,0 +1,21 @@
<?php
namespace App\Enums;
enum UserFields: string
{
case guid = 'guid';
case guid_faculty = 'guid_faculty';
case guid_group = 'guid_group';
case name = 'name';
case first_name = 'first_name';
case last_name = 'last_name';
case middle_name = 'middle_name';
case number_record_book = 'number_record_book';
case email = 'email';
case faculty = 'faculty';
case course = 'course';
case group = 'group';
case code = 'code';
case form_education = 'form_education';
}

View File

@ -0,0 +1,16 @@
<?php
namespace App\Helpers;
use App\Enums\UserFields;
class AllfieldsUserHelper
{
public static function get()
{
return array_reduce(UserFields::cases(), function ($carry, $item) {
$carry[$item->name] = $item->value;
return $carry;
}, []);
}
}

View File

@ -2,18 +2,92 @@
namespace App\Http\Controllers; namespace App\Http\Controllers;
use App\Helpers\HashHelper;
use App\Http\Requests\ProfileUpdateRequest; use App\Http\Requests\ProfileUpdateRequest;
use App\Models\HashRecord;
use App\Services\GetInfoOfStudent;
use App\Services\TestConnect;
use Illuminate\Http\RedirectResponse; use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request; use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth; use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Redirect; use Illuminate\Support\Facades\Redirect;
use Illuminate\View\View; use Illuminate\View\View;
class ProfileController extends Controller class ProfileController extends Controller
{ {
/** public function show(): View
* Display the user's profile form. {
*/ $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 public function edit(Request $request): View
{ {
return view('profile.edit', [ return view('profile.edit', [

View File

@ -4,6 +4,7 @@ namespace App\Models;
// use Illuminate\Contracts\Auth\MustVerifyEmail; // use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Relations\HasOne;
use Illuminate\Foundation\Auth\User as Authenticatable; use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable; use Illuminate\Notifications\Notifiable;
@ -18,9 +19,22 @@ class User extends Authenticatable
* @var array<int, string> * @var array<int, string>
*/ */
protected $fillable = [ protected $fillable = [
'id',
'name', 'name',
'email', 'email',
'password', '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', 'password' => 'hashed',
]; ];
} }
public function hashRecord(): HasOne
{
return $this->hasOne(HashRecord::class);
}
} }

View File

@ -13,7 +13,7 @@ return new class () extends Migration {
Schema::create('users', function (Blueprint $table) { Schema::create('users', function (Blueprint $table) {
$table->id(); $table->id();
$table->string('name'); $table->string('name');
$table->string('email')->unique(); $table->string('email')->nullable()->unique();
$table->timestamp('email_verified_at')->nullable(); $table->timestamp('email_verified_at')->nullable();
$table->string('password'); $table->string('password');
$table->rememberToken(); $table->rememberToken();

View File

@ -0,0 +1,44 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class () extends Migration {
public function up(): void
{
Schema::table('users', function (Blueprint $table) {
$table->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',
);
});
}
};