Skip to content

Commit

Permalink
Prend en charge les catégories pour les caractéristiques spéciales (API)
Browse files Browse the repository at this point in the history
  • Loading branch information
polosson committed Jan 27, 2021
1 parent ba9ea34 commit ae29df6
Show file tree
Hide file tree
Showing 11 changed files with 397 additions and 106 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ Ce projet adhère au principe du [Semantic Versioning](https://semver.org/spec/v
## 0.12.0 (unreleased)

- Améliore le calcul du matériel restant dans les événements.
- Ajoute la possibilité de limiter les caractéristiques spéciales du matériel par catégorie (#91).

## 0.11.0 (2021-01-14)

Expand Down
2 changes: 1 addition & 1 deletion server/src/App/ApiRouter.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ class ApiRouter
{
private $_routes = [
'get' => [
'/attributes[/]' => 'AttributeController:getAll',
'/users[/]' => 'UserController:getAll',
'/users/{id:[0-9]+}[/]' => 'UserController:getOne',
'/users/{id:[0-9]+}/settings[/]' => 'UserController:getSettings',
Expand All @@ -28,7 +29,6 @@ class ApiRouter
'/materials[/]' => 'MaterialController:getAll',
'/materials/{id:[0-9]+}[/]' => 'MaterialController:getOne',
'/materials/{id:[0-9]+}/tags[/]' => 'MaterialController:getTags',
'/materials/attributes[/]' => 'MaterialController:getAttributes',
'/events[/]' => 'EventController:getAll',
'/events/{id:[0-9]+}[/]' => 'EventController:getOne',
'/events/{id:[0-9]+}/missing-materials[/]' => 'EventController:getMissingMaterials',
Expand Down
42 changes: 42 additions & 0 deletions server/src/App/Controllers/AttributeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,48 @@

namespace Robert2\API\Controllers;

use Slim\Http\Request;
use Slim\Http\Response;

class AttributeController extends BaseController
{
public function getAll(Request $request, Response $response): Response
{
$categoryId = $request->getQueryParam('category', null);

$attributes = $this->model->orderBy('name', 'asc');
if (!empty($categoryId)) {
$attributes
->whereDoesntHave('categories')
->orWhereHas('categories', function ($query) use ($categoryId) {
$query->where('categories.id', $categoryId);
});
}

$results = $attributes->with('categories')->get()->toArray();

return $response->withJson($results);
}

public function create(Request $request, Response $response): Response
{
$postData = $request->getParsedBody();
if (empty($postData)) {
throw new \InvalidArgumentException(
"Missing request data to process validation",
ERROR_VALIDATION
);
}

$attribute = $this->model->edit(null, $postData);

if (isset($postData['categories'])) {
$attribute->Categories()->sync($postData['categories']);
}

$categories = $attribute->Categories()->get()->toArray();
$result = $attribute->toArray() + compact('categories');

return $response->withJson($result, SUCCESS_CREATED);
}
}
7 changes: 0 additions & 7 deletions server/src/App/Controllers/MaterialController.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,13 +87,6 @@ public function getAll(Request $request, Response $response): Response
return $response->withJson($results);
}

public function getAttributes(Request $request, Response $response): Response
{
$attributes = new Attribute();
$result = $attributes->getAll()->get();
return $response->withJson($result->toArray());
}

// ------------------------------------------------------
// -
// - Setters
Expand Down
33 changes: 29 additions & 4 deletions server/src/App/Models/Attribute.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

namespace Robert2\API\Models;

use Illuminate\Database\Eloquent\Builder;
use Robert2\API\Validation\Validator as V;

class Attribute extends BaseModel
Expand All @@ -21,7 +22,7 @@ public function __construct(array $attributes = [])
v::equals('float'),
v::equals('boolean')
),
'unit' => V::optional(V::length(1, 8)),
'unit' => V::optional(V::length(1, 8)),
'max_length' => V::optional(V::numeric()),
];
}
Expand All @@ -40,16 +41,23 @@ public function Materials()
->select(['materials.id', 'name']);
}

public function Categories()
{
return $this->belongsToMany('Robert2\API\Models\Category', 'attribute_categories')
->orderBy('name')
->select(['categories.id', 'name']);
}

// ——————————————————————————————————————————————————————
// —
// — Mutators
// —
// ——————————————————————————————————————————————————————

protected $casts = [
'name' => 'string',
'type' => 'string',
'unit' => 'string',
'name' => 'string',
'type' => 'string',
'unit' => 'string',
'max_length' => 'integer',
];

Expand All @@ -59,6 +67,23 @@ public function getMaterialsAttribute()
return $materials ? $materials->toArray() : null;
}

public function getCategoriesAttribute()
{
return $this->Categories()->get()->toArray();
}

// ——————————————————————————————————————————————————————
// —
// — Getters
// —
// ——————————————————————————————————————————————————————

public function getAll(bool $withDeleted = false): Builder
{
$builder = parent::getAll($withDeleted);
return $builder->with('categories');
}

// ——————————————————————————————————————————————————————
// —
// — Setters
Expand Down
11 changes: 8 additions & 3 deletions server/src/App/Models/Category.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public function SubCategories()

public function Materials()
{
$fields = [
return $this->hasMany('Robert2\API\Models\Material')->select([
'id',
'name',
'description',
Expand All @@ -52,9 +52,14 @@ public function Materials()
'stock_quantity',
'out_of_order_quantity',
'replacement_price',
];
]);
}

return $this->hasMany('Robert2\API\Models\Material')->select($fields);
public function Attributes()
{
return $this->belongsToMany('Robert2\API\Models\Attribute', 'attribute_categories')
->using('Robert2\API\Models\AttributeCategoriesPivot')
->select(['attributes.id', 'attributes.name', 'attributes.type', 'attributes.unit']);
}

// ——————————————————————————————————————————————————————
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php
use Phinx\Migration\AbstractMigration;

class AddAttributesCategoriesRelation extends AbstractMigration
{
public function up()
{
$table = $this->table('attribute_categories');
$table
->addColumn('attribute_id', 'integer')
->addColumn('category_id', 'integer')
->addIndex(['attribute_id'])
->addForeignKey('attribute_id', 'attributes', 'id', [
'delete' => 'CASCADE',
'update' => 'NO_ACTION',
'constraint' => 'fk_attribute_categories_attribute'
])
->addIndex(['category_id'])
->addForeignKey('category_id', 'categories', 'id', [
'delete' => 'CASCADE',
'update' => 'NO_ACTION',
'constraint' => 'fk_attribute_categories_category'
])
->create();
}

public function down()
{
$this->table('attribute_categories')->drop()->save();
}
}
14 changes: 14 additions & 0 deletions server/tests/Fixtures/seed/attribute_categories.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[
{
"attribute_id": 1,
"category_id": 1
},
{
"attribute_id": 1,
"category_id": 2
},
{
"attribute_id": 3,
"category_id": 1
}
]
Loading

0 comments on commit ae29df6

Please sign in to comment.