fix enregistrementd'un rapport de checks
This commit is contained in:
parent
5450bc7c37
commit
3693efd572
|
@ -2,17 +2,18 @@ class Api::V1::CheckReportsController < Api::V1::BaseController
|
||||||
|
|
||||||
# POST /check-reports or /check-reports.json
|
# POST /check-reports or /check-reports.json
|
||||||
def create
|
def create
|
||||||
|
|
||||||
@check_report = CheckReport.new(check_report_params)
|
@check_report = CheckReport.new(check_report_params)
|
||||||
|
|
||||||
if @check_report.checks.empty?
|
if @check_report.checks.empty?
|
||||||
render json: { message: "At least 1 check must be present" }, status: :unprocessable_entity
|
render json: { message: "At least 1 check must be present" }, status: :unprocessable_entity
|
||||||
else
|
else
|
||||||
checks_params = ActionController::Parameters.new(@check_report.checks).permit(:name, :description).map { |check|
|
checks_params = @check_report.checks.map { |check|
|
||||||
{
|
{
|
||||||
hostname: @check_report.hostname,
|
hostname: @check_report.hostname,
|
||||||
date: @check_report.date,
|
date: @check_report.date,
|
||||||
name: check.fetch(:name, ""),
|
name: check.fetch("name", ""),
|
||||||
description: check.fetch(:description, "")
|
description: check.fetch("description", "")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
checks = Check.create(checks_params)
|
checks = Check.create(checks_params)
|
||||||
|
@ -20,7 +21,7 @@ class Api::V1::CheckReportsController < Api::V1::BaseController
|
||||||
persisted = checks.count(&:persisted?)
|
persisted = checks.count(&:persisted?)
|
||||||
invalid = checks.count(&:invalid?)
|
invalid = checks.count(&:invalid?)
|
||||||
|
|
||||||
if checks.all?(:persisted?)
|
if checks.all?(&:persisted?)
|
||||||
render json: { message: "#{persisted} checks created" }, status: :created
|
render json: { message: "#{persisted} checks created" }, status: :created
|
||||||
else
|
else
|
||||||
render json: { message: "error while creating checks : #{persisted} persisted, #{invalid} invalid" }, status: :unprocessable_entity
|
render json: { message: "error while creating checks : #{persisted} persisted, #{invalid} invalid" }, status: :unprocessable_entity
|
||||||
|
|
Loading…
Reference in a new issue