source: app/Http/Controllers/OfficerController.php@ 3c89e27

main
Last change on this file since 3c89e27 was 3c89e27, checked in by bube-ristovska <ristovska725@…>, 4 months ago

Refactored duplicate code

  • Property mode set to 100644
File size: 3.2 KB
RevLine 
[75151c6]1<?php
2
3namespace App\Http\Controllers;
4
[6b10b67]5use App\Models\Policeman;
[cf84baa]6use Illuminate\Contracts\Auth\Authenticatable;
[75151c6]7use Illuminate\Http\Request;
[92df8cd]8use Illuminate\Support\Carbon;
[6b10b67]9use Illuminate\Support\Facades\DB;
[d9c4096]10use Illuminate\Support\Facades\Session;
[75151c6]11
12class OfficerController extends Controller
13{
[3c89e27]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 }
[75151c6]26 function employees()
27 {
[d9c4096]28 if(Session::get('pe_id') == null) {
29 return view('login');
30 }
[7e9dadd]31 if(Session::get('is_policeman')){
[3c89e27]32 $police_station = $this->policeStationIsPoliceman();
[7e9dadd]33 } else {
[3c89e27]34 $police_station = $this->policeStationIsOfficer();
[7e9dadd]35 }
[3c89e27]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
[7e9dadd]41
[6b10b67]42 return view('employees', [
[7e9dadd]43 'employees' => $results,
44 'p_address'=>$police_station[0]->p_address
[6b10b67]45 ]);
46 }
47
[249bf91]48 function show($id){
49 if(Session::get('is_policeman')){
[3c89e27]50 $police_station = $this->policeStationIsPoliceman();
[249bf91]51 } else {
[3c89e27]52 $police_station = $this->policeStationIsOfficer();
[249bf91]53 }
[3c89e27]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();
[249bf91]62 return view('employee', [
63 'employee' => $result[0],
64 'p_address'=>$police_station[0]->p_address,
65 'cases' => $cases
66 ]);
67 }
68
[6b10b67]69 function register()
70 {
71 return view('register-policeman');
[75151c6]72 }
[92df8cd]73 function register_post()
74 {
75 $policeman = request()->validate([
76 'badge_no' => 'required',
77 'embg' => 'required',
78 'password' => 'required',
79 'rank'=>'required'
80 ]);
81
[7e9dadd]82
[3c89e27]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"]]);
[7e9dadd]96 return redirect()->back()->with('message',"Додадено");
[92df8cd]97 }
[cf84baa]98
99
[3c89e27]100
101
[75151c6]102}
Note: See TracBrowser for help on using the repository browser.