Skip to content

Commit

Permalink
add domain model
Browse files Browse the repository at this point in the history
  • Loading branch information
jiangplus committed Sep 10, 2024
1 parent 3169af7 commit 8633898
Show file tree
Hide file tree
Showing 11 changed files with 82 additions and 32 deletions.
9 changes: 6 additions & 3 deletions app/controllers/api/group_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,12 @@ def create
end

group = Group.new(group_params)
group.update(
handle: handle
)
ActiveRecord::Base.transaction do
group.update(
handle: handle
)
Domain.create(handle: handle, fullname: "#{handle}.sola.day", item_type: "Group", item_id: group.id)
end

Membership.create(profile_id: profile.id, group_id: group.id, role: "owner", status: "active")
group.increment!(:memberships_count)
Expand Down
6 changes: 4 additions & 2 deletions app/controllers/api/profile_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -240,8 +240,10 @@ def create
render json: { result: "error", message: "profile handle exists" }
return
end

profile.update(handle: handle)
ActiveRecord::Base.transaction do
profile.update(handle: handle)
Domain.create(handle: handle, fullname: "#{handle}.sola.day", item_type: "Profile", item_id: profile.id)
end
render json: { result: "ok" }
end

Expand Down
4 changes: 2 additions & 2 deletions app/controllers/api/service_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ def send_email
code = rand(10_000..100_000)
token = ProfileToken.create(context: params[:context], sent_to: params[:email], code: code)

mailer = SigninMailer.with(code: code, recipient: params[:email]).signin_email
mailer.deliver_now!
# mailer = SigninMailer.with(code: code, recipient: params[:email]).signin_email
# mailer.deliver_now!

render json: { result: "ok", email: params[:email] }
end
Expand Down
2 changes: 2 additions & 0 deletions app/models/domain.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
class Domain < ApplicationRecord
end
11 changes: 11 additions & 0 deletions db/migrate/20240910055659_create_domains.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class CreateDomains < ActiveRecord::Migration[7.2]
def change
create_table :domains do |t|
t.string :handle
t.string :fullname
t.string :item_type
t.integer :item_id
t.timestamps
end
end
end
11 changes: 10 additions & 1 deletion db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 4 additions & 3 deletions test/controllers/api/group_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@ class Api::GroupControllerTest < ActionDispatch::IntegrationTest
assert group.active?
assert group.is_owner(profile.id)
assert group.memberships_count == 1
assert Domain.find_by(handle: "newworld", item_type: "Group", item_id: group.id).present?
end

test "api#group/update" do
test "api#group/update" do # optimize this test function
assert_changes "Group.find_by(handle: 'guildx').timezone" do
profile = Profile.find_by(handle: "cookie")
auth_token = profile.gen_auth_token
group = Group.find_by(handle: "guildx")
Expand All @@ -31,8 +33,7 @@ class Api::GroupControllerTest < ActionDispatch::IntegrationTest
timezone: "asia/hongkong",
} }
assert_response :success
group = Group.find_by(handle: "guildx")
assert group.timezone == "asia/hongkong"
end
end

test "api#group/transfer_owner fails for non-member recipient" do
Expand Down
33 changes: 15 additions & 18 deletions test/controllers/api/profile_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,25 @@

class Api::ProfileControllerTest < ActionDispatch::IntegrationTest

# test "api#profile/signin_with_email" do
# post api_service_send_email_url, params: { context: "email-signin", email: "[email protected]" }
# assert_response :success
# p response.body
test "api#profile/signin_with_email" do
post api_service_send_email_url, params: { context: "email-signin", email: "[email protected]" }
assert_response :success

# post api_profile_signin_with_email_url, params: { email: "[email protected]", code: ProfileToken.last.code }
# assert_response :success
# p response.body
# auth_token = JSON.parse(response.body)["auth_token"]
# p Profile.find_by(email: "[email protected]")
post api_profile_signin_with_email_url, params: { email: "[email protected]", code: ProfileToken.last.code }
assert_response :success
auth_token = JSON.parse(response.body)["auth_token"]
assert Profile.find_by(email: "[email protected]")

# post api_profile_create_url, params: { auth_token: auth_token, handle: "example" }
# assert_response :success
# p response.body
post api_profile_create_url, params: { auth_token: auth_token, handle: "example" }
assert_response :success
assert Domain.find_by(handle: "example", item_type: "Profile", item_id: Profile.find_by(handle: "example").id).present?

# get api_profile_get_by_email_url, params: { email: "[email protected]" }
# assert_response :success
# p response.body
get api_profile_get_by_email_url, params: { email: "[email protected]" }
assert_response :success

# get api_profile_me_url, params: { auth_token: auth_token }
# assert_response :success
# end
get api_profile_me_url, params: { auth_token: auth_token }
assert_response :success
end

# test "api#profile/set_verified_email" do
# post api_service_send_email_url, params: { context: "email-verify", email: "[email protected]" }
Expand Down
13 changes: 10 additions & 3 deletions test/controllers/api/voucher_controller_test.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
require "test_helper"

class Api::VoucherControllerTest < ActionDispatch::IntegrationTest
# test "the truth" do
# assert true
# end
# generate test for voucher/create
test "api#voucher/create" do
profile = Profile.find_by(handle: "cookie")
auth_token = profile.gen_auth_token

post api_voucher_create_url, params: { auth_token: auth_token, voucher: {
code: "VOUCHER123",
description: "Voucher for 123",
} }
end
end
11 changes: 11 additions & 0 deletions test/fixtures/domains.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html

# This model initially had no columns defined. If you add columns to the
# model remove the "{}" from the fixture names and add the columns immediately
# below each fixture, per the syntax in the comments below
#
# one: {}
# column: value
#
# two: {}
# column: value
7 changes: 7 additions & 0 deletions test/models/domain_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require "test_helper"

class DomainTest < ActiveSupport::TestCase
# test "the truth" do
# assert true
# end
end

0 comments on commit 8633898

Please sign in to comment.