Class: Api::V1::JobsController
- Inherits:
-
BaseController
- Object
- ActionController::API
- ApplicationController
- BaseController
- Api::V1::JobsController
- 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
-
#create ⇒ JSON
POST /api/v1/jobs Membuat job baru.
-
#destroy ⇒ 204 No Content
DELETE /api/v1/jobs/:id Menghapus sebuah job berdasarkan ID.
-
#index ⇒ JSON
GET /api/v1/jobs Mengambil daftar job dengan opsi filter dan pagination.
-
#show ⇒ JSON
GET /api/v1/jobs/:id Menampilkan detail dari sebuah job berdasarkan ID.
-
#update ⇒ JSON
PATCH/PUT /api/v1/jobs/:id Mengupdate atribut job tertentu.
Methods included from Cacheable
Methods included from Paginatable
#current_page, #pagination_meta, #per_page
Instance Method Details
#create ⇒ JSON
POST /api/v1/jobs Membuat job baru
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 |
#destroy ⇒ 204 No Content
DELETE /api/v1/jobs/:id Menghapus sebuah job berdasarkan ID
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 |
#index ⇒ JSON
GET /api/v1/jobs Mengambil daftar job dengan opsi filter dan pagination.
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.([ "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: (paginated_jobs) ) end |
#show ⇒ JSON
GET /api/v1/jobs/:id Menampilkan detail dari sebuah job berdasarkan ID
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 |
#update ⇒ JSON
PATCH/PUT /api/v1/jobs/:id Mengupdate atribut job tertentu
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 |