Browse Source

Pass licenses in index and return more photo attributes

Douglas Thrift 6 years ago
parent
commit
1c43d0f4f1
2 changed files with 35 additions and 6 deletions
  1. 32 6
      app.rb
  2. 3 0
      views/index.erb

+ 32 - 6
app.rb

@@ -28,9 +28,9 @@ migration 'create users, licenses, and photos tables' do
 
   database.create_table :photos do
     column :id, Integer, primary_key: true
-    # foreign_key :user_id, :users
+    # foreign_key :user_id, :users, on_delete: :cascade, on_update: :restrict
     foreign_key :owner, :users
-    # foreign_key :license_id, :licenses
+    # foreign_key :license_id, :licenses, on_delete: :cascade, on_update: :restrict
     foreign_key :license, :licenses
     column :json, 'text'
     column :ignore, 'boolean'
@@ -48,6 +48,15 @@ migration 'rename photo owner and license columns to user_id and license_id' do
   end
 end
 
+migration 'add on delete and update constraints to photo user_id and license_id' do
+  database.alter_table :photos do
+    drop_foreign_key [:user_id]
+    add_foreign_key [:user_id], :users, on_delete: :cascade, on_update: :restrict
+    drop_foreign_key [:license_id]
+    add_foreign_key [:license_id], :licenses, on_delete: :cascade, on_update: :restrict
+  end
+end
+
 class User < Sequel::Model
   one_to_many :photo
   unrestrict_primary_key
@@ -56,6 +65,18 @@ end
 class License < Sequel::Model
   one_to_many :photo
   unrestrict_primary_key
+
+  def as_json(*)
+    {
+      id: id,
+      name: name,
+      url: url,
+    }
+  end
+
+  def to_json(*args)
+    as_json.to_json(*args)
+  end
 end
 
 class Photo < Sequel::Model
@@ -72,8 +93,12 @@ class Photo < Sequel::Model
       id: id,
       license: license_id,
       ignore: ignore,
-      image: FlickRaw.url_q(flickraw),
-      link: FlickRaw.url_photopage(flickraw),
+      img: FlickRaw.url_q(flickraw),
+      url: FlickRaw.url_photopage(flickraw),
+      title: flickraw.title,
+      public: flickraw.ispublic != 0,
+      friend: flickraw.isfriend != 0,
+      family: flickraw.isfamily != 0,
     }
   end
 
@@ -102,13 +127,14 @@ before do
 end
 
 get '/' do
-  flickr.photos.licenses.getInfo.each do |flickr_license|
+  @licenses = License.all
+  @licenses = flickr.photos.licenses.getInfo.map do |flickr_license|
     License.create do |license|
       license.id = flickr_license.id
       license.name = flickr_license.name
       license.url = flickr_license.url
     end
-  end if License.count == 0
+  end if @licenses.count == 0
   erb :index
 end
 

+ 3 - 0
views/index.erb

@@ -1,4 +1,7 @@
 <p>Hello <%= @user.fullname %>!</p>
 
+<script>
+  var licenses = <%= @licenses.to_json %>;
+</script>
 <script src="https://code.jquery.com/jquery-3.2.1.min.js" integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4=" crossorigin="anonymous"></script>
 <script src="/js/app.js"></script>