aonestar
.public
Tables
(current)
Columns
Constraints
Relationships
Orphan Tables
Anomalies
Routines
sp_save_calendar_event
Parameters
Name
Type
Mode
p_data
jsonb
IN
Definition
DECLARE v_state text; v_message text; v_detail text; v_hint text; v_context text; v_rec record; BEGIN SELECT * INTO v_rec FROM jsonb_to_record(p_data) AS t( id int, calendar_id int, name text, start_date date, end_date date, all_day boolean, start_time time, end_time time, color text, remark text, enabled boolean); IF NOT EXISTS (SELECT 1 FROM public.calendar WHERE id = v_rec.calendar_id) THEN RETURN fn_result_error('50503', sys_msg('50503', 'Calendar not found (%s)'), coalesce(v_rec.calendar_id::text, '')); END IF; IF v_rec.end_date IS NOT NULL AND v_rec.end_date < v_rec.start_date THEN RETURN fn_result_error('50506', sys_msg('50506', 'Start date must not be after end date')); END IF; IF v_rec.id IS NULL OR v_rec.id < 1 THEN INSERT INTO public.calendar_event(calendar_id, name, start_date, end_date, all_day, start_time, end_time, color, remark, enabled) VALUES (v_rec.calendar_id, v_rec.name, v_rec.start_date, v_rec.end_date, coalesce(v_rec.all_day, true), v_rec.start_time, v_rec.end_time, nullif(v_rec.color, ''), v_rec.remark, coalesce(v_rec.enabled, true)) RETURNING id INTO v_rec.id; ELSE UPDATE public.calendar_event SET calendar_id = v_rec.calendar_id, name = v_rec.name, start_date = v_rec.start_date, end_date = v_rec.end_date, all_day = coalesce(v_rec.all_day, all_day), start_time = v_rec.start_time, end_time = v_rec.end_time, color = nullif(v_rec.color, ''), remark = v_rec.remark, enabled = coalesce(v_rec.enabled, enabled) WHERE id = v_rec.id; IF NOT FOUND THEN RETURN fn_result_error('50505', sys_msg('50505', 'Calendar event not found (%s)'), v_rec.id::text); END IF; END IF; RETURN fn_result_success((SELECT to_json(e) FROM public.calendar_event e WHERE e.id = v_rec.id)); EXCEPTION WHEN OTHERS THEN GET STACKED DIAGNOSTICS v_state = RETURNED_SQLSTATE, v_message = MESSAGE_TEXT, v_detail = PG_EXCEPTION_DETAIL, v_hint = PG_EXCEPTION_HINT, v_context = PG_EXCEPTION_CONTEXT; RETURN fn_handle_error(v_state, v_message, v_detail, v_hint, v_context, 'public.sp_save_calendar_event', p_data); END