merge adming and guest into users

This commit is contained in:
Mohammad Kanaan 2026-05-30 17:54:26 +03:00
parent 6b9d9dee8a
commit 362ed3596b
16 changed files with 80 additions and 189 deletions

View file

@ -1,51 +0,0 @@
class GuestsController < ApplicationController
before_action :set_guest, only: %i[ show update destroy ]
# GET /guests
def index
@guests = Guest.all
render json: @guests
end
# GET /guests/1
def show
render json: @guest
end
# POST /guests
def create
@guest = Guest.new(guest_params)
if @guest.save
render json: @guest, status: :created, location: @guest
else
render json: @guest.errors, status: :unprocessable_content
end
end
# PATCH/PUT /guests/1
def update
if @guest.update(guest_params)
render json: @guest
else
render json: @guest.errors, status: :unprocessable_content
end
end
# DELETE /guests/1
def destroy
@guest.destroy!
end
private
# Use callbacks to share common setup or constraints between actions.
def set_guest
@guest = Guest.find(params.expect(:id))
end
# Only allow a list of trusted parameters through.
def guest_params
params.expect(guest: [ :username, :name ])
end
end

View file

@ -1,14 +0,0 @@
# == Schema Information
#
# Table name: guests
#
# id :bigint not null, primary key
# name :string
# username :string
# created_at :datetime not null
# updated_at :datetime not null
#
class Guest < ApplicationRecord
has_many :price_reports, dependent: :destroy
has_many :moderation_logs, as: :moderatable, dependent: :destroy
end

View file

@ -7,7 +7,7 @@
# reason :string # reason :string
# created_at :datetime not null # created_at :datetime not null
# updated_at :datetime not null # updated_at :datetime not null
# admin_id :integer not null # admin_id :bigint not null
# moderatable_id :integer not null # moderatable_id :integer not null
# #
# Indexes # Indexes
@ -17,9 +17,9 @@
# #
# Foreign Keys # Foreign Keys
# #
# fk_rails_... (admin_id => admins.id) # fk_rails_... (admin_id => users.id)
# #
class ModerationLog < ApplicationRecord class ModerationLog < ApplicationRecord
belongs_to :moderatable, polymorphic: true belongs_to :moderatable, polymorphic: true
belongs_to :admin belongs_to :user, foreign_key: :admin_id
end end

View file

@ -10,25 +10,25 @@
# upvotes :integer # upvotes :integer
# created_at :datetime not null # created_at :datetime not null
# updated_at :datetime not null # updated_at :datetime not null
# guest_id :integer not null
# product_id :integer not null # product_id :integer not null
# store_id :integer not null # store_id :integer not null
# user_id :bigint not null
# #
# Indexes # Indexes
# #
# index_price_reports_on_guest_id (guest_id)
# index_price_reports_on_product_id (product_id) # index_price_reports_on_product_id (product_id)
# index_price_reports_on_store_id (store_id) # index_price_reports_on_store_id (store_id)
# index_price_reports_on_user_id (user_id)
# #
# Foreign Keys # Foreign Keys
# #
# fk_rails_... (guest_id => guests.id)
# fk_rails_... (product_id => products.id) # fk_rails_... (product_id => products.id)
# fk_rails_... (store_id => stores.id) # fk_rails_... (store_id => stores.id)
# fk_rails_... (user_id => users.id)
# #
class PriceReport < ApplicationRecord class PriceReport < ApplicationRecord
belongs_to :product belongs_to :product
belongs_to :store belongs_to :store
belongs_to :guest belongs_to :user
has_many :moderation_logs, as: :moderatable, dependent: :destroy has_many :moderation_logs, as: :moderatable, dependent: :destroy
end end

View file

@ -1,18 +1,20 @@
# == Schema Information # == Schema Information
# #
# Table name: admins # Table name: users
# #
# id :bigint not null, primary key # id :bigint not null, primary key
# email :string # email :string
# name :string # name :string
# password_digest :string # password_digest :string
# role :integer
# created_at :datetime not null # created_at :datetime not null
# updated_at :datetime not null # updated_at :datetime not null
# #
# Indexes # Indexes
# #
# index_admins_on_email (email) UNIQUE # index_users_on_email (email) UNIQUE
# #
class Admin < ApplicationRecord class User < ApplicationRecord
has_many :moderation_logs, dependent: :destroy has_many :price_reports, dependent: :nullify
has_many :moderation_logs, foreign_key: :admin_id, dependent: :nullify
end end

View file

@ -1,6 +1,6 @@
Rails.application.routes.draw do Rails.application.routes.draw do
resources :price_reports resources :price_reports, only: [:show, :update, :destroy]
resources :guests resources :users
resources :stores resources :stores
resources :chains resources :chains
resources :products resources :products

View file

@ -0,0 +1,13 @@
class CreateUsers < ActiveRecord::Migration[8.0]
def change
create_table :users do |t|
t.string :email
t.string :name
t.string :password_digest
t.integer :role
t.timestamps
end
add_index :users, :email, unique: true
end
end

View file

@ -0,0 +1,17 @@
class DropGuestsAndAdmins < ActiveRecord::Migration[8.0]
def change
remove_foreign_key :price_reports, :guests
remove_foreign_key :moderation_logs, :admins
change_column :price_reports, :guest_id, :bigint
change_column :moderation_logs, :admin_id, :bigint
drop_table :admins
drop_table :guests
rename_column :price_reports, :guest_id, :user_id
add_foreign_key :price_reports, :users
add_foreign_key :moderation_logs, :users, column: :admin_id
end
end

38
db/schema.rb generated
View file

@ -10,7 +10,7 @@
# #
# It's strongly recommended that you check this file into your version control system. # It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema[8.0].define(version: 2026_05_17_111138) do ActiveRecord::Schema[8.0].define(version: 2026_05_30_141612) do
# These are extensions that must be enabled in order to support this database # These are extensions that must be enabled in order to support this database
enable_extension "pg_catalog.plpgsql" enable_extension "pg_catalog.plpgsql"
@ -42,33 +42,17 @@ ActiveRecord::Schema[8.0].define(version: 2026_05_17_111138) do
t.index ["blob_id", "variation_digest"], name: "index_active_storage_variant_records_uniqueness", unique: true t.index ["blob_id", "variation_digest"], name: "index_active_storage_variant_records_uniqueness", unique: true
end end
create_table "admins", force: :cascade do |t|
t.string "name"
t.string "email"
t.string "password_digest"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["email"], name: "index_admins_on_email", unique: true
end
create_table "chains", force: :cascade do |t| create_table "chains", force: :cascade do |t|
t.string "name" t.string "name"
t.datetime "created_at", null: false t.datetime "created_at", null: false
t.datetime "updated_at", null: false t.datetime "updated_at", null: false
end end
create_table "guests", force: :cascade do |t|
t.string "username"
t.string "name"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "moderation_logs", force: :cascade do |t| create_table "moderation_logs", force: :cascade do |t|
t.string "reason" t.string "reason"
t.string "moderatable_type", null: false t.string "moderatable_type", null: false
t.integer "moderatable_id", null: false t.integer "moderatable_id", null: false
t.integer "admin_id", null: false t.bigint "admin_id", null: false
t.datetime "created_at", null: false t.datetime "created_at", null: false
t.datetime "updated_at", null: false t.datetime "updated_at", null: false
t.index ["admin_id"], name: "index_moderation_logs_on_admin_id" t.index ["admin_id"], name: "index_moderation_logs_on_admin_id"
@ -83,12 +67,12 @@ ActiveRecord::Schema[8.0].define(version: 2026_05_17_111138) do
t.datetime "reported_at" t.datetime "reported_at"
t.integer "product_id", null: false t.integer "product_id", null: false
t.integer "store_id", null: false t.integer "store_id", null: false
t.integer "guest_id", null: false t.bigint "user_id", null: false
t.datetime "created_at", null: false t.datetime "created_at", null: false
t.datetime "updated_at", null: false t.datetime "updated_at", null: false
t.index ["guest_id"], name: "index_price_reports_on_guest_id"
t.index ["product_id"], name: "index_price_reports_on_product_id" t.index ["product_id"], name: "index_price_reports_on_product_id"
t.index ["store_id"], name: "index_price_reports_on_store_id" t.index ["store_id"], name: "index_price_reports_on_store_id"
t.index ["user_id"], name: "index_price_reports_on_user_id"
end end
create_table "products", force: :cascade do |t| create_table "products", force: :cascade do |t|
@ -109,11 +93,21 @@ ActiveRecord::Schema[8.0].define(version: 2026_05_17_111138) do
t.index ["chain_id"], name: "index_stores_on_chain_id" t.index ["chain_id"], name: "index_stores_on_chain_id"
end end
create_table "users", force: :cascade do |t|
t.string "email"
t.string "name"
t.string "password_digest"
t.integer "role"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["email"], name: "index_users_on_email", unique: true
end
add_foreign_key "active_storage_attachments", "active_storage_blobs", column: "blob_id" add_foreign_key "active_storage_attachments", "active_storage_blobs", column: "blob_id"
add_foreign_key "active_storage_variant_records", "active_storage_blobs", column: "blob_id" add_foreign_key "active_storage_variant_records", "active_storage_blobs", column: "blob_id"
add_foreign_key "moderation_logs", "admins" add_foreign_key "moderation_logs", "users", column: "admin_id"
add_foreign_key "price_reports", "guests"
add_foreign_key "price_reports", "products" add_foreign_key "price_reports", "products"
add_foreign_key "price_reports", "stores" add_foreign_key "price_reports", "stores"
add_foreign_key "price_reports", "users"
add_foreign_key "stores", "chains" add_foreign_key "stores", "chains"
end end

View file

@ -1,38 +0,0 @@
require "test_helper"
class GuestsControllerTest < ActionDispatch::IntegrationTest
setup do
@guest = guests(:one)
end
test "should get index" do
get guests_url, as: :json
assert_response :success
end
test "should create guest" do
assert_difference("Guest.count") do
post guests_url, params: { guest: { name: @guest.name, username: @guest.username } }, as: :json
end
assert_response :created
end
test "should show guest" do
get guest_url(@guest), as: :json
assert_response :success
end
test "should update guest" do
patch guest_url(@guest), params: { guest: { name: @guest.name, username: @guest.username } }, as: :json
assert_response :success
end
test "should destroy guest" do
assert_difference("Guest.count", -1) do
delete guest_url(@guest), as: :json
end
assert_response :no_content
end
end

View file

@ -1,19 +0,0 @@
# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
# == Schema Information
#
# Table name: guests
#
# id :bigint not null, primary key
# name :string
# username :string
# created_at :datetime not null
# updated_at :datetime not null
#
one:
username: MyString
name: MyString
two:
username: MyString
name: MyString

View file

@ -9,7 +9,7 @@
# reason :string # reason :string
# created_at :datetime not null # created_at :datetime not null
# updated_at :datetime not null # updated_at :datetime not null
# admin_id :integer not null # admin_id :bigint not null
# moderatable_id :integer not null # moderatable_id :integer not null
# #
# Indexes # Indexes
@ -19,14 +19,14 @@
# #
# Foreign Keys # Foreign Keys
# #
# fk_rails_... (admin_id => admins.id) # fk_rails_... (admin_id => users.id)
# #
one: one:
reason: MyString reason: MyString
moderatable: one (Chain) moderatable: one (Chain)
admin: one user: one
two: two:
reason: MyString reason: MyString
moderatable: two (Chain) moderatable: two (Chain)
admin: two user: two

View file

@ -2,25 +2,28 @@
# == Schema Information # == Schema Information
# #
# Table name: admins # Table name: users
# #
# id :bigint not null, primary key # id :bigint not null, primary key
# email :string # email :string
# name :string # name :string
# password_digest :string # password_digest :string
# role :integer
# created_at :datetime not null # created_at :datetime not null
# updated_at :datetime not null # updated_at :datetime not null
# #
# Indexes # Indexes
# #
# index_admins_on_email (email) UNIQUE # index_users_on_email (email) UNIQUE
# #
one: one:
name: Admin email: guest@test.com
email: admin@test.com name: Guest
password_digest: MyString password_digest: MyString
role: 0
two: two:
name: Mod email: admin@test.com
email: mod@test.com name: Admin
password_digest: MyString password_digest: MyString
role: 1

View file

@ -1,17 +0,0 @@
# == Schema Information
#
# Table name: guests
#
# id :bigint not null, primary key
# name :string
# username :string
# created_at :datetime not null
# updated_at :datetime not null
#
require "test_helper"
class GuestTest < ActiveSupport::TestCase
# test "the truth" do
# assert true
# end
end

View file

@ -7,7 +7,7 @@
# reason :string # reason :string
# created_at :datetime not null # created_at :datetime not null
# updated_at :datetime not null # updated_at :datetime not null
# admin_id :integer not null # admin_id :bigint not null
# moderatable_id :integer not null # moderatable_id :integer not null
# #
# Indexes # Indexes
@ -17,7 +17,7 @@
# #
# Foreign Keys # Foreign Keys
# #
# fk_rails_... (admin_id => admins.id) # fk_rails_... (admin_id => users.id)
# #
require "test_helper" require "test_helper"

View file

@ -1,21 +1,22 @@
# == Schema Information # == Schema Information
# #
# Table name: admins # Table name: users
# #
# id :bigint not null, primary key # id :bigint not null, primary key
# email :string # email :string
# name :string # name :string
# password_digest :string # password_digest :string
# role :integer
# created_at :datetime not null # created_at :datetime not null
# updated_at :datetime not null # updated_at :datetime not null
# #
# Indexes # Indexes
# #
# index_admins_on_email (email) UNIQUE # index_users_on_email (email) UNIQUE
# #
require "test_helper" require "test_helper"
class AdminTest < ActiveSupport::TestCase class UserTest < ActiveSupport::TestCase
# test "the truth" do # test "the truth" do
# assert true # assert true
# end # end