From dd200f01388287ca12ffae0a9709d838b06a03ba Mon Sep 17 00:00:00 2001 From: Franklin Date: Mon, 8 May 2023 14:28:19 -0400 Subject: [PATCH] Added daos --- migrations/7_property_arrangement.sql | 2 +- sql/property/fetch_with_realtor_id_paged.sql | 55 +- ...erty_id.sql => fetch_with_property_id.sql} | 2 +- .../fetch_with_property_ids.sql | 9 + sql/property_arrangement/insert.sql | 4 +- sql/property_arrangement/update.sql | 4 +- sqlx-data.json | 1000 +++++++++++------ src/dao/mod.rs | 2 + src/dao/property.rs | 45 +- src/dao/property_arrangement.rs | 63 ++ src/dao/view.rs | 7 +- src/routes/property.rs | 8 +- src/routes/view.rs | 10 +- 13 files changed, 810 insertions(+), 401 deletions(-) rename sql/property_arrangement/{get_with_property_id.sql => fetch_with_property_id.sql} (78%) create mode 100644 sql/property_arrangement/fetch_with_property_ids.sql create mode 100644 src/dao/property_arrangement.rs diff --git a/migrations/7_property_arrangement.sql b/migrations/7_property_arrangement.sql index 1b6f0cf..57504ef 100644 --- a/migrations/7_property_arrangement.sql +++ b/migrations/7_property_arrangement.sql @@ -1,6 +1,6 @@ CREATE TABLE IF NOT EXISTS "property_arrangement" ( property_id UUID PRIMARY KEY, - price_usd FLOAT8 NOT NULL, + price FLOAT8 NOT NULL, currency VARCHAR NOT NULL, arrangement VARCHAR NOT NULL, time_created TIMESTAMPTZ NOT NULL, diff --git a/sql/property/fetch_with_realtor_id_paged.sql b/sql/property/fetch_with_realtor_id_paged.sql index cf64cbd..3efc08c 100644 --- a/sql/property/fetch_with_realtor_id_paged.sql +++ b/sql/property/fetch_with_realtor_id_paged.sql @@ -1,22 +1,37 @@ SELECT - id, - project_id, - realtor_id, - media as "media: _", - property_type as "property_type: _", - property_sale_type as "property_sale_type: _", - country, - city, - district, - order_index, - thumbnail_format as "thumbnail_format: _", - rooms, - bathrooms, - area, - parking_spots, - admin_tag, - time_created, - last_updated -FROM property WHERE realtor_id = $1 -ORDER BY time_created DESC + p.id, + p.project_id, + p.realtor_id, + p.media as "media: _", + p.property_type as "property_type: _", + p.property_sale_type as "property_sale_type: _", + p.country, + p.city, + p.district, + p.order_index, + p.thumbnail_format as "thumbnail_format: _", + p.rooms, + p.bathrooms, + p.area, + p.parking_spots, + p.admin_tag, + p.time_created, + p.last_updated +FROM property p, property_arrangement pa +WHERE p.realtor_id = $1 +AND pa.property_id = p.id +-- FILTERS +AND (pa.arrangement = $3 OR $3 IS null) -- Arrangement +AND (p.property_type = $4 OR $4 IS null) -- Property Type +AND (p.country = $5 OR $5 IS null) -- Country +AND (p.city = $6 OR $6 IS null) -- City +AND (p.district = $7 OR $7 IS null) -- District +AND (p.rooms = $8 OR $8 IS null) -- Room amount +AND (p.bathrooms = $9 OR $9 IS null) -- Bathroom amount +AND (p.area >= $10 OR $10 IS null) -- Area greater than +AND (p.parking_spots = $11 OR $11 IS null) -- Parking spots amount +AND (pa.price <= $12 OR $12 IS null) -- Price Less than +AND (pa.price >= $13 OR $13 IS null) -- Price Greater than +-- END OF FILTERS +ORDER BY p.time_created DESC LIMIT 25 OFFSET $2; \ No newline at end of file diff --git a/sql/property_arrangement/get_with_property_id.sql b/sql/property_arrangement/fetch_with_property_id.sql similarity index 78% rename from sql/property_arrangement/get_with_property_id.sql rename to sql/property_arrangement/fetch_with_property_id.sql index 1869929..20912ec 100644 --- a/sql/property_arrangement/get_with_property_id.sql +++ b/sql/property_arrangement/fetch_with_property_id.sql @@ -1,6 +1,6 @@ SELECT property_id, - price_usd, + price, currency, arrangement as "arrangement: _", time_created, diff --git a/sql/property_arrangement/fetch_with_property_ids.sql b/sql/property_arrangement/fetch_with_property_ids.sql new file mode 100644 index 0000000..d93d4c0 --- /dev/null +++ b/sql/property_arrangement/fetch_with_property_ids.sql @@ -0,0 +1,9 @@ +SELECT + property_id, + price, + currency, + arrangement as "arrangement: _", + time_created, + last_updated +FROM property_arrangement +WHERE property_id = ANY($1); \ No newline at end of file diff --git a/sql/property_arrangement/insert.sql b/sql/property_arrangement/insert.sql index c8cc1c2..ef8f0ee 100644 --- a/sql/property_arrangement/insert.sql +++ b/sql/property_arrangement/insert.sql @@ -1,6 +1,6 @@ INSERT INTO property_arrangement ( property_id, - price_usd, + price, currency, arrangement, time_created, @@ -9,7 +9,7 @@ INSERT INTO property_arrangement ( $1, $2, $3, $4, $5, $5 ) RETURNING property_id, - price_usd, + price, currency, arrangement as "arrangement: _", time_created, diff --git a/sql/property_arrangement/update.sql b/sql/property_arrangement/update.sql index bdd235d..07d4896 100644 --- a/sql/property_arrangement/update.sql +++ b/sql/property_arrangement/update.sql @@ -1,12 +1,12 @@ UPDATE property_arrangement SET - price_usd = $2, + price = $2, currency = $3, arrangement = $4, last_updated = $5 WHERE property_id = $1 RETURNING property_id, - price_usd, + price, currency, arrangement as "arrangement: _", time_created, diff --git a/sqlx-data.json b/sqlx-data.json index d614f7f..d9c6bac 100644 --- a/sqlx-data.json +++ b/sqlx-data.json @@ -116,122 +116,6 @@ }, "query": "SELECT \n id,\n title,\n description,\n realtor_id,\n media as \"media: _\",\n project_condition as \"project_condition: _\",\n project_type as \"project_type: _\",\n project_state as \"project_state: _\",\n country,\n city, \n district,\n admin_tag,\n floors,\n finish_date,\n order_index,\n time_created,\n last_updated\nFROM project WHERE id = $1;\n" }, - "066b243662a9077b8c5ce72277930c746558b1725a15faa7c235b63b00fde59c": { - "describe": { - "columns": [ - { - "name": "id", - "ordinal": 0, - "type_info": "Uuid" - }, - { - "name": "title", - "ordinal": 1, - "type_info": "Varchar" - }, - { - "name": "description", - "ordinal": 2, - "type_info": "Text" - }, - { - "name": "realtor_id", - "ordinal": 3, - "type_info": "Uuid" - }, - { - "name": "media: _", - "ordinal": 4, - "type_info": "Text" - }, - { - "name": "project_condition: _", - "ordinal": 5, - "type_info": "Varchar" - }, - { - "name": "project_type: _", - "ordinal": 6, - "type_info": "Varchar" - }, - { - "name": "project_state: _", - "ordinal": 7, - "type_info": "Varchar" - }, - { - "name": "country", - "ordinal": 8, - "type_info": "Varchar" - }, - { - "name": "city", - "ordinal": 9, - "type_info": "Varchar" - }, - { - "name": "district", - "ordinal": 10, - "type_info": "Varchar" - }, - { - "name": "admin_tag", - "ordinal": 11, - "type_info": "Varchar" - }, - { - "name": "floors", - "ordinal": 12, - "type_info": "Int2" - }, - { - "name": "finish_date", - "ordinal": 13, - "type_info": "Date" - }, - { - "name": "order_index", - "ordinal": 14, - "type_info": "Int4" - }, - { - "name": "time_created", - "ordinal": 15, - "type_info": "Timestamptz" - }, - { - "name": "last_updated", - "ordinal": 16, - "type_info": "Timestamptz" - } - ], - "nullable": [ - false, - true, - false, - false, - false, - false, - false, - false, - false, - false, - false, - true, - false, - false, - false, - false, - false - ], - "parameters": { - "Left": [ - "Uuid" - ] - } - }, - "query": "SELECT \n id,\n title,\n description,\n realtor_id,\n media as \"media: _\",\n project_condition as \"project_condition: _\",\n project_type as \"project_type: _\",\n project_state as \"project_state: _\",\n country,\n city, \n district,\n admin_tag,\n floors,\n finish_date,\n order_index,\n time_created,\n last_updated\nFROM project WHERE realtor_id = $1;\n" - }, "1de871eb06903280d3be4e4ef8f67cde596807784e726ff62d03c2c9f6d6ba76": { "describe": { "columns": [ @@ -312,6 +196,60 @@ }, "query": "SELECT * FROM realtor WHERE shortcode = $1;" }, + "22aa2761cf17dba99859d46e40c8b5e04a112f2fdae12494f4aabc384cde4727": { + "describe": { + "columns": [ + { + "name": "property_id", + "ordinal": 0, + "type_info": "Uuid" + }, + { + "name": "price", + "ordinal": 1, + "type_info": "Float8" + }, + { + "name": "currency", + "ordinal": 2, + "type_info": "Varchar" + }, + { + "name": "arrangement: _", + "ordinal": 3, + "type_info": "Varchar" + }, + { + "name": "time_created", + "ordinal": 4, + "type_info": "Timestamptz" + }, + { + "name": "last_updated", + "ordinal": 5, + "type_info": "Timestamptz" + } + ], + "nullable": [ + false, + false, + false, + false, + false, + false + ], + "parameters": { + "Left": [ + "Uuid", + "Float8", + "Varchar", + "Varchar", + "Timestamptz" + ] + } + }, + "query": "UPDATE property_arrangement SET \n price = $2,\n currency = $3,\n arrangement = $4,\n last_updated = $5\nWHERE property_id = $1\nRETURNING \n property_id,\n price,\n currency,\n arrangement as \"arrangement: _\",\n time_created,\n last_updated;" + }, "2482798d8601fd41678197770bf51af67cd962d210ff65b9236f24f0425d7341": { "describe": { "columns": [ @@ -401,116 +339,6 @@ }, "query": "INSERT INTO realtor (\n id, name, bio, phone_number, email, profile_picture_url, show_projects, shortcode, remax_agent_id, time_created, last_updated\n) VALUES (\n $1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $10\n) RETURNING *;" }, - "39cc220eaa29a94125da6d2baf3fc82a26c1ee626f3ecacb2b1cab3151cbe00c": { - "describe": { - "columns": [ - { - "name": "id", - "ordinal": 0, - "type_info": "Uuid" - }, - { - "name": "project_id", - "ordinal": 1, - "type_info": "Uuid" - }, - { - "name": "realtor_id", - "ordinal": 2, - "type_info": "Uuid" - }, - { - "name": "media: _", - "ordinal": 3, - "type_info": "Text" - }, - { - "name": "property_type: _", - "ordinal": 4, - "type_info": "Varchar" - }, - { - "name": "property_sale_type: _", - "ordinal": 5, - "type_info": "Varchar" - }, - { - "name": "country", - "ordinal": 6, - "type_info": "Varchar" - }, - { - "name": "city", - "ordinal": 7, - "type_info": "Varchar" - }, - { - "name": "district", - "ordinal": 8, - "type_info": "Varchar" - }, - { - "name": "price_usd", - "ordinal": 9, - "type_info": "Float8" - }, - { - "name": "rooms", - "ordinal": 10, - "type_info": "Int2" - }, - { - "name": "bathrooms", - "ordinal": 11, - "type_info": "Float4" - }, - { - "name": "area", - "ordinal": 12, - "type_info": "Float4" - }, - { - "name": "admin_tag", - "ordinal": 13, - "type_info": "Varchar" - }, - { - "name": "time_created", - "ordinal": 14, - "type_info": "Timestamptz" - }, - { - "name": "last_updated", - "ordinal": 15, - "type_info": "Timestamptz" - } - ], - "nullable": [ - false, - true, - false, - false, - false, - false, - true, - true, - true, - false, - false, - false, - false, - true, - false, - false - ], - "parameters": { - "Left": [ - "Uuid" - ] - } - }, - "query": "SELECT \n id,\n project_id,\n realtor_id,\n media as \"media: _\",\n property_type as \"property_type: _\",\n property_sale_type as \"property_sale_type: _\",\n country,\n city,\n district,\n price_usd,\n rooms,\n bathrooms,\n area,\n admin_tag,\n time_created,\n last_updated\nFROM property WHERE id = $1;" - }, "3acc0efdfdf6fd4e86b0f197cd27b7378507b82915127b76a6cf76a8f7cb5618": { "describe": { "columns": [ @@ -698,6 +526,56 @@ }, "query": "INSERT INTO project (\n id,\n title,\n description,\n realtor_id,\n media,\n project_condition,\n project_type,\n project_state,\n country,\n city, \n district,\n admin_tag,\n floors,\n finish_date,\n order_index,\n time_created,\n last_updated\n) VALUES (\n $1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $16\n) RETURNING \n id,\n title,\n description,\n realtor_id,\n media as \"media: _\",\n project_condition as \"project_condition: _\",\n project_type as \"project_type: _\",\n project_state as \"project_state: _\",\n country,\n city, \n district,\n admin_tag,\n floors,\n finish_date,\n order_index,\n time_created,\n last_updated;" }, + "5356455cba4a2740f833ca895e41fa120164a7e1fad9edb367aa680a2353ee02": { + "describe": { + "columns": [ + { + "name": "property_id", + "ordinal": 0, + "type_info": "Uuid" + }, + { + "name": "price", + "ordinal": 1, + "type_info": "Float8" + }, + { + "name": "currency", + "ordinal": 2, + "type_info": "Varchar" + }, + { + "name": "arrangement: _", + "ordinal": 3, + "type_info": "Varchar" + }, + { + "name": "time_created", + "ordinal": 4, + "type_info": "Timestamptz" + }, + { + "name": "last_updated", + "ordinal": 5, + "type_info": "Timestamptz" + } + ], + "nullable": [ + false, + false, + false, + false, + false, + false + ], + "parameters": { + "Left": [ + "UuidArray" + ] + } + }, + "query": "SELECT \n property_id,\n price,\n currency,\n arrangement as \"arrangement: _\",\n time_created,\n last_updated\nFROM property_arrangement\nWHERE property_id = ANY($1);" + }, "5cb3e189c5a575ebe13353f8824ac7baa96a9937ab7735216e064583f6cd68bc": { "describe": { "columns": [ @@ -829,6 +707,144 @@ }, "query": "UPDATE project SET \n title = $2,\n description = $3,\n realtor_id = $4,\n media = $5,\n project_condition = $6,\n project_type = $7,\n project_state = $8,\n country = $9,\n city = $10,\n district = $11,\n admin_tag = $12,\n floors = $13,\n finish_date = $14,\n order_index = $15,\n last_updated = $16\nWHERE id = $1\nRETURNING \n id,\n title,\n description,\n realtor_id,\n media as \"media: _\",\n project_condition as \"project_condition: _\",\n project_type as \"project_type: _\",\n project_state as \"project_state: _\",\n country,\n city, \n district,\n admin_tag,\n floors,\n finish_date,\n order_index,\n time_created,\n last_updated;" }, + "665bdd8b25cca64df6ad3194f33013340596405b4666e94382a6913b1888f6c1": { + "describe": { + "columns": [ + { + "name": "id", + "ordinal": 0, + "type_info": "Uuid" + }, + { + "name": "project_id", + "ordinal": 1, + "type_info": "Uuid" + }, + { + "name": "realtor_id", + "ordinal": 2, + "type_info": "Uuid" + }, + { + "name": "media: _", + "ordinal": 3, + "type_info": "Text" + }, + { + "name": "property_type: _", + "ordinal": 4, + "type_info": "Varchar" + }, + { + "name": "property_sale_type: _", + "ordinal": 5, + "type_info": "Varchar" + }, + { + "name": "country", + "ordinal": 6, + "type_info": "Varchar" + }, + { + "name": "city", + "ordinal": 7, + "type_info": "Varchar" + }, + { + "name": "district", + "ordinal": 8, + "type_info": "Varchar" + }, + { + "name": "order_index", + "ordinal": 9, + "type_info": "Int4" + }, + { + "name": "thumbnail_format: _", + "ordinal": 10, + "type_info": "Varchar" + }, + { + "name": "rooms", + "ordinal": 11, + "type_info": "Int2" + }, + { + "name": "bathrooms", + "ordinal": 12, + "type_info": "Float4" + }, + { + "name": "area", + "ordinal": 13, + "type_info": "Float4" + }, + { + "name": "parking_spots", + "ordinal": 14, + "type_info": "Int2" + }, + { + "name": "admin_tag", + "ordinal": 15, + "type_info": "Varchar" + }, + { + "name": "time_created", + "ordinal": 16, + "type_info": "Timestamptz" + }, + { + "name": "last_updated", + "ordinal": 17, + "type_info": "Timestamptz" + } + ], + "nullable": [ + false, + true, + false, + false, + false, + false, + false, + false, + false, + false, + false, + false, + false, + false, + false, + true, + false, + false + ], + "parameters": { + "Left": [ + "Uuid", + "Uuid", + "Uuid", + "Text", + "Varchar", + "Varchar", + "Varchar", + "Varchar", + "Varchar", + "Int4", + "Varchar", + "Int2", + "Float4", + "Float4", + "Int2", + "Varchar", + "Timestamptz" + ] + } + }, + "query": "INSERT INTO property (\n id,\n project_id,\n realtor_id,\n media,\n property_type,\n property_sale_type,\n country,\n city,\n district,\n order_index,\n thumbnail_format,\n rooms,\n bathrooms,\n area,\n parking_spots,\n admin_tag,\n time_created,\n last_updated\n) VALUES (\n $1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17, $17\n) RETURNING \n id,\n project_id,\n realtor_id,\n media as \"media: _\",\n property_type as \"property_type: _\",\n property_sale_type as \"property_sale_type: _\",\n country,\n city,\n district,\n order_index,\n thumbnail_format as \"thumbnail_format: _\",\n rooms,\n bathrooms,\n area,\n parking_spots,\n admin_tag,\n time_created,\n last_updated;" + }, "7464713d873ea4ad81fdbaef816eb57ce3bdcdf3aa669036e05158e230abd82c": { "describe": { "columns": [ @@ -877,7 +893,7 @@ }, "query": "INSERT INTO view (\n id, thing_id, thing_pk, trackable_id, time_created\n) VALUES (\n $1, $2, $3, $4, $5\n) RETURNING \nid, thing_id, thing_pk as \"thing_pk: _\", trackable_id, time_created" }, - "9affd78ec46e47dc9b7239e41c6e88b3aa96f48bf95ffe8faae35fcee6f5af9f": { + "873b8dc31432ef0ccf728bf87c6390e04cbbf6f63aec4dcf6d4b0c33e85eb8f2": { "describe": { "columns": [ { @@ -886,78 +902,83 @@ "type_info": "Uuid" }, { - "name": "project_id", + "name": "title", "ordinal": 1, - "type_info": "Uuid" + "type_info": "Varchar" + }, + { + "name": "description", + "ordinal": 2, + "type_info": "Text" }, { "name": "realtor_id", - "ordinal": 2, + "ordinal": 3, "type_info": "Uuid" }, { "name": "media: _", - "ordinal": 3, + "ordinal": 4, "type_info": "Text" }, { - "name": "property_type: _", - "ordinal": 4, - "type_info": "Varchar" - }, - { - "name": "property_sale_type: _", + "name": "project_condition: _", "ordinal": 5, "type_info": "Varchar" }, { - "name": "country", + "name": "project_type: _", "ordinal": 6, "type_info": "Varchar" }, { - "name": "city", + "name": "project_state: _", "ordinal": 7, "type_info": "Varchar" }, { - "name": "district", + "name": "country", "ordinal": 8, "type_info": "Varchar" }, { - "name": "price_usd", + "name": "city", "ordinal": 9, - "type_info": "Float8" - }, - { - "name": "rooms", - "ordinal": 10, - "type_info": "Int2" - }, - { - "name": "bathrooms", - "ordinal": 11, - "type_info": "Float4" - }, - { - "name": "area", - "ordinal": 12, - "type_info": "Float4" - }, - { - "name": "admin_tag", - "ordinal": 13, "type_info": "Varchar" }, { - "name": "time_created", + "name": "district", + "ordinal": 10, + "type_info": "Varchar" + }, + { + "name": "admin_tag", + "ordinal": 11, + "type_info": "Varchar" + }, + { + "name": "floors", + "ordinal": 12, + "type_info": "Int2" + }, + { + "name": "finish_date", + "ordinal": 13, + "type_info": "Date" + }, + { + "name": "order_index", "ordinal": 14, + "type_info": "Int4" + }, + { + "name": "time_created", + "ordinal": 15, "type_info": "Timestamptz" }, { "name": "last_updated", - "ordinal": 15, + "ordinal": 16, "type_info": "Timestamptz" } ], @@ -968,15 +989,16 @@ false, false, false, - true, - true, - true, + false, false, false, false, false, true, false, + false, + false, + false, false ], "parameters": { @@ -985,131 +1007,61 @@ ] } }, - "query": "SELECT \n id,\n project_id,\n realtor_id,\n media as \"media: _\",\n property_type as \"property_type: _\",\n property_sale_type as \"property_sale_type: _\",\n country,\n city,\n district,\n price_usd,\n rooms,\n bathrooms,\n area,\n admin_tag,\n time_created,\n last_updated\nFROM property WHERE realtor_id = $1;" + "query": "SELECT \n id,\n title,\n description,\n realtor_id,\n media as \"media: _\",\n project_condition as \"project_condition: _\",\n project_type as \"project_type: _\",\n project_state as \"project_state: _\",\n country,\n city,\n district,\n admin_tag,\n floors,\n finish_date,\n order_index,\n time_created,\n last_updated\nFROM project WHERE realtor_id = $1;" }, - "a6525881c04a3ddf038d566b8a41dc2e4670a20c0c6e4a0687a927538661db56": { + "a335033d78f1a2211afed07daff01d72152d05ad78fe068d62a42d3f08c83207": { "describe": { "columns": [ { - "name": "id", + "name": "property_id", "ordinal": 0, "type_info": "Uuid" }, { - "name": "project_id", + "name": "price", "ordinal": 1, - "type_info": "Uuid" - }, - { - "name": "realtor_id", - "ordinal": 2, - "type_info": "Uuid" - }, - { - "name": "media: _", - "ordinal": 3, - "type_info": "Text" - }, - { - "name": "property_type: _", - "ordinal": 4, - "type_info": "Varchar" - }, - { - "name": "property_sale_type: _", - "ordinal": 5, - "type_info": "Varchar" - }, - { - "name": "country", - "ordinal": 6, - "type_info": "Varchar" - }, - { - "name": "city", - "ordinal": 7, - "type_info": "Varchar" - }, - { - "name": "district", - "ordinal": 8, - "type_info": "Varchar" - }, - { - "name": "price_usd", - "ordinal": 9, "type_info": "Float8" }, { - "name": "rooms", - "ordinal": 10, - "type_info": "Int2" + "name": "currency", + "ordinal": 2, + "type_info": "Varchar" }, { - "name": "bathrooms", - "ordinal": 11, - "type_info": "Float4" - }, - { - "name": "area", - "ordinal": 12, - "type_info": "Float4" - }, - { - "name": "admin_tag", - "ordinal": 13, + "name": "arrangement: _", + "ordinal": 3, "type_info": "Varchar" }, { "name": "time_created", - "ordinal": 14, + "ordinal": 4, "type_info": "Timestamptz" }, { "name": "last_updated", - "ordinal": 15, + "ordinal": 5, "type_info": "Timestamptz" } ], "nullable": [ false, - true, false, false, false, false, - true, - true, - true, - false, - false, - false, - false, - true, - false, false ], "parameters": { "Left": [ "Uuid", - "Uuid", - "Uuid", - "Text", - "Varchar", - "Varchar", - "Varchar", - "Varchar", - "Varchar", "Float8", - "Int2", - "Float4", - "Float4", + "Varchar", "Varchar", "Timestamptz" ] } }, - "query": "UPDATE property SET\n project_id = $2,\n realtor_id = $3,\n media = $4,\n property_type = $5,\n property_sale_type = $6,\n country = $7,\n city = $8,\n district = $9,\n price_usd = $10,\n rooms = $11,\n bathrooms = $12,\n area = $13,\n admin_tag = $14,\n last_updated = $15\nWHERE id = $1\nRETURNING \n id,\n project_id,\n realtor_id,\n media as \"media: _\",\n property_type as \"property_type: _\",\n property_sale_type as \"property_sale_type: _\",\n country,\n city,\n district,\n price_usd,\n rooms,\n bathrooms,\n area,\n admin_tag,\n time_created,\n last_updated;" + "query": "INSERT INTO property_arrangement (\n property_id,\n price,\n currency,\n arrangement,\n time_created, \n last_updated\n) VALUES (\n $1, $2, $3, $4, $5, $5\n) RETURNING \n property_id,\n price,\n currency,\n arrangement as \"arrangement: _\",\n time_created,\n last_updated;" }, "b64e2047bd463a7fd60c3c11ba087bc6f6ec7729bd7bd08dc0444265e9f42690": { "describe": { @@ -1173,7 +1125,57 @@ }, "query": "INSERT INTO trackable (\n id, who_was, ip_address, browser_width, browser_height, user_agent, time_created\n) VALUES(\n $1, $2, $3, $4, $5, $6, $7\n) RETURNING *;" }, - "ba31aaf3792054e90629a08ef393136e77d7300eb7ef111a0decc97c05d9cc87": { + "c11000c18fb05826a5d18f1d8ca843a3669d9c5397680c8408c05dc827de576a": { + "describe": { + "columns": [ + { + "name": "property_id", + "ordinal": 0, + "type_info": "Uuid" + }, + { + "name": "price", + "ordinal": 1, + "type_info": "Float8" + }, + { + "name": "currency", + "ordinal": 2, + "type_info": "Varchar" + }, + { + "name": "arrangement: _", + "ordinal": 3, + "type_info": "Varchar" + }, + { + "name": "time_created", + "ordinal": 4, + "type_info": "Timestamptz" + }, + { + "name": "last_updated", + "ordinal": 5, + "type_info": "Timestamptz" + } + ], + "nullable": [ + false, + false, + false, + false, + false, + false + ], + "parameters": { + "Left": [ + "Uuid" + ] + } + }, + "query": "SELECT \n property_id,\n price,\n currency,\n arrangement as \"arrangement: _\",\n time_created,\n last_updated\nFROM property_arrangement\nWHERE property_id = $1;" + }, + "c30c3771ddcad09931fd9b3842ab34f043d19da0ca2a1953349329cb2cf083c3": { "describe": { "columns": [ { @@ -1222,38 +1224,48 @@ "type_info": "Varchar" }, { - "name": "price_usd", + "name": "order_index", "ordinal": 9, - "type_info": "Float8" + "type_info": "Int4" + }, + { + "name": "thumbnail_format: _", + "ordinal": 10, + "type_info": "Varchar" }, { "name": "rooms", - "ordinal": 10, + "ordinal": 11, "type_info": "Int2" }, { "name": "bathrooms", - "ordinal": 11, - "type_info": "Float4" - }, - { - "name": "area", "ordinal": 12, "type_info": "Float4" }, { - "name": "admin_tag", + "name": "area", "ordinal": 13, + "type_info": "Float4" + }, + { + "name": "parking_spots", + "ordinal": 14, + "type_info": "Int2" + }, + { + "name": "admin_tag", + "ordinal": 15, "type_info": "Varchar" }, { "name": "time_created", - "ordinal": 14, + "ordinal": 16, "type_info": "Timestamptz" }, { "name": "last_updated", - "ordinal": 15, + "ordinal": 17, "type_info": "Timestamptz" } ], @@ -1264,9 +1276,133 @@ false, false, false, + false, + false, + false, + false, + false, + false, + false, + false, + false, true, + false, + false + ], + "parameters": { + "Left": [ + "Uuid" + ] + } + }, + "query": "SELECT \n id,\n project_id,\n realtor_id,\n media as \"media: _\",\n property_type as \"property_type: _\",\n property_sale_type as \"property_sale_type: _\",\n country,\n city,\n district,\n order_index,\n thumbnail_format as \"thumbnail_format: _\",\n rooms,\n bathrooms,\n area,\n parking_spots,\n admin_tag,\n time_created,\n last_updated\nFROM property WHERE id = $1;" + }, + "c4271ee88c4ffeeac98be4c3a7e9a870f3309e3460a3d0986b70a367a1af310b": { + "describe": { + "columns": [ + { + "name": "id", + "ordinal": 0, + "type_info": "Uuid" + }, + { + "name": "project_id", + "ordinal": 1, + "type_info": "Uuid" + }, + { + "name": "realtor_id", + "ordinal": 2, + "type_info": "Uuid" + }, + { + "name": "media: _", + "ordinal": 3, + "type_info": "Text" + }, + { + "name": "property_type: _", + "ordinal": 4, + "type_info": "Varchar" + }, + { + "name": "property_sale_type: _", + "ordinal": 5, + "type_info": "Varchar" + }, + { + "name": "country", + "ordinal": 6, + "type_info": "Varchar" + }, + { + "name": "city", + "ordinal": 7, + "type_info": "Varchar" + }, + { + "name": "district", + "ordinal": 8, + "type_info": "Varchar" + }, + { + "name": "order_index", + "ordinal": 9, + "type_info": "Int4" + }, + { + "name": "thumbnail_format: _", + "ordinal": 10, + "type_info": "Varchar" + }, + { + "name": "rooms", + "ordinal": 11, + "type_info": "Int2" + }, + { + "name": "bathrooms", + "ordinal": 12, + "type_info": "Float4" + }, + { + "name": "area", + "ordinal": 13, + "type_info": "Float4" + }, + { + "name": "parking_spots", + "ordinal": 14, + "type_info": "Int2" + }, + { + "name": "admin_tag", + "ordinal": 15, + "type_info": "Varchar" + }, + { + "name": "time_created", + "ordinal": 16, + "type_info": "Timestamptz" + }, + { + "name": "last_updated", + "ordinal": 17, + "type_info": "Timestamptz" + } + ], + "nullable": [ + false, true, - true, + false, + false, + false, + false, + false, + false, + false, + false, + false, false, false, false, @@ -1286,16 +1422,18 @@ "Varchar", "Varchar", "Varchar", - "Float8", + "Int4", + "Varchar", "Int2", "Float4", "Float4", + "Int2", "Varchar", "Timestamptz" ] } }, - "query": "INSERT INTO property (\n id,\n project_id,\n realtor_id,\n media,\n property_type,\n property_sale_type,\n country,\n city,\n district,\n price_usd,\n rooms,\n bathrooms,\n area,\n admin_tag,\n time_created,\n last_updated\n) VALUES (\n $1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $15\n) RETURNING \n id,\n project_id,\n realtor_id,\n media as \"media: _\",\n property_type as \"property_type: _\",\n property_sale_type as \"property_sale_type: _\",\n country,\n city,\n district,\n price_usd,\n rooms,\n bathrooms,\n area,\n admin_tag,\n time_created,\n last_updated;" + "query": "UPDATE property SET\n project_id = $2,\n realtor_id = $3,\n media = $4,\n property_type = $5,\n property_sale_type = $6,\n country = $7,\n city = $8,\n district = $9,\n order_index = $10,\n thumbnail_format = $11,\n rooms = $12,\n bathrooms = $13,\n area = $14,\n parking_spots = $15,\n admin_tag = $16,\n last_updated = $17\nWHERE id = $1\nRETURNING \n id,\n project_id,\n realtor_id,\n media as \"media: _\",\n property_type as \"property_type: _\",\n property_sale_type as \"property_sale_type: _\",\n country,\n city,\n district,\n order_index,\n thumbnail_format as \"thumbnail_format: _\",\n rooms,\n bathrooms,\n area,\n parking_spots,\n admin_tag,\n time_created,\n last_updated;" }, "c571a9279bdcec6420fd1f80d8777832d13e135353a3d40aaf9549c5e80eccab": { "describe": { @@ -1385,6 +1523,140 @@ }, "query": "UPDATE realtor SET\n name = $2,\n bio = $3,\n phone_number = $4,\n email = $5,\n profile_picture_url = $6,\n show_projects = $7,\n remax_agent_id = $8,\n last_updated = $9\nWHERE id = $1\nRETURNING *;" }, + "ca31c99797615d66e383432403a6c8baab4907ebad96944d38075a8917e1c4e9": { + "describe": { + "columns": [ + { + "name": "id", + "ordinal": 0, + "type_info": "Uuid" + }, + { + "name": "project_id", + "ordinal": 1, + "type_info": "Uuid" + }, + { + "name": "realtor_id", + "ordinal": 2, + "type_info": "Uuid" + }, + { + "name": "media: _", + "ordinal": 3, + "type_info": "Text" + }, + { + "name": "property_type: _", + "ordinal": 4, + "type_info": "Varchar" + }, + { + "name": "property_sale_type: _", + "ordinal": 5, + "type_info": "Varchar" + }, + { + "name": "country", + "ordinal": 6, + "type_info": "Varchar" + }, + { + "name": "city", + "ordinal": 7, + "type_info": "Varchar" + }, + { + "name": "district", + "ordinal": 8, + "type_info": "Varchar" + }, + { + "name": "order_index", + "ordinal": 9, + "type_info": "Int4" + }, + { + "name": "thumbnail_format: _", + "ordinal": 10, + "type_info": "Varchar" + }, + { + "name": "rooms", + "ordinal": 11, + "type_info": "Int2" + }, + { + "name": "bathrooms", + "ordinal": 12, + "type_info": "Float4" + }, + { + "name": "area", + "ordinal": 13, + "type_info": "Float4" + }, + { + "name": "parking_spots", + "ordinal": 14, + "type_info": "Int2" + }, + { + "name": "admin_tag", + "ordinal": 15, + "type_info": "Varchar" + }, + { + "name": "time_created", + "ordinal": 16, + "type_info": "Timestamptz" + }, + { + "name": "last_updated", + "ordinal": 17, + "type_info": "Timestamptz" + } + ], + "nullable": [ + false, + true, + false, + false, + false, + false, + false, + false, + false, + false, + false, + false, + false, + false, + false, + true, + false, + false + ], + "parameters": { + "Left": [ + "Uuid", + "Int8", + "Text", + "Text", + "Text", + "Text", + "Text", + "Int2", + "Float4", + "Float4", + "Int2", + "Float8", + "Float8" + ] + } + }, + "query": "SELECT \n p.id,\n p.project_id,\n p.realtor_id,\n p.media as \"media: _\",\n p.property_type as \"property_type: _\",\n p.property_sale_type as \"property_sale_type: _\",\n p.country,\n p.city,\n p.district,\n p.order_index,\n p.thumbnail_format as \"thumbnail_format: _\",\n p.rooms,\n p.bathrooms,\n p.area,\n p.parking_spots,\n p.admin_tag,\n p.time_created,\n p.last_updated\nFROM property p, property_arrangement pa\nWHERE p.realtor_id = $1\nAND pa.property_id = p.id\n-- FILTERS\nAND (pa.arrangement = $3 OR $3 IS null) -- Arrangement\nAND (p.property_type = $4 OR $4 IS null) -- Property Type\nAND (p.country = $5 OR $5 IS null) -- Country\nAND (p.city = $6 OR $6 IS null) -- City\nAND (p.district = $7 OR $7 IS null) -- District\nAND (p.rooms = $8 OR $8 IS null) -- Room amount\nAND (p.bathrooms = $9 OR $9 IS null) -- Bathroom amount\nAND (p.area >= $10 OR $10 IS null) -- Area greater than\nAND (p.parking_spots = $11 OR $11 IS null) -- Parking spots amount\nAND (pa.price <= $12 OR $12 IS null) -- Price Less than\nAND (pa.price >= $13 OR $13 IS null) -- Price Greater than\n-- END OF FILTERS\nORDER BY p.time_created DESC\nLIMIT 25 OFFSET $2;" + }, "e9fb0b1fe0e33d2a8aa4bc20be141a9f4bcd68d1c69af1dce93c43c8ba96ebc1": { "describe": { "columns": [ diff --git a/src/dao/mod.rs b/src/dao/mod.rs index 1817f85..c29f876 100644 --- a/src/dao/mod.rs +++ b/src/dao/mod.rs @@ -5,6 +5,8 @@ pub mod project; #[allow(unused)] pub mod property; #[allow(unused)] +pub mod property_arrangement; +#[allow(unused)] pub mod realtor; #[allow(unused)] pub mod trackable; diff --git a/src/dao/property.rs b/src/dao/property.rs index 4393b3a..833a1e9 100644 --- a/src/dao/property.rs +++ b/src/dao/property.rs @@ -1,4 +1,4 @@ -use realtor_lp_types::domain::property::Property; +use realtor_lp_types::{domain::property::Property, dto::filter::Filter}; use sqlx::{PgPool, Postgres, Transaction}; use uuid::Uuid; @@ -43,14 +43,55 @@ pub async fn get_property_with_id( pub async fn fetch_with_realtor_id_paged( conn: &PgPool, realtor_id: &Uuid, + filters: &Vec, page: &i64, ) -> Result, sqlx::Error> { let offset = (page - 1) * 25; + + let mut arrangement_filter = None; + let mut property_type_filter = None; + let mut country_filter = None; + let mut city_filter = None; + let mut district_filter = None; + let mut room_count_filter = None; + let mut bathroom_count_filter = None; + let mut min_area_filter = None; + let mut parking_spots_count_filter = None; + let mut min_price_filter = None; + let mut max_price_filter = None; + + for filter in filters { + match filter { + Filter::Country(country) => country_filter = Some(country), + Filter::City(city) => city_filter = Some(city), + Filter::District(district) => district_filter = Some(district), + Filter::PriceGreaterThan(price_greater_than) => min_price_filter = Some(price_greater_than), + Filter::PriceLessThan(price_less_than) => max_price_filter = Some(price_less_than), + Filter::Rooms(room_count) => room_count_filter = Some(room_count), + Filter::Bathrooms(bathroom_count) => bathroom_count_filter = Some(bathroom_count), + Filter::PropertyType(property_type) => property_type_filter = Some(property_type), + Filter::MinArea(min_area) => min_area_filter = Some(min_area), + Filter::ParkingSpots(parking_spots_count) => parking_spots_count_filter = Some(parking_spots_count), + Filter::Arrangement(arrangement) => arrangement_filter = Some(arrangement), + } + } + sqlx::query_file_as!( Property, "sql/property/fetch_with_realtor_id_paged.sql", realtor_id, - offset + offset, + arrangement_filter as _, + property_type_filter as _, + country_filter, + city_filter, + district_filter, + room_count_filter, + bathroom_count_filter, + min_area_filter, + parking_spots_count_filter, + max_price_filter, + min_price_filter, ) .fetch_all(conn) .await diff --git a/src/dao/property_arrangement.rs b/src/dao/property_arrangement.rs new file mode 100644 index 0000000..bb9e846 --- /dev/null +++ b/src/dao/property_arrangement.rs @@ -0,0 +1,63 @@ +use realtor_lp_types::domain::price::PropertyPrice; +use sqlx::{PgPool, Postgres, Transaction}; +use uuid::Uuid; + +pub async fn insert_arrangement<'a>( + tx: &mut Transaction<'a, Postgres>, + arrangement: PropertyPrice, +) -> Result { + sqlx::query_file_as!( + PropertyPrice, + "sql/property_arrangement/insert.sql", + arrangement.property_id, + arrangement.price, + arrangement.currency, + arrangement.arrangement as _, + arrangement.time_created, + ) + .fetch_one(tx) + .await +} + +pub async fn fetch_arrangements_with_property_id( + conn: &PgPool, + property_id: &Uuid, +) -> Result, sqlx::Error> { + sqlx::query_file_as!( + PropertyPrice, + "sql/property_arrangement/fetch_with_property_id.sql", + property_id + ) + .fetch_all(conn) + .await +} + +pub async fn fetch_arrangements_with_property_ids( + conn: &PgPool, + property_ids: &Vec, +) -> Result, sqlx::Error> { + sqlx::query_file_as!( + PropertyPrice, + "sql/property_arrangement/fetch_with_property_ids.sql", + property_ids + ) + .fetch_all(conn) + .await +} + +pub async fn update_arrangement<'a>( + tx: &mut Transaction<'a, Postgres>, + arrangement: PropertyPrice, +) -> Result { + sqlx::query_file_as!( + PropertyPrice, + "sql/property_arrangement/update.sql", + arrangement.property_id, + arrangement.price, + arrangement.currency, + arrangement.arrangement as _, + arrangement.last_updated + ) + .fetch_one(tx) + .await +} diff --git a/src/dao/view.rs b/src/dao/view.rs index 519d6b1..2c97f72 100644 --- a/src/dao/view.rs +++ b/src/dao/view.rs @@ -1,7 +1,10 @@ use realtor_lp_types::domain::view::View; -use sqlx::{PgPool, Transaction, Postgres}; +use sqlx::{PgPool, Postgres, Transaction}; -pub async fn insert_view<'a>(transaction: &mut Transaction<'a, Postgres>, view: View) -> Result { +pub async fn insert_view<'a>( + transaction: &mut Transaction<'a, Postgres>, + view: View, +) -> Result { sqlx::query_file_as!( View, "sql/view/insert.sql", diff --git a/src/routes/property.rs b/src/routes/property.rs index 72de7b9..92ff606 100644 --- a/src/routes/property.rs +++ b/src/routes/property.rs @@ -1,6 +1,9 @@ use std::sync::Arc; -use actix_web::{web::{Path, Data}, get}; +use actix_web::{ + get, + web::{Data, Path}, +}; use actix_web_utils::extensions::typed_response::TypedHttpResponse; use err::MessageResource; use realtor_lp_types::{ @@ -93,6 +96,5 @@ pub async fn fetch_realtor_properties_paged( conn: Data>, path_vars: Path<(Uuid, i64)>, ) -> TypedHttpResponse> { - success!(Default::default()) -} \ No newline at end of file +} diff --git a/src/routes/view.rs b/src/routes/view.rs index bc6b738..450e28a 100644 --- a/src/routes/view.rs +++ b/src/routes/view.rs @@ -2,11 +2,13 @@ use actix_web_utils::extensions::typed_response::TypedHttpResponse; use realtor_lp_types::dto::payloads::view::ViewForCreationPayload; use sqlx::PgPool; -use crate::{dao, handle_tx, handle_db_write_op, success}; - +use crate::{dao, handle_db_write_op, handle_tx, success}; pub async fn new_view(conn: &PgPool, view: ViewForCreationPayload) -> TypedHttpResponse<()> { let mut transaction = handle_tx!(conn.begin()); - handle_db_write_op!(dao::view::insert_view(&mut transaction, view.into()), transaction); + handle_db_write_op!( + dao::view::insert_view(&mut transaction, view.into()), + transaction + ); success!(()) -} \ No newline at end of file +}