# frozen_string_literal: true require_relative 'window' require_relative 'student_list' require_relative 'student_list_db' require 'win32api' require_relative '../student/student' require_relative 'student_file_adapter' require_relative 'add_controller' require_relative '../strategy/student_list_json' require_relative '../strategy/student_list_txt' require_relative '../strategy/student_list_yaml' require_relative '../all_data/data_list_student_short' require_relative 'update_controller' require 'fox16' include Fox require_relative 'update_name_controller' require_relative 'update_git_controller' require_relative 'update_contact_controller' require 'logger' class StudentListController def initialize(view) @view = view @data_list = DataListStudentShort.new([]) @data_list.add_observer(@view) @student_list = StudentList.new(StudentListDBAdapter.new) adapter_path='C:/Users/Lenovo/ruby_labs_new/lab2/tests/students.yaml' @logger = Logger.new('controller.log') end def refresh_data(k_page, number_students) #begin @logger.info("Refreshing data with k_page=#{k_page} and number_students=#{number_students}") @data_list = @student_list.get_k_n_student_short_list(k_page, number_students, @data_list) #rescue # on_db_conn_error #end @view.update_count_students(@student_list.count_student) @logger.info("Data refreshed successfully with k_page=#{k_page} and number_students=#{number_students}") end def on_db_conn_error api = Win32API.new('user32', 'MessageBox', ['L', 'P', 'P', 'L'], 'I') api.call(0, "No connection to DB", "Error", 0) exit(false) end def student_add controller = AddStudentController.new(@student_list) show_dialog(controller) end def get_student_id(index) @data_list.select(index) id = @data_list.get_select @data_list.clear_selected id end public def student_change_name(index) @logger.info('Changing student name') puts 'update name' id = get_student_id(index) controller = ChangeStudentNameController.new(@student_list, id) show_dialog(controller) end def student_change_git(index) @logger.info('Changing student Git') puts 'update git' id = get_student_id(index) controller = ChangeStudentGitController.new(@student_list, id) show_dialog(controller) end def student_change_contact(index) @logger.info('Changing student contact') puts 'update name' id = get_student_id(index) controller = ChangeStudentContactController.new(@student_list, id) show_dialog(controller) end def student_delete(indexes) @data_list.select(*indexes) id_list = @data_list.get_select @data_list.clear_selected id_list.each{|student_id| @student_list.delete_student(student_id)} @view.refresh @logger.info("Deleted students with IDs: #{id_list.join(', ')}") end private def show_dialog(controller) # controller = AddStudentController.new(@student_list) view = CreateStudentDialog.new(@view, controller) controller.add_view(view) controller.execute @view.refresh end end