Skip to content

Commit

Permalink
Feature/start 40 (#3)
Browse files Browse the repository at this point in the history
* Add controllers, migrations,models,repositories and services

* Remove missing columns

* Repair migration

* Repair migration

* Repair migration

* Repair bug in migration

* Remove empty swagger

* Change localization swagger

* Change namespasec in testcase

* Add changes after CR

* Repair bug in test. Set specyfic version in swagger

* Change value in test-cc github

* Add filter to phpunit in cc

* Change value in env

* Start create api admin

* Change method name

* Add tests to list

* Repair bug in enums

* Update test-cc.yml

* Add crud to consultation

* Remove missing controller

* Add swagger and destroy action

* Repair tests

* Change autorizator in request

* Add changes with CR

Co-authored-by: Hubert Krzysztofiak <hubert.krzysztofiak@escolasoft.com>
Co-authored-by: Mateusz Qunabu <mateusz@qunabu.com>
  • Loading branch information
3 people authored Feb 9, 2022
1 parent bc93ed3 commit 27b5dfe
Show file tree
Hide file tree
Showing 38 changed files with 1,186 additions and 126 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test-cc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,4 @@ jobs:
- name: Upload
run: ./cc-test-reporter upload-coverage -i codeclimate.0.json
env:
CC_TEST_REPORTER_ID: 8539f41f60574ac436ad0be5c4a05633ca02b4614ecbcf0acd9dd76ef4130d80
CC_TEST_REPORTER_ID: 004a62af5e00aac588594d19de2be2e6c6d0844332c6a5a4f54b7fdfb00e0798
6 changes: 2 additions & 4 deletions database/factories/ConsultationFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
use EscolaLms\Consultations\Enum\ConsultationStatusEnum;
use EscolaLms\Consultations\Models\Consultation;
use EscolaLms\Core\Enums\UserRole;
use EscolaLms\Courses\Database\Factories\FakerMarkdownProvider\FakerProvider;
use Illuminate\Database\Eloquent\Factories\Factory;

class ConsultationFactory extends Factory
Expand All @@ -19,11 +18,10 @@ public function definition()
$now = now();
return [
'base_price' => $this->faker->numberBetween(1, 200),
'name' => $this->faker->word,
'name' => $this->faker->sentence(10),
'status' => $this->faker->randomElement(ConsultationStatusEnum::getValues()),
'description' => $this->faker->sentence,
'author_id' => empty($tutor) ? null : $tutor->id,
'calendar_url' => $this->faker->url,
'author_id' => empty($tutor) ? null : $tutor->getKey(),
'started_at' => $now,
'finished_at' => (clone $now)->modify('+1 hour'),
];
Expand Down
5 changes: 2 additions & 3 deletions database/factories/ConsultationParticipantFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
use EscolaLms\Consultations\Enum\ConsultationParticipantStatusEnum;
use EscolaLms\Consultations\Models\Consultation;
use EscolaLms\Consultations\Models\ConsultationParticipant;
use EscolaLms\Courses\Database\Factories\FakerMarkdownProvider\FakerProvider;
use Illuminate\Database\Eloquent\Factories\Factory;

class ConsultationParticipantFactory extends Factory
Expand All @@ -15,8 +14,8 @@ class ConsultationParticipantFactory extends Factory

public function definition()
{
$consultation = Consultation::inRandomOrder()->first();
$user = User::where('id', '<>', $consultation->author_id)->inRandomOrder()->first();
$consultation = Consultation::inRandomOrder()->firstOrFail();
$user = User::where('id', '<>', $consultation->author_id)->inRandomOrder()->firstOrFail();

return [
'consultation_id' => $consultation->getKey(),
Expand Down
32 changes: 32 additions & 0 deletions database/migrations/2022_02_07_142406_remove_missing_columns.php
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 RemoveMissingColumns extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('consultations', function (Blueprint $table) {
$table->dropColumn('calendar_url');
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('consultations', function (Blueprint $table) {
$table->text('calendar_url')->nullable();
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

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

class AddColumnsDateAndStatusToOrders extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
if (
Schema::hasTable('orders') &&
!Schema::hasColumns('orders', ['executed_at', 'executed_status'])
) {
Schema::table('orders', function (Blueprint $table) {
$table->dateTime('executed_at')->nullable();
$table->string('executed_status')->nullable();
});
}
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
if (
Schema::hasTable('orders') &&
Schema::hasColumns('orders', ['executed_at', 'executed_status'])
) {
Schema::table('orders', function (Blueprint $table) {
$table->dropColumn(['executed_at', 'executed_status']);
});
}
}
}
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 AddMissingsColumnToConsultationsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('consultations', function (Blueprint $table) {
$table->string('duration')->nullable();
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('consultations', function (Blueprint $table) {
$table->dropColumn('duration');
});
}
}
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 RemoveRedundantColumnsFromConsultationsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('consultations', function (Blueprint $table) {
$table->dropColumn('duration');
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('consultations', function (Blueprint $table) {
$table->string('duration')->nullable();
});
}
}
17 changes: 5 additions & 12 deletions database/seeders/ConsultationsPermissionSeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,28 +21,21 @@ public function run()
Permission::findOrCreate(ConsultationsPermissionsEnum::CONSULTATION_UPDATE, 'api');
Permission::findOrCreate(ConsultationsPermissionsEnum::CONSULTATION_DELETE, 'api');
Permission::findOrCreate(ConsultationsPermissionsEnum::CONSULTATION_CREATE, 'api');
Permission::findOrCreate(ConsultationsPermissionsEnum::CONSULTATION_ATTEND, 'api');

Permission::findOrCreate(ConsultationsPermissionsEnum::CONSULTATION_UPDATE_OWNED, 'api');
Permission::findOrCreate(ConsultationsPermissionsEnum::CONSULTATION_DELETE_OWNED, 'api');
Permission::findOrCreate(ConsultationsPermissionsEnum::CONSULTATION_ATTEND_OWNED, 'api');
Permission::findOrCreate(ConsultationsPermissionsEnum::CONSULTATION_READ, 'api');

$admin->givePermissionTo([
ConsultationsPermissionsEnum::CONSULTATION_LIST,
ConsultationsPermissionsEnum::CONSULTATION_UPDATE,
ConsultationsPermissionsEnum::CONSULTATION_DELETE,
ConsultationsPermissionsEnum::CONSULTATION_CREATE,
ConsultationsPermissionsEnum::CONSULTATION_ATTEND,
ConsultationsPermissionsEnum::CONSULTATION_UPDATE_OWNED,
ConsultationsPermissionsEnum::CONSULTATION_DELETE_OWNED,
ConsultationsPermissionsEnum::CONSULTATION_ATTEND_OWNED,
ConsultationsPermissionsEnum::CONSULTATION_READ,
]);
$tutor->givePermissionTo([
ConsultationsPermissionsEnum::CONSULTATION_LIST,
ConsultationsPermissionsEnum::CONSULTATION_UPDATE,
ConsultationsPermissionsEnum::CONSULTATION_DELETE,
ConsultationsPermissionsEnum::CONSULTATION_CREATE,
ConsultationsPermissionsEnum::CONSULTATION_UPDATE_OWNED,
ConsultationsPermissionsEnum::CONSULTATION_DELETE_OWNED,
ConsultationsPermissionsEnum::CONSULTATION_ATTEND_OWNED,
ConsultationsPermissionsEnum::CONSULTATION_READ,
]);
}
}
15 changes: 15 additions & 0 deletions src/Dto/BaseDto.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace EscolaLms\Consultations\Dto;

use EscolaLms\Consultations\Dto\Traits\DtoHelper;

abstract class BaseDto
{
use DtoHelper;

public function __construct(array $data = [])
{
$this->setterByData($data);
}
}
28 changes: 28 additions & 0 deletions src/Dto/ConsultationDto.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace EscolaLms\Consultations\Dto;

use EscolaLms\Consultations\Dto\Contracts\ModelDtoContract;
use EscolaLms\Consultations\Models\Consultation;

class ConsultationDto extends BaseDto implements ModelDtoContract
{
protected string $name;
protected string $status;
protected string $description;
protected ?string $startedAt;
protected ?string $finishedAt;
protected ?int $basePrice;
protected ?int $authorId;

public function model(): Consultation
{
return Consultation::newModelInstance();
}

public function toArray($filters = false): array
{
$result = $this->fillInArray($this->model()->getFillable());
return $filters ? array_filter($result) : $result;
}
}
11 changes: 11 additions & 0 deletions src/Dto/Contracts/ModelDtoContract.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace EscolaLms\Consultations\Dto\Contracts;

use Illuminate\Database\Eloquent\Model;

interface ModelDtoContract
{
public function model(): Model;
public function toArray($filters = false): array;
}
13 changes: 3 additions & 10 deletions src/Dto/FilterListDto.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,32 +7,25 @@
use EscolaLms\Core\Repositories\Criteria\Primitives\EqualCriterion;
use EscolaLms\Core\Repositories\Criteria\Primitives\InCriterion;

class FilterListDto
class FilterListDto extends BaseDto
{
use DtoHelper;

private string $name;
private int $basePrice;
private array $status;

private array $criteria = [];

public function __construct(array $data = [])
{
$this->setterByData($data);
}

public static function prepareFilters(array $search)
{
$dto = new self($search);
if ($dto->getName()) {
$dto->addToCriteria(new ConsultationSearch($dto->getName()));
}
if ($dto->getBasePrice()) {
$dto->addToCriteria(new EqualCriterion('base_price', $dto->getBasePrice()));
$dto->addToCriteria(new EqualCriterion('consultations.base_price', $dto->getBasePrice()));
}
if ($dto->getStatus()) {
$dto->addToCriteria(new InCriterion('status', $dto->getStatus()));
$dto->addToCriteria(new InCriterion('consultations.status', $dto->getStatus()));
}
return $dto->criteria;
}
Expand Down
23 changes: 23 additions & 0 deletions src/Dto/Traits/DtoHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace EscolaLms\Consultations\Dto\Traits;

use Illuminate\Support\Str;

trait DtoHelper
{
protected function setterByData(array $data): void
Expand All @@ -12,8 +14,29 @@ protected function setterByData(array $data): void
}, $k);
if (method_exists($this, 'set' . $key)) {
$this->{'set' . $key}($v);
} else {
$key = lcfirst($key);
$this->$key = $v;
}
}
}

protected function getterByAttribute(string $attribute)
{
$key = Str::studly($attribute);
if (method_exists($this, 'set' . $key)) {
return $this->{'get' . $key}();
}
return $this->{lcfirst($key)} ?? null;
}

protected function fillInArray(array $fillables): array
{
$result = [];
foreach ($fillables as $fill) {
$result[$fill] = $this->getterByAttribute($fill);
}
return $result;
}

}
5 changes: 0 additions & 5 deletions src/Enum/ConsultationStatusEnum.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,4 @@ class ConsultationStatusEnum extends BasicEnum
public const DRAFT = 'draft';
public const PUBLISHED = 'published';
public const ARCHIVED = 'archived';
public const STATUSES = [
self::DRAFT,
self::PUBLISHED,
self::ARCHIVED,
];
}
6 changes: 1 addition & 5 deletions src/Enum/ConsultationsPermissionsEnum.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,5 @@ class ConsultationsPermissionsEnum extends BasicEnum
const CONSULTATION_CREATE = 'consultation_create';
const CONSULTATION_UPDATE = 'consultation_update';
const CONSULTATION_DELETE = 'consultation_delete';
const CONSULTATION_ATTEND = 'consultation_read';

const CONSULTATION_UPDATE_OWNED = 'consultation_update_authored';
const CONSULTATION_DELETE_OWNED = 'consultation_delete_authored';
const CONSULTATION_ATTEND_OWNED = 'consultation_read_authored';
const CONSULTATION_READ = 'consultation_read';
}
Loading

0 comments on commit 27b5dfe

Please sign in to comment.