Advertisement
Guest User

indigo2

a guest
Nov 7th, 2018
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.39 KB | None | 0 0
  1. require "kemal"
  2. require "granite"
  3. require "granite/adapter/pg"
  4. require "pg"
  5.  
  6. Granite::Adapters << Granite::Adapter::Pg.new({name: "pg", url: "postgresql://postgres:postgres@localhost:5432/indigo2"})
  7.  
  8. class User < Granite::Base
  9. adapter pg
  10. table_name users
  11.  
  12. field username : String
  13. field nickname : String
  14. field password : String
  15. field active : Bool
  16. field admin : Bool
  17. timestamps
  18.  
  19. has_one :profile
  20. has_many posts : Post
  21.  
  22. validate_not_blank :username
  23. validate_min_length :username, 3
  24. validate_max_length :username, 32
  25. validate_not_blank :nickname
  26. validate_max_length :nickname, 32
  27. end
  28.  
  29. class Profile < Granite::Base
  30. adapter pg
  31. table_name profiles
  32.  
  33. field user_id : Int32
  34. field pronouns : Int32
  35. field bio : String
  36. timestamps
  37.  
  38. belongs_to :user
  39.  
  40. validate_not_blank :pronouns
  41. validate_max_length :bio, 2000
  42. end
  43.  
  44. class Community < Granite::Base
  45. adapter pg
  46. table_name communities
  47.  
  48. field title : String
  49. field description : String
  50. field icon : String
  51. field banner : String
  52. field featured : Bool
  53. timestamps
  54.  
  55. has_many posts : Post
  56.  
  57. validate_not_blank :title
  58. validate_max_length :title, 64
  59. validate_max_length :description, 2000
  60. end
  61.  
  62. class Post < Granite::Base
  63. adapter pg
  64. table_name posts
  65.  
  66. field user_id : Int32
  67. field community_id : Int32
  68. field body : String
  69. timestamps
  70.  
  71. belongs_to :user
  72. belongs_to :community
  73.  
  74. validate_not_blank :user_id
  75. validate_not_blank :community_id
  76. validate_not_blank :body
  77. validate_max_length :body, 2000
  78. end
  79.  
  80. User.migrator.drop_and_create
  81. Profile.migrator.drop_and_create
  82. Community.migrator.drop_and_create
  83. Post.migrator.drop_and_create
  84.  
  85. community = Community.new
  86.  
  87. community.title = "ok"
  88. community.description = "ok"
  89. community.icon = "https://upload.wikimedia.org/wikipedia/commons/thumb/1/17/OK-button_-_Macro_photography_of_a_remote_control.jpg/220px-OK-button_-_Macro_photography_of_a_remote_control.jpg"
  90. community.banner = "https://upload.wikimedia.org/wikipedia/commons/thumb/1/17/OK-button_-_Macro_photography_of_a_remote_control.jpg/220px-OK-button_-_Macro_photography_of_a_remote_control.jpg"
  91. community.featured = true
  92. community.save
  93.  
  94. serve_static({"gzip" => true, "dir_listing" => false})
  95.  
  96. get "/" do |env|
  97. env.response.headers["X-PJAX"] = ""
  98. communities = Community.where(featured: true).limit(4)
  99. title = "Communities"
  100.  
  101. if env.response.headers["X-PJAX"]
  102. render "src/views/communities/index.ecr", "src/views/layout.ecr"
  103. else
  104. render "src/views/communities/index.ecr"
  105. end
  106. end
  107.  
  108. get "/communities/:id" do |env|
  109. env.response.headers["X-PJAX"] = ""
  110. community = Community.where(id: env.params.url["id"]).limit(1)
  111. title = community.title
  112.  
  113. if env.response.headers["X-PJAX"]
  114. render "src/views/communities/show.ecr", "src/views/layout.ecr"
  115. else
  116. render "src/views/communities/show.ecr"
  117. end
  118. end
  119.  
  120. get "/register" do |env|
  121. env.response.headers["X-PJAX"] = ""
  122. title = "Register"
  123. if env.response.headers["X-PJAX"]
  124. render "src/views/users/register.ecr", "src/views/layout.ecr"
  125. else
  126. render "src/views/users/register.ecr"
  127. end
  128. end
  129.  
  130. get "/login" do |env|
  131. env.response.headers["X-PJAX"] = ""
  132. title = "Login"
  133.  
  134. if env.response.headers["X-PJAX"]
  135. render "src/views/users/login.ecr", "src/views/layout.ecr"
  136. else
  137. render "src/views/users/login.ecr"
  138. end
  139. end
  140.  
  141. Kemal.run
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement