add login logic
This commit is contained in:
19
app/Http/Controllers/Api/CommentsController.php
Normal file
19
app/Http/Controllers/Api/CommentsController.php
Normal file
@@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Api;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
class CommentsController extends Controller
|
||||
{
|
||||
public function store(Request $request, int $id): JsonResponse
|
||||
{
|
||||
return response()->json([
|
||||
'id' => 1,
|
||||
'text' => $request->get('text'),
|
||||
'post_id' => $id
|
||||
], Response::HTTP_CREATED);
|
||||
}
|
||||
}
|
||||
67
app/Http/Controllers/Api/PostsController.php
Normal file
67
app/Http/Controllers/Api/PostsController.php
Normal file
@@ -0,0 +1,67 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Api;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Http\Response;
|
||||
|
||||
class PostsController extends Controller
|
||||
{
|
||||
public function index(): JsonResponse
|
||||
{
|
||||
return response()->json([
|
||||
[
|
||||
'id' => 1,
|
||||
'title' => 'My first post',
|
||||
],
|
||||
[
|
||||
'id' => 2,
|
||||
'title' => 'New post',
|
||||
]
|
||||
]);
|
||||
}
|
||||
|
||||
public function destroy(int $id): JsonResponse
|
||||
{
|
||||
// мы бы здесь написали вызов запроса delete из БД
|
||||
return response()->json([
|
||||
'success' => true,
|
||||
], Response::HTTP_ACCEPTED);
|
||||
}
|
||||
|
||||
public function store(Request $request): JsonResponse
|
||||
{
|
||||
return response()->json([
|
||||
'title' => $request->get('title'),
|
||||
'text' => $request->get('text'),
|
||||
], Response::HTTP_CREATED);
|
||||
}
|
||||
|
||||
public function update(Request $request, int $id): JsonResponse
|
||||
{
|
||||
return response()->json([
|
||||
'title' => $request->get('title'),
|
||||
'text' => $request->get('text'),
|
||||
], Response::HTTP_ACCEPTED);
|
||||
}
|
||||
|
||||
public function show(int $id): JsonResponse
|
||||
{
|
||||
if ($id === 5) {
|
||||
return response()->json(
|
||||
[
|
||||
'status' => 'Not found'
|
||||
],
|
||||
Response::HTTP_NOT_FOUND
|
||||
);
|
||||
}
|
||||
|
||||
return response()->json([
|
||||
'id' => $id,
|
||||
'title' => 'My first post',
|
||||
'text' => 'My first post text',
|
||||
]);
|
||||
}
|
||||
}
|
||||
30
app/Http/Controllers/ProjectsController.php
Normal file
30
app/Http/Controllers/ProjectsController.php
Normal file
@@ -0,0 +1,30 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Models\Project;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
class ProjectsController extends Controller
|
||||
{
|
||||
public function index()
|
||||
{
|
||||
return response()->json(Project::all()->toJson());
|
||||
}
|
||||
|
||||
public function create(Request $request)
|
||||
{
|
||||
$title = $request->get('title');
|
||||
$description = $request->get('description');
|
||||
$creatorUserId = 1;
|
||||
|
||||
$project = new Project();
|
||||
$project->title = $title;
|
||||
$project->description = $description;
|
||||
$project->creator_user_id = $creatorUserId;
|
||||
|
||||
$project->save();
|
||||
|
||||
return response()->json($project->toJson());
|
||||
}
|
||||
}
|
||||
25
app/Http/Controllers/UsersController.php
Normal file
25
app/Http/Controllers/UsersController.php
Normal file
@@ -0,0 +1,25 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Models\User;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Hash;
|
||||
|
||||
class UsersController extends Controller
|
||||
{
|
||||
public function create(Request $request)
|
||||
{
|
||||
$user = new User();
|
||||
$name = $request->get('name');
|
||||
$email = $request->get('email');
|
||||
$password = Hash::make($request->get('password'));
|
||||
|
||||
$user->name = $name;
|
||||
$user->email = $email;
|
||||
$user->password = $password;
|
||||
$user->save();
|
||||
|
||||
return ['token' => $user->createToken('frontend')->plainTextToken];
|
||||
}
|
||||
}
|
||||
15
app/Models/Project.php
Normal file
15
app/Models/Project.php
Normal file
@@ -0,0 +1,15 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
class Project extends Model
|
||||
{
|
||||
//
|
||||
protected $fillable = [
|
||||
'title',
|
||||
'description',
|
||||
'creator_user_id',
|
||||
];
|
||||
}
|
||||
@@ -6,11 +6,12 @@ namespace App\Models;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Foundation\Auth\User as Authenticatable;
|
||||
use Illuminate\Notifications\Notifiable;
|
||||
use Laravel\Sanctum\HasApiTokens;
|
||||
|
||||
class User extends Authenticatable
|
||||
{
|
||||
/** @use HasFactory<\Database\Factories\UserFactory> */
|
||||
use HasFactory, Notifiable;
|
||||
use HasFactory, Notifiable, HasApiTokens;
|
||||
|
||||
/**
|
||||
* The attributes that are mass assignable.
|
||||
|
||||
Reference in New Issue
Block a user