Skip to content
Snippets Groups Projects
Commit bd3c4cf9 authored by Tibo's avatar Tibo
Browse files

Refactored Server class

parent dfecef8e
No related branches found
No related tags found
No related merge requests found
<?php
namespace App\Http\Controllers;
use App\Server;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Validator;
use Illuminate\Http\Request;
class ServerController extends Controller
{
public function __construct()
{
// Uncomment to require authentication
$this->middleware('auth');
}
/**
* Get a validator for an incoming registration request.
*
* @param array $data
* @return \Illuminate\Contracts\Validation\Validator
*/
protected function validator(array $data)
{
return Validator::make($data, [
'name' => 'required|string|regex:/^[a-zA-Z0-9\s-\.]+$/|max:255'
]);
}
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
// return view("server.index", array("servers" => Server::all()->sortBy("name")));
}
/**
* Show the form for creating a new resource.
* We use the same view for create and update => provide an empty Server.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
return view("server.edit", ["server" => new Server()]);
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
return $this->saveAndRedirect($request, new Server());
}
/**
* Display the specified resource.
*
* @param Server $server * @return \Illuminate\Http\Response
*/
public function show(Server $server)
{
return view("server.show", array("server" => $server));
}
/**
* Show the form for editing the specified resource.
*
* @param Server $server * @return \Illuminate\Http\Response
*/
public function edit(Server $server)
{
return view("server.edit", array("server" => $server));
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param Server $server * @return \Illuminate\Http\Response
*/
public function update(Request $request, Server $server)
{
return $this->saveAndRedirect($request, $server);
}
private function saveAndRedirect(Request $request, Server $server)
{
$this->validator($request->all())->validate();
$organization = \App\Organization::find($request->organization_id);
$server->name = $request->name;
$server->organization_id = $request->organization_id;
$server->save();
return redirect(action("OrganizationController@show", ["organization" => $organization]));
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy($id)
{
Server::find($id)->delete();
return back();
}
}
......@@ -11,4 +11,8 @@ class Organization extends Model
public function users() {
return $this->belongsToMany("App\User");
}
public function servers() {
return $this->hasMany("App\Server");
}
}
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Server extends Model
{
protected $fillable = ["token"];
public function __construct(array $attributes = array()) {
$attributes["token"] = str_random(32);
parent::__construct($attributes);
}
public function organization() {
return $this->belongsTo("App\Organization");
}
}
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateServersTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('servers', function (Blueprint $table) {
$table->increments('id');
$table->integer("organization_id");
$table->timestamps();
$table->text("name");
$table->text("token");
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('servers');
}
}
......@@ -2,33 +2,65 @@
@section('content')
<div class="container">
<div class="row justify-content-center">
<div class="col-md-8">
<div class="card">
<div class="card-header">{{ $organization->name }}</div>
<h1>{{ $organization->name }}</h1>
<div class="card-body">
<p>Name: {{ $organization->name }}</p>
<p>
<a href="{{ action('ServerController@create') }}" class="btn btn-primary">
<i class="fa fa-plus-circle" aria-hidden="true"></i> New server
</a>
</p>
<table class="table table-striped">
<tr>
<th>Name</th>
<th>ID</th>
<th>Token</th>
<th></th>
</tr>
@foreach($organization->servers as $server)
<tr>
<td>{{ $server->name }}</td>
<td>{{ $server->id }}</td>
<td>{{ $server->token }}</td>
<td class="text-right">
<a class="btn btn-primary btn-sm"
href="{{ action('ServerController@show', ['Server' => $server]) }}">
<i class="fa fa-search" aria-hidden="true"></i> Show
</a>
<div>
<a class="btn btn-primary"
href="{{ action('OrganizationController@edit', ['Organization' => $organization]) }}">
Edit
</a>
<a class="btn btn-primary btn-sm"
href="{{ action('ServerController@edit', ['Server' => $server]) }}">
<i class="fa fa-pencil" aria-hidden="true"></i> Edit
</a>
<form method="POST"
action="{{ action('OrganizationController@destroy', ['Organization' => $organization]) }}"
style="display: inline-block">
{{ csrf_field() }}
{{ method_field("DELETE") }}
<button class="btn btn-danger">
Delete
</button>
</form>
</div>
</div>
</div>
</div>
<form method="POST"
action="{{ action('ServerController@destroy', ['Server' => $server]) }}"
style="display: inline-block">
{{ csrf_field() }}
{{ method_field("DELETE") }}
<button class="btn btn-danger btn-sm">
<i class="fa fa-times-circle" aria-hidden="true"></i> Delete
</button>
</form>
</td>
</tr>
@endforeach
</table>
<div>
<a class="btn btn-primary"
href="{{ action('OrganizationController@edit', ['Organization' => $organization]) }}">
Edit
</a>
<form method="POST"
action="{{ action('OrganizationController@destroy', ['Organization' => $organization]) }}"
style="display: inline-block">
{{ csrf_field() }}
{{ method_field("DELETE") }}
<button class="btn btn-danger">
Delete
</button>
</form>
</div>
</div>
@endsection
@extends('layouts.app')
@section('content')
<div class="container">
<div class="row justify-content-center">
<div class="col-md-8">
<div class="card">
<div class="card-header">Server</div>
<div class="card-body">
@if (!$server->exists)
<form method="POST" action="{{ action("ServerController@store") }}">
@else
<form method="POST"
action="{{ action("ServerController@update", ["server" => $server]) }}">
{{ method_field("PUT") }}
@endif
{{ csrf_field() }}
<div class="form-group row">
<label for="organization_id" class="col-md-4 col-form-label text-md-right">Organization</label>
<div class="col-md-6">
<select id="organization_id"
class="form-control{{ $errors->has('organization_id') ? ' is-invalid' : '' }}"
name="organization_id"
required autofocus>
@foreach (Auth::user()->organizations as $organization)
<option value="{{ $organization->id }}" {{ old('organization_id', $server->organization_id) == $organization->id ? "selected" : "" }}">{{ $organization->name }}</option>
@endforeach
</select>
@if ($errors->has('name'))
<span class="invalid-feedback">
<strong>{{ $errors->first('organization_id') }}</strong>
</span>
@endif
</div>
</div>
<div class="form-group row">
<label for="name" class="col-md-4 col-form-label text-md-right">Name</label>
<div class="col-md-6">
<input id="name" type="text"
class="form-control{{ $errors->has('name') ? ' is-invalid' : '' }}"
name="name"
value="{{ old('name', $server->name) }}" required>
@if ($errors->has('name'))
<span class="invalid-feedback">
<strong>{{ $errors->first('name') }}</strong>
</span>
@endif
</div>
</div>
<div class="form-group row">
<div class="col-md-6 offset-md-4">
<button type="submit" class="btn btn-primary">
<i class="fa fa-check" aria-hidden="true"></i> Save
</button>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
@endsection
@extends('layouts.app')
@section('title', 'Servers')
@section('content')
<div class="container">
<h1>Servers</h1>
<p>
<a href="{{ action('ServerController@create') }}" class="btn btn-primary">
<i class="fa fa-plus-circle" aria-hidden="true"></i> New
</a>
</p>
<table class="table table-striped">
<tr>
<th>Name</th>
<th></th>
</tr>
@foreach($servers as $server)
<tr>
<td>{{ $server->name }}</td>
<td class="text-right">
<a class="btn btn-primary btn-sm"
href="{{ action('ServerController@show', ['Server' => $server]) }}">
<i class="fa fa-search" aria-hidden="true"></i> Show
</a>
<a class="btn btn-primary btn-sm"
href="{{ action('ServerController@edit', ['Server' => $server]) }}">
<i class="fa fa-pencil" aria-hidden="true"></i> Edit
</a>
<form method="POST"
action="{{ action('ServerController@destroy', ['Server' => $server]) }}"
style="display: inline-block">
{{ csrf_field() }}
{{ method_field("DELETE") }}
<button class="btn btn-danger btn-sm">
<i class="fa fa-times-circle" aria-hidden="true"></i> Delete
</button>
</form>
</td>
</tr>
@endforeach
</table>
</div>
@endsection
\ No newline at end of file
@extends('layouts.app')
@section('content')
<div class="container">
<div class="row justify-content-center">
<div class="col-md-8">
<div class="card">
<div class="card-header">{{ $server->name }}</div>
<div class="card-body">
<p>Name: {{ $server->name }}</p>
<div>
<a class="btn btn-primary"
href="{{ action('ServerController@edit', ['Server' => $server]) }}">
<i class="fa fa-pencil" aria-hidden="true"></i> Edit
</a>
<form method="POST"
action="{{ action('ServerController@destroy', ['Server' => $server]) }}"
style="display: inline-block">
{{ csrf_field() }}
{{ method_field("DELETE") }}
<button class="btn btn-danger">
<i class="fa fa-times-circle" aria-hidden="true"></i> Delete
</button>
</form>
</div>
</div>
</div>
</div>
</div>
</div>
@endsection
......@@ -18,4 +18,5 @@ Route::get('/', function () {
Auth::routes();
Route::get('/home', 'HomeController@index')->name('home');
Route::resource('app/organizations', 'OrganizationController');
\ No newline at end of file
Route::resource('app/organizations', 'OrganizationController');
Route::resource('app/servers', 'ServerController');
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment