Changeset 3c89e27


Ignore:
Timestamp:
08/26/24 16:17:04 (3 months ago)
Author:
bube-ristovska <ristovska725@…>
Branches:
main
Parents:
5372778
Message:

Refactored duplicate code

Location:
app/Http/Controllers
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • app/Http/Controllers/CrimeCaseController.php

    r5372778 r3c89e27  
    99class CrimeCaseController extends Controller
    1010{
     11    private function policeStationIsPoliceman()
     12    {
     13        return DB::table('police_station')
     14            ->where('p_id', Session::get('p_id'))
     15            ->get();
     16    }
     17    private function policeStationIsOfficer()
     18    {
     19        return DB::table('police_station')
     20            ->where('pe_id', Session::get('pe_id'))
     21            ->get();
     22    }
    1123    function cases(){
    1224        if(Session::get('pe_id') == null) {
     
    1426        }
    1527
    16 
    1728        if(Session::get('is_policeman')){
    18             $police_station = DB::select('select * from police_station where p_id=:p_id;',['p_id'=>  Session::get('p_id')]);
     29            $police_station = $this->policeStationIsPoliceman();
    1930        } else {
    20             $police_station = DB::select('select * from police_station where pe_id=:pe_id;',['pe_id'=>  Session::get('pe_id')]);
     31            $police_station = $this->policeStationIsOfficer();
    2132        }
    2233
    23         $cases = DB::select('select * from crime_case where p_id=:p_id;',['p_id'=> $police_station[0]->p_id]);
    24 
     34        $cases = DB::table('crime_case')
     35            ->where('p_id', $police_station[0]->p_id)
     36            ->get();
    2537
    2638        return view('cases', [
     
    4456            'incident_place'=>'required'
    4557        ]);
    46         $statement["statement_date"] = Carbon::now()->format('Y-m-d');
    47         $covek = DB::select('select pe_id from people where embg=:embg;',['embg'=> $statement["embg"]]);
     58         $statement["statement_date"] = Carbon::now()->format('Y-m-d');
     59         $covek = DB::select('select pe_id from people where embg=:embg;',['embg'=> $statement["embg"]]);
    4860         $s_id_b = DB::select('select MAX(s_id) from statements');
    4961         $s_id = $s_id_b[0]->max;
    50         $s_id = $s_id +1 ;
    51         $policaec =  DB::select('select pe_id from policeman where badge_no=:badge_no;',['badge_no'=> Session::get("badge_no")]);
     62         $s_id = $s_id +1 ;
     63         $policaec =  DB::select('select pe_id from policeman where badge_no=:badge_no;',['badge_no'=> Session::get("badge_no")]);
    5264
    5365        if ($role === 'witness') {
     
    8496    }
    8597    function finished_cases(){
    86 
    8798        if(Session::get('is_policeman')){
    88             $police_station = DB::select('select * from police_station where p_id=:p_id;',['p_id'=>  Session::get('p_id')]);
     99            $police_station = $this->policeStationIsPoliceman();
    89100        } else {
    90             $police_station = DB::select('select * from police_station where pe_id=:pe_id;',['pe_id'=>  Session::get('pe_id')]);
     101            $police_station = $this->policeStationIsOfficer();
    91102        }
    92103
    93         $cases = DB::select('select * from crime_case where p_id=:p_id and c_status=\'Z\';', ['p_id' => $police_station[0]->p_id]);
    94 
     104        $cases = DB::table('crime_case')
     105            ->where('p_id', $police_station[0]->p_id)
     106            ->where('c_status', 'Z')
     107            ->get();
    95108        return view('archive', [
    96109            'cases' => $cases,
     
    100113    function case($wildcard){
    101114        Session::put('c_id', $wildcard);
    102         $case = DB::select('select * from crime_case where c_id=:c_id;',['c_id'=> $wildcard]);
    103         $p_address = DB::select('select p_address from police_station where p_id=:p_id;',['p_id'=> $case[0]->p_id]);
    104         $statements = DB::select('select * from statements where c_id=:c_id;',['c_id'=> $wildcard]);
    105 
     115        $case = DB::table('crime_case')
     116            ->where('c_id', $wildcard)
     117            ->get();
     118        $p_address = DB::table('police_station')
     119            ->where('p_id', $case[0]->p_id)
     120            ->value('p_address');
     121        $statements = DB::table('statements')
     122            ->where('c_id', $wildcard)
     123            ->get();
    106124
    107125        $victims=[];
     
    110128        $evidence = [];
    111129        foreach ($statements as $statement) {
    112             $evidence_id = DB::select('select * from mentions_evidence where s_id=:s_id;',['s_id'=> $statement->s_id]);
    113             if (!empty($evidence_id)) { // Check if $evidence_id is not empty
    114                 $evidence_id[] = $evidence_id[0];
    115             }
     130            $evidence_id = DB::table('mentions_evidence')
     131                ->where('s_id', $statement->s_id)
     132                ->get();
     133//            if (!empty($evidence_id)) { // Check if $evidence_id is not empty
     134//                $evidence_id[] = $evidence_id[0];
     135//            }
    116136        }
    117137         $evidence_id=collect($evidence_id)->unique();
    118138        foreach ($evidence_id as $e) {
    119             $evidence = DB::select('select * from evidence where e_id=:e_id;',['e_id'=> $e->e_id]);
    120             $evidence[] = $evidence[0];
     139            $evidence = DB::table('evidence')
     140                ->where('e_id', $e->e_id)->get();
     141
    121142        }
    122143        foreach ($statements as $st){
    123144            if (!($st->victim_pe_id)==NULL){
    124                 $victim=DB::select('select * from people where pe_id=:pe_id;',['pe_id'=> $st->victim_pe_id]);
     145                $victim = DB::table('people')
     146                    ->where('pe_id', $st->victim_pe_id)->get();
    125147                $victims[] = $victim[0];
    126148            }
     
    128150        foreach ($statements as $st){
    129151            if (!($st->witness_pe_id)==NULL) {
    130                 $witnes = DB::select('select * from people where pe_id=:pe_id;', ['pe_id' => $st->witness_pe_id]);
     152                $witnes = DB::table('people')
     153                    ->where('pe_id', $st->witness_pe_id)->get();
    131154                $witness[] = $witnes[0];
    132155            }
    133156        }
    134157
    135 
    136158        return view('case', [
    137159            'case' => $case[0],
    138             'p_address'=>$p_address[0]->p_address,
     160            'p_address'=>$p_address,
    139161            'statements'=>$statements,
    140162            'evidence'=>$evidence,
  • app/Http/Controllers/OfficerController.php

    r5372778 r3c89e27  
    1212class OfficerController extends Controller
    1313{
     14    private function policeStationIsPoliceman()
     15    {
     16        return DB::table('police_station')
     17            ->where('p_id', Session::get('p_id'))
     18            ->get();
     19    }
     20    private function policeStationIsOfficer()
     21    {
     22       return DB::table('police_station')
     23           ->where('pe_id', Session::get('pe_id'))
     24           ->get();
     25    }
    1426    function employees()
    1527    {
     
    1830        }
    1931        if(Session::get('is_policeman')){
    20             $police_station = DB::select('select * from police_station where p_id=:p_id;',['p_id'=>  Session::get('p_id')]);
     32            $police_station = $this->policeStationIsPoliceman();
    2133        } else {
    22             $police_station = DB::select('select * from police_station where pe_id=:pe_id;',['pe_id'=>  Session::get('pe_id')]);
     34            $police_station = $this->policeStationIsOfficer();
    2335        }
    24         $results = DB::select('select * from policeman join people on policeman.pe_id = people.pe_id where p_id=:p_id;',['p_id'=>  $police_station[0]->p_id]);
    25 //        $results = DB::select('select * from policeman join people on policeman.pe_id = people.pe_id;');
     36        $results = DB::table('policeman')
     37            ->join('people', 'policeman.pe_id', '=', 'people.pe_id')
     38            ->where('policeman.p_id', $police_station[0]->p_id)
     39            ->get();
     40
    2641
    2742        return view('employees', [
     
    3348    function show($id){
    3449        if(Session::get('is_policeman')){
    35             $police_station = DB::select('select * from police_station where p_id=:p_id;',['p_id'=>  Session::get('p_id')]);
     50            $police_station = $this->policeStationIsPoliceman();
    3651        } else {
    37             $police_station = DB::select('select * from police_station where pe_id=:pe_id;',['pe_id'=>  Session::get('pe_id')]);
     52            $police_station = $this->policeStationIsOfficer();
    3853        }
    39         $result = DB::select('select * from policeman join people on policeman.pe_id = people.pe_id where p_id=:p_id and people.pe_id=:pe_id;',['p_id'=>  $police_station[0]->p_id, 'pe_id' => $id]);
    40         $cases = DB::select('select * from statements where pe_id=:pe_id;',['pe_id' => $id]);
    41 
     54        $result = DB::table('policeman')
     55            ->join('people', 'policeman.pe_id', '=', 'people.pe_id')
     56            ->where('p_id', $police_station[0]->p_id)
     57            ->where('people.pe_id', $id)
     58            ->get();
     59        $cases = DB::table('statements')
     60            ->where('pe_id', $id)
     61            ->get();
    4262        return view('employee', [
    4363            'employee' => $result[0],
     
    6181
    6282
    63         $police_station = DB::select('select * from police_station where pe_id=:pe_id;',['pe_id'=>  Session::get('pe_id')]);
    64 
    65         $pe_id = DB::select('select pe_id from people where embg = :embg;', ['embg' => $policeman["embg"]]);
    66         DB::insert('INSERT INTO policeman (pe_id, badge_no, p_date_of_employment, rank, p_id, p_password) VALUES (?, ?, ?, ?, ?, ?)', [$pe_id[0]->pe_id, $policeman["badge_no"], Carbon::now()->format('Y-m-d'), $policeman["rank"], $police_station[0]->p_id,$policeman["password"]]);
     83        $police_station = $this->policeStationIsOfficer();
     84        $pe_id = DB::table('people')
     85            ->where('embg', $policeman['embg'])->get();
     86        $data = [
     87            'pe_id' => $pe_id[0]->pe_id,
     88            'badge_no' => $policeman["badge_no"],
     89            'p_date_of_employment' => Carbon::now()->format('Y-m-d'),
     90            'rank' => $policeman["rank"],
     91            'p_id' => $police_station[0]->p_id,
     92            'p_password' => $policeman["password"]
     93        ];
     94        DB::table('policeman')->insert($data);
     95//        DB::insert('INSERT INTO policeman (pe_id, badge_no, p_date_of_employment, rank, p_id, p_password) VALUES (?, ?, ?, ?, ?, ?)', [$pe_id[0]->pe_id, $policeman["badge_no"], Carbon::now()->format('Y-m-d'), $policeman["rank"], $police_station[0]->p_id,$policeman["password"]]);
    6796        return redirect()->back()->with('message',"Додадено");
    6897    }
    6998
    7099
     100
     101
    71102}
  • app/Http/Controllers/PeopleController.php

    r5372778 r3c89e27  
    1313            return view('login');
    1414        }
    15         $peoples = DB::select('select * from people;');
    16 
     15        $peoples = DB::table('people')->get();
    1716        return view('filter', [
    1817            'peoples' => $peoples
     
    7574    {
    7675        $embg = $request->input('embg');
    77         $person = DB::select('SELECT * FROM people WHERE embg = :embg', ['embg' => $embg]);
    78 
     76        $person = DB::table('people')
     77            ->where('embg', $embg)->get();
    7978        return response()->json($person[0] ?? null);
    8079    }
  • app/Http/Controllers/SessionsController.php

    r5372778 r3c89e27  
    2424            'password' => 'required'
    2525        ]);
     26
    2627        $password = $credentials['password'];
    2728        $badge_no = $credentials['badge_no'];
     29        if (!is_numeric($badge_no)) {
     30            // Redirect back with an error message
     31            return back()->withErrors(['password' => 'Invalid credentials']);
     32        }
    2833        // mozhe da se najavi kako policaec i kako officer, znaeme koj e koj po znachkata
    2934
     
    6974        return back()->withErrors(['password' => 'Invalid credentials']);
    7075    }
    71 
    7276    public function logout()
    7377    {
Note: See TracChangeset for help on using the changeset viewer.