Class: Api::V1::WebContentsController

Inherits:
JSONAPI::ResourceController
  • Object
show all
Defined in:
app/controllers/pwb/api/v1/web_contents_controller.rb

Instance Method Summary collapse

Instance Method Details

#create_content_with_photoObject

below used when uploading carousel images



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'app/controllers/pwb/api/v1/web_contents_controller.rb', line 38

def create_content_with_photo
  tag = params[:tag]
  photo = ContentPhoto.create

  key = tag.underscore.camelize + photo.id.to_s
  new_content = Content.create(tag: tag, key: key)

  # photo.subdomain = subdomain
  # photo.folder = current_tenant_model.whitelabel_country_code
  # photo.tenant_id = current_tenant_model.id
  if params[:file]
    photo.image = params[:file]
  end
  photo.save!
  new_content.content_photos.push photo

  # http://typeoneerror.com/labs/jsonapi-resources-ember-data/
  # resource for model
  resource = Api::V1::WebContentResource.new(new_content, nil)

  # serializer for resource
  serializer = JSONAPI::ResourceSerializer.new(Api::V1::WebContentResource)
  # jsonapi-compliant hash (ready to be send to render)

  photo.reload
  # above needed to ensure image_url is available
  # might need below if upload in prod is slow..

  # upload_confirmed = false
  # tries = 0
  # until upload_confirmed
  #   if photo.image_url.present?
  #     upload_confirmed = true
  #   else
  #     sleep 1
  #     photo.reload
  #     tries += 1
  #     if tries > 5
  #       upload_confirmed = true
  #     end
  #   end
  # end

  render json: serializer.serialize_to_hash(resource)

  # return render json: new_content.to_json
  # return render :json => { :error => "Sorry...", :status => "444", :data => "ssss" }, :status => 422
end

#update_photoObject

below is used by logo_photo and about_us_photo, where only one photo is allowed



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'app/controllers/pwb/api/v1/web_contents_controller.rb', line 9

def update_photo
   = params[:content_tag]
  # photo = ContentPhoto.find(params[:id])
  # find would throw error if not found
  photo = ContentPhoto.find_by_id(params[:id])
  unless photo
    if 
      # where photo has never been set before, associated Content will not exist
      content = Content.find_by_key() || Content.create({ key: , tag: 'appearance' })
      photo = ContentPhoto.create
      if  == "logo"
        # TODO: This is a workaround
        # need to have a way of determining content that should only have
        # one photo and enforcing that
        content.content_photos.destroy_all
      end
      content.content_photos.push photo
    end
    # TODO: - handle where no photo or content_tag..
  end
  if params[:file]
    photo.image = params[:file]
  end
  photo.save!
  photo.reload
  render json: photo.to_json
end