diff --git a/app/controllers/api/spaces_controller.rb b/app/controllers/api/spaces_controller.rb new file mode 100644 index 000000000..f3b63a19f --- /dev/null +++ b/app/controllers/api/spaces_controller.rb @@ -0,0 +1,48 @@ +class API::SpacesController < API::ApiController + before_action :authenticate_user!, except: [:index, :show] + respond_to :json + + def index + @spaces = Space.includes(:space_image) + end + + def show + @space = Space.includes(:space_files, :projects).friendly.find(params[:id]) + end + + def create + authorize Space + @space = Space.new(space_params) + if @space.save + render :show, status: :created, location: @space + else + render json: @space.errors, status: :unprocessable_entity + end + end + + def update + authorize Space + @space = get_space + if @space.update(space_params) + render :show, status: :ok, location: @space + else + render json: @space.errors, status: :unprocessable_entity + end + end + + def destroy + authorize @space + @space.destroy + head :no_content + end + + private + def get_space + Space.friendly.find(params[:id]) + end + + def space_params + params.require(:space).permit(:name, :description, :characteristics, space_image_attributes: [:attachment], + space_files_attributes: [:id, :attachment, :_destroy]) + end +end diff --git a/app/views/api/spaces/index.json.jbuilder b/app/views/api/spaces/index.json.jbuilder new file mode 100644 index 000000000..c26cdde86 --- /dev/null +++ b/app/views/api/spaces/index.json.jbuilder @@ -0,0 +1,4 @@ +json.array!(@spaces) do |space| + json.extract! space, :id, :name, :description, :slug + json.space_image space.space_image.attachment.medium.url if space.space_image +end diff --git a/app/views/api/spaces/show.json.jbuilder b/app/views/api/spaces/show.json.jbuilder new file mode 100644 index 000000000..2b2de32ec --- /dev/null +++ b/app/views/api/spaces/show.json.jbuilder @@ -0,0 +1,7 @@ +json.extract! @space, :id, :name, :description, :characteristics, :created_at, :updated_at +json.space_image @space.space_image.attachment.large.url if @space.space_image +json.space_files_attributes @space.space_files do |f| + json.id f.id + json.attachment f.attachment_identifier + json.attachment_url f.attachment_url +end \ No newline at end of file