Skip to content

Commit

Permalink
Merge pull request #57 from IT-Academy-BCN/feature/faqsCrud
Browse files Browse the repository at this point in the history
[BE] Faqs Crud #53
  • Loading branch information
CloudSalander authored Jun 6, 2023
2 parents 46b843e + 0014c1a commit e094e07
Show file tree
Hide file tree
Showing 7 changed files with 547 additions and 2 deletions.
114 changes: 114 additions & 0 deletions app/Http/Controllers/api/FaqController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
<?php

namespace App\Http\Controllers\api;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\Faq;
use Illuminate\Validation\ValidationException;

class FaqController extends Controller
{
/**
* Get a list of all faqs.
*
* @param
* @return \Illuminate\Http\JsonResponse
*/
public function index()
{
return response()->json(['faqs' => Faq::all()]);
}

/**
* Get an specific FAQ.
*
* @param Faq $id
* @return \Illuminate\Http\JsonResponse
*/
public function show($id)
{
$faq = Faq::find($id);

return response()->json([
'id' => $faq->id,
'title' => $faq->title,
'description' => $faq->description,
'created_at'=> $faq->created_at,
'updated_at' => $faq->updated_at
]);
}

/**
* Save a new FAQ from request.
*
* @param Request $request
* @return \Illuminate\Http\JsonResponse
*/
public function store(Request $request)
{
try {
$validatedData = $request->validate([
'title' => ['required', 'string', 'max:255'],
'description' => ['required', 'string'],
]);

$faq = Faq::create($validatedData);

return response()->json(['faq' => $faq], 201);
} catch (ValidationException $e) {
return response()->json(['errors' => $e->errors()], 422);
}
}

/**
* Update specific FAQ.
*
* @param Request $request
* @param Faq $id
* @return \Illuminate\Http\JsonResponse
*/
public function update(Request $request, $id)
{
try {
$faqs = Faq::find($id);

if (!$faqs) {
return response()->json(['error' => 'FAQ not found'], 404);
}

$validatedData = $request->validate([
'title' => 'required|string|max:255',
'description' => 'required|string',
]);

$faqs->title = $validatedData['title'];
$faqs->description = $validatedData['description'];
$faqs->save();

return response()->json(['message' => 'FAQ updated successfully']);
} catch (ValidationException $e) {
return response()->json(['errors' => $e->errors()], 422);
}
}

/**
* Delete a specific FAQ.
*
* @param Request $request
* @param Faq $id
* @return \Illuminate\Http\JsonResponse
*/
public function destroy($id)
{
$faqs = Faq::find($id);

if (!$faqs) {
return response()->json(['error' => 'FAQ not found'], 404);
}

$faqs->delete();

return response()->json(['message' => 'FAQ deleted successfully']);
}
}
21 changes: 21 additions & 0 deletions app/Models/Faq.php
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 Faq extends Model
{
use HasFactory;

/**
* The attributes that are mass assignable.
*
* @var array<int, string>
*/
protected $fillable = [
'title',
'description'
];
}
24 changes: 24 additions & 0 deletions database/factories/FaqFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace Database\Factories;

use Illuminate\Database\Eloquent\Factories\Factory;

/**
* @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\Faq>
*/
class FaqFactory extends Factory
{
/**
* Define the model's default state.
*
* @return array<string, mixed>
*/
public function definition(): array
{
return [
'title' => fake()->sentence,
'description' => fake()->paragraph
];
}
}
2 changes: 2 additions & 0 deletions database/factories/UserFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ public function definition(): array
'email' => fake()->unique()->safeEmail(),
'dni' => $this->faker->regexify('[0-9]{8}[A-Z]'),
'password' => Hash::make('password'),
'status' => 'ACTIVE',
'role' => 'ADMIN',
'email_verified_at' => now(),
'remember_token' => Str::random(10),
];
Expand Down
1 change: 1 addition & 0 deletions database/seeders/DatabaseSeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,6 @@ public function run(): void
// \App\Models\User::factory(10)->create();

\App\Models\User::factory(3)->create();
\App\Models\Faq::factory(3)->create();
}
}
9 changes: 7 additions & 2 deletions routes/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\api\UserController;
use App\Http\Controllers\api\FaqController;
use App\Http\Controllers\api\AuthController;

/*
Expand All @@ -19,6 +20,10 @@
Route::post('/register', [UserController::class, 'store'])->name('register');
Route::post('/login', [AuthController::class, 'login'])->name('login');

Route::middleware('auth:sanctum')->get('/user', function (Request $request) {
return $request->user();
Route::middleware(['auth:api'])->prefix('faqs')->group(function () {
Route::get('/', [FaqController::class, 'index']);
Route::get('/{id}', [FaqController::class, 'show']);
Route::post('/', [FaqController::class, 'store']);
Route::put('/{id}', [FaqController::class, 'update']);
Route::delete('/{id}', [FaqController::class, 'destroy']);
});
Loading

0 comments on commit e094e07

Please sign in to comment.