
/**
 *  nechceme mazat z databaze guest zakaznika, ani ho upravovat
 */

IF OBJECT_ID ('FARKAF_sch.trg_guest','TR') IS NOT NULL
   DROP TRIGGER FARKAF_sch.trg_guest;

GO
CREATE TRIGGER FARKAF_sch.trg_guest ON FARKAF_sch.registered
FOR DELETE, UPDATE
AS
BEGIN
	IF EXISTS (	SELECT * FROM deleted WHERE id = 0)
	BEGIN
		RAISERROR ('quest user cannot be deleted nor modified', 10, 0);
		ROLLBACK TRANSACTION;
		RETURN;
	END;
END;
GO

/**
 *  take zajistime automaticke kopirovani
 *  zmenenych rezervaci do historie
 */

IF OBJECT_ID ('FARKAF_sch.trg_reservation_history','TR') IS NOT NULL
   DROP TRIGGER FARKAF_sch.trg_reservation_history;

GO
CREATE TRIGGER FARKAF_sch.trg_reservation_history ON reservation
AFTER INSERT, UPDATE, DELETE
AS
BEGIN
	INSERT INTO reservation_history(
		id_reservation,
		first_name,
		last_name,
		street,
		city,
		postal_code,
		created,
		pending,
		id_registered,
		total_price)
	SELECT
		id,
		first_name,
		last_name,
		street,
		city,
		postal_code,
		created,
		pending,
		id_registered,
		total_price
	FROM inserted;
	INSERT INTO reservation_history(
		id_reservation,
		first_name,
		last_name,
		street,
		city,
		postal_code,
		created,
		pending,
		id_registered,
		total_price)
	SELECT
		id,
		first_name,
		last_name,
		street,
		city,
		postal_code,
		created,
		pending,
		id_registered,
		total_price
	FROM deleted;
END;

