Tạo api trang criterias and test report
This commit is contained in:
		
							parent
							
								
									54dca3d271
								
							
						
					
					
						commit
						8c24cffe95
					
				| 
						 | 
				
			
			@ -0,0 +1,191 @@
 | 
			
		|||
<?php
 | 
			
		||||
 | 
			
		||||
namespace Modules\Admin\app\Http\Controllers;
 | 
			
		||||
 | 
			
		||||
use App\Http\Controllers\Controller;
 | 
			
		||||
use App\Traits\AnalyzeData;
 | 
			
		||||
use App\Traits\HasFilterRequest;
 | 
			
		||||
use App\Traits\HasOrderByRequest;
 | 
			
		||||
use App\Traits\HasSearchRequest;
 | 
			
		||||
use Carbon\Carbon;
 | 
			
		||||
use Carbon\CarbonPeriod;
 | 
			
		||||
use Illuminate\Http\Request;
 | 
			
		||||
use Modules\Admin\app\Models\Criteria;
 | 
			
		||||
use Modules\Admin\app\Models\Sprint;
 | 
			
		||||
use Modules\Admin\app\Models\SprintCriteria;
 | 
			
		||||
use Modules\Admin\app\Models\UserCriteria;
 | 
			
		||||
 | 
			
		||||
class CriteriasController extends Controller
 | 
			
		||||
{
 | 
			
		||||
    use HasOrderByRequest;
 | 
			
		||||
    use HasFilterRequest;
 | 
			
		||||
    use HasSearchRequest;
 | 
			
		||||
    use AnalyzeData;
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Get all criterias of a sprint
 | 
			
		||||
     *
 | 
			
		||||
     * @param int $sprintId The id of the sprint
 | 
			
		||||
     * @return \Illuminate\Database\Eloquent\Collection|null A collection of Criteria models or null if the sprint is not found
 | 
			
		||||
     */
 | 
			
		||||
    public function getCriteriasForSprint($sprintId)
 | 
			
		||||
    {
 | 
			
		||||
        $sprint = Sprint::with('criterias')->find($sprintId);
 | 
			
		||||
        // Nếu không tìm thấy sprint, trả về response lỗi
 | 
			
		||||
        if (!$sprint) {
 | 
			
		||||
            return AbstractController::ResultError('Sprint not found', [], 404);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        // Trả về thông tin sprint và criterias
 | 
			
		||||
        return AbstractController::ResultSuccess($sprint);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function convertDataReponse($criterias)
 | 
			
		||||
    {
 | 
			
		||||
        $data = [];
 | 
			
		||||
        foreach ($criterias as $key => $criteria) {
 | 
			
		||||
            $data[$key]["criteria_name"] = $criteria->name;
 | 
			
		||||
            $data[$key]["description"] = $criteria->description;
 | 
			
		||||
            $data[$key]["type"] = $criteria->type;
 | 
			
		||||
            foreach ($criteria->users as $keyChild => $user) {
 | 
			
		||||
                $dataChild[$keyChild]["user_id"] = $user->id;
 | 
			
		||||
                $dataChild[$keyChild]["user_name"] = $user->name;
 | 
			
		||||
                $dataChild[$keyChild]["email"] = $user->email;
 | 
			
		||||
                $dataChild[$keyChild]["point"] = $user->pivot->point;
 | 
			
		||||
                $dataChild[$keyChild]["note"] = $user->pivot->note;
 | 
			
		||||
                $dataChild[$keyChild]["sprint_id"] = $user->pivot->sprint_id;
 | 
			
		||||
            }
 | 
			
		||||
            $data[$key]['users'] = $dataChild;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        return $data;
 | 
			
		||||
    }
 | 
			
		||||
    /**
 | 
			
		||||
     * Get all criterias of a user
 | 
			
		||||
     *
 | 
			
		||||
     * @param int $userId The id of the user
 | 
			
		||||
     * @return \Illuminate\Database\Eloquent\Collection|null A collection of Criteria models or null if the user is not found
 | 
			
		||||
     */
 | 
			
		||||
    public function getCriteriasForUser($userId)
 | 
			
		||||
    {
 | 
			
		||||
        $criterias = Criteria::whereHas('users', function ($query) use ($userId) {
 | 
			
		||||
            $query->where('user_id', $userId);
 | 
			
		||||
        })->with(['users' => function ($query) use ($userId) {
 | 
			
		||||
            $query->where('user_id', $userId);
 | 
			
		||||
        }])->get();
 | 
			
		||||
 | 
			
		||||
        $data = self::convertDataReponse($criterias);
 | 
			
		||||
        return AbstractController::ResultSuccess($data);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Get all criterias of a user for a sprint
 | 
			
		||||
     *
 | 
			
		||||
     * @param int $userId The id of the user
 | 
			
		||||
     * @param int $sprintId The id of the sprint
 | 
			
		||||
     * @return \Illuminate\Database\Eloquent\Collection|null A collection of Criteria models or null if the user or sprint is not found
 | 
			
		||||
     */
 | 
			
		||||
    public function getCriteriasForUserBySprint($userId, $sprintId)
 | 
			
		||||
    {
 | 
			
		||||
        $criterias = Criteria::whereHas('users', function ($query) use ($userId, $sprintId) {
 | 
			
		||||
            $query->where('user_id', $userId)->where('sprint_id', $sprintId);
 | 
			
		||||
        })->with(['users' => function ($query) use ($userId, $sprintId) {
 | 
			
		||||
            $query->where('user_id', $userId)->where('sprint_id', $sprintId);
 | 
			
		||||
        }])->get();
 | 
			
		||||
 | 
			
		||||
        $data = self::convertDataReponse($criterias);
 | 
			
		||||
        return AbstractController::ResultSuccess($data);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Get all criterias
 | 
			
		||||
     *
 | 
			
		||||
     * @return \Illuminate\Database\Eloquent\Collection A collection of Criteria models
 | 
			
		||||
     */
 | 
			
		||||
    public function getAllCriterias(Request $request)
 | 
			
		||||
    {
 | 
			
		||||
        if ($request->type) {
 | 
			
		||||
            $responseData = Criteria::where('type', $request->type)->get();
 | 
			
		||||
        } else {
 | 
			
		||||
            $responseData = Criteria::all();
 | 
			
		||||
        }
 | 
			
		||||
        return AbstractController::ResultSuccess($responseData);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Update or create multiple SprintCriteria and UserCriteria records
 | 
			
		||||
     *
 | 
			
		||||
     * @param int $sprintId The id of the sprint
 | 
			
		||||
     * @param array $criteriaData An array of criteria data. Each element of the array should be an associative array with the following keys:
 | 
			
		||||
     *     - criteria_id: The id of the criteria
 | 
			
		||||
     *     - point: The point of the criteria
 | 
			
		||||
     *     - expect_result: The expected result of the criteria
 | 
			
		||||
     *     - actual_result: The actual result of the criteria
 | 
			
		||||
     *     - note: The note of the criteria
 | 
			
		||||
     *     - user_id (optional): The id of the user
 | 
			
		||||
     *     - user_point (optional): The point of the user (default is the same as point)
 | 
			
		||||
     *     - user_note (optional): The note of the user (default is the same as note)
 | 
			
		||||
     * @return bool True if successful, false otherwise
 | 
			
		||||
     */
 | 
			
		||||
    public function updateCriteriasForSprint($sprintId, Request $request)
 | 
			
		||||
    {
 | 
			
		||||
        $user = auth('admins')->user();
 | 
			
		||||
 | 
			
		||||
        // Tạo hoặc lấy Sprint
 | 
			
		||||
        $sprint = Sprint::firstOrCreate(['id' => $sprintId], [
 | 
			
		||||
            'name' => 'Default Sprint Name',
 | 
			
		||||
            'start_date' => now(),
 | 
			
		||||
            'end_date' => now()->addDays(7),
 | 
			
		||||
            'project_id' => 1,
 | 
			
		||||
        ]);
 | 
			
		||||
 | 
			
		||||
        $criteriaData = $request->criterias;
 | 
			
		||||
 | 
			
		||||
        foreach ($criteriaData as $data) {
 | 
			
		||||
            // print_r($data);
 | 
			
		||||
            $criteriaId = (int)$data['criteria_id'];
 | 
			
		||||
 | 
			
		||||
            // Kiểm tra xem criteria có tồn tại không
 | 
			
		||||
            $criteria = Criteria::find($criteriaId);
 | 
			
		||||
            if (!$criteria) {
 | 
			
		||||
                // Nếu không tồn tại, có thể ném ra ngoại lệ hoặc trả về lỗi
 | 
			
		||||
                return response()->json(['error' => 'Criteria ID ' . $criteriaId . ' does not exist'], 400);
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            $point = $data['point'];
 | 
			
		||||
            $expectResult = $data['expect_result'];
 | 
			
		||||
            $actualResult = $data['actual_result'];
 | 
			
		||||
            $note = $data['note'];
 | 
			
		||||
 | 
			
		||||
            // Cập nhật hoặc tạo mới SprintCriteria
 | 
			
		||||
            SprintCriteria::updateOrCreate(
 | 
			
		||||
                ['sprint_id' => $sprint->id, 'criteria_id' => $criteriaId],
 | 
			
		||||
                ['point' => $point, 'expect_result' => $expectResult, 'actual_result' => $actualResult, 'note' => $note]
 | 
			
		||||
            );
 | 
			
		||||
 | 
			
		||||
            if (isset($data['users'])) {
 | 
			
		||||
                // Xóa hết các bản ghi UserCriteria theo sprint_id và criteria_id
 | 
			
		||||
                UserCriteria::where('criteria_id', $criteriaId)
 | 
			
		||||
                    ->where('sprint_id', $sprint->id)
 | 
			
		||||
                    ->delete();
 | 
			
		||||
                foreach ($data['users'] as $userData) {
 | 
			
		||||
                    $userId = $userData['user_id'];
 | 
			
		||||
                    $userPoint = $userData['point'] ?? $point;
 | 
			
		||||
                    $userNote = $userData['note'] ?? $note;
 | 
			
		||||
 | 
			
		||||
                    // Chèn lại các bản ghi mới vào bảng UserCriteria
 | 
			
		||||
                    UserCriteria::create([
 | 
			
		||||
                        'user_id' => $userId,
 | 
			
		||||
                        'criteria_id' => (int)$criteriaId,
 | 
			
		||||
                        'sprint_id' => $sprint->id,
 | 
			
		||||
                        'point' => $userPoint,
 | 
			
		||||
                        'note' => $userNote,
 | 
			
		||||
                        'created_by' => $user->name
 | 
			
		||||
                    ]);
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        return AbstractController::ResultSuccess("");
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			@ -0,0 +1,74 @@
 | 
			
		|||
<?php
 | 
			
		||||
 | 
			
		||||
namespace Modules\Admin\app\Http\Controllers;
 | 
			
		||||
 | 
			
		||||
use App\Http\Controllers\Controller;
 | 
			
		||||
use App\Traits\AnalyzeData;
 | 
			
		||||
use App\Traits\HasFilterRequest;
 | 
			
		||||
use App\Traits\HasOrderByRequest;
 | 
			
		||||
use App\Traits\HasSearchRequest;
 | 
			
		||||
use Illuminate\Http\Request;
 | 
			
		||||
use Modules\Admin\app\Models\TestCaseForSprint;
 | 
			
		||||
 | 
			
		||||
class TestCaseForSprintController extends Controller
 | 
			
		||||
{
 | 
			
		||||
    use HasOrderByRequest;
 | 
			
		||||
    use HasFilterRequest;
 | 
			
		||||
    use HasSearchRequest;
 | 
			
		||||
    use AnalyzeData;
 | 
			
		||||
 | 
			
		||||
    public function getAllReportsForSprint($sprintId)
 | 
			
		||||
    {
 | 
			
		||||
        $testReports = TestCaseForSprint::where('sprint_id', $sprintId)->get();
 | 
			
		||||
 | 
			
		||||
        if ($testReports->isEmpty()) {
 | 
			
		||||
            return AbstractController::ResultError('No test reports found for this sprint', [], 404);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        return AbstractController::ResultSuccess($testReports);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function createTestReport(Request $request, $sprintId)
 | 
			
		||||
    {
 | 
			
		||||
        $user = auth('admins')->user();
 | 
			
		||||
 | 
			
		||||
        $request->validate([
 | 
			
		||||
            'name' => 'required|string|max:255',
 | 
			
		||||
            'position' => 'required|integer',
 | 
			
		||||
            'input' => 'required|string',
 | 
			
		||||
            'expect_output' => 'required|string',
 | 
			
		||||
            'actual_output' => 'required|string',
 | 
			
		||||
            'note' => 'nullable|string',
 | 
			
		||||
            'issue_id_on_jira' => 'nullable|string',
 | 
			
		||||
            'bug_id_on_jira' => 'nullable|string',
 | 
			
		||||
        ]);
 | 
			
		||||
 | 
			
		||||
        $testReport = TestCaseForSprint::create([
 | 
			
		||||
            'name' => $request->name,
 | 
			
		||||
            'position' => $request->position,
 | 
			
		||||
            'input' => $request->input,
 | 
			
		||||
            'expect_output' => $request->expect_output,
 | 
			
		||||
            'actual_output' => $request->actual_output,
 | 
			
		||||
            'note' => $request->note,
 | 
			
		||||
            'issue_id_on_jira' => $request->issue_id_on_jira,
 | 
			
		||||
            'bug_id_on_jira' => $request->bug_id_on_jira,
 | 
			
		||||
            'sprint_id' => $sprintId,
 | 
			
		||||
            'created_by' => $user->name,
 | 
			
		||||
        ]);
 | 
			
		||||
        return AbstractController::ResultSuccess($testReport);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function deleteTestReport(Request $request)
 | 
			
		||||
    {
 | 
			
		||||
        $id = $request->input('id');
 | 
			
		||||
 | 
			
		||||
        $testReport = TestCaseForSprint::find($id);
 | 
			
		||||
 | 
			
		||||
        if (!$testReport) {
 | 
			
		||||
            return AbstractController::ResultError("Test report not found", [], 404);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        $testReport->delete();
 | 
			
		||||
        return AbstractController::ResultSuccess("Test report deleted successfully");
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			@ -0,0 +1,24 @@
 | 
			
		|||
<?php
 | 
			
		||||
 | 
			
		||||
namespace Modules\Admin\app\Models;
 | 
			
		||||
 | 
			
		||||
use Illuminate\Database\Eloquent\Model;
 | 
			
		||||
 | 
			
		||||
class Criteria extends Model
 | 
			
		||||
{
 | 
			
		||||
    protected $table = 'criterias';
 | 
			
		||||
    protected $fillable = ['name', 'description', 'type'];
 | 
			
		||||
 | 
			
		||||
    public function sprints()
 | 
			
		||||
    {
 | 
			
		||||
        return $this->belongsToMany(Sprint::class, 'sprints_criterias')
 | 
			
		||||
            ->withPivot('point', 'expect_result', 'actual_result', 'note');
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function users()
 | 
			
		||||
    {
 | 
			
		||||
        return $this->belongsToMany(User::class, 'users_criterias')
 | 
			
		||||
            ->withPivot('point', 'note', 'sprint_id')
 | 
			
		||||
            ;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			@ -0,0 +1,17 @@
 | 
			
		|||
<?php
 | 
			
		||||
 | 
			
		||||
namespace Modules\Admin\app\Models;
 | 
			
		||||
 | 
			
		||||
use Illuminate\Database\Eloquent\Model;
 | 
			
		||||
 | 
			
		||||
class Sprint extends Model
 | 
			
		||||
{
 | 
			
		||||
    protected $table = 'sprint';
 | 
			
		||||
    protected $fillable = ['name', 'point', 'project_id', 'start_date', 'end_date', 'complete_date'];
 | 
			
		||||
 | 
			
		||||
    public function criterias()
 | 
			
		||||
    {
 | 
			
		||||
        return $this->belongsToMany(Criteria::class, 'sprints_criterias')
 | 
			
		||||
                    ->withPivot('point', 'expect_result', 'actual_result', 'note');
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			@ -0,0 +1,11 @@
 | 
			
		|||
<?php
 | 
			
		||||
 | 
			
		||||
namespace Modules\Admin\app\Models;
 | 
			
		||||
 | 
			
		||||
use Illuminate\Database\Eloquent\Model;
 | 
			
		||||
 | 
			
		||||
class SprintCriteria extends Model
 | 
			
		||||
{
 | 
			
		||||
    protected $table = 'sprints_criterias';
 | 
			
		||||
    protected $fillable = ['sprint_id', 'criteria_id', 'point', 'expect_result', 'actual_result', 'note'];
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			@ -0,0 +1,22 @@
 | 
			
		|||
<?php
 | 
			
		||||
 | 
			
		||||
namespace Modules\Admin\app\Models;
 | 
			
		||||
 | 
			
		||||
use Illuminate\Database\Eloquent\Model;
 | 
			
		||||
 | 
			
		||||
class TestCaseForSprint extends Model
 | 
			
		||||
{
 | 
			
		||||
    protected $table = 'test_cases_for_sprint';
 | 
			
		||||
    protected $fillable = [
 | 
			
		||||
        'name',
 | 
			
		||||
        'position',
 | 
			
		||||
        'input',
 | 
			
		||||
        'expect_output',
 | 
			
		||||
        'actual_output',
 | 
			
		||||
        'note',
 | 
			
		||||
        'issue_id_on_jira',
 | 
			
		||||
        'bug_id_on_jira',
 | 
			
		||||
        'sprint_id',
 | 
			
		||||
        'created_by',
 | 
			
		||||
    ];
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			@ -0,0 +1,25 @@
 | 
			
		|||
<?php
 | 
			
		||||
 | 
			
		||||
namespace Modules\Admin\app\Models;
 | 
			
		||||
 | 
			
		||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
 | 
			
		||||
use Illuminate\Database\Eloquent\Model;
 | 
			
		||||
use Illuminate\Notifications\Notifiable;
 | 
			
		||||
use Laravel\Sanctum\HasApiTokens;
 | 
			
		||||
use Spatie\Permission\Traits\HasRoles;
 | 
			
		||||
 | 
			
		||||
class User extends Model
 | 
			
		||||
{
 | 
			
		||||
    use HasApiTokens, HasFactory, Notifiable;
 | 
			
		||||
    use HasRoles;
 | 
			
		||||
 | 
			
		||||
    public function __construct()
 | 
			
		||||
    {
 | 
			
		||||
        $this->table = 'users';
 | 
			
		||||
        $this->guarded = [];
 | 
			
		||||
        $this->hidden = [
 | 
			
		||||
            'password',
 | 
			
		||||
            'forgot_code',
 | 
			
		||||
        ];
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			@ -0,0 +1,11 @@
 | 
			
		|||
<?php
 | 
			
		||||
 | 
			
		||||
namespace Modules\Admin\app\Models;
 | 
			
		||||
 | 
			
		||||
use Illuminate\Database\Eloquent\Model;
 | 
			
		||||
 | 
			
		||||
class UserCriteria extends Model
 | 
			
		||||
{
 | 
			
		||||
    protected $table = 'users_criterias';
 | 
			
		||||
    protected $fillable = ['user_id', 'criteria_id', 'sprint_id', 'point', 'note'];
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			@ -15,6 +15,8 @@ use Modules\Admin\app\Http\Controllers\SettingController;
 | 
			
		|||
use Modules\Admin\app\Http\Controllers\TicketController;
 | 
			
		||||
use Modules\Admin\app\Http\Controllers\TimekeepingController;
 | 
			
		||||
use Modules\Admin\app\Http\Controllers\TrackingController;
 | 
			
		||||
use Modules\Admin\app\Http\Controllers\CriteriasController;
 | 
			
		||||
use Modules\Admin\app\Http\Controllers\TestCaseForSprintController;
 | 
			
		||||
use Modules\Admin\app\Http\Middleware\AdminMiddleware;
 | 
			
		||||
 | 
			
		||||
/*
 | 
			
		||||
| 
						 | 
				
			
			@ -147,6 +149,20 @@ Route::middleware('api')
 | 
			
		|||
                Route::get('/delete', [TicketController::class, 'deleteTicket'])->middleware('check.permission:admin.hr.staff');
 | 
			
		||||
                Route::post('/handle-ticket', [TicketController::class, 'handleTicket'])->middleware('check.permission:admin');
 | 
			
		||||
            });
 | 
			
		||||
 | 
			
		||||
            Route::group([
 | 
			
		||||
                'prefix' => 'criterias',
 | 
			
		||||
            ], function () {
 | 
			
		||||
                Route::get('/sprints/{sprintId}', [CriteriasController::class, 'getCriteriasForSprint'])->middleware('check.permission:admin');
 | 
			
		||||
                Route::get('/users/{userId}', [CriteriasController::class, 'getCriteriasForUser'])->middleware('check.permission:admin');
 | 
			
		||||
                Route::get('/users/{userId}/sprints/{sprintId}', [CriteriasController::class, 'getCriteriasForUserBySprint'])->middleware('check.permission:admin');
 | 
			
		||||
                Route::get('/getAll', [CriteriasController::class, 'getAllCriterias'])->middleware('check.permission:admin');
 | 
			
		||||
                Route::post('/sprints/{sprintId}', [CriteriasController::class, 'updateCriteriasForSprint'])->middleware('check.permission:admin');
 | 
			
		||||
 | 
			
		||||
                Route::get('/test-cases/getAll/{sprintId}', [TestCaseForSprintController::class, 'getAllReportsForSprint'])->middleware('check.permission:admin,tester');
 | 
			
		||||
                Route::post('/test-cases/{sprintId}', [TestCaseForSprintController::class, 'createTestReport'])->middleware('check.permission:admin,tester');
 | 
			
		||||
                Route::get('/test-cases/delete', [TestCaseForSprintController::class, 'deleteTestReport'])->middleware('check.permission:admin,tester');
 | 
			
		||||
            });
 | 
			
		||||
        });
 | 
			
		||||
    });
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
		Reference in New Issue