Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create or update model from request #309

Closed
girardinsamuel opened this issue Jan 14, 2021 · 5 comments
Closed

Create or update model from request #309

girardinsamuel opened this issue Jan 14, 2021 · 5 comments
Labels
feature request A feature that does not yet exist but will be a good addition to the library medium These issues are geared for people who have contributed to the project before

Comments

@girardinsamuel
Copy link
Contributor

girardinsamuel commented Jan 14, 2021

I was wondering if there was a quicker way to create/update a model from request data

I have an an example here
https://github.com/girardinsamuel/pingcrm-masonite/blob/fix/3.0/app/http/controllers/UsersController.py#L86

where I want to create a user with data contained in request (which should be validated first). I would like to be able to write something like :

User.create(
	request,
	photo_path=photo_path,
    account_id=self.request.user().account.id,
)

or

User.create(
	request.only("first_name", "last_name", "email"),
	photo_path=photo_path,
    account_id=self.request.user().account.id,
)

or

User.create(
	request.validate(
      validate.required(["email", "first_name", "last_name"]),
      validate.length(["first_name", "last_name", "email"], max=50),
      validate.email("email")
	),
	photo_path=photo_path,
    account_id=self.request.user().account.id,
)

and right now I am a doing this

self.request.validate(
    validate.required(["email", "first_name", "last_name"]),
    validate.length(["first_name", "last_name", "email"], max=50),
    validate.email("email"),
)

User.create(
    first_name=self.request.input("first_name"),
    last_name=self.request.input("last_name"),
    email=self.request.input("email"),
    photo_path=photo_path,
    account_id=self.request.user().account.id,
)
@josephmancuso josephmancuso added medium These issues are geared for people who have contributed to the project before feature request A feature that does not yet exist but will be a good addition to the library labels Jan 18, 2021
@josephmancuso
Copy link
Member

Not entirely sure how this would work. This would need to be generic. I think the solution here is to just pass the dictionary from whatever object here like in your example:

User.create(
	request.only("first_name", "last_name", "email")
)

The other option would be to set a generic method on the object like all() or something which would be more trouble than its worth.

Best approach here is to just make a method on the model that converts the model to a dictionary.

Closing but still open for ideas

@girardinsamuel
Copy link
Contributor Author

I am totally okay with this approach:

User.create(
	request.only("first_name", "last_name", "email")
)
  • request.only is already available
  • I guess we just have to handle arguments of create() method to accept a dict ? (i did not check how it is done now)

@Marlysson
Copy link
Contributor

Marlysson commented Jan 21, 2021

@girardinsamuel This feature would be similar with RequestForm from Laravel?

@girardinsamuel
Copy link
Contributor Author

@Marlysson not sure what it is exactly, but on a Laravel code I have seen this:
https://github.com/inertiajs/pingcrm/blob/master/app/Http/Controllers/UsersController.php#L94
it's short so I like it and I guess for a basic usage it should not be hard to do it with masonite

@girardinsamuel
Copy link
Contributor Author

I confirm it's working with existing masonite orm features
Here is a real life example
https://github.com/girardinsamuel/pingcrm-masonite/blob/fix/3.0/app/http/controllers/UsersController.py#L90

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature request A feature that does not yet exist but will be a good addition to the library medium These issues are geared for people who have contributed to the project before
Projects
None yet
Development

No branches or pull requests

3 participants