shnaider_carproj 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/Gemfile +10 -0
- data/Gemfile.lock +71 -0
- data/diagrams/add_owner.jpg +0 -0
- data/diagrams/delete_owner.jpg +0 -0
- data/diagrams/er.png +0 -0
- data/diagrams/owner.jpg +0 -0
- data/diagrams/owner.sai2 +0 -0
- data/diagrams/start.jpg +0 -0
- data/lib/car/controllers/publisher_input_form_controller_create.rb +44 -0
- data/lib/car/controllers/publisher_input_form_controller_edit.rb +52 -0
- data/lib/car/controllers/publisher_list_controller.rb +99 -0
- data/lib/car/tenant_db_data_source.rb +63 -0
- data/lib/car/ui/tenant_input_form.rb +69 -0
- data/lib/car/ui/tenant_list_view.rb +168 -0
- data/lib/controllers/tab_students_controller.rb +43 -0
- data/lib/data_sources/car_db_data_source.rb +43 -0
- data/lib/data_sources/db_client.rb +34 -0
- data/lib/db_config/carshering_config.yaml +5 -0
- data/lib/db_config/config.yaml +5 -0
- data/lib/db_config/migrations/create_db.sql +3 -0
- data/lib/db_config/migrations/create_tables.sql +29 -0
- data/lib/db_config/mock_data/mock_data.sql +55 -0
- data/lib/models/car.rb +31 -0
- data/lib/models/owner.rb +32 -0
- data/lib/models/student.rb +102 -0
- data/lib/models/student_base.rb +100 -0
- data/lib/models/student_short.rb +50 -0
- data/lib/models/tenant.rb +40 -0
- data/lib/owner/controllers/owner_input_form_controller_create.rb +44 -0
- data/lib/owner/controllers/owner_input_form_controller_edit.rb +53 -0
- data/lib/owner/controllers/owner_list_controller.rb +107 -0
- data/lib/owner/owner_db_data_source.rb +70 -0
- data/lib/owner/ui/owner_input_form.rb +69 -0
- data/lib/owner/ui/owner_list_view.rb +169 -0
- data/lib/repositories/adapters/db_source_adapter.rb +54 -0
- data/lib/repositories/adapters/file_source_adapter.rb +37 -0
- data/lib/repositories/containers/data_list.rb +74 -0
- data/lib/repositories/containers/data_list_student_short.rb +18 -0
- data/lib/repositories/containers/data_table.rb +35 -0
- data/lib/repositories/data_sources/db_data_source.rb +32 -0
- data/lib/repositories/data_sources/file_data_source.rb +75 -0
- data/lib/repositories/data_sources/transformers/data_transformer_base.rb +15 -0
- data/lib/repositories/data_sources/transformers/data_transformer_json.rb +16 -0
- data/lib/repositories/data_sources/transformers/data_transformer_yaml.rb +16 -0
- data/lib/repositories/student_repository.rb +32 -0
- data/lib/state_holders/list_state_notifier.rb +62 -0
- data/lib/tenant/controllers/tenant_input_form_controller_create.rb +44 -0
- data/lib/tenant/controllers/tenant_input_form_controller_edit.rb +53 -0
- data/lib/tenant/controllers/tenant_list_controller.rb +107 -0
- data/lib/tenant/tenant_db_data_source.rb +90 -0
- data/lib/tenant/ui/tenant_input_form.rb +69 -0
- data/lib/tenant/ui/tenant_list_view.rb +169 -0
- data/lib/views/main_window.rb +25 -0
- data/lib/views/tab_students.rb +148 -0
- data/requirements.docx +0 -0
- data/shnaider_carproj.gemspec +15 -0
- data/test/car_test.rb +33 -0
- data/test/logger.rb +27 -0
- data/test/main.rb +5 -0
- data/test/owner_test.rb +48 -0
- data/test/state_notifier_test.rb +80 -0
- data/test/tenant_test.rb +48 -0
- metadata +118 -0
@@ -0,0 +1,34 @@
|
|
1
|
+
require 'mysql2'
|
2
|
+
|
3
|
+
class DBClient
|
4
|
+
private_class_method :new
|
5
|
+
@instance_mutex = Mutex.new
|
6
|
+
|
7
|
+
def initialize
|
8
|
+
db_config = YAML.load_file('./db_config/carshering_config.yaml').transform_keys(&:to_sym)
|
9
|
+
@client = Mysql2::Client.new(db_config)
|
10
|
+
@client.query_options.merge!(symbolize_keys: true)
|
11
|
+
end
|
12
|
+
|
13
|
+
def self.instance
|
14
|
+
return @instance if @instance
|
15
|
+
|
16
|
+
@instance_mutex.synchronize do
|
17
|
+
@instance ||= new
|
18
|
+
end
|
19
|
+
|
20
|
+
@instance
|
21
|
+
end
|
22
|
+
|
23
|
+
def prepare_exec(statement, *params)
|
24
|
+
@client.prepare(statement).execute(*params)
|
25
|
+
end
|
26
|
+
|
27
|
+
def query(statement)
|
28
|
+
@client.query(statement)
|
29
|
+
end
|
30
|
+
|
31
|
+
def last_id
|
32
|
+
@client.last_id
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
USE carshering;
|
2
|
+
|
3
|
+
-- Table 1: Owner
|
4
|
+
CREATE TABLE Owner
|
5
|
+
(
|
6
|
+
OwnerID INT PRIMARY KEY AUTO_INCREMENT,
|
7
|
+
FirstName VARCHAR(50) NOT NULL,
|
8
|
+
LastName VARCHAR(50) NOT NULL,
|
9
|
+
FatherName VARCHAR(50)
|
10
|
+
);
|
11
|
+
|
12
|
+
-- Table 2: Tenant
|
13
|
+
CREATE TABLE Tenant
|
14
|
+
(
|
15
|
+
TenantID INT PRIMARY KEY AUTO_INCREMENT,
|
16
|
+
FirstName VARCHAR(50) NOT NULL,
|
17
|
+
LastName VARCHAR(50) NOT NULL,
|
18
|
+
Phone VARCHAR(25)
|
19
|
+
);
|
20
|
+
-- Table 3: Car
|
21
|
+
CREATE TABLE Car
|
22
|
+
(
|
23
|
+
CarID INT PRIMARY KEY AUTO_INCREMENT,
|
24
|
+
Model VARCHAR(255) NOT NULL,
|
25
|
+
OwnerID INT NOT NULL,
|
26
|
+
TenantID INT NOT NULL,
|
27
|
+
FOREIGN KEY (OwnerID) REFERENCES Owner (OwnerID),
|
28
|
+
FOREIGN KEY (TenantID) REFERENCES Tenant (TenantID)
|
29
|
+
);
|
@@ -0,0 +1,55 @@
|
|
1
|
+
USE carshering;
|
2
|
+
|
3
|
+
-- Insert 30 owners
|
4
|
+
INSERT INTO Owner (FirstName, LastName, FatherName)
|
5
|
+
VALUES
|
6
|
+
('John', 'Doe', 'Paul'),
|
7
|
+
('Jane', 'Doe', NULL),
|
8
|
+
('Robert', 'Johnson', 'Michael'),
|
9
|
+
('Emily', 'Smith', NULL),
|
10
|
+
('David', 'Lee', 'William'),
|
11
|
+
('Sarah', 'Taylor', NULL),
|
12
|
+
('Thomas', 'Brown', NULL),
|
13
|
+
('Elizabeth', 'Wilson', 'Jennifer'),
|
14
|
+
('Richard', 'Anderson', 'Matthew'),
|
15
|
+
('Karen', 'Martin', NULL),
|
16
|
+
('William', 'Thompson', NULL),
|
17
|
+
('Nancy', 'Garcia', NULL),
|
18
|
+
('Michael', 'Davis', 'Anthony'),
|
19
|
+
('Mary', 'Miller', NULL),
|
20
|
+
('Christopher', 'Jackson', NULL),
|
21
|
+
('Jessica', 'Perez', 'Marie'),
|
22
|
+
('Brian', 'Moore', NULL),
|
23
|
+
('Megan', 'Allen', NULL),
|
24
|
+
('Anthony', 'Young', NULL),
|
25
|
+
('Laura', 'Harris', 'Christine'),
|
26
|
+
('Kevin', 'King', NULL),
|
27
|
+
('Stephanie', 'Scott', NULL),
|
28
|
+
('Jason', 'Turner', 'Eric'),
|
29
|
+
('Melissa', 'Walker', NULL),
|
30
|
+
('Mark', 'Collins', NULL),
|
31
|
+
('Tiffany', 'Nelson', NULL),
|
32
|
+
('Eric', 'Gonzalez', NULL),
|
33
|
+
('Amy', 'Carter', 'Jennifer'),
|
34
|
+
('Matthew', 'Baker', NULL),
|
35
|
+
('Samantha', 'Edwards', NULL);
|
36
|
+
|
37
|
+
-- Insert 9 Tenants
|
38
|
+
INSERT INTO Tenant (FirstName, LastName, Phone)
|
39
|
+
VALUES
|
40
|
+
('Penguin', 'Huse', '+79009009090'),
|
41
|
+
('Harper', 'Collins', NULL),
|
42
|
+
('Hachette', 'Caroup', NULL),
|
43
|
+
('Macmillan', 'Pusher', '+79990900909'),
|
44
|
+
('Simon' , 'Schuster', '+79876009090'),
|
45
|
+
('Oxford', 'Usiress', NULL),
|
46
|
+
('Cambe', 'Uveress', '+79999999999'),
|
47
|
+
('Pear', 'Etion', NULL),
|
48
|
+
('Bloom', 'Puling', '+79690807766');
|
49
|
+
|
50
|
+
INSERT INTO car(Model, OwnerID, TenantID)
|
51
|
+
VALUES
|
52
|
+
('BMW', 2, 4),
|
53
|
+
('Audi', 5, 7),
|
54
|
+
('Lada', 7, 4),
|
55
|
+
('Tesla', 12, 9);
|
data/lib/models/car.rb
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
class Car
|
2
|
+
attr_reader :car_id, :model, :owner_id, :tenant_id
|
3
|
+
|
4
|
+
def initialize(car_id, model, owner_id, tenant_id)
|
5
|
+
validate_null('car_id', car_id)
|
6
|
+
validate_null('model', model)
|
7
|
+
validate_null('owner_id', owner_id)
|
8
|
+
validate_null('tenant_id', tenant_id)
|
9
|
+
|
10
|
+
validate_title_length(model)
|
11
|
+
|
12
|
+
@car_id = car_id
|
13
|
+
@model = model
|
14
|
+
@owner_id = owner_id
|
15
|
+
@tenant_id = tenant_id
|
16
|
+
end
|
17
|
+
|
18
|
+
private
|
19
|
+
|
20
|
+
def validate_null(name, value)
|
21
|
+
if value.nil?
|
22
|
+
raise ArgumentError, "Argument '#{name}' cannot be null"
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
def validate_title_length(model)
|
27
|
+
if model.length > 255
|
28
|
+
raise ArgumentError, "Model exceeds 255 characters limit: #{model}"
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
data/lib/models/owner.rb
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
class Owner
|
2
|
+
attr_reader :owner_id, :first_name, :last_name, :father_name
|
3
|
+
|
4
|
+
def initialize(owner_id, first_name, last_name, father_name = nil)
|
5
|
+
validate_null('owner_id', owner_id)
|
6
|
+
validate_null('first_name', first_name)
|
7
|
+
validate_null('last_name', last_name)
|
8
|
+
|
9
|
+
validate_name_length(first_name, last_name, father_name)
|
10
|
+
|
11
|
+
@owner_id = owner_id
|
12
|
+
@first_name = first_name
|
13
|
+
@last_name = last_name
|
14
|
+
@father_name = father_name
|
15
|
+
end
|
16
|
+
|
17
|
+
private
|
18
|
+
|
19
|
+
def validate_null(name, value)
|
20
|
+
if value.nil?
|
21
|
+
raise ArgumentError, "Argument '#{name}' cannot be null"
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
def validate_name_length(first_name, last_name, father_name)
|
26
|
+
[first_name, last_name, father_name].each do |name|
|
27
|
+
if name && name.length > 50
|
28
|
+
raise ArgumentError, "Name exceeds 50 characters limit: #{name}"
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,102 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'json'
|
4
|
+
require_relative 'student_base'
|
5
|
+
|
6
|
+
class Student < StudentBase
|
7
|
+
# Делаем new предка публичным
|
8
|
+
public_class_method :new
|
9
|
+
|
10
|
+
def self.from_hash(hash)
|
11
|
+
raise ArgumentError, 'Fields required: fist_name, last_name, father_name' unless hash.key?(:first_name) && hash.key?(:last_name) && hash.key?(:father_name)
|
12
|
+
|
13
|
+
first_name = hash.delete(:first_name)
|
14
|
+
last_name = hash.delete(:last_name)
|
15
|
+
father_name = hash.delete(:father_name)
|
16
|
+
|
17
|
+
Student.new(last_name, first_name, father_name, **hash)
|
18
|
+
end
|
19
|
+
|
20
|
+
# Конструктор из JSON строки
|
21
|
+
def self.from_json_str(str)
|
22
|
+
params = JSON.parse(str, { symbolize_names: true })
|
23
|
+
from_hash(params)
|
24
|
+
end
|
25
|
+
|
26
|
+
# Делаем публичными геттеры и сеттеры базового класса
|
27
|
+
public :phone, :telegram, :email, 'id=', 'phone=', 'telegram=', 'email=', 'git='
|
28
|
+
|
29
|
+
# Стандартные геттеры для полей
|
30
|
+
attr_reader :last_name, :first_name, :father_name
|
31
|
+
|
32
|
+
# Стандартный конструктор
|
33
|
+
def initialize(last_name, first_name, father_name, **options)
|
34
|
+
self.last_name = last_name
|
35
|
+
self.first_name = first_name
|
36
|
+
self.father_name = father_name
|
37
|
+
super(**options)
|
38
|
+
end
|
39
|
+
|
40
|
+
# Сеттеры с валидацией перед присваиванием
|
41
|
+
def last_name=(new_last_name)
|
42
|
+
raise ArgumentError, "Invalid argument: last_name=#{new_last_name}" unless Student.valid_name?(new_last_name)
|
43
|
+
|
44
|
+
@last_name = new_last_name
|
45
|
+
end
|
46
|
+
|
47
|
+
def first_name=(new_first_name)
|
48
|
+
raise ArgumentError, "Invalid argument: first_name=#{new_first_name}" unless Student.valid_name?(new_first_name)
|
49
|
+
|
50
|
+
@first_name = new_first_name
|
51
|
+
end
|
52
|
+
|
53
|
+
def father_name=(new_father_name)
|
54
|
+
raise ArgumentError, "Invalid argument: father_name=#{new_father_name}" unless Student.valid_name?(new_father_name)
|
55
|
+
|
56
|
+
@father_name = new_father_name
|
57
|
+
end
|
58
|
+
|
59
|
+
# Отдельный сеттер для массовой установки контактов
|
60
|
+
def set_contacts(phone: nil, telegram: nil, email: nil)
|
61
|
+
self.phone = phone if phone
|
62
|
+
self.telegram = telegram if telegram
|
63
|
+
self.email = email if email
|
64
|
+
end
|
65
|
+
|
66
|
+
# Имя пользователя в формате Фамилия И. О.
|
67
|
+
def last_name_and_initials
|
68
|
+
"#{last_name} #{first_name[0]}. #{father_name[0]}."
|
69
|
+
end
|
70
|
+
|
71
|
+
# Краткая информация о пользователе
|
72
|
+
def short_info
|
73
|
+
info = {}
|
74
|
+
info[:last_name_and_initials] = last_name_and_initials
|
75
|
+
info[:contact] = short_contact
|
76
|
+
info[:git] = git
|
77
|
+
JSON.generate(info)
|
78
|
+
end
|
79
|
+
|
80
|
+
# Методы приведения объекта к строке
|
81
|
+
def to_s
|
82
|
+
result = "#{last_name} #{first_name} #{father_name}"
|
83
|
+
%i[id phone telegram email git].each do |attr|
|
84
|
+
attr_val = send(attr)
|
85
|
+
result += ", #{attr}=#{attr_val}" unless attr_val.nil?
|
86
|
+
end
|
87
|
+
result
|
88
|
+
end
|
89
|
+
|
90
|
+
def to_hash
|
91
|
+
attrs = {}
|
92
|
+
%i[last_name first_name father_name id phone telegram email git].each do |attr|
|
93
|
+
attr_val = send(attr)
|
94
|
+
attrs[attr] = attr_val unless attr_val.nil?
|
95
|
+
end
|
96
|
+
attrs
|
97
|
+
end
|
98
|
+
|
99
|
+
def to_json_str
|
100
|
+
JSON.generate(to_hash)
|
101
|
+
end
|
102
|
+
end
|
@@ -0,0 +1,100 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
class StudentBase
|
4
|
+
# Запрещаем создание базового класса (он "абстрактный")
|
5
|
+
private_class_method :new
|
6
|
+
|
7
|
+
# Валидаторы для полей
|
8
|
+
def self.valid_name?(name)
|
9
|
+
name.match(/(^[А-Я][а-я]+$)|(^[A-Z][a-z]+$)/)
|
10
|
+
end
|
11
|
+
|
12
|
+
def self.valid_phone?(phone)
|
13
|
+
phone.match(/^\+?[78] ?[(-]?\d{3} ?[)-]?[ -]?\d{3}[ -]?\d{2}[ -]?\d{2}$/)
|
14
|
+
end
|
15
|
+
|
16
|
+
def self.valid_profile_name?(profile_name)
|
17
|
+
profile_name.match(/^[a-zA-Z0-9_.]+$/)
|
18
|
+
end
|
19
|
+
|
20
|
+
def self.valid_email?(email)
|
21
|
+
email.match(/^(?:[a-z0-9!#$%&'*+\/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+\/=?^_`{|}~-]+)*|"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])*")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\[(?:(?:(2(5[0-5]|[0-4][0-9])|1[0-9][0-9]|[1-9]?[0-9]))\.){3}(?:(2(5[0-5]|[0-4][0-9])|1[0-9][0-9]|[1-9]?[0-9])|[a-z0-9-]*[a-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\])$/)
|
22
|
+
end
|
23
|
+
|
24
|
+
# Стандартные геттеры и сеттеры для полей
|
25
|
+
|
26
|
+
protected
|
27
|
+
|
28
|
+
attr_writer :id
|
29
|
+
attr_reader :phone, :telegram, :email
|
30
|
+
|
31
|
+
public
|
32
|
+
|
33
|
+
attr_reader :id, :git
|
34
|
+
|
35
|
+
# Стандартный конструктор
|
36
|
+
def initialize(id: nil, phone: nil, telegram: nil, email: nil, git: nil)
|
37
|
+
self.id = id
|
38
|
+
self.phone = phone
|
39
|
+
self.telegram = telegram
|
40
|
+
self.email = email
|
41
|
+
self.git = git
|
42
|
+
end
|
43
|
+
|
44
|
+
# Краткая информация о первом доступном контакте пользователя
|
45
|
+
def short_contact
|
46
|
+
contact = {}
|
47
|
+
%i[telegram email phone].each do |attr|
|
48
|
+
attr_val = send(attr)
|
49
|
+
next if attr_val.nil?
|
50
|
+
|
51
|
+
contact[:type] = attr
|
52
|
+
contact[:value] = attr_val
|
53
|
+
return contact
|
54
|
+
end
|
55
|
+
|
56
|
+
nil
|
57
|
+
end
|
58
|
+
|
59
|
+
protected
|
60
|
+
|
61
|
+
# Сеттеры с валидацией перед присваиванием
|
62
|
+
def phone=(new_phone)
|
63
|
+
raise ArgumentError, "Invalid argument: phone=#{new_phone}" unless new_phone.nil? || StudentBase.valid_phone?(new_phone)
|
64
|
+
|
65
|
+
@phone = new_phone
|
66
|
+
end
|
67
|
+
|
68
|
+
def telegram=(new_telegram)
|
69
|
+
raise ArgumentError, "Invalid argument: telegram=#{new_telegram}" unless new_telegram.nil? || StudentBase.valid_profile_name?(new_telegram)
|
70
|
+
|
71
|
+
@telegram = new_telegram
|
72
|
+
end
|
73
|
+
|
74
|
+
def git=(new_git)
|
75
|
+
raise ArgumentError, "Invalid argument: git=#{new_git}" unless new_git.nil? || StudentBase.valid_profile_name?(new_git)
|
76
|
+
|
77
|
+
@git = new_git
|
78
|
+
end
|
79
|
+
|
80
|
+
def email=(new_email)
|
81
|
+
raise ArgumentError, "Invalid argument: email=#{new_email}" unless new_email.nil? || StudentBase.valid_email?(new_email)
|
82
|
+
|
83
|
+
@email = new_email
|
84
|
+
end
|
85
|
+
|
86
|
+
public
|
87
|
+
|
88
|
+
# Валидаторы объекта
|
89
|
+
def has_contacts?
|
90
|
+
!phone.nil? || !telegram.nil? || !email.nil?
|
91
|
+
end
|
92
|
+
|
93
|
+
def has_git?
|
94
|
+
!git.nil?
|
95
|
+
end
|
96
|
+
|
97
|
+
def valid?
|
98
|
+
has_contacts? && has_git?
|
99
|
+
end
|
100
|
+
end
|
@@ -0,0 +1,50 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
class StudentShort < StudentBase
|
4
|
+
# Делаем new предка публичным
|
5
|
+
public_class_method :new
|
6
|
+
|
7
|
+
# Стандартные геттеры и сеттеры
|
8
|
+
|
9
|
+
private
|
10
|
+
|
11
|
+
attr_writer :last_name_and_initials, :contact
|
12
|
+
|
13
|
+
public
|
14
|
+
|
15
|
+
attr_reader :last_name_and_initials, :contact
|
16
|
+
|
17
|
+
# Конструктор из Student
|
18
|
+
def self.from_student(student)
|
19
|
+
raise ArgumentError, 'Student ID is required' if student.id.nil?
|
20
|
+
|
21
|
+
StudentShort.new(student.id, student.short_info)
|
22
|
+
end
|
23
|
+
|
24
|
+
# Стандартный конструктор
|
25
|
+
def initialize(id, info_str)
|
26
|
+
params = JSON.parse(info_str, { symbolize_names: true })
|
27
|
+
raise ArgumentError, 'Fields required: last_name_and_initials' if !params.key?(:last_name_and_initials) || params[:last_name_and_initials].nil?
|
28
|
+
|
29
|
+
self.id = id
|
30
|
+
self.last_name_and_initials = params[:last_name_and_initials]
|
31
|
+
self.contact = params[:contact]
|
32
|
+
self.git = params[:git]
|
33
|
+
|
34
|
+
options = {}
|
35
|
+
options[:id] = id
|
36
|
+
options[:git] = git
|
37
|
+
options[contact[:type].to_sym] = contact[:value] if contact
|
38
|
+
super(**options)
|
39
|
+
end
|
40
|
+
|
41
|
+
# Методы приведения объекта к строке
|
42
|
+
def to_s
|
43
|
+
result = last_name_and_initials
|
44
|
+
%i[id contact git].each do |attr|
|
45
|
+
attr_val = send(attr)
|
46
|
+
result += ", #{attr}=#{attr_val}" unless attr_val.nil?
|
47
|
+
end
|
48
|
+
result
|
49
|
+
end
|
50
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
class Tenant
|
2
|
+
PHONE_REGEX = /^\+?[78] ?[(-]?\d{3} ?[)-]?[ -]?\d{3}[ -]?\d{2}[ -]?\d{2}$/
|
3
|
+
attr_reader :tenant_id, :first_name, :last_name, :phone
|
4
|
+
|
5
|
+
def initialize(tenant_id, first_name, last_name, phone = nil)
|
6
|
+
validate_null('tenant_id', tenant_id)
|
7
|
+
validate_null('first_name', first_name)
|
8
|
+
validate_null('last_name', last_name)
|
9
|
+
|
10
|
+
validate_name_length(first_name, last_name)
|
11
|
+
validate_phone(phone)
|
12
|
+
|
13
|
+
@tenant_id = tenant_id
|
14
|
+
@first_name = first_name
|
15
|
+
@last_name = last_name
|
16
|
+
@phone = phone
|
17
|
+
end
|
18
|
+
|
19
|
+
private
|
20
|
+
|
21
|
+
def validate_null(name, value)
|
22
|
+
if value.nil?
|
23
|
+
raise ArgumentError, "Argument '#{name}' cannot be null"
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
def validate_name_length(first_name, last_name)
|
28
|
+
[first_name, last_name].each do |name|
|
29
|
+
if name && name.length > 50
|
30
|
+
raise ArgumentError, "Name exceeds 50 characters limit: #{name}"
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
def validate_phone(phone)
|
36
|
+
if phone && !phone.match?(PHONE_REGEX)
|
37
|
+
raise ArgumentError, "Invalid phone format: #{phone}"
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
@@ -0,0 +1,44 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'win32api'
|
4
|
+
|
5
|
+
class OwnerInputFormControllerCreate
|
6
|
+
def initialize(parent_controller)
|
7
|
+
@parent_controller = parent_controller
|
8
|
+
@owner_rep = OwnerDBDataSource.new
|
9
|
+
end
|
10
|
+
|
11
|
+
def set_view(view)
|
12
|
+
@view = view
|
13
|
+
end
|
14
|
+
|
15
|
+
def on_view_created
|
16
|
+
# begin
|
17
|
+
# @student_rep = StudentRepository.new(DBSourceAdapter.new)
|
18
|
+
# rescue Mysql2::Error::ConnectionError
|
19
|
+
# on_db_conn_error
|
20
|
+
# end
|
21
|
+
end
|
22
|
+
|
23
|
+
def process_fields(fields)
|
24
|
+
begin
|
25
|
+
puts fields
|
26
|
+
item = Owner.new(-1, *fields.values)
|
27
|
+
puts item
|
28
|
+
item = @owner_rep.add(item)
|
29
|
+
@parent_controller.state_notifier.add(item)
|
30
|
+
@view.close
|
31
|
+
rescue ArgumentError => e
|
32
|
+
api = Win32API.new('user32', 'MessageBox', ['L', 'P', 'P', 'L'], 'I')
|
33
|
+
api.call(0, e.message, 'Error', 0)
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
private
|
38
|
+
|
39
|
+
def on_db_conn_error
|
40
|
+
api = Win32API.new('user32', 'MessageBox', ['L', 'P', 'P', 'L'], 'I')
|
41
|
+
api.call(0, "No connection to DB", "Error", 0)
|
42
|
+
@view.close
|
43
|
+
end
|
44
|
+
end
|
@@ -0,0 +1,53 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'win32api'
|
4
|
+
|
5
|
+
class OwnerInputFormControllerEdit
|
6
|
+
def initialize(parent_controller, item)
|
7
|
+
@parent_controller = parent_controller
|
8
|
+
@item = item
|
9
|
+
@owner_rep = OwnerDBDataSource.new
|
10
|
+
end
|
11
|
+
|
12
|
+
def set_view(view)
|
13
|
+
@view = view
|
14
|
+
end
|
15
|
+
|
16
|
+
def on_view_created
|
17
|
+
# begin
|
18
|
+
# @student_rep = StudentRepository.new(DBSourceAdapter.new)
|
19
|
+
# rescue Mysql2::Error::ConnectionError
|
20
|
+
# on_db_conn_error
|
21
|
+
# end
|
22
|
+
|
23
|
+
# @item = @author_rep.get(@item_id)
|
24
|
+
# @view.make_readonly(:git, :telegram, :email, :phone)
|
25
|
+
populate_fields(@item)
|
26
|
+
end
|
27
|
+
|
28
|
+
def populate_fields(item)
|
29
|
+
@view.set_value(:first_name, item.first_name)
|
30
|
+
@view.set_value(:last_name, item.last_name)
|
31
|
+
@view.set_value(:father_name, item.father_name)
|
32
|
+
end
|
33
|
+
|
34
|
+
def process_fields(fields)
|
35
|
+
begin
|
36
|
+
item = Owner.new(@item.owner_id, *fields.values)
|
37
|
+
item = @owner_rep.change(item)
|
38
|
+
@parent_controller.state_notifier.replace(@item, item)
|
39
|
+
@view.close
|
40
|
+
rescue ArgumentError => e
|
41
|
+
api = Win32API.new('user32', 'MessageBox', ['L', 'P', 'P', 'L'], 'I')
|
42
|
+
api.call(0, e.message, 'Error', 0)
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
private
|
47
|
+
|
48
|
+
def on_db_conn_error
|
49
|
+
api = Win32API.new('user32', 'MessageBox', ['L', 'P', 'P', 'L'], 'I')
|
50
|
+
api.call(0, "No connection to DB", "Error", 0)
|
51
|
+
@view.close
|
52
|
+
end
|
53
|
+
end
|