diff --git a/app/Http/Controllers/Api/EventsController.php b/app/Http/Controllers/Api/EventsController.php index fb11d13..8a4bcf4 100644 --- a/app/Http/Controllers/Api/EventsController.php +++ b/app/Http/Controllers/Api/EventsController.php @@ -25,6 +25,7 @@ class EventsController extends Controller 'id' => $event->id, 'user' => $event->user->name, 'user_id' => $event->user_id, + 'project_id' => $event->project_id, 'title' => $event->title, 'body' => $event->body, 'start' => $event->start, @@ -42,8 +43,9 @@ class EventsController extends Controller public function store(Request $request) { $this->validate($request, [ + 'project_id' => 'nullable|numeric|exists:projects,id', 'title' => 'required|string|max:60', - 'body' => 'string|max:255', + 'body' => 'nullable|string|max:255', 'start' => 'required|date|date_format:Y-m-d H:i:s', 'end' => 'date|date_format:Y-m-d H:i:s', 'is_allday' => '', @@ -51,6 +53,7 @@ class EventsController extends Controller $event = new Event; $event->user_id = auth()->id(); + $event->project_id = $request->get('project_id'); $event->title = $request->get('title'); $event->body = $request->get('body'); $event->start = $request->get('start'); @@ -65,6 +68,7 @@ class EventsController extends Controller ->transformWith(function($event) { return [ 'id' => $event->id, + 'project_id' => $event->project_id, 'user' => $event->user->name, 'title' => $event->title, 'body' => $event->body, @@ -87,17 +91,19 @@ class EventsController extends Controller { $this->validate($request, [ 'id' => 'required|numeric|exists:user_events,id', + 'project_id' => 'nullable|numeric|exists:projects,id', 'title' => 'required|string|max:60', - 'body' => 'string|max:255', + 'body' => 'nullable|string|max:255', 'is_allday' => '', ]); $event = Event::findOrFail($request->get('id')); $this->authorize('update', $event); + $event->project_id = $request->get('project_id'); $event->title = $request->get('title'); $event->body = $request->get('body'); - $event->is_allday = !!$request->get('is_allday'); + $event->is_allday = $request->get('is_allday') == 'true' ? 1 : 0; $event->save(); @@ -107,6 +113,7 @@ class EventsController extends Controller ->transformWith(function($event) { return [ 'id' => $event->id, + 'project_id' => $event->project_id, 'user' => $event->user->name, 'title' => $event->title, 'body' => $event->body, diff --git a/resources/views/users/calendar.blade.php b/resources/views/users/calendar.blade.php index 7be6312..2d3af4c 100644 --- a/resources/views/users/calendar.blade.php +++ b/resources/views/users/calendar.blade.php @@ -49,6 +49,12 @@
+ +
+ {!! FormField::select('project_id', $projects, ['label' => false, 'id' => 'project1']) !!} +
+
+
@@ -95,6 +101,12 @@
+ +
+ {!! FormField::select('project_id', $projects, ['label' => false, 'id' => 'project2']) !!} +
+
+
@@ -170,13 +182,14 @@ $(".antosubmit").on("click", function() { var title = $("#title").val(); var body = $("#descr").val(); + var project_id = $('#project1').val(); var is_allday = $("#is_allday").is(':checked'); if (title) { $.ajax({ url: "{{ route('api.events.store') }}", method: "POST", - data: { title: title, body: body, start: started.format("YYYY-MM-DD HH:mm:ss"), end: ended.format("YYYY-MM-DD HH:mm:ss"), is_allday: is_allday }, + data: { title: title, body: body, project_id: project_id, start: started.format("YYYY-MM-DD HH:mm:ss"), end: ended.format("YYYY-MM-DD HH:mm:ss"), is_allday: is_allday }, success: function(response){ if(response.message == 'event.created') { calendar.fullCalendar('renderEvent', { @@ -187,6 +200,7 @@ end: ended.format("YYYY-MM-DD HH:mm"), user: "{{ auth()->user()->name }}", user_id: "{{ auth()->id() }}", + project_id: project_id, allDay: is_allday, editable: true }, true); @@ -215,7 +229,8 @@ $('#user2').text(calEvent.user); $('#title2').val(calEvent.title); $('#descr2').val(calEvent.body); - $('#is_allday2').val(calEvent.allDay); + $('#project2').val(calEvent.project_id); + $('#is_allday2').prop('checked', calEvent.allDay); $('#CalenderModalEdit').modal(); } else { @@ -254,12 +269,13 @@ $("#antoform2").off("submit").on("submit", function() { calEvent.title = $("#title2").val(); calEvent.body = $("#descr2").val(); + calEvent.project_id = $('#project2').val(); calEvent.is_allday = $("#is_allday2").is(':checked'); $.ajax({ url: "{{ route('api.events.update') }}", method: "PATCH", - data: { id: calEvent.id, title: calEvent.title, body: calEvent.body, is_allday: calEvent.is_allday }, + data: { id: calEvent.id, title: calEvent.title, body: calEvent.body, project_id: calEvent.project_id, is_allday: calEvent.is_allday }, success: function(response){ if(response.message == 'event.updated') $('#calendar').fullCalendar('updateEvent',calEvent); diff --git a/routes/web/calendar.php b/routes/web/calendar.php index 715efa6..47e9530 100644 --- a/routes/web/calendar.php +++ b/routes/web/calendar.php @@ -5,6 +5,7 @@ Route::group(['middleware' => ['web','auth'], 'namespace' => 'Api'], function() * Savety Calendar */ Route::get('my-calendar', ['as' => 'users.calendar', 'uses' => function() { - return view('users.calendar'); + $projects = App\Entities\Projects\Project::orderBy('name')->pluck('name', 'id'); + return view('users.calendar', compact('projects')); }]); }); \ No newline at end of file diff --git a/tests/Feature/Api/ApiEventsTest.php b/tests/Feature/Api/ApiEventsTest.php index 0796fec..8789a6f 100644 --- a/tests/Feature/Api/ApiEventsTest.php +++ b/tests/Feature/Api/ApiEventsTest.php @@ -2,6 +2,7 @@ namespace Tests\Feature\Api; +use App\Entities\Projects\Project; use App\Entities\Users\Event; use App\Entities\Users\User; use Tests\TestCase; @@ -12,7 +13,7 @@ class ApiEventsTest extends TestCase public function it_can_get_all_existing_events() { $user = factory(User::class)->create(); - $events = factory(Event::class, 5)->create(['user_id' => $user->id]); + $events = factory(Event::class, 2)->create(['user_id' => $user->id]); $this->getJson(route('api.events.index'), [ 'Authorization' => 'Bearer ' . $user->api_token @@ -28,6 +29,7 @@ class ApiEventsTest extends TestCase 'start', 'end', 'allDay', + 'project_id', ] ]); } @@ -36,11 +38,13 @@ class ApiEventsTest extends TestCase public function user_can_create_new_event() { $user = factory(User::class)->create(); + $project = factory(Project::class)->create(); $this->postJson(route('api.events.store'), [ 'title' => 'New Event Title', 'body' => 'New Event Body', 'start' => '2016-07-21 12:20:00', + 'project_id' => $project->id, ], [ 'Authorization' => 'Bearer ' . $user->api_token ]); @@ -51,6 +55,7 @@ class ApiEventsTest extends TestCase $this->seeJson([ 'message' => trans('event.created'), + 'project_id' => $project->id, 'user' => $user->name, 'title' => 'New Event Title', 'body' => 'New Event Body', @@ -60,6 +65,7 @@ class ApiEventsTest extends TestCase ]); $this->seeInDatabase('user_events', [ + 'project_id' => $project->id, 'user_id' => $user->id, 'title' => 'New Event Title', 'body' => 'New Event Body', @@ -73,10 +79,12 @@ class ApiEventsTest extends TestCase public function user_can_update_their_event() { $user = factory(User::class)->create(); + $project = factory(Project::class)->create(); $event = factory(Event::class)->create(['user_id' => $user->id]); // dump($event->toArray()); $this->patchJson(route('api.events.update'), [ 'id' => $event->id, + 'project_id' => $project->id, 'title' => 'New Event Title', 'body' => 'New Event Body', 'is_allday' => 'true', @@ -88,6 +96,7 @@ class ApiEventsTest extends TestCase $this->seeJson([ 'message' => trans('event.updated'), + 'project_id' => $project->id, 'user' => $user->name, 'title' => 'New Event Title', 'body' => 'New Event Body', @@ -95,6 +104,7 @@ class ApiEventsTest extends TestCase $this->seeInDatabase('user_events', [ 'user_id' => $user->id, + 'project_id' => $project->id, 'title' => 'New Event Title', 'body' => 'New Event Body', ]);