Code Coverage |
||||||||||
Classes and Traits |
Functions and Methods |
Lines |
||||||||
| Total | |
0.00% |
0 / 1 |
|
0.00% |
0 / 6 |
CRAP | |
0.00% |
0 / 56 |
| HelpNeededController | |
0.00% |
0 / 1 |
|
0.00% |
0 / 6 |
132 | |
0.00% |
0 / 56 |
| __construct | |
0.00% |
0 / 1 |
2 | |
0.00% |
0 / 2 |
|||
| index | |
0.00% |
0 / 1 |
2 | |
0.00% |
0 / 5 |
|||
| store | |
0.00% |
0 / 1 |
6 | |
0.00% |
0 / 15 |
|||
| show | |
0.00% |
0 / 1 |
2 | |
0.00% |
0 / 5 |
|||
| update | |
0.00% |
0 / 1 |
20 | |
0.00% |
0 / 21 |
|||
| destroy | |
0.00% |
0 / 1 |
6 | |
0.00% |
0 / 8 |
|||
| 1 | <?php |
| 2 | |
| 3 | namespace Qmp\Laravel\Help\Controllers; |
| 4 | |
| 5 | use App\Http\Controllers\Controller; |
| 6 | use Illuminate\Http\Response; |
| 7 | use Illuminate\Http\Request; |
| 8 | use Illuminate\Support\Facades\Log; |
| 9 | use Illuminate\Support\Facades\Redis; |
| 10 | use Qmp\Laravel\Help\Models\HelpNeeded; |
| 11 | use Qmp\Laravel\MicroService\Controllers\AbstractMicroServiceController; |
| 12 | use Qmp\Laravel\Mail\Mail\MailHelpNeededCompleted; |
| 13 | use Qmp\Laravel\Mail\MailSender; |
| 14 | |
| 15 | class HelpNeededController extends AbstractMicroServiceController |
| 16 | { |
| 17 | public function __construct(Request $request) |
| 18 | { |
| 19 | parent::__construct($request); |
| 20 | } |
| 21 | |
| 22 | /** |
| 23 | * Display a listing of the resource. |
| 24 | * |
| 25 | * @return \Illuminate\Http\Response |
| 26 | */ |
| 27 | public function index() |
| 28 | { |
| 29 | $helps = HelpNeeded::select() |
| 30 | ->with('user') |
| 31 | ->with('completedBy') |
| 32 | ->simplePaginate(1000); |
| 33 | return response()->json($helps); |
| 34 | } |
| 35 | |
| 36 | /** |
| 37 | * Store a newly created resource in storage. |
| 38 | * |
| 39 | * @param \Illuminate\Http\Request $request |
| 40 | * @return \Illuminate\Http\Response |
| 41 | */ |
| 42 | public function store(Request $request) |
| 43 | { |
| 44 | $request->validate([ |
| 45 | 'subject' => 'required|max:255', |
| 46 | 'description' => 'required|max:255', |
| 47 | 'url' => 'required|max:255', |
| 48 | 'screenshot' => 'required', |
| 49 | 'logs' => 'required|array', |
| 50 | ]); |
| 51 | |
| 52 | try { |
| 53 | $help = new HelpNeeded(); |
| 54 | $help->user_id = $this->userId; |
| 55 | $help->subject = $request->subject; |
| 56 | $help->description = $request->description; |
| 57 | $help->url = $request->url; |
| 58 | $help->screenshot = $request->screenshot; |
| 59 | $help->logs = json_encode($request->logs); |
| 60 | |
| 61 | $help->save(); |
| 62 | } catch(\Exception $e) { |
| 63 | Log::debug('Unable to store entity:' . var_export(['message' => $e->getMessage(), 'line' => $e->getLine(), 'file' => $e->getFile()], true)); |
| 64 | return response()->json(['error' => $e->getMessage()], Response::HTTP_UNPROCESSABLE_ENTITY); |
| 65 | } |
| 66 | |
| 67 | Redis::publish('help-needed', json_encode(['action' => 'update-helps'])); |
| 68 | |
| 69 | return response(['status' => 'ok', 'help' => $help->id], Response::HTTP_CREATED); |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Display the specified resource. |
| 74 | * |
| 75 | * @return \Illuminate\Http\Response |
| 76 | */ |
| 77 | public function show($id) |
| 78 | { |
| 79 | $help = HelpNeeded::where('id',$id) |
| 80 | ->with('user') |
| 81 | ->with('completedBy') |
| 82 | ->first(); |
| 83 | return response()->json($help); |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * Update the specified resource in storage. |
| 88 | * |
| 89 | * @param \Illuminate\Http\Request $request |
| 90 | * @return \Illuminate\Http\Response |
| 91 | */ |
| 92 | public function update(Request $request, $id) |
| 93 | { |
| 94 | $request->validate([ |
| 95 | 'status' => 'boolean', |
| 96 | ]); |
| 97 | |
| 98 | $help = HelpNeeded::findOrFail($id); |
| 99 | |
| 100 | try { |
| 101 | |
| 102 | $help->status = $request->status; |
| 103 | $help->completed_by = $request->status ? $this->userId : null; |
| 104 | $help->save(); |
| 105 | |
| 106 | $help = HelpNeeded::where('id',$id) |
| 107 | ->with('user') |
| 108 | ->with('completedBy') |
| 109 | ->first(); |
| 110 | |
| 111 | if($request->status === '1') |
| 112 | { |
| 113 | // Send mail help needed completed |
| 114 | MailSender::model(new MailHelpNeededCompleted($help)) |
| 115 | ->to($help->user->email) |
| 116 | ->dispatch(); |
| 117 | } |
| 118 | |
| 119 | } catch (\Exception $e) { |
| 120 | Log::debug('Unable to update entity:' . var_export(['message' => $e->getMessage(), 'line' => $e->getLine(), 'file' => $e->getFile()], true)); |
| 121 | return response()->json(['error' => $e->getMessage()], Response::HTTP_UNPROCESSABLE_ENTITY); |
| 122 | } |
| 123 | |
| 124 | Redis::publish('help-needed', json_encode([ |
| 125 | 'action' => 'update-help', |
| 126 | 'help_id' => $id |
| 127 | ])); |
| 128 | |
| 129 | return response(['status' => 'ok'], Response::HTTP_OK); |
| 130 | } |
| 131 | |
| 132 | /** |
| 133 | * Remove the specified resource from storage. |
| 134 | * |
| 135 | * @return \Illuminate\Http\Response |
| 136 | */ |
| 137 | public function destroy($id) |
| 138 | { |
| 139 | try { |
| 140 | HelpNeeded::destroy($id); |
| 141 | } catch (\Exception $e) { |
| 142 | Log::debug('Unable to destroy entity:' . var_export(['message' => $e->getMessage(), 'line' => $e->getLine(), 'file' => $e->getFile()], true)); |
| 143 | return response()->json(['error' => $e->getMessage()], Response::HTTP_UNPROCESSABLE_ENTITY); |
| 144 | } |
| 145 | |
| 146 | Redis::publish('help-needed', json_encode([ |
| 147 | 'action' => 'destroy-help', |
| 148 | 'help_id' => $id |
| 149 | ])); |
| 150 | |
| 151 | return response(['status' => 'ok'], Response::HTTP_OK); |
| 152 | } |
| 153 | } |