-
Notifications
You must be signed in to change notification settings - Fork 0
hw18 - Testing api #214
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ivanzn55
wants to merge
1
commit into
IZolotarev/hw16
Choose a base branch
from
IZolotarev/hw18
base: IZolotarev/hw16
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
hw18 - Testing api #214
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,98 @@ | ||
| <?php | ||
|
|
||
| namespace App\Http\API\V1; | ||
|
|
||
| use App\Http\Controllers\Controller; | ||
| use App\Models\User; | ||
| use App\Models\UserProfile; | ||
| use Illuminate\Http\Request; | ||
| use Illuminate\Support\Facades\Validator; | ||
| use Illuminate\Contracts\Auth\Authenticatable; | ||
|
|
||
| class PersonalCabinetController extends Controller | ||
| { | ||
| /** | ||
| * Получить данные пользователя | ||
| * | ||
| * @param Authenticatable $user Авторизованный пользователь | ||
| * @return \Illuminate\Http\JsonResponse | ||
| */ | ||
| public function index(Authenticatable $user) | ||
| { | ||
| $user = $user->load('profile'); | ||
|
|
||
| return response()->json([ | ||
| 'user' => $user, | ||
| 'profile' => $user->profile, | ||
| ]); | ||
| } | ||
|
|
||
| /** | ||
| * Обновить профиль пользователя | ||
| * | ||
| * @param Request $request | ||
| * @param User $user Авторизованный пользователь | ||
| * @return \Illuminate\Http\JsonResponse | ||
| */ | ||
| public function update(Request $request, Authenticatable $user) | ||
| { | ||
| $validator = Validator::make($request->all(), [ | ||
| 'name' => 'sometimes|string|max:255', | ||
| 'email' => 'sometimes|string|email|max:255|unique:users,email,' . $user->id, | ||
| 'phone' => 'nullable|string|max:20', | ||
| 'address' => 'nullable|string|max:255', | ||
| 'birth_date' => 'nullable|date', | ||
| 'about' => 'nullable|string', | ||
| ]); | ||
|
|
||
| if ($validator->fails()) { | ||
| return response()->json($validator->errors(), 422); | ||
| } | ||
|
|
||
| // Обновляем основные данные пользователя | ||
| if ($request->has('name')) { | ||
| $user->name = $request->name; | ||
| } | ||
|
|
||
| if ($request->has('email')) { | ||
| $user->email = $request->email; | ||
| } | ||
|
|
||
| $user->save(); | ||
|
|
||
| // Обновляем или создаем профиль | ||
| $profileData = $request->only(['phone', 'address', 'birth_date', 'about']); | ||
|
|
||
| if ($user->profile) { | ||
| $user->profile->update($profileData); | ||
| } else { | ||
| $profileData['user_id'] = $user->id; | ||
| UserProfile::create($profileData); | ||
| } | ||
|
|
||
| return response()->json([ | ||
| 'message' => 'Profile updated successfully', | ||
| 'user' => $user->load('profile'), | ||
| ]); | ||
| } | ||
|
|
||
| /** | ||
| * Удалить аккаунт пользователя | ||
| * | ||
| * @param User $user Авторизованный пользователь | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Этого параметра в фукнции нет |
||
| * @return \Illuminate\Http\JsonResponse | ||
| */ | ||
| public function destroy() | ||
| { | ||
| // Получаем аутентифицированного пользователя | ||
| $user = auth('jwt')->user(); | ||
|
|
||
| if ($user) { | ||
| $user->delete(); | ||
| } | ||
|
|
||
| return response()->json([ | ||
| 'message' => 'Account deleted successfully', | ||
| ]); | ||
| } | ||
| } | ||
This file contains hidden or 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 hidden or 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,52 @@ | ||
| <?php | ||
|
|
||
| namespace App\Models; | ||
|
|
||
| use Illuminate\Database\Eloquent\Factories\HasFactory; | ||
| use Illuminate\Database\Eloquent\Model; | ||
|
|
||
| /** | ||
| * App\Models\UserProfile | ||
| * | ||
| * @property int $id | ||
| * @property int $user_id | ||
| * @property string|null $phone | ||
| * @property string|null $address | ||
| * @property \Illuminate\Support\Carbon|null $birth_date | ||
| * @property string|null $about | ||
| * @property \Illuminate\Support\Carbon|null $created_at | ||
| * @property \Illuminate\Support\Carbon|null $updated_at | ||
| * @property-read \App\Models\User $user | ||
| * | ||
| * @method static \Database\Factories\UserProfileFactory factory(...$parameters) | ||
| * @method static \Illuminate\Database\Eloquent\Builder|UserProfile newModelQuery() | ||
| * @method static \Illuminate\Database\Eloquent\Builder|UserProfile newQuery() | ||
| * @method static \Illuminate\Database\Eloquent\Builder|UserProfile query() | ||
| * @method static \Illuminate\Database\Eloquent\Builder|UserProfile whereAbout($value) | ||
| * @method static \Illuminate\Database\Eloquent\Builder|UserProfile whereAddress($value) | ||
| * @method static \Illuminate\Database\Eloquent\Builder|UserProfile whereBirthDate($value) | ||
| * @method static \Illuminate\Database\Eloquent\Builder|UserProfile whereCreatedAt($value) | ||
| * @method static \Illuminate\Database\Eloquent\Builder|UserProfile whereId($value) | ||
| * @method static \Illuminate\Database\Eloquent\Builder|UserProfile wherePhone($value) | ||
| * @method static \Illuminate\Database\Eloquent\Builder|UserProfile whereUpdatedAt($value) | ||
| * @method static \Illuminate\Database\Eloquent\Builder|UserProfile whereUserId($value) | ||
| * | ||
| * @mixin \Eloquent | ||
| */ | ||
| class UserProfile extends Model | ||
| { | ||
| use HasFactory; | ||
|
|
||
| protected $fillable = [ | ||
| 'user_id', | ||
| 'phone', | ||
| 'address', | ||
| 'birth_date', | ||
| 'about', | ||
| ]; | ||
|
|
||
| public function user() | ||
| { | ||
| return $this->belongsTo(User::class); | ||
| } | ||
| } |
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Сериализация отдана на откуп элоквент. он конечно выдаст в соответствии с полями в базе. Но это делает ваш код хрупким, потому что формат выдачи API зависит от структуры базы неявно.