-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #57 from IT-Academy-BCN/feature/faqsCrud
[BE] Faqs Crud #53
- Loading branch information
Showing
7 changed files
with
547 additions
and
2 deletions.
There are no files selected for viewing
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,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']); | ||
} | ||
} |
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 Faq extends Model | ||
{ | ||
use HasFactory; | ||
|
||
/** | ||
* The attributes that are mass assignable. | ||
* | ||
* @var array<int, string> | ||
*/ | ||
protected $fillable = [ | ||
'title', | ||
'description' | ||
]; | ||
} |
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,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 | ||
]; | ||
} | ||
} |
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
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
Oops, something went wrong.