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

: we should make this part of the model (JSON write only lazy getter) #304

Open
github-actions bot opened this issue Sep 26, 2022 · 0 comments
Open
Assignees
Labels

Comments

@github-actions
Copy link

in the mean time this will work and our docs still look okay

##########################################################################

# TODO:: we should make this part of the model (JSON write only lazy getter)

        })
      end

      paginate_results(elastic, query)
    end

    @[AC::Route::GET("/:id")]
    def show(
      @[AC::Param::Info(name: "instances", description: "return assets that are a subset of this asset?", example: "true")]
      include_instances : Bool = false
    ) : Model::Asset | Hash(String, Array(PlaceOS::Model::AssetInstance) | JSON::Any)
      if include_instances
        # TODO:: we should make this part of the model (JSON write only lazy getter)
        # in the mean time this will work and our docs still look okay
        render json: with_fields(current_asset, {
          "asset_instances" => current_asset.asset_instances.to_a,
        })
      end
      current_asset
    end

    @[AC::Route::PATCH("/:id", body: :asset)]
    @[AC::Route::PUT("/:id", body: :asset)]
    def update(asset : Model::Asset) : Model::Asset
      current = current_asset
      current.assign_attributes(asset)
      raise Error::ModelValidation.new(current.errors) unless current.save
      current
    end

    @[AC::Route::POST("/", body: :asset, status_code: HTTP::Status::CREATED)]
    def create(asset : Model::Asset) : Model::Asset
      raise Error::ModelValidation.new(asset.errors) unless asset.save
      asset
    end

    @[AC::Route::DELETE("/:id", status_code: HTTP::Status::ACCEPTED)]
    def destroy : Nil
      current_asset.destroy # expires the cache in after callback
      head :ok
    end

    @[AC::Route::GET("/:id/asset_instances")]
    def asset_instances : Array(Model::AssetInstance)
      instances = current_asset.asset_instances.to_a
      set_collection_headers(instances.size, Model::AssetInstance.table_name)

      instances
    end
  end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant