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

feat: add upsert method and enable it for environments #2746

Merged
merged 2 commits into from Jun 16, 2023

Conversation

mathnogueira
Copy link
Member

This PR adds a new method for upsert the resource into the database. And enables it for transactions

Checklist

  • tested locally
  • added new dependencies
  • updated the docs
  • added a test

Comment on lines 269 to 280
if err != nil {
if err == sql.ErrNoRows {
created, err := m.rh.Create(r.Context(), targetResource.Spec)
if err != nil {
writeError(w, encoder, http.StatusInternalServerError, fmt.Errorf("cannot create entity: %w", err))
return
}

writeResponse(http.StatusCreated, created)
return
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion: we can join the two to avoid nesting:

Suggested change
if err != nil {
if err == sql.ErrNoRows {
created, err := m.rh.Create(r.Context(), targetResource.Spec)
if err != nil {
writeError(w, encoder, http.StatusInternalServerError, fmt.Errorf("cannot create entity: %w", err))
return
}
writeResponse(http.StatusCreated, created)
return
}
}
if err != nil && err == sql.ErrNoRows {
created, err := m.rh.Create(r.Context(), targetResource.Spec)
if err != nil {
writeError(w, encoder, http.StatusInternalServerError, fmt.Errorf("cannot create entity: %w", err))
return
}
writeResponse(http.StatusCreated, created)
return
}

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Your suggestion made me realize I was ignoring the err when it was not a sql.ErrNoRows

@@ -295,3 +295,33 @@ func (resourceClient ResourceClient) Create(ctx context.Context, file file.File)
file = file.SaveChanges(IOReadCloserToString(resp.Body))
return &file, nil
}

func (resourceClient ResourceClient) Upsert(ctx context.Context, file file.File) (*file.File, error) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like that we have this out of the box now

@mathnogueira mathnogueira merged commit c519fff into main Jun 16, 2023
24 checks passed
@mathnogueira mathnogueira deleted the feat/upsert-endpoint branch June 16, 2023 19:51
danielbdias pushed a commit that referenced this pull request Jun 16, 2023
* add upsert method and enable it for environments

* handle error when getting entity
danielbdias added a commit that referenced this pull request Jun 16, 2023
* Adding initial APIs for the example

* Adding code for payment-executor-api

* Adding full example

* Adding trace-based tests

* Updating README

* Apply suggestions from code review

Co-authored-by: Adnan Rahić <adnan@kubeshop.io>

* Adding documentation to the example

* fix(backend): matching trigger span duration and trigger time durations (#2740)

* fix(frontend): adding default value for data store connection types (#2742)

* fix(backend): fixing response status code for grpc request (#2741)

* feat: add upsert method and enable it for environments (#2746)

* add upsert method and enable it for environments

* handle error when getting entity

* PR suggestions

---------

Co-authored-by: Adnan Rahić <adnan@kubeshop.io>
Co-authored-by: Oscar Reyes <oscar-rreyes1@hotmail.com>
Co-authored-by: Matheus Nogueira <matheus.nogueira2008@gmail.com>
mathnogueira added a commit that referenced this pull request Jun 19, 2023
* add upsert method and enable it for environments

* handle error when getting entity
mathnogueira added a commit that referenced this pull request Jun 19, 2023
* Adding initial APIs for the example

* Adding code for payment-executor-api

* Adding full example

* Adding trace-based tests

* Updating README

* Apply suggestions from code review

Co-authored-by: Adnan Rahić <adnan@kubeshop.io>

* Adding documentation to the example

* fix(backend): matching trigger span duration and trigger time durations (#2740)

* fix(frontend): adding default value for data store connection types (#2742)

* fix(backend): fixing response status code for grpc request (#2741)

* feat: add upsert method and enable it for environments (#2746)

* add upsert method and enable it for environments

* handle error when getting entity

* PR suggestions

---------

Co-authored-by: Adnan Rahić <adnan@kubeshop.io>
Co-authored-by: Oscar Reyes <oscar-rreyes1@hotmail.com>
Co-authored-by: Matheus Nogueira <matheus.nogueira2008@gmail.com>
mathnogueira added a commit that referenced this pull request Jun 19, 2023
* add upsert method and enable it for environments

* handle error when getting entity
mathnogueira added a commit that referenced this pull request Jun 19, 2023
* Adding initial APIs for the example

* Adding code for payment-executor-api

* Adding full example

* Adding trace-based tests

* Updating README

* Apply suggestions from code review

Co-authored-by: Adnan Rahić <adnan@kubeshop.io>

* Adding documentation to the example

* fix(backend): matching trigger span duration and trigger time durations (#2740)

* fix(frontend): adding default value for data store connection types (#2742)

* fix(backend): fixing response status code for grpc request (#2741)

* feat: add upsert method and enable it for environments (#2746)

* add upsert method and enable it for environments

* handle error when getting entity

* PR suggestions

---------

Co-authored-by: Adnan Rahić <adnan@kubeshop.io>
Co-authored-by: Oscar Reyes <oscar-rreyes1@hotmail.com>
Co-authored-by: Matheus Nogueira <matheus.nogueira2008@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants