kot13 / bootstrapi

A better framework for building API with PHP. Built using Slim 3, Eloquent, Zend-ACL
88 stars 22 forks source link

Suggestions about use case #23

Open tutunci opened 6 years ago

tutunci commented 6 years ago

Hi, I'd like to have some suggestions about this use case: I've an entity Company, Users belongs to a Company with a relation 1-N I also have an entity Offers, users create offers and must be related to the company of the user creating it. At the same user getting offers must receive a list of offers belonging to the user's company. So ideally I'd like to have that users that have role "user" don't need to declare the company_id of belonging when creating/getting an offer, on the opposite if the user has a role "admin" the company_id must be passed in parameters.

So first questions are:

Thanks, Maurizio

kot13 commented 6 years ago

Hello! You have to write all this yourself.

A few ideas: 1) Override the method actionCreate in the controller - UserController 2) Override the method create in the model - \App\Model\User 3) Add an observer similarly by App\Observers\CreatedByAndUpdatedByObserver or App\Observers\LoggerObserver

You may also need documentation about the validator: https://laravel.com/docs/5.6/validation#rule-required-if

tutunci commented 6 years ago

Hi @kot13, I was able to implement following your suggestions, thanks!!!

I was looking at "?include=relationEntity" for a related entity but doing some tests and checking the code I was unable to find anything. Can you please drive me about this feature?

Thanks Maurizio

kot13 commented 6 years ago

If you execute the query: http://hostname.local/api/user/1?include=role you will get:

{
    "data": {
        "type": "user",
        "id": "1",
        "attributes": {
            "full_name": "Администратор",
            "email": "admin@example.com",
            "role_id": 1,
            "created_at": "2017-07-20T22:39:57+0000",
            "updated_at": "2017-07-20T22:39:57+0000",
            "created_by": null,
            "updated_by": null,
            "status": 1
        },
        "relationships": {
            "role": {
                "data": {
                    "type": "role",
                    "id": "1"
                }
            }
        },
        "links": {
            "self": "http://hostname.local/api/user/1"
        }
    },
    "included": [
        {
            "type": "role",
            "id": "1",
            "attributes": {
                "name": "admin",
                "description": "Администратор",
                "created_at": "2017-07-20T22:39:56+0000",
                "updated_at": "2017-07-20T22:39:56+0000",
                "created_by": null,
                "updated_by": null
            },
            "relationships": {
                "rights": {
                    "data": []
                }
            }
        }
    ]
}

All code in jsonapi library by neomerx and Eloquent ORM. To look it is in: \App\Schema\UserSchema::getRelationships and \App\Model\User::role

tutunci commented 6 years ago

Hi @kot13 , thanks for the feedback. I'm able to see the relationship as defined in the files but I didn't see the included as result in output. I'll dig about the reason.

Thanks Maurizio

kot13 commented 6 years ago

Hi!

I'll dig about the reason.

Look at this code: https://github.com/kot13/bootstrapi/blob/master/app/src/Common/JsonApiEncoder.php

37-38 lines