Class: SequenceGenerator::SequencesController

Inherits:
ApplicationController show all
Defined in:
app/controllers/sequence_generator/sequences_controller.rb

Instance Method Summary collapse

Instance Method Details

#createObject



6
7
8
9
10
11
12
13
14
15
# File 'app/controllers/sequence_generator/sequences_controller.rb', line 6

def create
  sequence = Sequence.new(create_sequence_params)
  if sequence.save
    render json: sequence, serializer: SequenceSerializer
  else
    api_error(status: :unprocessable_entity,
              message: 'sequence creation failed',
              errors: sequence.errors)
  end
end

#indexObject



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'app/controllers/sequence_generator/sequences_controller.rb', line 28

def index
    sequence = Sequence.where(purpose: params[:purpose], scope: params[:scope])
                       .where('valid_from <= ? AND valid_till >= ?', Time.now, Time.now).first
    if sequence
      sequence_number = sequence.generate_sequence_number
      render json: { sequence_number: sequence_number, purpose: params[:purpose],
                     scope: params[:scope] }, status: :ok
    else
      original_sequence = Sequence.where(purpose: params[:purpose], scope: params[:scope]).last
      if original_sequence.nil?
        api_error(status: :unprocessable_entity,
                  message: 'Sequence is not Created')
      else
        valid_from = original_sequence.valid_from
        valid_till = original_sequence.valid_till
        new_start_at = original_sequence.sequential_id
        difference = (valid_till - valid_from).to_i
        new_valid_from = Date.today
        new_valid_till = new_valid_from + difference
        if original_sequence.reset_from_next_year
          sequence = Sequence.create!(original_sequence.as_json.except('id', 'start_at',
                                                                       'valid_from', 'valid_till',
                                                                       'sequential_id',
                                                                       'created_at', 'updated_at')
                                          .merge!(start_at: 1,
                                                  valid_from: new_valid_from,
                                                  valid_till: new_valid_till,
                                                  sequential_id: 0))
        else
          sequence = Sequence.create!(original_sequence.as_json.except('id', 'start_at',
                                                                       'valid_from', 'valid_till',
                                                                       'sequential_id',
                                                                       'created_at', 'updated_at')
                                          .merge!(start_at: new_start_at,
                                                  valid_from: new_valid_from,
                                                  valid_till: new_valid_till,
                                                  sequential_id: new_start_at))
        end
        sequence_number = sequence.generate_sequence_number
        render json: { sequence_number: sequence_number, purpose: params[:purpose],
                       scope: params[:scope] }, status: :ok
      end
    end
end

#updateObject



17
18
19
20
21
22
23
24
25
26
# File 'app/controllers/sequence_generator/sequences_controller.rb', line 17

def update
  sequence = Sequence.find(params[:id])
  if sequence.update(update_sequence_params)
    render json: sequence, serializer: SequenceSerializer
  else
    api_error(status: :unprocessable_entity,
              message: 'sequence update failed',
              errors: sequence.errors)
  end
end