Alpha Chen 1 year ago
parent 94dc7b5796
commit 50dd7f1533
Signed by: alpha
SSH Key Fingerprint: SHA256:3fOT8fiYQG/aK9ntivV3Bqtg8AYQ7q4nV6ZgihOA20g

@ -6,6 +6,10 @@ gem "sequel"
gem "sqlite3" gem "sqlite3"
group :dev do group :dev do
# Manually adding IRB as a dependency so I can adjust colors until
# https://github.com/ruby/reline/pull/552 is in Ruby itself
gem "irb"
gem "minitest" gem "minitest"
gem "rake" gem "rake"
end end

@ -2,16 +2,28 @@ GEM
remote: https://rubygems.org/ remote: https://rubygems.org/
specs: specs:
bigdecimal (3.1.4) bigdecimal (3.1.4)
io-console (0.6.0)
irb (1.9.0)
rdoc
reline (>= 0.3.8)
minitest (5.20.0) minitest (5.20.0)
psych (5.1.1.1)
stringio
rake (13.0.6) rake (13.0.6)
rdoc (6.6.0)
psych (>= 4.0.0)
reline (0.4.0)
io-console (~> 0.5)
sequel (5.73.0) sequel (5.73.0)
bigdecimal bigdecimal
sqlite3 (1.6.8-arm64-darwin) sqlite3 (1.6.8-arm64-darwin)
stringio (3.0.9)
PLATFORMS PLATFORMS
arm64-darwin-22 arm64-darwin-22
DEPENDENCIES DEPENDENCIES
irb
minitest minitest
rake rake
sequel sequel

@ -0,0 +1,15 @@
Sequel.migration do
change do
alter_table(:notes) do
add_primary_key :id
end
alter_table(:sources) do
add_primary_key :id
end
alter_table(:titles) do
add_primary_key :id
end
end
end

@ -1,13 +1,13 @@
require_relative "../models" require_relative "../models"
module Alphadex module Alphadex
def self.add_bookmark(url, title, note, tags) def self.add_bookmark(url:, title:, note:, tags:)
fail "url already exists" unless Url.where(url:).empty? fail "url already exists" unless Url.where(url:).empty?
DB.transaction do DB.transaction do
entity = Entity.create entity = Entity.create
entity.url = Url.new(url:) entity.url = Url.new(url:)
entity.title = Title.new(title:) entity.title = Title.new(title:) if title
entity.add_note(text: note) entity.add_note(text: note)
tags.map { Tag.find_or_create(name: _1) }.each do |tag| tags.map { Tag.find_or_create(name: _1) }.each do |tag|
entity.add_tag(tag) entity.add_tag(tag)

@ -4,35 +4,38 @@ require_relative "db"
Sequel::Model.plugin :timestamps Sequel::Model.plugin :timestamps
class Entity < Sequel::Model module Alphadex
class Entity < Sequel::Model
one_to_many :notes one_to_many :notes
one_to_many :sources one_to_many :sources
one_to_one :title one_to_one :title
many_to_many :tags many_to_many :tags
one_to_one :url one_to_one :url
# override this since I have `created_at` on this join table for some reason
def add_tag(tag) def add_tag(tag)
DB[:entities_tags].insert(entity_id: self.id, tag_id: tag.id, created_at: DateTime.now) DB[:entities_tags].insert(entity_id: self.id, tag_id: tag.id, created_at: DateTime.now)
end end
end end
class Note < Sequel::Model class Note < Sequel::Model
many_to_one :entity many_to_one :entity
end end
class Source < Sequel::Model class Source < Sequel::Model
many_to_one :entity many_to_one :entity
many_to_one :source, class: :Entity many_to_one :source, class: :Entity
end end
class Title < Sequel::Model class Title < Sequel::Model
many_to_one :entity many_to_one :entity
end end
class Tag < Sequel::Model class Tag < Sequel::Model
many_to_many :entities many_to_many :entities
end end
class Url < Sequel::Model class Url < Sequel::Model
one_to_one :entity many_to_one :entity
end
end end

Loading…
Cancel
Save