Files
UNIT3D-Community-Edition/app/Http/Controllers/Staff/NoteController.php
HDVinnie 611fcd6bc1 refactor: if / else / elseif statements
- Change If Else Value Assign To Early Return
- Change Nested Ifs To Early Return
- Remove Always Else
2020-02-12 12:34:34 -05:00

85 lines
2.2 KiB
PHP

<?php
/**
* NOTICE OF LICENSE.
*
* UNIT3D is open-sourced software licensed under the GNU Affero General Public License v3.0
* The details is bundled with this project in the file LICENSE.txt.
*
* @project UNIT3D
*
* @license https://www.gnu.org/licenses/agpl-3.0.en.html/ GNU Affero General Public License v3.0
* @author HDVinnie
*/
namespace App\Http\Controllers\Staff;
use App\Http\Controllers\Controller;
use App\Models\Note;
use App\Models\User;
use Illuminate\Http\Request;
class NoteController extends Controller
{
/**
* Display All User Notes.
*
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
*/
public function index()
{
$notes = Note::latest()->paginate(25);
return view('Staff.note.index', ['notes' => $notes]);
}
/**
* Store A New User Note.
*
* @param \Illuminate\Http\Request $request
* @param $username
*
* @return Illuminate\Http\RedirectResponse
*/
public function store(Request $request, $username)
{
$staff = $request->user();
$user = User::where('username', '=', $username)->firstOrFail();
$note = new Note();
$note->user_id = $user->id;
$note->staff_id = $staff->id;
$note->message = $request->input('message');
$v = validator($note->toArray(), [
'user_id' => 'required',
'staff_id' => 'required',
'message' => 'required',
]);
if ($v->fails()) {
return redirect()->route('users.show', ['username' => $user->username])
->withErrors($v->errors());
}
$note->save();
return redirect()->route('users.show', ['username' => $user->username])
->withSuccess('Note Has Successfully Posted');
}
/**
* Delete A User Note.
*
* @param $id
*
* @return Illuminate\Http\RedirectResponse
*/
public function destroy($id)
{
$note = Note::findOrFail($id);
$user = User::findOrFail($note->user_id);
$note->delete();
return redirect()->route('users.show', ['username' => $user->username])
->withSuccess('Note Has Successfully Been Deleted');
}
}