Skip to content

Commit

Permalink
Infrastructure holds database and github
Browse files Browse the repository at this point in the history
  • Loading branch information
soumyaray committed Oct 31, 2017
1 parent 2155a9f commit c78fa59
Show file tree
Hide file tree
Showing 30 changed files with 225 additions and 153 deletions.
2 changes: 2 additions & 0 deletions .pryrc
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require 'hirb'

Hirb.enable
Expand Down
2 changes: 1 addition & 1 deletion Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ namespace :db do
desc 'Run migrations'
task :migrate do
puts "Migrating #{app.environment} database to latest"
Sequel::Migrator.run(app.DB, 'infrastructure/db/migrations')
Sequel::Migrator.run(app.DB, 'infrastructure/database/migrations')
end

desc 'Drop all tables'
Expand Down
10 changes: 6 additions & 4 deletions app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class Api < Roda
routing.on 'repo', String, String do |ownername, reponame|
# GET /api/v0.1/repo/:ownername/:reponame request
routing.get do
repo = Database::ORM[Entity::Repo]
repo = Repository::For[Entity::Repo]
.find_full_name(ownername, reponame)

routing.halt(404, error: 'Repository not found') unless repo
Expand All @@ -33,13 +33,15 @@ class Api < Roda
# POST '/api/v0.1/repo/:ownername/:reponame
routing.post do
begin
github_repo = Github::RepoMapper.new(app.config)
repo = github_repo.load(ownername, reponame)
repo = Github::RepoMapper.new(app.config)
.load(ownername, reponame)
rescue StandardError
routing.halt(404, error: "Repo not found")
end

stored_repo = Database::ORM[Entity::Repo].find_or_create(repo)
stored_repo = Repository::For[repo.class].find_or_create(repo)
response.status = 201
response['Location'] = "/api/v0.1/repo/#{ownername}/#{reponame}"
stored_repo.to_h
end
end
Expand Down
4 changes: 2 additions & 2 deletions config/app.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
development:
db_filename: infrastructure/db/dev.db
db_filename: infrastructure/database/dev.db

test:
db_filename: infrastructure/db/test.db
db_filename: infrastructure/database/test.db
43 changes: 43 additions & 0 deletions domain/database_repositories/collaborators.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# frozen_string_literal: true

module CodePraise
module Repository
# Repository for Collaborators
class Collaborators
def self.find_id(id)
db_record = Database::CollaboratorOrm.first(id: id)
rebuild_entity(db_record)
end

def self.find_username(username)
db_record = Database::CollaboratorOrm.first(username: username)
rebuild_entity(db_record)
end

def self.find_or_create(entity)
find_username(entity.username) || create_from(entity)
end

def self.create_from(entity)
db_collaborator = Database::CollaboratorOrm.create(
origin_id: entity.origin_id,
username: entity.username,
email: entity.email
)

self.rebuild_entity(db_collaborator)
end

def self.rebuild_entity(db_record)
return nil unless db_record

Entity::Collaborator.new(
id: db_record.id,
origin_id: db_record.origin_id,
username: db_record.username,
email: db_record.email
)
end
end
end
end
10 changes: 10 additions & 0 deletions domain/database_repositories/for.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# frozen_string_literal: true

module CodePraise
module Repository
For = {
Entity::Repo => Repos,
Entity::Collaborator => Collaborators
}.freeze
end
end
5 changes: 5 additions & 0 deletions domain/database_repositories/init.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# frozen_string_literal: true

require_relative 'repos.rb'
require_relative 'collaborators.rb'
require_relative 'for.rb'
70 changes: 70 additions & 0 deletions domain/database_repositories/repos.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
# frozen_string_literal: true

module CodePraise
module Repository
# Repository for Repo Entities
class Repos
def self.find_full_name(ownername, reponame)
# SELECT * FROM `repos` LEFT JOIN `collaborators`
# ON (`collaborators`.`id` = `repos`.`owner_id`)
# WHERE ((`username` = 'owername') AND (`name` = 'reponame'))
db_repo = Database::RepoOrm.left_join(:collaborators, id: :owner_id)
.where(username: ownername, name: reponame)
.first
rebuild_entity(db_repo)
end

def self.find_id(id)
Database::RepoOrm.first(id: id)&.rebuild_entity
end

def self.find_origin_id(origin_id)
db_record = Database::RepoOrm.first(origin_id: origin_id)
rebuild_entity(db_record)
end

def self.find_or_create(entity)
find_origin_id(entity.origin_id) || create_from(entity)
end

def self.create_from(entity)
new_owner = Collaborators.find_or_create(entity.owner)
db_owner = Database::CollaboratorOrm.first(id: new_owner.id)

db_repo = Database::RepoOrm.create(
origin_id: entity.origin_id,
name: entity.name,
size: entity.size,
git_url: entity.git_url,
owner: db_owner
)

entity.contributors.each do |contrib|
stored_contrib = Collaborators.find_or_create(contrib)
contrib = Database::CollaboratorOrm.first(id: stored_contrib.id)
db_repo.add_contributor(contrib)
end

rebuild_entity(db_repo)
end

def self.rebuild_entity(db_record)
return nil unless db_record

contribs = db_record.contributors.map do |db_contrib|
Collaborators.rebuild_entity(db_contrib)
end

Entity::Repo.new(
id: db_record.id,
origin_id: db_record.origin_id,
name: db_record.name,
size: db_record.size,
git_url: db_record.git_url,
owner: Collaborators.rebuild_entity(db_record.owner),
contributors: contribs
)
end
end
end
end
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
6 changes: 6 additions & 0 deletions domain/init.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# frozen_string_literal: true

folders = %w[entities database_repositories github_mappers]
folders.each do |folder|
require_relative "#{folder}/init.rb"
end
18 changes: 18 additions & 0 deletions infrastructure/database/orm/collaborator_orm.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# frozen_string_literal: true

module CodePraise
module Database
# Object-Relational Mapper for Collaborators
class CollaboratorOrm < Sequel::Model(:collaborators)
one_to_many :owned_repos,
class: :'CodePraise::Database::RepoOrm',
key: :owner_id

many_to_many :contributed_repos,
join_table: :repos_contributors,
left_key: :contributor_id, right_key: :repo_id

plugin :timestamps, update_on_create: true
end
end
end
File renamed without changes.
10 changes: 10 additions & 0 deletions infrastructure/database/orm/orm.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# # frozen_string_literal: true

# module CodePraise
# module Database
# ORM = {
# CodePraise::Entity::Repo => RepoOrm,
# CodePraise::Entity::Collaborator => CollaboratorOrm
# }.freeze
# end
# end
18 changes: 18 additions & 0 deletions infrastructure/database/orm/repo_orm.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# frozen_string_literal: true

module CodePraise
module Database
# Object Relational Mapper for Repo Entities
class RepoOrm < Sequel::Model(:repos)
many_to_one :owner,
class: :'CodePraise::Database::CollaboratorOrm'

many_to_many :contributors,
class: :'CodePraise::Database::CollaboratorOrm',
join_table: :repos_contributors,
left_key: :repo_id, right_key: :collaborator_id

plugin :timestamps, update_on_create: true
end
end
end
49 changes: 0 additions & 49 deletions infrastructure/db/orm/collaborator_orm.rb

This file was deleted.

10 changes: 0 additions & 10 deletions infrastructure/db/orm/orm.rb

This file was deleted.

72 changes: 0 additions & 72 deletions infrastructure/db/orm/repo_orm.rb

This file was deleted.

File renamed without changes.
1 change: 0 additions & 1 deletion lib/init.rb → infrastructure/github/init.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# frozen_string_literal: false

require_relative 'github_api.rb'
require_relative 'mappers/init.rb'
5 changes: 4 additions & 1 deletion infrastructure/init.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# frozen_string_literal: false

require_relative 'db/orm/init.rb'
folders = %w[github database/orm]
folders.each do |folder|
require_relative "#{folder}/init.rb"
end
2 changes: 1 addition & 1 deletion init.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# frozen_string_literal: true

folders = %w[config entities infrastructure lib]
folders = %w[config infrastructure domain]
folders.each do |folder|
require_relative "#{folder}/init.rb"
end
Expand Down
Loading

0 comments on commit c78fa59

Please sign in to comment.