Class: Api::V1::PhotosController

Inherits:
BaseController show all
Defined in:
app/controllers/api/v1/photos_controller.rb

Instance Attribute Summary

Attributes included from OpenidConnect::ProtectedResourceEndpoint

#current_token

Instance Method Summary collapse

Methods included from OpenidConnect::ProtectedResourceEndpoint

#access_token?, #require_access_token

Instance Method Details

#createObject


38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'app/controllers/api/v1/photos_controller.rb', line 38

def create
  image = params.require(:image)
  public_photo = params.has_key?(:aspect_ids)
  raise RuntimeError unless public_photo || private_modify?

  base_params = params.permit(:aspect_ids, :pending, :set_profile_photo)
  photo = photo_service.create_from_params_and_file(base_params, image)
  raise RuntimeError unless photo

  render json: photo_json(photo)
rescue CarrierWave::IntegrityError, ActionController::ParameterMissing, RuntimeError
  render_error 422, "Failed to create the photo"
end

#destroyObject

Raises:

  • (ActiveRecord::RecordNotFound)

52
53
54
55
56
57
58
59
60
61
62
63
# File 'app/controllers/api/v1/photos_controller.rb', line 52

def destroy
  photo = current_user.photos.where(guid: params[:id]).first
  raise ActiveRecord::RecordNotFound unless photo

  raise ActiveRecord::RecordNotFound unless photo.public? || private_modify?

  if current_user.retract(photo)
    head :no_content
  else
    render_error 422, "Not allowed to delete the photo"
  end
end

#indexObject


18
19
20
21
22
23
24
25
26
27
# File 'app/controllers/api/v1/photos_controller.rb', line 18

def index
  query = if private_read?
            current_user.photos
          else
            current_user.photos.where(public: true)
          end
  photos_page = time_pager(query).response
  photos_page[:data] = photos_page[:data].map {|photo| photo_json(photo) }
  render_paged_api_response photos_page
end

#showObject

Raises:

  • (ActiveRecord::RecordNotFound)

29
30
31
32
33
34
35
36
# File 'app/controllers/api/v1/photos_controller.rb', line 29

def show
  photo = photo_service.visible_photo(params.require(:id))
  raise ActiveRecord::RecordNotFound unless photo

  raise ActiveRecord::RecordNotFound unless photo.public? || private_read?

  render json: photo_json(photo)
end