Skip to content

Commit

Permalink
refactor: sections info sended using resources (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
BaaltRodrigo authored Oct 10, 2023
1 parent 8522d70 commit 736a897
Show file tree
Hide file tree
Showing 13 changed files with 249 additions and 9 deletions.
11 changes: 9 additions & 2 deletions app/Http/Controllers/CalendarController.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use App\Models\Calendar;
use App\Models\Section;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Support\Str;


Expand Down Expand Up @@ -72,13 +73,13 @@ public function addSection(AddSectionRequest $request, Calendar $calendar)
$validated = $request->validated();
// Check if section exists in calendar
if ($calendar->sections()->where('id', $validated['section_id'])->exists()) {
return response()->json(['message' => 'Section already exists in calendar'], 409);
return response()->json(['message' => 'Section already exists in calendar'], Response::HTTP_CONFLICT);
}

// Check if section is from same academic charge
$section = $calendar->academicCharge->sections()->where('id', $validated['section_id'])->first();
if (!$section) {
return response()->json(['message' => 'Section does not exist in academic charge'], 404);
return response()->json(['message' => 'Section does not exist in academic charge'], Response::HTTP_NOT_FOUND);
}

// TODO: Check if calendarable type id is the same as section school or career
Expand All @@ -95,7 +96,13 @@ public function addSection(AddSectionRequest $request, Calendar $calendar)

public function removeSection(Calendar $calendar, Section $section)
{
// check if there is a section to detach
if (!$calendar->sections()->where('id', $section->id)->exists()) {
return response()->json(['message' => 'Section does not exist in calendar'], Response::HTTP_NOT_FOUND);
}

$calendar->sections()->detach($section);

return response()->json([
'message' => 'Section removed from calendar',
'calendar' => $calendar
Expand Down
7 changes: 3 additions & 4 deletions app/Http/Controllers/v1/AcademicChargeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
use App\Http\Requests\StoreAcademicChargeRequest as StoreRequest;
use App\Http\Requests\UpdateAcademicChargeRequest as UpdateRequest;
use App\Http\Resources\AcademicChargeResource;
use App\Http\Resources\Collection\SectionCollection;
use App\Http\Resources\Collections\AcademicChargeCollection;
use App\Http\Resources\Collections\CareerCollection;
use App\Http\Resources\Collections\SchoolCollection;
use App\Models\AcademicCharge;
use App\Models\Career;
use Illuminate\Http\Request;

class AcademicChargeController extends Controller
Expand Down Expand Up @@ -74,7 +74,7 @@ public function careers(AcademicCharge $charge): CareerCollection
return new CareerCollection($charge->sections->pluck('career')->unique());
}

public function sections(ListSectionRequest $request, AcademicCharge $charge)
public function sections(ListSectionRequest $request, AcademicCharge $charge): SectionCollection
{
// General filtering using resource id and their type
$validated = $request->validated();
Expand All @@ -83,8 +83,7 @@ public function sections(ListSectionRequest $request, AcademicCharge $charge)
->where($validated['resource_type'].'_id', $validated['resource_id'])
->get();

return $sections;

return SectionCollection::make($sections);
}

public function schools(AcademicCharge $charge): SchoolCollection
Expand Down
1 change: 1 addition & 0 deletions app/Http/Requests/AddSectionRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public function rules(): array
{
return [
'section_id' => ['required', 'integer', 'exists:academic_charge_subject,id'],
'options' => ['sometimes', 'nullable', 'json'],
];
}
}
8 changes: 7 additions & 1 deletion app/Http/Resources/CalendarResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Http\Resources;

use App\Http\Resources\Collection\SectionCollection;
use App\Http\Resources\Identifiers\AcademicChargeIdentifier;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;
Expand Down Expand Up @@ -30,7 +31,12 @@ public function toArray(Request $request): array
'academic_charge' => $this->whenLoaded('academicCharge', function () {
return AcademicChargeIdentifier::make($this->academicCharge);
}),
'sections' => [],
'sections' => SectionCollection::make(
$this->when(
$this->relationLoaded('sections'),
$this->sections
)
)
];
}
}
20 changes: 20 additions & 0 deletions app/Http/Resources/Collection/SectionCollection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace App\Http\Resources\Collection;

use App\Http\Resources\SectionResource;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\ResourceCollection;

class SectionCollection extends ResourceCollection
{
/**
* Transform the resource collection into an array.
*
* @return array<int|string, mixed>
*/
public function toArray(Request $request): array
{
return SectionResource::collection($this->collection)->toArray($request);
}
}
20 changes: 20 additions & 0 deletions app/Http/Resources/Collections/ScheduleCollection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace App\Http\Resources\Collections;

use App\Http\Resources\ScheduleResource;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\ResourceCollection;

class ScheduleCollection extends ResourceCollection
{
/**
* Transform the resource collection into an array.
*
* @return array<int|string, mixed>
*/
public function toArray(Request $request): array
{
return ScheduleResource::collection($this->collection)->toArray($request);
}
}
23 changes: 23 additions & 0 deletions app/Http/Resources/ScheduleResource.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace App\Http\Resources;

use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;

class ScheduleResource extends JsonResource
{
/**
* Transform the resource into an array.
*
* @return array<string, mixed>
*/
public function toArray(Request $request): array
{
return [
'day' => $this->day,
'classroom' => $this->classroom,
'times' => $this->times,
];
}
}
52 changes: 52 additions & 0 deletions app/Http/Resources/SectionResource.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

namespace App\Http\Resources;

use App\Http\Resources\Collections\ScheduleCollection;
use App\Http\Resources\Identifiers\CareerIdentifier;
use App\Http\Resources\Identifiers\SchoolIdentifier;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;

class SectionResource extends JsonResource
{
/**
* Transform the resource into an array.
*
* @return array<string, mixed>
*/
public function toArray(Request $request): array
{
return [
'id' => $this->id,
'code' => $this->code,
'shift' => $this->shift,
'teacher' => $this->teacher,
// Relationships
'schedules' => ScheduleCollection::make(
$this->when(
$this->relationLoaded('schedules'),
$this->schedules
)
),
'subject' => SubjectResource::make(
$this->when(
$this->relationLoaded('subject'),
$this->subject
)
),
'career' => CareerIdentifier::make(
$this->when(
$this->relationLoaded('career'),
$this->career
)
),
'school' => SchoolIdentifier::make(
$this->when(
$this->relationLoaded('school'),
$this->school
)
)
];
}
}
24 changes: 24 additions & 0 deletions app/Http/Resources/SubjectResource.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace App\Http\Resources;

use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;

class SubjectResource extends JsonResource
{
/**
* Transform the resource into an array.
*
* @return array<string, mixed>
*/
public function toArray(Request $request): array
{
return [
'id' => $this->id,
'code' => $this->code,
'name' => $this->name,
'level' => $this->level,
];
}
}
3 changes: 2 additions & 1 deletion app/Models/Calendar.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ public function charge(): BelongsTo

public function sections(): BelongsToMany
{
return $this->belongsToMany(Section::class, 'calendar_section', 'calendar_id', 'section_id');
return $this->belongsToMany(Section::class, 'calendar_section', 'calendar_id', 'section_id')
->withPivot('options');
}

public function calendarable(): MorphTo
Expand Down
2 changes: 1 addition & 1 deletion app/Models/Section.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class Section extends Model

protected $table = 'academic_charge_subject';

protected $with = ['subject', 'schedules'];
protected $with = ['subject', 'schedules', 'career', 'school'];

// The attributes that are mass assignable.
protected $fillable = [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public function up(): void
Schema::create('calendar_section', function (Blueprint $table) {
$table->foreignId('calendar_id')->constrained()->cascadeOnDelete();
$table->unsignedBigInteger('section_id');
$table->json('options')->nullable();

$table->foreign('section_id')
->references('id')
Expand Down
86 changes: 86 additions & 0 deletions tests/Feature/CalendarTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use App\Models\Section;
use App\Models\Subject;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Http\Response;
use Illuminate\Support\Str;
use Tests\TestCase;
use Tests\Traits\UseFirebaseUser;
Expand Down Expand Up @@ -175,4 +176,89 @@ public function test_it_allows_owners_to_delete_calendar_section(): void
'section_id' => $section->id
]);
}

// test to allow owners to add a section to a calendar
public function test_it_allows_owners_to_add_section_to_calendar(): void
{
$user = $this->createUser();
$section = Section::factory()->create();
$calendar = Calendar::factory()->create([
'user_id' => $user->id,
'academic_charge_id' => $section->academic_charge_id,
'calendarable_type' => get_class($section->career),
'calendarable_id' => $section->career->id
]);
$this->actingAsFirebaseUser();

$response = $this->postJson(route('calendars.sections.store', $calendar->uuid), [
'section_id' => $section->id
]);

$response->assertStatus(201);
$this->assertDatabaseHas('calendar_section', [
'calendar_id' => $calendar->id,
'section_id' => $section->id
]);
}

// test to allow owners to delete a section from a calendar
public function test_it_allows_owners_to_delete_section_from_calendar(): void
{
$user = $this->createUser();
$section = Section::factory()->create();
$calendar = Calendar::factory()->create([
'user_id' => $user->id,
'academic_charge_id' => $section->academic_charge_id,
'calendarable_type' => get_class($section->career),
'calendarable_id' => $section->career->id
]);
$calendar->sections()->attach($section);
$this->actingAsFirebaseUser();

$response = $this->deleteJson(route('calendars.sections.destroy', [$calendar->uuid, $section->id]));

$response->assertStatus(204);
$this->assertDatabaseMissing('calendar_section', [
'calendar_id' => $calendar->id,
'section_id' => $section->id
]);
}

public function test_it_does_not_add_duplicate_sections_to_calendars(): void
{
$user = $this->createUser();
$section = Section::factory()->create();
$calendar = Calendar::factory()->create([
'user_id' => $user->id,
'academic_charge_id' => $section->academic_charge_id,
'calendarable_type' => get_class($section->career),
'calendarable_id' => $section->career->id
]);
$calendar->sections()->attach($section);
$this->actingAsFirebaseUser();

$response = $this->postJson(route('calendars.sections.store', $calendar->uuid), [
'section_id' => $section->id
]);

$response->assertStatus(Response::HTTP_CONFLICT);
}

public function test_it_returns_404_removing_non_attached_sections(): void
{
$user = $this->createUser();
$section = Section::factory()->create();
$calendar = Calendar::factory()->create([
'user_id' => $user->id,
'academic_charge_id' => $section->academic_charge_id,
'calendarable_type' => get_class($section->career),
'calendarable_id' => $section->career->id
]);
$this->actingAsFirebaseUser();

$response = $this->deleteJson(route('calendars.sections.destroy', [$calendar->uuid, $section->id]));

$response->assertStatus(Response::HTTP_NOT_FOUND);
}

}

0 comments on commit 736a897

Please sign in to comment.