aboutsummaryrefslogtreecommitdiff
path: root/app/controllers/resources_controller.rb
blob: fdb69cceb9cb09cff24c4ae859d9a790f4b2b752 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
class ResourcesController < ApplicationController
  before_action :set_resource, only: [:new, :show, :edit, :update, :delete, :destroy]

  # GET /resources
  def index
    @resources = Resource.order(:uuid).page params[:page]
  end

  # GET /resources/new
  def new
  end

  # POST /resources
  def create
    # Check list:
    # 1. UUID is not set, or not known
    # 2. Agent is correct
    # 3. Name, Description, Phone, Email, etc. do not yield results
    # 4. Background job to validate fields and save
    # 5. Or Background job to list similar items
    # 6. Return to user with new record or list of mergeable ones
  end

  # GET /resources/:id
  def show
  end

  # GET /resources/:id/edit
  def edit
  end

  # PATCH /resources/:id
  def update
    # Check list:
    # 1. Compare records for changes
    # 2. Validate each change
    # 3. Moderate queue or save

    updated_properties = @resource.feature['properties']
    updated_geometry   = @resource.feature['geometry']

    updated = false

    flash.now[:notice] = updated_geometry.inspect

    if @resource.uuid.present? && params[:resource][:uuid].present? && params[:resource][:uuid] != @resource.uuid
      flash.now[:error] = "UUID cannot be changed."
      render :edit, status: :unprocessable_entity and return
    end

    params[:resource].each do |prop|
      next if %w(longitude latitude).include?(prop.first.to_s)
      if prop.last.present? && prop.last != @resource.send("#{prop.first.to_s}")
        updated = true
        updated_properties[prop.first.to_s] = prop.last
      end
    end

    lon, lat = updated_geometry['coordinates']
    flash.now.notice = "lon #{lon} => #{params[:resource][:longitude]}, lat #{lat} =>#{params[:resource][:latitude]}"
    if params[:resource][:longitude].present? && params[:resource][:longitude] != lon
      updated = true
      updated_geometry['coordinates'][0] = params[:resource][:longitude]
      Rails.logger.info "UPDATED LON TO #{params[:resource][:longitude]} /// #{updated_geometry.inspect}"
    end
    if params[:resource][:latitude].present? && params[:resource][:latitude] != lat
      updated = true
      updated_geometry['coordinates'][1] = params[:resource][:latitude]
    end

    if updated == true
      Rails.logger.info "Got a Resource update..."
      @resource.feature['properties'] = updated_properties
      @resource.feature['geometry']   = updated_geometry
      if @resource.save
        flash.notice = "Good! It worked! #{@resource.feature['geometry']}"
        redirect_to resource_url(@resource) and return
      else
        flash.now[:notice] = @resource.errors.inspect
      end
    end
    flash.now[:alert] = "Update failed!"
    render :edit
  end

  # GET /resources/:id/delete
  def delete
  end

  # DELETE /resources/:id
  def destroy
    # Check list
    # 1. User belongs to Agent and is :maintainer?
  end

  private

  def resource_params
    params.require(:resource).permit(:uuid, :name, :summary, :description, :email, :city, :postal_code, :phone_number, :website, :entry_number, :categories, :latitude, :longitude)
  end

  def set_resource
    @resource = Resource.find_by(uuid: params[:id]) || Resource.new
  end
end