Giter VIP home page Giter VIP logo

laravel-lumen-crud-wizard-decorator's Introduction

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,
  • flatens 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,

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."
        ]
    }
}

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
}

laravel-lumen-crud-wizard-decorator's People

Contributors

macropay-solutions avatar

Stargazers

 avatar Dmitry Churkin avatar marius-mcp avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.