bootstrapi icon indicating copy to clipboard operation
bootstrapi copied to clipboard

Suggestions about use case

Open tutunci opened this issue 6 years ago • 5 comments

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:

  • is there a way to declare a field as mandatory or not based on the user role?
  • how can I automatically add in the Offer actionCreate the company_id based on the user logged?

Thanks, Maurizio

tutunci avatar May 08 '18 15:05 tutunci

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

kot13 avatar May 19 '18 14:05 kot13

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

tutunci avatar May 21 '18 07:05 tutunci

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": "[email protected]",
            "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

kot13 avatar May 21 '18 17:05 kot13

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

tutunci avatar May 22 '18 18:05 tutunci

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

kot13 avatar May 24 '18 20:05 kot13