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

Refactored duplicate code

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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}
Note: See TracChangeset for help on using the changeset viewer.