--
-- PostgreSQL database dump
--

\restrict rWgv9Lrdp6aUlx4m78Q5taMiGZPMYSi2kZvE4Yoeg8bEzxclehA1eKWafZgUpuj

-- Dumped from database version 18.4
-- Dumped by pg_dump version 18.4

-- Started on 2026-05-25 22:26:40

SET statement_timeout = 0;
SET lock_timeout = 0;
SET idle_in_transaction_session_timeout = 0;
SET transaction_timeout = 0;
SET client_encoding = 'UTF8';
SET standard_conforming_strings = on;
SELECT pg_catalog.set_config('search_path', '', false);
SET check_function_bodies = false;
SET xmloption = content;
SET client_min_messages = warning;
SET row_security = off;

--
-- TOC entry 6 (class 2615 OID 16816)
-- Name: nn_db; Type: SCHEMA; Schema: -; Owner: postgres
--

CREATE SCHEMA nn_db;


ALTER SCHEMA nn_db OWNER TO postgres;

--
-- TOC entry 247 (class 1255 OID 16922)
-- Name: application_get(integer); Type: FUNCTION; Schema: nn_db; Owner: postgres
--

CREATE FUNCTION nn_db.application_get(in_pk_user integer) RETURNS TABLE(pk_application integer, car_number character varying, description character varying, fk_status integer, title character varying, fk_user integer, username character varying, pk_image integer, file_name character varying, file_ext character varying)
    LANGUAGE plpgsql
    AS $$
BEGIN
	RETURN QUERY SELECT 
		nn_db.application.pk_application
		, nn_db.application.car_number
		, nn_db.application.description
		, nn_db.application.fk_status
		, nn_db.status.title
		, nn_db.application.fk_user
		, nn_db.user.username
		, nn_db.image.pk_image
		, nn_db.image.file_name
		, nn_db.image.file_ext
	FROM nn_db.application
	INNER JOIN nn_db.status 
		ON nn_db.status.pk_status = nn_db.application.fk_status
	INNER JOIN nn_db.user
		ON nn_db.user.pk_user = nn_db.application.fk_user
	LEFT JOIN nn_db.image
		ON nn_db.image.fk_application = nn_db.application.pk_application
	WHERE 
		CASE WHEN in_pk_user IS NULL
			THEN nn_db.application.fk_user IN (SELECT nn_db.user.pk_user FROM nn_db.user)
		ELSE
			nn_db.application.fk_user = in_pk_user
		END
	ORDER BY nn_db.application.pk_application;
END;
$$;


ALTER FUNCTION nn_db.application_get(in_pk_user integer) OWNER TO postgres;

--
-- TOC entry 229 (class 1255 OID 16914)
-- Name: application_patch(integer, integer); Type: FUNCTION; Schema: nn_db; Owner: postgres
--

CREATE FUNCTION nn_db.application_patch(in_pk_application integer, in_fk_status integer) RETURNS TABLE(confirm boolean)
    LANGUAGE plpgsql
    AS $$
BEGIN
	UPDATE nn_db.application
		SET nn_db.application.fk_status = in_fk_status
	WHERE
		nn_db.application.pk_application = in_pk_application;
		
	RETURN QUERY SELECT (	
		CASE WHEN 
			(SELECT fk_status FROM nn_db.application WHERE pk_application = in_pk_application LIMIT 1) = in_fk_status
		THEN True
		ELSE False
		END);
END;
$$;


ALTER FUNCTION nn_db.application_patch(in_pk_application integer, in_fk_status integer) OWNER TO postgres;

--
-- TOC entry 244 (class 1255 OID 16908)
-- Name: application_post(character varying, character varying, integer); Type: FUNCTION; Schema: nn_db; Owner: postgres
--

CREATE FUNCTION nn_db.application_post(in_car_number character varying, in_description character varying, in_fk_user integer) RETURNS TABLE(pk_application integer)
    LANGUAGE plpgsql
    AS $$
BEGIN
	INSERT INTO nn_db.application (
		car_number
		, description
		, fk_status
		, fk_user)
	VALUES (
		in_car_number
		, in_description
		, (SELECT MIN(nn_db.status.pk_status) FROM nn_db.status)
		, in_fk_user);
	RETURN QUERY SELECT 
		MAX(nn_db.application.pk_application) 
	FROM nn_db.application;
END;
$$;


ALTER FUNCTION nn_db.application_post(in_car_number character varying, in_description character varying, in_fk_user integer) OWNER TO postgres;

--
-- TOC entry 246 (class 1255 OID 16917)
-- Name: image_post(character varying, character varying, integer); Type: FUNCTION; Schema: nn_db; Owner: postgres
--

CREATE FUNCTION nn_db.image_post(in_file_name character varying, in_file_ext character varying, in_fk_application integer) RETURNS TABLE(pk_image integer)
    LANGUAGE plpgsql
    AS $$
BEGIN
	INSERT INTO nn_db.image (file_name, file_ext, fk_application)
	VALUES (in_file_name, in_file_ext, in_fk_application);
	RETURN QUERY SELECT MAX(nn_db.image.pk_image) FROM nn_db.image;
END;
$$;


ALTER FUNCTION nn_db.image_post(in_file_name character varying, in_file_ext character varying, in_fk_application integer) OWNER TO postgres;

--
-- TOC entry 230 (class 1255 OID 16915)
-- Name: status_get(); Type: FUNCTION; Schema: nn_db; Owner: postgres
--

CREATE FUNCTION nn_db.status_get() RETURNS TABLE(pk_status integer, title character varying)
    LANGUAGE plpgsql
    AS $$
BEGIN
	RETURN QUERY SELECT 
		nn_db.status.pk_status, nn_db.status.title 
	FROM nn_db.status
	ORDER BY nn_db.status.pk_status;
END;
$$;


ALTER FUNCTION nn_db.status_get() OWNER TO postgres;

--
-- TOC entry 241 (class 1255 OID 16916)
-- Name: status_post(character varying); Type: FUNCTION; Schema: nn_db; Owner: postgres
--

CREATE FUNCTION nn_db.status_post(in_title character varying) RETURNS TABLE(pk_status integer)
    LANGUAGE plpgsql
    AS $$
BEGIN
	INSERT INTO nn_db.status (title)
	VALUES (in_title);

	RETURN QUERY SELECT MAX(nn_db.status.pk_status) FROM nn_db.status;
END;
$$;


ALTER FUNCTION nn_db.status_post(in_title character varying) OWNER TO postgres;

--
-- TOC entry 228 (class 1255 OID 16921)
-- Name: user_activate(integer); Type: FUNCTION; Schema: nn_db; Owner: postgres
--

CREATE FUNCTION nn_db.user_activate(in_pk_user integer) RETURNS TABLE(confirm boolean)
    LANGUAGE plpgsql
    AS $$
BEGIN
	UPDATE nn_db.user
		SET is_active = True
	WHERE
		pk_user = in_pk_user;
	RETURN QUERY SELECT 
		is_active = True 
	FROM nn_db.user
	WHERE
		nn_db.user.pk_user = in_pk_user;
END;
$$;


ALTER FUNCTION nn_db.user_activate(in_pk_user integer) OWNER TO postgres;

--
-- TOC entry 243 (class 1255 OID 16920)
-- Name: user_get(character varying); Type: FUNCTION; Schema: nn_db; Owner: postgres
--

CREATE FUNCTION nn_db.user_get(in_search_value character varying) RETURNS TABLE(pk_user integer, lastname character varying, firstname character varying, middlename character varying, username character varying, password_hash character varying, email character varying, phone character varying, is_admin boolean, is_active boolean)
    LANGUAGE plpgsql
    AS $$
BEGIN
	RETURN QUERY SELECT 
		nn_db.user.pk_user
		, nn_db.user.lastname
		, nn_db.user.firstname
		, nn_db.user.middlename
		, nn_db.user.username
		, nn_db.user.password_hash
		, nn_db.user.email
		, nn_db.user.phone
		, nn_db.user.is_admin
		, nn_db.user.is_active
	FROM nn_db.user
	WHERE
		nn_db.user.username ILIKE in_search_value
		OR nn_db.user.email ILIKE in_search_value;
END;
$$;


ALTER FUNCTION nn_db.user_get(in_search_value character varying) OWNER TO postgres;

--
-- TOC entry 245 (class 1255 OID 16919)
-- Name: user_post(character varying, character varying, character varying, character varying, character varying, character varying, character varying); Type: FUNCTION; Schema: nn_db; Owner: postgres
--

CREATE FUNCTION nn_db.user_post(in_lastname character varying, in_firstname character varying, in_middlename character varying, in_username character varying, in_password_hash character varying, in_email character varying, in_phone character varying) RETURNS TABLE(pk_user integer)
    LANGUAGE plpgsql
    AS $$
BEGIN
	INSERT INTO nn_db.user(
		lastname
		, firstname
		, middlename
		, username
		, password_hash
		, email
		, phone
		, is_admin
		, is_active)
	VALUES(
		in_lastname
		, in_firstname
		, in_middlename
		, in_username
		, in_password_hash
		, in_email
		, in_phone
		, False
		, False);
		
	RETURN QUERY SELECT MAX(nn_db.user.pk_user) FROM nn_db.user;
END;
$$;


ALTER FUNCTION nn_db.user_post(in_lastname character varying, in_firstname character varying, in_middlename character varying, in_username character varying, in_password_hash character varying, in_email character varying, in_phone character varying) OWNER TO postgres;

SET default_tablespace = '';

SET default_table_access_method = heap;

--
-- TOC entry 225 (class 1259 OID 16848)
-- Name: application; Type: TABLE; Schema: nn_db; Owner: postgres
--

CREATE TABLE nn_db.application (
    pk_application integer NOT NULL,
    car_number character varying(12) NOT NULL,
    description character varying(255) NOT NULL,
    fk_status integer NOT NULL,
    fk_user integer NOT NULL
);


ALTER TABLE nn_db.application OWNER TO postgres;

--
-- TOC entry 224 (class 1259 OID 16847)
-- Name: application_pk_application_seq; Type: SEQUENCE; Schema: nn_db; Owner: postgres
--

CREATE SEQUENCE nn_db.application_pk_application_seq
    AS integer
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;


ALTER SEQUENCE nn_db.application_pk_application_seq OWNER TO postgres;

--
-- TOC entry 5011 (class 0 OID 0)
-- Dependencies: 224
-- Name: application_pk_application_seq; Type: SEQUENCE OWNED BY; Schema: nn_db; Owner: postgres
--

ALTER SEQUENCE nn_db.application_pk_application_seq OWNED BY nn_db.application.pk_application;


--
-- TOC entry 227 (class 1259 OID 16865)
-- Name: image; Type: TABLE; Schema: nn_db; Owner: postgres
--

CREATE TABLE nn_db.image (
    pk_image integer NOT NULL,
    file_name character varying(60) NOT NULL,
    file_ext character varying(5) NOT NULL,
    fk_application integer NOT NULL
);


ALTER TABLE nn_db.image OWNER TO postgres;

--
-- TOC entry 226 (class 1259 OID 16864)
-- Name: image_pk_image_seq; Type: SEQUENCE; Schema: nn_db; Owner: postgres
--

CREATE SEQUENCE nn_db.image_pk_image_seq
    AS integer
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;


ALTER SEQUENCE nn_db.image_pk_image_seq OWNER TO postgres;

--
-- TOC entry 5012 (class 0 OID 0)
-- Dependencies: 226
-- Name: image_pk_image_seq; Type: SEQUENCE OWNED BY; Schema: nn_db; Owner: postgres
--

ALTER SEQUENCE nn_db.image_pk_image_seq OWNED BY nn_db.image.pk_image;


--
-- TOC entry 223 (class 1259 OID 16839)
-- Name: status; Type: TABLE; Schema: nn_db; Owner: postgres
--

CREATE TABLE nn_db.status (
    pk_status integer NOT NULL,
    title character varying(60) NOT NULL
);


ALTER TABLE nn_db.status OWNER TO postgres;

--
-- TOC entry 222 (class 1259 OID 16838)
-- Name: status_pk_status_seq; Type: SEQUENCE; Schema: nn_db; Owner: postgres
--

CREATE SEQUENCE nn_db.status_pk_status_seq
    AS integer
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;


ALTER SEQUENCE nn_db.status_pk_status_seq OWNER TO postgres;

--
-- TOC entry 5013 (class 0 OID 0)
-- Dependencies: 222
-- Name: status_pk_status_seq; Type: SEQUENCE OWNED BY; Schema: nn_db; Owner: postgres
--

ALTER SEQUENCE nn_db.status_pk_status_seq OWNED BY nn_db.status.pk_status;


--
-- TOC entry 221 (class 1259 OID 16819)
-- Name: user; Type: TABLE; Schema: nn_db; Owner: postgres
--

CREATE TABLE nn_db."user" (
    pk_user integer NOT NULL,
    lastname character varying(60) NOT NULL,
    firstname character varying(60) NOT NULL,
    middlename character varying(60),
    username character varying(60) NOT NULL,
    password_hash character varying(255) NOT NULL,
    email character varying(60) NOT NULL,
    phone character varying(12) NOT NULL,
    is_admin boolean DEFAULT false NOT NULL,
    is_active boolean DEFAULT false NOT NULL
);


ALTER TABLE nn_db."user" OWNER TO postgres;

--
-- TOC entry 220 (class 1259 OID 16818)
-- Name: user_pk_user_seq; Type: SEQUENCE; Schema: nn_db; Owner: postgres
--

CREATE SEQUENCE nn_db.user_pk_user_seq
    AS integer
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;


ALTER SEQUENCE nn_db.user_pk_user_seq OWNER TO postgres;

--
-- TOC entry 5014 (class 0 OID 0)
-- Dependencies: 220
-- Name: user_pk_user_seq; Type: SEQUENCE OWNED BY; Schema: nn_db; Owner: postgres
--

ALTER SEQUENCE nn_db.user_pk_user_seq OWNED BY nn_db."user".pk_user;


--
-- TOC entry 4838 (class 2604 OID 16851)
-- Name: application pk_application; Type: DEFAULT; Schema: nn_db; Owner: postgres
--

ALTER TABLE ONLY nn_db.application ALTER COLUMN pk_application SET DEFAULT nextval('nn_db.application_pk_application_seq'::regclass);


--
-- TOC entry 4839 (class 2604 OID 16868)
-- Name: image pk_image; Type: DEFAULT; Schema: nn_db; Owner: postgres
--

ALTER TABLE ONLY nn_db.image ALTER COLUMN pk_image SET DEFAULT nextval('nn_db.image_pk_image_seq'::regclass);


--
-- TOC entry 4837 (class 2604 OID 16842)
-- Name: status pk_status; Type: DEFAULT; Schema: nn_db; Owner: postgres
--

ALTER TABLE ONLY nn_db.status ALTER COLUMN pk_status SET DEFAULT nextval('nn_db.status_pk_status_seq'::regclass);


--
-- TOC entry 4834 (class 2604 OID 16822)
-- Name: user pk_user; Type: DEFAULT; Schema: nn_db; Owner: postgres
--

ALTER TABLE ONLY nn_db."user" ALTER COLUMN pk_user SET DEFAULT nextval('nn_db.user_pk_user_seq'::regclass);


--
-- TOC entry 5003 (class 0 OID 16848)
-- Dependencies: 225
-- Data for Name: application; Type: TABLE DATA; Schema: nn_db; Owner: postgres
--

COPY nn_db.application (pk_application, car_number, description, fk_status, fk_user) FROM stdin;
2	В925ОУ51	Нарушение правил парковки	1	1
3	К855МК51	Пересечение сплошной линии	1	1
4	Р355РУ86	Пересечение сплошной линии	1	2
7	О000О899	Тестовое описание	1	1
8	А952АА51	Тестовое описание 2	1	3
9	D123DD132	123123	1	1
10	L999ll00	123123321	1	2
\.


--
-- TOC entry 5005 (class 0 OID 16865)
-- Dependencies: 227
-- Data for Name: image; Type: TABLE DATA; Schema: nn_db; Owner: postgres
--

COPY nn_db.image (pk_image, file_name, file_ext, fk_application) FROM stdin;
3	file1	png	2
4	file2	png	2
5	file3	png	2
6	file3	png	3
7	file4	jpeg	3
\.


--
-- TOC entry 5001 (class 0 OID 16839)
-- Dependencies: 223
-- Data for Name: status; Type: TABLE DATA; Schema: nn_db; Owner: postgres
--

COPY nn_db.status (pk_status, title) FROM stdin;
1	Новое
2	Подтверждено
3	Отклонено
\.


--
-- TOC entry 4999 (class 0 OID 16819)
-- Dependencies: 221
-- Data for Name: user; Type: TABLE DATA; Schema: nn_db; Owner: postgres
--

COPY nn_db."user" (pk_user, lastname, firstname, middlename, username, password_hash, email, phone, is_admin, is_active) FROM stdin;
1	Петров	Иван	Витальевич	IVPetrov	some_password_hash_that_more_length	ivpetrov@ya.ru	89021234567	f	t
2	Ковров	Михаил	Сергеевич	MSKovrov	some_password_hash	MSKovrov@mail.ru	+79009282828	f	t
3	Шулимов	Александр	Андреевич	AAShulimov	pbkdf2_sha256$1200000$extra$0E3KdFr8rKF1iaeEckwdMEla54AXR7xvs7hPPIMMFJE=	AAShulimov@mail.ru	89021377664	f	f
\.


--
-- TOC entry 5015 (class 0 OID 0)
-- Dependencies: 224
-- Name: application_pk_application_seq; Type: SEQUENCE SET; Schema: nn_db; Owner: postgres
--

SELECT pg_catalog.setval('nn_db.application_pk_application_seq', 11, true);


--
-- TOC entry 5016 (class 0 OID 0)
-- Dependencies: 226
-- Name: image_pk_image_seq; Type: SEQUENCE SET; Schema: nn_db; Owner: postgres
--

SELECT pg_catalog.setval('nn_db.image_pk_image_seq', 8, true);


--
-- TOC entry 5017 (class 0 OID 0)
-- Dependencies: 222
-- Name: status_pk_status_seq; Type: SEQUENCE SET; Schema: nn_db; Owner: postgres
--

SELECT pg_catalog.setval('nn_db.status_pk_status_seq', 3, false);


--
-- TOC entry 5018 (class 0 OID 0)
-- Dependencies: 220
-- Name: user_pk_user_seq; Type: SEQUENCE SET; Schema: nn_db; Owner: postgres
--

SELECT pg_catalog.setval('nn_db.user_pk_user_seq', 5, true);


--
-- TOC entry 4845 (class 2606 OID 16858)
-- Name: application application_pkey; Type: CONSTRAINT; Schema: nn_db; Owner: postgres
--

ALTER TABLE ONLY nn_db.application
    ADD CONSTRAINT application_pkey PRIMARY KEY (pk_application);


--
-- TOC entry 4847 (class 2606 OID 16874)
-- Name: image image_pkey; Type: CONSTRAINT; Schema: nn_db; Owner: postgres
--

ALTER TABLE ONLY nn_db.image
    ADD CONSTRAINT image_pkey PRIMARY KEY (pk_image);


--
-- TOC entry 4843 (class 2606 OID 16846)
-- Name: status status_pkey; Type: CONSTRAINT; Schema: nn_db; Owner: postgres
--

ALTER TABLE ONLY nn_db.status
    ADD CONSTRAINT status_pkey PRIMARY KEY (pk_status);


--
-- TOC entry 4841 (class 2606 OID 16837)
-- Name: user user_pkey; Type: CONSTRAINT; Schema: nn_db; Owner: postgres
--

ALTER TABLE ONLY nn_db."user"
    ADD CONSTRAINT user_pkey PRIMARY KEY (pk_user);


--
-- TOC entry 4848 (class 2606 OID 16890)
-- Name: application application_fk_status_fkey; Type: FK CONSTRAINT; Schema: nn_db; Owner: postgres
--

ALTER TABLE ONLY nn_db.application
    ADD CONSTRAINT application_fk_status_fkey FOREIGN KEY (fk_status) REFERENCES nn_db.status(pk_status) ON UPDATE CASCADE ON DELETE SET NULL NOT VALID;


--
-- TOC entry 4849 (class 2606 OID 16895)
-- Name: application application_fk_user_fkey; Type: FK CONSTRAINT; Schema: nn_db; Owner: postgres
--

ALTER TABLE ONLY nn_db.application
    ADD CONSTRAINT application_fk_user_fkey FOREIGN KEY (fk_user) REFERENCES nn_db."user"(pk_user) ON UPDATE CASCADE ON DELETE SET NULL NOT VALID;


--
-- TOC entry 4850 (class 2606 OID 16900)
-- Name: image image_fk_application_fkey; Type: FK CONSTRAINT; Schema: nn_db; Owner: postgres
--

ALTER TABLE ONLY nn_db.image
    ADD CONSTRAINT image_fk_application_fkey FOREIGN KEY (fk_application) REFERENCES nn_db.application(pk_application) ON UPDATE CASCADE ON DELETE CASCADE NOT VALID;


-- Completed on 2026-05-25 22:26:40

--
-- PostgreSQL database dump complete
--

\unrestrict rWgv9Lrdp6aUlx4m78Q5taMiGZPMYSi2kZvE4Yoeg8bEzxclehA1eKWafZgUpuj

