-
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
9 changed files
with
203 additions
and
11 deletions.
There are no files selected for viewing
109 changes: 109 additions & 0 deletions
109
app/Http/Controllers/Api/Admin/ExperienceLevelsController.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,109 @@ | ||
<?php | ||
|
||
namespace App\Http\Controllers\Api\Admin; | ||
|
||
use App\Http\Controllers\Controller; | ||
use App\Models\ExperienceLevel; // Đảm bảo rằng bạn đã import model này | ||
use Illuminate\Http\Request; | ||
use Illuminate\Support\Facades\Validator; | ||
|
||
class ExperienceLevelsController extends Controller | ||
{ | ||
// Hiển thị danh sách các cấp độ kinh nghiệm | ||
public function index() | ||
{ | ||
$experienceLevels = ExperienceLevel::all(); | ||
return response()->json([ | ||
'success' => true, | ||
'message' => "Lấy danh sách cấp độ kinh nghiệm thành công", | ||
'data' => $experienceLevels, | ||
'status_code' => 200 | ||
]); | ||
} | ||
|
||
// Tạo cấp độ kinh nghiệm mới | ||
public function store(Request $request) | ||
{ | ||
$data = $request->only('name'); // Lấy dữ liệu từ request | ||
|
||
$validator = Validator::make($data, [ | ||
'name' => 'required|string|min:3|max:50', | ||
], [ | ||
'name.required' => 'Trường tên là bắt buộc.', | ||
'name.min' => 'Tên cấp độ kinh nghiệm phải có ít nhất :min ký tự.', | ||
'name.max' => 'Tên cấp độ kinh nghiệm không được vượt quá :max ký tự.', | ||
]); | ||
|
||
if ($validator->fails()) { | ||
return response()->json([ | ||
'success' => false, | ||
'message' => 'Lỗi xác thực', | ||
'errors' => $validator->errors(), | ||
'status_code' => 400 | ||
], 400); | ||
} | ||
|
||
$experienceLevel = ExperienceLevel::create($validator->validated()); | ||
|
||
return response()->json([ | ||
'success' => true, | ||
'message' => "Tạo cấp độ kinh nghiệm thành công!", | ||
'data' => $experienceLevel, | ||
'status_code' => 200 | ||
]); | ||
} | ||
|
||
// Hiển thị chi tiết một cấp độ kinh nghiệm | ||
public function show(ExperienceLevel $experienceLevel) | ||
{ | ||
return response()->json([ | ||
'success' => true, | ||
'message' => 'Lấy thông tin cấp độ kinh nghiệm thành công', | ||
'data' => $experienceLevel, | ||
'status_code' => 200 | ||
]); | ||
} | ||
|
||
// Cập nhật thông tin cấp độ kinh nghiệm | ||
public function update(Request $request, ExperienceLevel $experienceLevel) | ||
{ | ||
$data = $request->only('name'); | ||
|
||
$validator = Validator::make($data, [ | ||
'name' => 'required|string|min:3|max:50', | ||
], [ | ||
'name.required' => 'Trường tên là bắt buộc.', | ||
'name.min' => 'Tên cấp độ kinh nghiệm phải có ít nhất :min ký tự.', | ||
'name.max' => 'Tên cấp độ kinh nghiệm không được vượt quá :max ký tự.', | ||
]); | ||
|
||
if ($validator->fails()) { | ||
return response()->json([ | ||
'success' => false, | ||
'message' => 'Lỗi xác thực', | ||
'errors' => $validator->errors(), | ||
'status_code' => 400 | ||
], 400); | ||
} | ||
|
||
$experienceLevel->update($validator->validated()); | ||
|
||
return response()->json([ | ||
'success' => true, | ||
'message' => 'Cập nhật cấp độ kinh nghiệm thành công', | ||
'data' => $experienceLevel, | ||
'status_code' => 200 | ||
]); | ||
} | ||
|
||
// Xóa cấp độ kinh nghiệm | ||
public function destroy(ExperienceLevel $experienceLevel) | ||
{ | ||
$experienceLevel->delete(); | ||
return response()->json([ | ||
'success' => true, | ||
'message' => 'Xóa cấp độ kinh nghiệm thành công', | ||
'status_code' => 200 | ||
]); | ||
} | ||
} |
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,16 @@ | ||
<?php | ||
|
||
namespace App\Models; | ||
|
||
use Illuminate\Database\Eloquent\Factories\HasFactory; | ||
use Illuminate\Database\Eloquent\Model; | ||
|
||
class ExperienceLevel extends Model | ||
{ | ||
use HasFactory; | ||
protected $table = 'experience_levels'; | ||
protected $primaryKey = 'id'; | ||
protected $guarded = []; | ||
|
||
|
||
} |
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
28 changes: 28 additions & 0 deletions
28
database/migrations/2024_10_15_051733_create_experience_levels_table.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,28 @@ | ||
<?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('experience_levels', function (Blueprint $table) { | ||
$table->id(); | ||
$table->string("name"); | ||
$table->timestamps(); | ||
}); | ||
} | ||
|
||
/** | ||
* Reverse the migrations. | ||
*/ | ||
public function down(): void | ||
{ | ||
Schema::dropIfExists('experience_levels'); | ||
} | ||
}; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php | ||
|
||
namespace Database\Seeders; | ||
|
||
use Illuminate\Database\Console\Seeds\WithoutModelEvents; | ||
use Illuminate\Database\Seeder; | ||
use Illuminate\Support\Facades\DB; | ||
|
||
class ExperienceLevelsSeeder extends Seeder | ||
{ | ||
/** | ||
* Run the database seeds. | ||
*/ | ||
public function run() | ||
{ | ||
// Dữ liệu cho bảng experience_levels | ||
$experienceLevels = [ | ||
['name' => '1-2 năm'], | ||
['name' => '3-5 năm'], | ||
['name' => '6-8 năm'], | ||
['name' => '9-11 năm'], | ||
['name' => '12+ năm'], | ||
]; | ||
|
||
// Chèn dữ liệu vào bảng | ||
DB::table('experience_levels')->insert($experienceLevels); | ||
} | ||
} |
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