Skip to content

Commit

Permalink
Feature/start 40 (#11)
Browse files Browse the repository at this point in the history
* Add changes with CR

* Add validate to generate jitsi object and add filter date for api constultations list

* Repair bug in register settingsProvider

* Force cart version, todo after changes in cart package

* Add changes after CR

* Add changes from new cart package

* Change inputs in request

* Add set relations to proposed terms and add possibility upload logotype

Co-authored-by: Hubert Krzysztofiak <hubert.krzysztofiak@escolasoft.com>
  • Loading branch information
HerbertIV and Hubert Krzysztofiak authored Feb 22, 2022
1 parent e7fac89 commit 6769e10
Show file tree
Hide file tree
Showing 28 changed files with 531 additions and 53 deletions.
21 changes: 21 additions & 0 deletions database/factories/ConsultationProposedTermFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace EscolaLms\Consultations\Database\Factories;

use EscolaLms\Consultations\Models\Consultation;
use EscolaLms\Consultations\Models\ConsultationProposedTerm;
use Illuminate\Database\Eloquent\Factories\Factory;

class ConsultationProposedTermFactory extends Factory
{
protected $model = ConsultationProposedTerm::class;

public function definition()
{
$consultation = Consultation::firstOrCreate();
return [
'consultation_id' => $consultation->getKey(),
'proposed_at' => $this->faker->dateTimeBetween($consultation->active_from, $consultation->active_to),
];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateConsultationProposedTermsTale extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('consultation_proposed_terms', function (Blueprint $table) {
$table->id();
$table->bigInteger('consultation_id')->unsigned();
$table->dateTime('proposed_at');
$table->timestamps();

$table->foreign('consultation_id')->on('consultations')->references('id')->cascadeOnDelete();
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('consultation_proposed_terms');
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class AddColumnImagePathForConsultationsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('consultations', function (Blueprint $table) {
$table->string('image_path')->nullable();
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('consultations', function (Blueprint $table) {
$table->dropColumn('image_path');
});
}
}
17 changes: 17 additions & 0 deletions src/Dto/ConsultationDto.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@

namespace EscolaLms\Consultations\Dto;

use Carbon\Carbon;
use EscolaLms\Consultations\Dto\Contracts\ModelDtoContract;
use EscolaLms\Consultations\Models\Consultation;
use EscolaLms\Consultations\Models\ConsultationProposedTerm;
use Illuminate\Http\UploadedFile;

class ConsultationDto extends BaseDto implements ModelDtoContract
{
Expand All @@ -26,4 +29,18 @@ public function toArray($filters = false): array
$result = $this->fillInArray($this->model()->getFillable());
return $filters ? array_filter($result) : $result;
}

protected function setProposedTerms(array $proposedTerms): void
{
$result = [];
foreach ($proposedTerms as $term) {
$result[] = new ConsultationProposedTerm(['proposed_at' => Carbon::make($term)]);
}
$this->relations['proposedTerms'] = $result;
}

public function setImage(UploadedFile $file)
{
$this->files['image_path'] = $file;
}
}
13 changes: 13 additions & 0 deletions src/Dto/Traits/DtoHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@

trait DtoHelper
{
protected array $relations = [];
protected array $files = [];

protected function setterByData(array $data): void
{
foreach ($data as $k => $v) {
Expand Down Expand Up @@ -39,4 +42,14 @@ protected function fillInArray(array $fillables): array
return $result;
}

public function getRelations(): array
{
return $this->relations;
}

public function getFiles(): array
{
return $this->files;
}

}
53 changes: 53 additions & 0 deletions src/Helpers/StrategyHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

namespace EscolaLms\Consultations\Helpers;

class StrategyHelper
{
private string $namespace;

public function __construct(string $baseStrategyName)
{
$this->setNamespace($baseStrategyName);
}

/**
* This method used strategy pattern and execute method given in the parameters
* Strategy dir it must contain minimum to file: BaseStrategy contain in pattern {{parentDir}}Strategy
* in localization ?/Strategies/{{parentDir}} and strategy class in the same localization
*
* @param string $className
* @param string $baseStrategyName
* @param string $method
* @param ...$params
* @return mixed|null
*/
public static function useStrategyPattern(
string $className,
string $baseStrategyName,
string $method,
...$params
) {
$strategyHelper = new StrategyHelper($baseStrategyName);
$class = $strategyHelper->namespace . '\\' . $className;
$baseStrategyClass = $strategyHelper->namespace . '\\' . $baseStrategyName;
if (
class_exists($class) &&
class_exists($baseStrategyClass) &&
method_exists($baseStrategyClass, $method)
) {
$strategy = new $baseStrategyClass(
new $class($params)
);

return $strategy->$method();
}
return null;
}

private function setNamespace(string $baseStrategyName): void
{
$this->namespace = 'EscolaLms\Consultations\Strategies\\' .
preg_replace('/^(.*)Strategy$/', '$1', $baseStrategyName);
}
}
10 changes: 10 additions & 0 deletions src/Http/Controllers/ConsultationAPIController.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use EscolaLms\Consultations\Http\Controllers\Swagger\ConsultationAPISwagger;
use EscolaLms\Consultations\Http\Requests\ListConsultationsRequest;
use EscolaLms\Consultations\Http\Requests\ReportTermConsultationRequest;
use EscolaLms\Consultations\Http\Resources\ConsultationProposedTermResource;
use EscolaLms\Consultations\Http\Resources\ConsultationSimpleResource;
use EscolaLms\Consultations\Services\Contracts\ConsultationServiceContract;
use EscolaLms\Core\Http\Controllers\EscolaLmsBaseController;
Expand Down Expand Up @@ -54,6 +55,15 @@ public function rejectTerm(int $consultationTermId): JsonResponse
return $this->sendSuccess(__('Consultation term reject successfully'));
}

public function proposedTerms(int $orderItemId): JsonResponse
{
$proposedTerms = $this->consultationServiceContract->proposedTerms($orderItemId);
return $this->sendResponseForResource(
ConsultationProposedTermResource::collection($proposedTerms),
__('Consultations propsed terms retrieved successfully')
);
}

public function generateJitsi(int $consultationTermId): JsonResponse
{
return $this->sendResponse(
Expand Down
4 changes: 2 additions & 2 deletions src/Http/Controllers/ConsultationController.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public function index(ListConsultationsRequest $listConsultationsRequest): JsonR
public function store(StoreConsultationRequest $storeConsultationRequest): JsonResponse
{
$dto = new ConsultationDto($storeConsultationRequest->all());
$consultation = $this->consultationServiceContract->store($dto->toArray());
$consultation = $this->consultationServiceContract->store($dto);
return $this->sendResponseForResource(
ConsultationSimpleResource::make($consultation),
__('Consultation saved successfully')
Expand All @@ -52,7 +52,7 @@ public function store(StoreConsultationRequest $storeConsultationRequest): JsonR
public function update(int $id, UpdateConsultationRequest $updateConsultationRequest): JsonResponse
{
$dto = new ConsultationDto($updateConsultationRequest->all());
$consultation = $this->consultationServiceContract->update($id, $dto->toArray());
$consultation = $this->consultationServiceContract->update($id, $dto);
return $this->sendResponseForResource(
ConsultationSimpleResource::make($consultation),
__('Consultation updated successfully')
Expand Down
3 changes: 3 additions & 0 deletions src/Http/Requests/StoreConsultationRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,12 @@ public function rules(): array
'status' => ['required', 'string', Rule::in(ConsultationStatusEnum::getValues())],
'description' => ['required', 'string', 'min:3'],
'duration' => ['nullable', 'string', 'max:80'],
'image' => ['nullable', 'file', 'image'],
'author_id' => ['required', 'integer', 'exists:users,id'],
'active_from' => ['date'],
'active_to' => ['date', 'after_or_equal:active_from'],
'proposed_dates' => ['array'],
'proposed_dates.*' => ['date', 'after_or_equal:active_from'],
];
}
}
3 changes: 3 additions & 0 deletions src/Http/Requests/UpdateConsultationRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,12 @@ public function rules(): array
'status' => ['required', 'string', Rule::in(ConsultationStatusEnum::getValues())],
'description' => ['required', 'string', 'min:3'],
'duration' => ['nullable', 'string', 'max:80'],
'image' => ['nullable', 'file', 'image'],
'author_id' => ['required', 'integer', 'exists:users,id'],
'active_from' => ['date'],
'active_to' => ['date', 'after_or_equal:active_from'],
'proposed_dates' => ['array'],
'proposed_dates.*' => ['date', 'after_or_equal:active_from'],
];
}
}
18 changes: 18 additions & 0 deletions src/Http/Resources/ConsultationProposedTermResource.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace EscolaLms\Consultations\Http\Resources;

use EscolaLms\Auth\Traits\ResourceExtandable;
use Illuminate\Http\Resources\Json\JsonResource;
use Illuminate\Support\Carbon;

class ConsultationProposedTermResource extends JsonResource
{
use ResourceExtandable;

public function toArray($request)
{
$proposedAt = is_string($this->proposed_at) ? Carbon::make($this->proposed_at) : $this->proposed_at;
return $proposedAt->format('Y-m-d H:i:s');
}
}
7 changes: 5 additions & 2 deletions src/Http/Resources/ConsultationSimpleResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,16 @@ public function toArray($request)
'id' => $this->id,
'created_at' => $this->created_at,
'updated_at' => $this->updated_at,
'started_at' => $this->started_at,
'finished_at' => $this->finished_at,
'active_from' => $this->active_from,
'active_to' => $this->active_to,
'name' => $this->name,
'base_price' => $this->base_price,
'author_id' => $this->author_id,
'status' => $this->status,
'description' => $this->description,
'duration' => $this->duration,
'image_path' => $this->image_path,
'proposed_terms' => ConsultationProposedTermResource::collection($this->proposedTerms)
];
return self::apply($fields, $this);
}
Expand Down
25 changes: 25 additions & 0 deletions src/Models/Consultation.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,14 @@
namespace EscolaLms\Consultations\Models;

use EscolaLms\Auth\Models\User;
use EscolaLms\Cart\Contracts\Base\BuyableTrait;
use EscolaLms\Cart\Models\OrderItem;
use EscolaLms\Consultations\Database\Factories\ConsultationFactory;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\MorphMany;


/**
Expand Down Expand Up @@ -74,6 +78,7 @@
class Consultation extends Model
{
use HasFactory;
use BuyableTrait;

protected $fillable = [
'base_price',
Expand All @@ -91,8 +96,28 @@ public function author(): BelongsTo
return $this->belongsTo(User::class, 'author_id');
}

public function orderItems(): MorphMany
{
return $this->morphMany(OrderItem::class, 'buyable');
}

public function proposedTerms(): HasMany
{
return $this->hasMany(ConsultationProposedTerm::class, 'consultation_id');
}

protected static function newFactory(): ConsultationFactory
{
return ConsultationFactory::new();
}

public function getBuyableDescription(): string
{
// TODO: Implement getBuyableDescription() method.
}

public function getBuyablePrice(?array $options = null): int
{
return $this->base_price ?? 0;
}
}
28 changes: 28 additions & 0 deletions src/Models/ConsultationProposedTerm.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace EscolaLms\Consultations\Models;

use EscolaLms\Consultations\Database\Factories\ConsultationProposedTermFactory;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

class ConsultationProposedTerm extends Model
{
use HasFactory;

protected $fillable = [
'consultation_id',
'proposed_at',
];

public function consultation(): BelongsTo
{
return $this->belongsTo(Consultation::class, 'consultation_id');
}

protected static function newFactory(): ConsultationProposedTermFactory
{
return ConsultationProposedTermFactory::new();
}
}
Loading

0 comments on commit 6769e10

Please sign in to comment.