Skip to content

Data composition/decoration for laravel-crud-wizard library including url query language

Notifications You must be signed in to change notification settings

macropay-solutions/laravel-lumen-crud-wizard-decorator

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

28 Commits
 
 

Repository files navigation

laravel-lumen-crud-wizard-decorator

This is a decorator and composition lib that can be used with Laravel crud wizard.

Features:

  • download as csv via stream without saving the file on server,
  • renames/maps the column names for the resource and its relations,
  • can also compose additional columns from the resource columns and from its relations' columns,
  • can restrict the number of columns returned to the requested ones, including in the csv download as stream,
  • flattens the resource with its relations into a single table,
  • eases the filtering on the relations' columns by transforming them into filters by the resource's column in regards to the url query string,
  • offers resource AND relations' column aggregations
  • can update the one to one relations on resource update
  • all json string values are parsed by htmlspecialchars function on decoration

It is recommended for projects that expose data via API to front end (JS).

The lib is proprietary and can be used only after an agreement.

If interested, please contact us.

Demo page.

Demo project.

image

Code example:

    namespace MacropaySolutions\LaravelCrudWizardDecorator\Decorators;
    
    class ExampleDecorator extends AbstractResourceDecorator
    {
        public array $withoutRelations = [];

        public function getResourceMappings(): array
        {
            return [
                'id' => 'ID',
                'updated_at' => 'updatedAt',
                'created_at' => 'createdAt',
            ];
        }
    
        /**
         * @inheritDoc
         */
        public function getRelationMappings(): array
        {
            return [
                'roleRelation' => [
                    'name' => 'roleRelationName',
                    'color' => 'roleRelationColor',
                ],
            ];
        }
    
        /**
         * @inheritDoc
         */
        public function getComposedColumns(): array
        {
            return [
                'hasRelations' => fn(array $row): bool => $this->getHasRoleRelations($row),
                'roleNameColor' => fn(array $row): ?string => $this->getNameColorRoleRelation($row),
            ];
        }
    
        private function getHasRoleRelations(array $row): bool
        {
            return isset($row['role_relation']);
        }
    
        private function getNameColorRoleRelation(array $row): ?string
        {
            return $this->getHasRoleRelations($row) ?
                ($row['role_relation']['name'] ?? '') . ' ' . ($row['role_relation']['name'] ?? '') :
                null;
        }
    }

Crud routes

  1. Create resource

  2. Get resource

  3. List filtered resource

  4. Update resource (or create)

  5. Delete resource

Crud routes

1 Create resource

POST /{resource}

headers:

  Accept: application/json
  
  ContentType: application/json

body:

  {
     "roleID": "1",
  }

Json Response:

200:

{
    "success": true,
    "code": 201,
    "locale": "en",
    "message": "success",
    "data": {
        "ID": 3,
        "roleID": "1",
        "updatedAt": "2022-10-27 09:05:49",
        "createdAt": "2022-10-27 09:04:46",
        "roleRelationName": "name",
        "roleRelationColor": "blue",
        "hasRelations": true,
        "roleNameColor": "name blue",
        "pki": "3"
    }
}

{
    "success": false,
    "code": 400,
    "locale": "en",
    "message": "The given data was invalid: The role id field is required.",
    "data": {
        "roleID": [
            "The role id field is required."
        ]
    }
}

2 Get resource

GET /info/{resource}/{identifier}

headers:

  Accept: application/json

Json Response:

200:

{
    "success": true,
    "code": 200,
    "locale": "en",
    "message": "success",
    "data": {
        "ID": 3,
        "roleID": "1",
        "updatedAt": "2022-10-27 09:05:49",
        "createdAt": "2022-10-27 09:04:46",
        "roleRelationName": "name",
        "roleRelationColor": "blue",
        "hasRelations": true,
        "roleNameColor": "name blue",
        "pki": "3"
    }
}

{
    "success": false,
    "code": 400,
    "locale": "en",
    "message": "Not found",
    "data": null
}

3 List filtered resource

GET /{resource}?perPage=10&page=2

GET /{resource}/{identifier}/{relation}?...

headers:

  Accept: application/json or text/csv or application/xls

Json Response:

200:

{
    "success": true,
    "code": 200,
    "locale": "en",
    "message": "success",
    "data": {
        "sums": null,
        "avgs": null,
        "mins": null,
        "maxs": null,
        "current_page": 1,
        "data": [
            {
                "ID": 3,
                "roleID": "1",
                "updatedAt": "2022-10-27 09:05:49",
                "createdAt": "2022-10-27 09:04:46",
                "roleRelationName": "name",
                "roleRelationColor": "blue",
                "hasRelations": true,
                "roleNameColor": "name blue",
                "pki": "3"
            }
        ],
        "from": 1,
        "last_page": 1,
        "per_page": 10,
        "to": 1,
        "total": 1,
        "filterable": [
            "ID",
            "roleID",
            "updatedAt",
            "createdAt",
            "roleRelationName",
            "roleRelationColor"
        ],
        "sortable": [
            "ID",
            "roleID"
        ],
        "relations": [
            "roleRelation"
        ]
    }
}

Binary response for application/xls

Binary response as stream for text/csv

4 Update resource (or create)

PUT /{resource}/{identifier}

headers:

  Accept: application/json
  
  ContentType: application/json

body:

  {
    "roleID": "2"
  }

Json Response:

200:

{
    "success": true,
    "code": 200, // or 201 for upsert
    "locale": "en",
    "message": "success",
    "data": {
        "ID": 3,
        "roleID": "2",
        "updatedAt": "2022-10-27 09:05:49",
        "createdAt": "2022-10-27 09:04:46",
        "roleRelationName": "name2",
        "roleRelationColor": "green",
        "hasRelations": true,
        "roleNameColor": "name2 green",
        "pki": "3"
    }
}

{
    "success": false,
    "code": 400,
    "locale": "en",
    "message": "The given data was invalid: The role id field is required.",
    "data": {
        "roleID": [
            "The role id field is required."
        ]
    }
}

Note: The decorator undecorates the POST, PUT and PATCH requests into resource key value pairs and among them for update (PUT) only, each of the keys that belong to a relation is undecorated under the relation name key, resulting the same structure as when retrieving the resource with that relation included when the relation is one to one using laravel-crud-wizard. This facilitates the update on the one to one relation also, if declared in the decorator property:

protected array $upsertOneToOneRelationsDbCrudMap = [RelatedModel::class => RelatedModelController:class];

Bear in mind that this undecoration is always considering the relations as one to one. In case of validation errors, only the keys from data object will be decorated!

5 Delete resource

DELETE /{resource}/{identifier}

headers:

  Accept: application/json

Json Response:

200:

{
    "success": true,
    "code": 204,
    "locale": "en",
    "message": "success",
    "data": null
}

{
    "success": false,
    "code": 400,
    "locale": "en",
    "message": "Not found",
    "data": null
}

About

Data composition/decoration for laravel-crud-wizard library including url query language

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published