Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(event): implements speakers and schedule relationship #74

Draft
wants to merge 11 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions app/Event.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,14 @@ class Event extends Model
'duration',
'enrollable',
];

public function speakers()
{
return $this->hasMany('App\Speakers');
}

public function schedule()
{
return $this->hasMany('App\Schedule');
}
}
31 changes: 20 additions & 11 deletions app/Http/Controllers/Cms/ScheduleController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,26 @@

use App\Http\Controllers\Controller;

use App\Event;
use App\Schedule;
use Illuminate\Http\Request;

class ScheduleController extends Controller
{
public function index()
public function index(Request $request)
{
$event_id = $request->event_id;
$request->session()->flash('event_id', $event_id);
$event = Event::where('id', '=', $event_id)->get(['id', 'name'])->first();
$schedules = Schedule::all();
return view('cms.schedule.index', compact('schedules'));

return view('cms.schedule.index', compact('schedules', 'event'));
}

public function create()
public function create(Request $request)
{
return view('cms.schedule.create');
$event_id = $request->session()->get('event_id');
return view('cms.schedule.create', ['event_id' => $event_id]);
}

public function store(Request $request)
Expand All @@ -27,17 +33,19 @@ public function store(Request $request)
'place' => 'required|max:255',
'description' => 'required',
'start_at' => 'date',
'end_at' => 'nullable|date|after_or_equal:start_at'
'end_at' => 'nullable|date|after_or_equal:start_at',
'event_id' => 'required'
]);

$schedule = Schedule::create($scheduleData);

return redirect()->route('schedule.index');
return redirect()->route('schedule.index', ['event_id' => $schedule->event_id]);
}

public function show(Schedule $schedule)
{
return view('cms.schedule.show', compact('schedule'));
$event = Event::where('id', '=', $schedule->event_id)->get(['name', 'id'])->first();
return view('cms.schedule.show', compact('schedule', 'event'));
}

public function edit(Schedule $schedule)
Expand All @@ -47,6 +55,7 @@ public function edit(Schedule $schedule)

public function update(Request $request, Schedule $schedule)
{
$event_id = $schedule->event_id;
$scheduleData = $request->validate([
'title' => 'required|max:255',
'place' => 'required|max:255',
Expand All @@ -57,19 +66,19 @@ public function update(Request $request, Schedule $schedule)

try {
$schedule = $schedule->update($scheduleData);
return redirect()->route('schedule.index')->withSuccess('Programação atualizada.');
return redirect()->route('schedule.index', ['event_id' => $event_id])->withSuccess('Programação atualizada.');
} catch (\Throwable $th) {
return redirect()->route('schedule.index')->withError('Falha ao atualizar programação.');
return redirect()->route('schedule.index', ['event_id' => $event_id])->withError('Falha ao atualizar programação.');
}
}

public function destroy(Schedule $schedule)
{
try {
$schedule->delete();
return redirect()->route('schedule.index')->withSuccess('Progração excluída.');
return redirect()->route('schedule.index', ['event_id' => $schedule->event_id])->withSuccess('Progração excluída.');
} catch (\Throwable $th) {
return redirect()->route('schedule.index')->withError('Erro ao excluir programação.');
return redirect()->route('schedule.index', ['event_id' => $schedule->event_id])->withError('Erro ao excluir programação.');
}
}
}
26 changes: 17 additions & 9 deletions app/Http/Controllers/Cms/SpeakerController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace App\Http\Controllers\Cms;

use App\Speaker;
use App\Event;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Storage;
Expand All @@ -15,21 +16,26 @@ class SpeakerController extends Controller
*
* @return \Illuminate\Http\Response
*/
public function index()
public function index(Request $request)
{
$speakers = Speaker::all();
$event_id = $request->event_id;

$request->session()->flash('event_id', $event_id);
$speakers = Speaker::where('event_id', '=', $event_id)->get();

return view('cms.speaker.index', ['speakers' => $speakers]);
$event = Event::where('id', '=', $event_id)->get(['name', 'id'])->first();
return view('cms.speaker.index', ['speakers' => $speakers, 'event' => $event]);
}

/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
public function create(Request $request)
{
return view('cms.speaker.create');
$event_id = $request->session()->get('event_id');
return view('cms.speaker.create', ['event_id' => $event_id]);
}

/**
Expand All @@ -43,6 +49,7 @@ public function store(Request $request)
$name = $request->name;
$biography = $request->biography;
$photo = $request->file('photo');
$eventId = $request->event_id;
$fileName = '';

if ($photo)
Expand All @@ -56,12 +63,13 @@ public function store(Request $request)
$speaker = Speaker::create([
'name' => $name,
'biography' => $biography,
'photo' => $fileName
'photo' => $fileName,
'event_id' => $eventId
]);

$speaker->save();

return redirect()->route('speaker.index')->withStatus('Palestrante cadastrado com sucesso.');
return redirect()->route('speaker.index', ['event_id' => $eventId])->withStatus('Palestrante cadastrado com sucesso.');
}

/**
Expand Down Expand Up @@ -111,7 +119,7 @@ public function update(Request $request, Speaker $speaker)

$speaker->update();

return redirect()->route('speaker.index')->withStatus('Palestrante atualizado com sucesso');
return redirect()->route('speaker.index', ['event_id' => $speaker->event_id])->withStatus('Palestrante atualizado com sucesso');
}

private function deletePhoto($photoName)
Expand All @@ -133,6 +141,6 @@ public function destroy(Speaker $speaker)
$this->deletePhoto($speaker->photo);
$speaker->delete();

return redirect()->route('speaker.index')->withStatus('Palestrante removido com sucesso');
return redirect()->route('speaker.index', ['event_id' => $speaker->event_id])->withStatus('Palestrante removido com sucesso');
}
}
3 changes: 2 additions & 1 deletion app/Schedule.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ class Schedule extends Model
'description',
'place',
'start_at',
'end_at'
'end_at',
'event_id'
];

protected $casts = [
Expand Down
2 changes: 1 addition & 1 deletion app/Speaker.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ class Speaker extends Model
* @var array
*/
protected $fillable = [
'name', 'biography', 'photo'
'name', 'biography', 'photo', 'event_id'
];
}
58 changes: 29 additions & 29 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading