Class: Api::V1::JobsController

Inherits:
BaseController show all
Includes:
Cacheable, Paginatable
Defined in:
app/controllers/api/v1/jobs_controller.rb

Constant Summary

Constants included from Paginatable

Paginatable::DEFAULT_PER_PAGE, Paginatable::MAX_PER_PAGE

Instance Method Summary collapse

Methods included from Cacheable

#invalidate_cache

Methods included from Paginatable

#current_page, #pagination_meta, #per_page

Instance Method Details

#createJSON

POST /api/v1/jobs Membuat job baru

Parameters:

  • job (Hash)

    Atribut job (title, description, status, user_id)

Returns:

  • (JSON)

    Job yang berhasil dibuat



54
55
56
57
58
# File 'app/controllers/api/v1/jobs_controller.rb', line 54

def create
  new_job = Job.create!(job_params)
  invalidate_cache("jobs", skip_show: true)
  render_success(new_job, status: :created, serializer: JobSerializer)
end

#destroy204 No Content

DELETE /api/v1/jobs/:id Menghapus sebuah job berdasarkan ID

Returns:

  • (204 No Content)


73
74
75
76
77
# File 'app/controllers/api/v1/jobs_controller.rb', line 73

def destroy
  @job.destroy!
  invalidate_cache("jobs", id: @job.id) # invalidate index + show cache per ID
  head :no_content
end

#indexJSON

GET /api/v1/jobs Mengambil daftar job dengan opsi filter dan pagination.

Parameters:

  • params (Hash)

    a customizable set of options

Returns:

  • (JSON)

    Daftar job yang sudah dipaginasi dan difilter



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'app/controllers/api/v1/jobs_controller.rb', line 15

def index
  jobs_scope     = filter_jobs.order(created_at: :desc)
  paginated_jobs = jobs_scope.page(current_page).per(per_page)

  cache_key = ActiveSupport::Cache.expand_cache_key([
    "jobs/index",
    "user-#{filter_params || 'all'}",
    "updated-#{jobs_scope.maximum(:updated_at)&.to_i || 0}",
    "page-#{current_page}",
    "per_page-#{per_page}" ])

  serialized_jobs = Rails.cache.fetch(cache_key, expires_in: 1.hour) do
    ActiveModelSerializers::SerializableResource.new(
      paginated_jobs,
      each_serializer: JobSerializer
    ).as_json
  end

  render_success(
    serialized_jobs,
    meta: pagination_meta(paginated_jobs)
  )
end

#showJSON

GET /api/v1/jobs/:id Menampilkan detail dari sebuah job berdasarkan ID

Returns:

  • (JSON)

    Job detail



42
43
44
45
46
47
48
# File 'app/controllers/api/v1/jobs_controller.rb', line 42

def show
  cache_key = "jobs/show/#{@job.id}/updated-#{@job.updated_at.to_i}"
  job_data = Rails.cache.fetch(cache_key, expires_in: 1.hour) do
    JobSerializer.new(@job).as_json
  end
  render_success(job_data)
end

#updateJSON

PATCH/PUT /api/v1/jobs/:id Mengupdate atribut job tertentu

Parameters:

  • job (Hash)

    Atribut job yang ingin diubah

Returns:

  • (JSON)

    Job yang berhasil diperbarui



64
65
66
67
68
# File 'app/controllers/api/v1/jobs_controller.rb', line 64

def update
  @job.update!(job_params)
  invalidate_cache("jobs", id: @job.id) # invalidate index + show cache per ID
  render_success(@job, serializer: JobSerializer)
end