-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
20 changed files
with
567 additions
and
84 deletions.
There are no files selected for viewing
38 changes: 38 additions & 0 deletions
38
app/Http/Controllers/Web/Account/EditProfileController.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<?php | ||
|
||
namespace App\Http\Controllers\Web\Account; | ||
|
||
use App\Http\Controllers\Controller; | ||
use App\Http\Requests\Account\EditProfileRequest; | ||
use App\Models\ProfileInfoModel; | ||
use Illuminate\Http\Request; | ||
|
||
class EditProfileController extends Controller { | ||
public function edit(EditProfileRequest $request) { | ||
$profile_info = ProfileInfoModel::updateOrCreate( | ||
['user_id' => auth()->user()->id], | ||
[ | ||
'bio' => $request->post('bio'), | ||
'link' => $request->post('link') | ||
] | ||
); | ||
|
||
auth()->user()->name = $request->name; | ||
$save = auth()->user()->save(); | ||
|
||
|
||
if($profile_info && $save) { | ||
return response()->json([ | ||
'status' => true, | ||
'type' => 'success', | ||
'message' => 'Данные успешно сохранены.' | ||
]); | ||
} | ||
|
||
return response()->json([ | ||
'status' => false, | ||
'type' => 'error', | ||
'message' => 'Произошла ошибка. Попробуйте еще раз.' | ||
]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?php | ||
|
||
namespace App\Http\Controllers\Web\Account; | ||
|
||
use App\Http\Controllers\Controller; | ||
use App\Models\User; | ||
use Illuminate\Http\Request; | ||
|
||
class ProfileController extends Controller | ||
{ | ||
public function render(User $user) { | ||
$profile_info = $user->profileInfo; | ||
return view('pages/account/profile', compact('user', 'profile_info')); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
|
||
namespace App\Http\Controllers\Web\Follow; | ||
|
||
use App\Http\Controllers\Controller; | ||
use App\Models\User; | ||
use Illuminate\Http\Request; | ||
|
||
class FollowsController extends Controller | ||
{ | ||
public function follow(User $user) { | ||
auth()->user()->toggleFollow($user); | ||
|
||
return response()->json([ | ||
'status' => true | ||
]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
<?php | ||
|
||
namespace App\Http\Requests\Account; | ||
|
||
use Illuminate\Contracts\Validation\Validator; | ||
use Illuminate\Foundation\Http\FormRequest; | ||
use Illuminate\Http\Exceptions\HttpResponseException; | ||
use Illuminate\Support\Facades\Auth; | ||
use Illuminate\Validation\Rules\Password; | ||
|
||
class EditProfileRequest extends FormRequest | ||
{ | ||
public function authorize() : bool { | ||
return Auth::check(); | ||
} | ||
|
||
public function rules(): array { | ||
return [ | ||
'name' => 'required|min:3|string|max:255', | ||
'bio' => 'nullable|string|min:5|max:255', | ||
'link' => 'nullable|active_url' | ||
]; | ||
} | ||
|
||
public function attributes() : array { | ||
return [ | ||
'name' => 'Имя', | ||
'bio' => 'Описание', | ||
'link' => 'Ссылка' | ||
]; | ||
} | ||
|
||
public function messages() : array { | ||
return [ | ||
'required' => ":attribute является обязательным полем.", | ||
'min' => ':attribute должен быть минимум :min символов.', | ||
'string' => ':attribute должен быть строкой.', | ||
'max' => ':attribute может быть максимум :max символов.', | ||
'active_url' => ':attribute должна быть рабочей ссылкой.' | ||
]; | ||
} | ||
|
||
protected function failedValidation(Validator $validator) : void { | ||
throw new HttpResponseException(response()->json([ | ||
'status' => false, | ||
'type' => 'error', | ||
'message' => $validator->errors()->first() | ||
])); | ||
|
||
} | ||
|
||
public function failedAuthorization() : void { | ||
throw new HttpResponseException(response()->json([ | ||
'status' => false, | ||
'type' => 'error', | ||
'message' => 'Вы не можете выполнить данное действие.' | ||
])); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?php | ||
|
||
namespace App\Models; | ||
|
||
use Illuminate\Database\Eloquent\Factories\HasFactory; | ||
use Illuminate\Database\Eloquent\Model; | ||
|
||
class ProfileInfoModel extends Model | ||
{ | ||
protected $table = 'profile_info'; | ||
|
||
protected $primaryKey = 'user_id'; | ||
|
||
protected $fillable = [ | ||
'user_id', | ||
'bio', | ||
'link' | ||
]; | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<?php | ||
|
||
namespace App\Traits; | ||
|
||
use App\Models\User; | ||
|
||
trait Followable | ||
{ | ||
public function follows() | ||
{ | ||
return $this->belongsToMany(User::class, 'follows', | ||
'user_id', 'following_user_id'); | ||
} | ||
|
||
public function follow(User $user) | ||
{ | ||
return $this->follows()->save($user); | ||
} | ||
|
||
public function unfollow(User $user) | ||
{ | ||
return $this->follows()->detach($user); | ||
} | ||
|
||
public function toggleFollow(User $user) { | ||
if($this->isFollowing($user)) { | ||
return $this->unfollow($user); | ||
} | ||
|
||
return $this->follow($user); | ||
} | ||
|
||
|
||
public function isFollowing(User $user) | ||
{ | ||
return $this->follows()->where('following_user_id', $user->id)->exists(); | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
database/migrations/2023_08_26_213732_profile_info_migration.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?php | ||
|
||
use Illuminate\Database\Migrations\Migration; | ||
use Illuminate\Database\Schema\Blueprint; | ||
use Illuminate\Support\Facades\Schema; | ||
|
||
return new class extends Migration | ||
{ | ||
/** | ||
* Run the migrations. | ||
*/ | ||
public function up(): void | ||
{ | ||
Schema::create('profile_info', function (Blueprint $table) { | ||
$table->primary('user_id'); | ||
$table->string('bio')->nullable(); | ||
$table->string('link')->nullable(); | ||
$table->foreignId('user_id'); | ||
$table->timestamps(); | ||
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade'); | ||
}); | ||
} | ||
|
||
/** | ||
* Reverse the migrations. | ||
*/ | ||
public function down(): void | ||
{ | ||
// | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
$('#edit_profile_form').submit(function (e) { | ||
e.preventDefault(); | ||
|
||
const _self = $(this); | ||
const submit_button = _self.find(':submit'); | ||
submit_button.addClass('disabled'); | ||
submit_button.prop('disabled', true); | ||
|
||
const url = _self.attr('action'); | ||
const data = new FormData(this); | ||
|
||
axios.post(url, data) | ||
.then(data => { | ||
if(data.data.status) { | ||
_self.closest('#edit_profile-modal').find('button[data-modal-hide]').trigger('click'); //закрытие модального окна | ||
createToast({...data.data}); | ||
} else { | ||
createToast({...data.data}); | ||
} | ||
}) | ||
.catch(() => { | ||
createToast({message: "Произошла ошибка. Попробуйте еще раз.", type: "error"}); | ||
}) | ||
.finally(() => { | ||
submit_button.removeClass('disabled'); | ||
submit_button.prop('disabled', false); | ||
}); | ||
}); |
Oops, something went wrong.