Skip to content

Commit

Permalink
Merge pull request #321 from EscolaLMS/feature/dictionary-product
Browse files Browse the repository at this point in the history
Register Dictionary class as product
  • Loading branch information
qunabu authored Mar 13, 2024
2 parents 5f9e0da + c845ea8 commit 2ff3199
Show file tree
Hide file tree
Showing 3 changed files with 208 additions and 163 deletions.
28 changes: 28 additions & 0 deletions app/Models/Dictionary.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace App\Models;

use EscolaLms\Cart\Contracts\Productable;
use EscolaLms\Cart\Contracts\ProductableTrait;
use EscolaLms\Cart\Models\Product;
use EscolaLms\Core\Models\User;
use EscolaLms\Courses\Events\CourseAccessStarted;
use EscolaLms\Courses\Events\CourseAssigned;
use EscolaLms\Courses\Events\CourseFinished;
use EscolaLms\Courses\Events\CourseUnassigned;
use EscolaLms\Dictionaries\Models\Dictionary as BaseDictionary;

class Dictionary extends BaseDictionary implements Productable
{
use ProductableTrait;

public function attachToUser(User $user, int $quantity = 1, ?Product $product = null): void
{
$this->users()->syncWithoutDetaching($user->getKey());
}

public function detachFromUser(User $user, int $quantity = 1, ?Product $product = null): void
{
$this->users()->detach($user->getKey());
}
}
10 changes: 10 additions & 0 deletions app/Providers/ShopServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use App\Models\Consultation;
use App\Models\Course;
use App\Models\Dictionary;
use App\Models\StationaryEvent;
use App\Models\Webinar;
use EscolaLms\Cart\Facades\Shop;
Expand All @@ -14,6 +15,7 @@
use EscolaLms\Consultations\Http\Resources\ConsultationTermsResource;
use EscolaLms\Courses\Http\Resources\CourseListResource;
use EscolaLms\Courses\Http\Resources\CourseSimpleResource;
use EscolaLms\Dictionaries\Http\Resources\DictionaryResource;
use EscolaLms\StationaryEvents\Http\Resources\StationaryEventResource;
use EscolaLms\Webinar\Http\Resources\WebinarSimpleResource;
use Illuminate\Contracts\Container\BindingResolutionException;
Expand Down Expand Up @@ -84,6 +86,14 @@ function($element) {
$element
)
);
Shop::registerProductableClass(Dictionary::class);
DictionaryResource::extend(
fn ($element) =>
$this->registerProductToResource(
Dictionary::class,
$element
)
);
}

private function registerProductToResource(string $class, $element): array
Expand Down
Loading

0 comments on commit 2ff3199

Please sign in to comment.