Sha256: 4b786f76e977e7a4bfde2884e86d8e5b0afb6404931ff6ce1f3d01719513851a

Contents?: true

Size: 1.71 KB

Versions: 2

Compression:

Stored size: 1.71 KB

Contents

# frozen_string_literal: true
require './LabStudents/util/logger_holder'
require 'win32api'

class StudentInputFormControllerCreate
  def initialize(parent_controller)
    @parent_controller = parent_controller
    LoggerHolder.instance.debug('StudentInputFormControllerCreate: initialized')
  end

  def set_view(view)
    @view = view
    LoggerHolder.instance.debug('StudentInputFormControllerCreate: view set')
  end

  def refresh
    @parent_controller.refresh_data(1, 20)
  end

  def on_view_created
    begin
      @student_rep = StudentRepository.new(DBSourceAdapter.new)
    rescue Mysql2::Error::ConnectionError => e
      on_db_conn_error(e)
    end
  end

  def process_fields(fields)
    begin
      last_name = fields.delete(:last_name)
      first_name = fields.delete(:first_name)
      father_name = fields.delete(:father_name)

      return if last_name.nil? || first_name.nil? || father_name.nil?

      student = Student.new(last_name, first_name, father_name, **fields)

      @student_rep.add_student(student)

      LoggerHolder.instance.debug('StudentInputFormControllerCreate: adding student to DB')

      @view.close
    rescue ArgumentError => e
      LoggerHolder.instance.debug("StudentInputFormControllerCreate: wrong fields: #{e.message}")
      api = Win32API.new('user32', 'MessageBox', ['L', 'P', 'P', 'L'], 'I')
      api.call(0, e.message, 'Error', 0)
    end
  end

  private

  def on_db_conn_error(error)
    LoggerHolder.instance.debug('StudentInputFormControllerCreate: DB connection error:')
    LoggerHolder.instance.error(error.message)
    api = Win32API.new('user32', 'MessageBox', ['L', 'P', 'P', 'L'], 'I')
    api.call(0, "No connection to DB говно", "Error", 0)
    @view.close
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
rubbish_code-1.0.0 lib/source/controllers/student_input_form/student_input_form_controller_create.rb
rubbish_code-0.1.4 lib/source/controllers/student_input_form/student_input_form_controller_create.rb