Skip to content

Commit

Permalink
Create pictures api
Browse files Browse the repository at this point in the history
  • Loading branch information
kayhide committed Jul 23, 2020
1 parent 8c04296 commit 734da90
Show file tree
Hide file tree
Showing 4 changed files with 132 additions and 4 deletions.
36 changes: 36 additions & 0 deletions rails/app/controllers/api/pictures_controller.rb
@@ -0,0 +1,36 @@
class Api::PicturesController < ApiController
before_action :set_picture_attachment, only: [:destroy]

def index
@pictures = current_user.pictures_blobs.order(id: :desc)
render json: @pictures.each_with_object(Picture).map(&:becomes).map(&method(:index_attributes))
end

def create
current_user.pictures.attach params.require(:file)
render :no_content, status: :created
end

def destroy
@picture_attachment.destroy
end

private

def set_picture_attachment
@picture_attachment = UserPicturesAttachment.find(params[:id])
end

def index_attributes picture
a = picture.user_attachment
a.attributes
.slice(*%w(id created_at))
.merge(picture.slice(*%w(filename byte_size)))
.merge(
user: a.user.attributes,
url: rails_blob_url(picture),
thumbnail_url: rails_representation_url(picture.variant(resize_to_fill: [300, 300]).processed)
)
end

end
12 changes: 8 additions & 4 deletions rails/app/models/picture.rb
@@ -1,7 +1,11 @@
class Picture < ActiveStorage::Blob
has_one :user_pictures_attachment, foreign_key: :blob_id
has_one :user, through: :user_pictures_attachment
has_many :puzzle_picture_attachments, foreign_key: :blob_id
has_many :puzzles, through: :puzzle_picture_attachments
has_one :user_attachment, foreign_key: :blob_id, class_name: :UserPicturesAttachment
has_one :user, through: :user_attachment

has_many :puzzle_attachments, foreign_key: :blob_id, class_name: :PuzzlePictureAttachment
has_many :puzzles, through: :puzzle_attachments

def blob
becomes(self.class.superclass)
end
end
1 change: 1 addition & 0 deletions rails/config/routes.rb
Expand Up @@ -22,6 +22,7 @@

namespace :api do
resource :auth, only: [:show, :create], controller: :auth
resources :pictures, only: [:index, :show, :create, :update, :destroy]
resources :games, only: [:index, :show, :create, :update, :destroy]
resources :puzzles, only: [:show]
end
Expand Down
87 changes: 87 additions & 0 deletions rails/spec/controllers/api/pictures_controller_spec.rb
@@ -0,0 +1,87 @@
require 'rails_helper'

RSpec.describe Api::PicturesController, type: :controller do
authenticate_user

let(:file) { 'pictures/mountain.jpg' }
let(:file_path) { fixture_path.join file }
let(:file_param) { fixture_file_upload file }
let(:valid_params) {
{
file: file_param
}
}

describe "GET #index" do
let!(:picture_attachments) {
2.times.map do
current_user.pictures.attach(io: File.open(file_path), filename: File.basename(file))
end
current_user.pictures.order(id: :asc).map { |a| a.becomes UserPicturesAttachment }
}

it "returns picture items" do
get :index
expect(response).to have_http_status(:ok)
body = JSON.parse response.body
expect(body.count).to eq 2
expect(body.map(&:keys)).to all match_array %w(id created_at filename byte_size user url thumbnail_url)
end
end

describe "POST #create" do
context "with valid params" do
let(:valid_params) {
{
file: fixture_file_upload(file)
}
}

it "returns created status" do
post :create, params: valid_params
expect(response).to have_http_status(:created)
expect(response.body).to be_blank
end

it "creates a Picture" do
expect {
post :create, params: valid_params
}.to change(Picture, :count).by(1)
end
end

context "with multiple files" do
let(:valid_params) {
{
file: [ fixture_file_upload(file), fixture_file_upload(file) ]
}
}

it "creates multiple Pictures" do
expect {
post :create, params: valid_params
}.to change(Picture, :count).by(2)
end
end
end

describe "DELETE #destroy" do
let!(:picture_attachment) {
current_user.pictures.attach(io: File.open(file_path), filename: File.basename(file))
current_user.pictures_attachments.last.becomes(UserPicturesAttachment)
}

it "destroys the picture association" do
expect {
delete :destroy, params: { id: picture_attachment.id }
}.to change { current_user.pictures.count }.by(-1)
end

it "enqueues PurgeJob" do
assert_enqueued_with job: ActiveStorage::PurgeJob do
delete :destroy, params: { id: picture_attachment.id }
end
end
end

end

0 comments on commit 734da90

Please sign in to comment.