/*
declare @id int
    set @id = 7
    delete from MotorAccess where mv_id = @id
    delete from [dbo].[MotorVehicle]
    where mv_id = @id
*/


CREATE OR REPLACE FUNCTION public.usp_search_user(
	iemail character varying,
	ifirstname character varying,
	ilastname character varying)
    RETURNS TABLE(id integer, fistname character varying, lastname character varying, email character varying, 
       stype integer, sactive boolean, srole integer, spassword character varying) 
 
AS $BODY$
begin
	return query SELECT
		e.id, 
        e.firstname,
        e.lastname,
        e.email,
        e.stype,
        e.sactive,
		e.srole,
		e.spassword
       
       

	FROM  staff e		
	WHERE (iemail = '' or e.email ilike iemail || '%')
	and (ilastname = '' or e.lastname ilike ilastname || '%')
	and (ifirstname = '' or e.firstname ilike ifirstname || '%');

end; 
$BODY$;

CREATE OR REPLACE FUNCTION get_staff (
       iemail character varying,
	ifirstname character varying,
	ilastname character varying
       ) 
    RETURNS TABLE (
        id int,        
        firstname varchar,
        lastname varchar,
        email varchar,
        stype INT,
        sactive boolean,
		srole INT,
		spassword varchar ) 
AS $$
BEGIN
RETURN QUERY
   e.id,
     e.firstname,
     e.lastname,
     e.email,
     e.stype,
     e.sactive,
     e.srole,
     e.spassword
 FROM  staff e
 WHERE (iemail = '' or e.email ilike iemail || '%')
   and (ilastname = '' or e.lastname ilike ilastname || '%')
   and (ifirstname = '' or e.firstname ilike ifirstname || '%');
END; $$

LANGUAGE 'plpgsql';


how to host under .net 8 web api under linux

1) create [webAPI_name].service
******************************************
# Example: /etc/systemd/system/yourwebapp.service
    [Unit]
    Description=Your .NET 8 Web API
    After=network.target

    [Service]
    WorkingDirectory=/var/www/yourwebapp
    ExecStart=/usr/bin/dotnet /var/www/yourwebapp/YourWebApi.dll
    Restart=always
    RestartSec=10
    SyslogIdentifier=yourwebapp
    User=www-data # or a dedicated user
    Environment=ASPNETCORE_ENVIRONMENT=Production

    [Install]
    WantedBy=multi-user.target
************************************************
2) start /stop the service under linux
***********************************************
    sudo systemctl enable yourwebapp.service
    sudo systemctl start yourwebapp.service
**********************************************

    3) host under nginx or apache
   ************************************************
            server {
            listen 80;
            server_name yourdomain.com;

            location / {
                proxy_pass http://localhost:5000; # Kestrel's listening port
                proxy_set_header Host $host;
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_set_header X-Forwarded-Proto $scheme;
            }
        }
***********************************************************************
    