From 9c97e50c53a64d4409db778d128aba1ccab9fd42 Mon Sep 17 00:00:00 2001 From: Maksym Sadovnychyy Date: Sat, 3 Sep 2022 21:47:58 +0200 Subject: [PATCH] (refactor): code review --- postman/reactredux.postman_collection.json | 977 +++++++++++------- .../Core/DomainObjects/Documents/BlogItem.cs | 7 +- webapi/DataProviders/CategoryDataProvider.cs | 2 +- webapi/DataProviders/ShopCartDataProvider.cs | 2 +- .../Extensions/IEquatableExtensionsTests.cs | 9 +- .../Tests/Extensions/ObjectExtensionsTests.cs | 2 +- .../Tests/Extensions/StringExtensionsTests.cs | 33 +- .../Models/Abstractions/PageModelBase.cs | 5 - .../Models/Abstractions/PersonModelBase.cs | 2 +- .../Abstractions/PostItemResponseModelBase.cs | 16 +- .../WeatherForecast/Models/CategoryModel.cs | 48 - webapi/WeatherForecast/Models/CommentModel.cs | 10 +- .../PageSections/FeaturedBlogSectionModel.cs | 5 - .../Models/PageSections/TitleSectionModel.cs | 2 +- .../Models/Requests/BlogItemRequestModel.cs | 14 +- .../Requests/CategoryItemRequestModel.cs | 7 +- .../Models/Requests/ImageRequestModel.cs | 56 + .../Models/Requests/L10n/CategoryL10nModel.cs | 8 +- .../Models/Requests/L10n/ImageL10nModel.cs | 7 +- .../Models/Requests/L10n/PostItemL10nModel.cs | 18 +- .../Requests/ShopCartItemRequestModel.cs | 4 +- .../Models/Requests/ShopItemRequestModel.cs | 36 +- .../Responses/BlogCategoriesResponseModel.cs | 15 - .../Responses/BlogFeaturedResponseModel.cs | 23 - .../Responses/BlogItemsResponseModel.cs | 7 - .../Responses/CategoryItemResponseModel.cs | 2 +- .../ImageResponseModel.cs} | 11 +- .../Responses/ShopCartItemResponseModel.cs | 20 +- .../Services/BlogItemService.cs | 152 ++- .../Services/BlogItemsService.cs | 64 +- .../Services/CategoryItemService.cs | 109 +- .../Services/CategoryItemsService.cs | 37 +- .../Services/ContentService.cs | 18 +- .../Services/ShopCartItemService.cs | 121 ++- .../Services/ShopCartItemsService.cs | 52 +- .../Services/ShopItemService.cs | 184 ++-- .../Services/ShopItemsService.cs | 63 +- 37 files changed, 1281 insertions(+), 867 deletions(-) delete mode 100644 webapi/WeatherForecast/Models/CategoryModel.cs create mode 100644 webapi/WeatherForecast/Models/Requests/ImageRequestModel.cs delete mode 100644 webapi/WeatherForecast/Models/Responses/BlogCategoriesResponseModel.cs delete mode 100644 webapi/WeatherForecast/Models/Responses/BlogFeaturedResponseModel.cs rename webapi/WeatherForecast/Models/{ImageModel.cs => Responses/ImageResponseModel.cs} (75%) diff --git a/postman/reactredux.postman_collection.json b/postman/reactredux.postman_collection.json index b7b770a..e5f987b 100644 --- a/postman/reactredux.postman_collection.json +++ b/postman/reactredux.postman_collection.json @@ -49,7 +49,7 @@ ] }, { - "name": "ShopCartItems", + "name": "CategoryItem", "item": [ { "name": "01-Get", @@ -68,7 +68,7 @@ } ], "url": { - "raw": "https://localhost:7151/api/ShopCartItems/404c8232-9048-4519-bfba-6e78dc7005ca/fdc5aa50-ee68-4bae-a8e6-b8ae2c258f60?locale=en-US", + "raw": "https://localhost:7151/api/CategoryItem/404c8232-9048-4519-bfba-6e78dc7005ca/e154e33f-3cc7-468d-bb66-e0390ddb9ae0", "protocol": "https", "host": [ "localhost" @@ -76,9 +76,222 @@ "port": "7151", "path": [ "api", - "ShopCartItems", + "CategoryItem", "404c8232-9048-4519-bfba-6e78dc7005ca", - "fdc5aa50-ee68-4bae-a8e6-b8ae2c258f60" + "e154e33f-3cc7-468d-bb66-e0390ddb9ae0" + ] + } + }, + "response": [] + }, + { + "name": "02-GetSlug", + "request": { + "method": "GET", + "header": [ + { + "key": "Content-Type", + "value": "application/json", + "type": "default" + }, + { + "key": "Accept", + "value": "application/json", + "type": "default" + } + ], + "url": { + "raw": "https://localhost:7151/api/CategoryItem/404c8232-9048-4519-bfba-6e78dc7005ca?slug=default", + "protocol": "https", + "host": [ + "localhost" + ], + "port": "7151", + "path": [ + "api", + "CategoryItem", + "404c8232-9048-4519-bfba-6e78dc7005ca" + ], + "query": [ + { + "key": "slug", + "value": "default" + } + ] + } + }, + "response": [] + }, + { + "name": "03-Post", + "request": { + "method": "POST", + "header": [ + { + "key": "Content-Type", + "value": "application/json", + "type": "default" + }, + { + "key": "Accept", + "value": "application/json", + "type": "default" + } + ], + "body": { + "mode": "raw", + "raw": "{\r\n \"l10n\": [\r\n {\r\n \"locale\": \"\",\r\n \"slug\": \"guides\",\r\n \"text\": \"Guides\"\r\n }\r\n ]\r\n}" + }, + "url": { + "raw": "https://localhost:7151/api/CategoryItem/404c8232-9048-4519-bfba-6e78dc7005ca", + "protocol": "https", + "host": [ + "localhost" + ], + "port": "7151", + "path": [ + "api", + "CategoryItem", + "404c8232-9048-4519-bfba-6e78dc7005ca" + ] + } + }, + "response": [] + }, + { + "name": "04-Put", + "request": { + "method": "PUT", + "header": [ + { + "key": "Content-Type", + "value": "application/json", + "type": "default" + }, + { + "key": "Accept", + "value": "application/json", + "type": "default" + } + ], + "body": { + "mode": "raw", + "raw": "{\r\n \"l10n\": [\r\n {\r\n \"locale\": \"en-US\",\r\n \"slug\": \"guides\",\r\n \"text\": \"Guides\"\r\n },\r\n {\r\n \"locale\": \"it-IT\",\r\n \"slug\": \"guide\",\r\n \"text\": \"Guide\"\r\n }\r\n ]\r\n}" + }, + "url": { + "raw": "https://localhost:7151/api/CategoryItem/404c8232-9048-4519-bfba-6e78dc7005ca/2c50f4de-70f2-4414-aabc-7a0d2eb0e203", + "protocol": "https", + "host": [ + "localhost" + ], + "port": "7151", + "path": [ + "api", + "CategoryItem", + "404c8232-9048-4519-bfba-6e78dc7005ca", + "2c50f4de-70f2-4414-aabc-7a0d2eb0e203" + ] + } + }, + "response": [] + }, + { + "name": "05-Delete", + "request": { + "method": "DELETE", + "header": [ + { + "key": "Content-Type", + "value": "application/json", + "type": "default" + }, + { + "key": "Accept", + "value": "application/json", + "type": "default" + } + ], + "url": { + "raw": "https://localhost:7151/api/CategoryItem/404c8232-9048-4519-bfba-6e78dc7005ca/2c50f4de-70f2-4414-aabc-7a0d2eb0e203", + "protocol": "https", + "host": [ + "localhost" + ], + "port": "7151", + "path": [ + "api", + "CategoryItem", + "404c8232-9048-4519-bfba-6e78dc7005ca", + "2c50f4de-70f2-4414-aabc-7a0d2eb0e203" + ] + } + }, + "response": [] + } + ] + }, + { + "name": "CategoryItems", + "item": [ + { + "name": "01-Get", + "request": { + "method": "GET", + "header": [ + { + "key": "Content-Type", + "value": "application/json", + "type": "default" + }, + { + "key": "Accept", + "value": "application/json", + "type": "default" + } + ], + "url": { + "raw": "https://localhost:7151/api/CategoryItems/404c8232-9048-4519-bfba-6e78dc7005ca", + "protocol": "https", + "host": [ + "localhost" + ], + "port": "7151", + "path": [ + "api", + "CategoryItems", + "404c8232-9048-4519-bfba-6e78dc7005ca" + ] + } + }, + "response": [] + }, + { + "name": "02-Get localized", + "request": { + "method": "GET", + "header": [ + { + "key": "Content-Type", + "value": "application/json", + "type": "default" + }, + { + "key": "Accept", + "value": "application/json", + "type": "default" + } + ], + "url": { + "raw": "https://localhost:7151/api/CategoryItems/404c8232-9048-4519-bfba-6e78dc7005ca?locale=en-US", + "protocol": "https", + "host": [ + "localhost" + ], + "port": "7151", + "path": [ + "api", + "CategoryItems", + "404c8232-9048-4519-bfba-6e78dc7005ca" ], "query": [ { @@ -89,6 +302,346 @@ } }, "response": [] + }, + { + "name": "03-Delete", + "request": { + "method": "DELETE", + "header": [ + { + "key": "Content-Type", + "value": "application/json", + "type": "default" + }, + { + "key": "Accept", + "value": "application/json", + "type": "default" + } + ], + "url": { + "raw": "https://localhost:7151/api/CategoryItems/404c8232-9048-4519-bfba-6e78dc7005ca", + "protocol": "https", + "host": [ + "localhost" + ], + "port": "7151", + "path": [ + "api", + "CategoryItems", + "404c8232-9048-4519-bfba-6e78dc7005ca" + ] + } + }, + "response": [] + } + ] + }, + { + "name": "ShopItem", + "item": [ + { + "name": "01-Get", + "request": { + "method": "GET", + "header": [ + { + "key": "Content-Type", + "value": "application/json", + "type": "default" + }, + { + "key": "Accept", + "value": "application/json", + "type": "default" + } + ], + "url": { + "raw": "https://localhost:7151/api/ShopItem/404c8232-9048-4519-bfba-6e78dc7005ca/SKU-01", + "protocol": "https", + "host": [ + "localhost" + ], + "port": "7151", + "path": [ + "api", + "ShopItem", + "404c8232-9048-4519-bfba-6e78dc7005ca", + "SKU-01" + ] + } + }, + "response": [] + }, + { + "name": "02-GetSlug", + "request": { + "method": "GET", + "header": [ + { + "key": "Content-Type", + "value": "application/json", + "type": "default" + }, + { + "key": "Accept", + "value": "application/json", + "type": "default" + } + ], + "url": { + "raw": "https://localhost:7151/api/ShopItem/404c8232-9048-4519-bfba-6e78dc7005ca?slug=shop-catalog-item-01", + "protocol": "https", + "host": [ + "localhost" + ], + "port": "7151", + "path": [ + "api", + "ShopItem", + "404c8232-9048-4519-bfba-6e78dc7005ca" + ], + "query": [ + { + "key": "slug", + "value": "shop-catalog-item-01" + } + ] + } + }, + "response": [] + }, + { + "name": "03-Post", + "request": { + "method": "POST", + "header": [ + { + "key": "Content-Type", + "value": "application/json", + "type": "default" + }, + { + "key": "Accept", + "value": "application/json", + "type": "default" + } + ], + "body": { + "mode": "raw", + "raw": "{\r\n \"l10n\":[\r\n {\r\n \"locale\":\"en-US\",\r\n \"slug\":\"shop-catalog-item-05\",\r\n \"description\": \"Descritpion...\",\r\n \"title\":\"Shop item title\",\r\n \"shortText\":\"Shor text...\",\r\n \"text\":\"Text...\",\r\n \"contentType\": \"HTML\",\r\n \"badges\":[\r\n \"sale\"\r\n ]\r\n }\r\n ],\r\n \r\n \"tags\":[\r\n \"react\",\r\n \"redux\",\r\n \"webapi\"\r\n ],\r\n \"categories\":[\r\n \"e154e33f-3cc7-468d-bb66-e0390ddb9ae0\"\r\n ],\r\n \"brandName\":\"Mongodb Brand & Name\",\r\n \"rating\":4.5,\r\n \"price\":20,\r\n \"newPrice\":10,\r\n \"quantity\":100\r\n}" + }, + "url": { + "raw": "https://localhost:7151/api/ShopItem/404c8232-9048-4519-bfba-6e78dc7005ca/SKU-05", + "protocol": "https", + "host": [ + "localhost" + ], + "port": "7151", + "path": [ + "api", + "ShopItem", + "404c8232-9048-4519-bfba-6e78dc7005ca", + "SKU-05" + ] + } + }, + "response": [] + }, + { + "name": "04-Put", + "request": { + "method": "PUT", + "header": [ + { + "key": "Content-Type", + "value": "application/json", + "type": "default" + }, + { + "key": "Accept", + "value": "application/json", + "type": "default" + } + ], + "body": { + "mode": "raw", + "raw": "{\r\n \"l10n\":[\r\n {\r\n \"locale\":\"en-US\",\r\n \"slug\":\"shop-catalog-item-05\",\r\n \"description\": \"New Descritpion...\",\r\n \"title\":\"Shop item title\",\r\n \"shortText\":\"New Shor text...\",\r\n \"text\":\"New Text...\",\r\n \"contentType\": \"HTML\",\r\n \"badges\":[\r\n \"sale\"\r\n ]\r\n }\r\n ],\r\n \r\n \"tags\":[\r\n \"react\",\r\n \"redux\",\r\n \"webapi\"\r\n ],\r\n \"categories\":[\r\n \"e154e33f-3cc7-468d-bb66-e0390ddb9ae0\"\r\n ],\r\n \"brandName\":\"Mongodb Brand & Name\",\r\n \"rating\":4.5,\r\n \"price\":20,\r\n \"newPrice\":10,\r\n \"quantity\":100\r\n}" + }, + "url": { + "raw": "https://localhost:7151/api/ShopItem/404c8232-9048-4519-bfba-6e78dc7005ca/SKU-05", + "protocol": "https", + "host": [ + "localhost" + ], + "port": "7151", + "path": [ + "api", + "ShopItem", + "404c8232-9048-4519-bfba-6e78dc7005ca", + "SKU-05" + ] + } + }, + "response": [] + }, + { + "name": "05-Delete", + "request": { + "method": "DELETE", + "header": [ + { + "key": "Content-Type", + "value": "application/json", + "type": "default" + }, + { + "key": "Accept", + "value": "application/json", + "type": "default" + } + ], + "url": { + "raw": "https://localhost:7151/api/ShopItem/404c8232-9048-4519-bfba-6e78dc7005ca/SKU-05", + "protocol": "https", + "host": [ + "localhost" + ], + "port": "7151", + "path": [ + "api", + "ShopItem", + "404c8232-9048-4519-bfba-6e78dc7005ca", + "SKU-05" + ] + } + }, + "response": [] + } + ] + }, + { + "name": "ShopItems", + "item": [ + { + "name": "01-Get", + "request": { + "method": "GET", + "header": [ + { + "key": "Content-Type", + "value": "application/json", + "type": "default" + }, + { + "key": "Accept", + "value": "application/json", + "type": "default" + } + ], + "url": { + "raw": "https://localhost:7151/api/ShopItems/404c8232-9048-4519-bfba-6e78dc7005ca?currentPage=1&itemsPerPage=4", + "protocol": "https", + "host": [ + "localhost" + ], + "port": "7151", + "path": [ + "api", + "ShopItems", + "404c8232-9048-4519-bfba-6e78dc7005ca" + ], + "query": [ + { + "key": "currentPage", + "value": "1" + }, + { + "key": "itemsPerPage", + "value": "4" + } + ] + } + }, + "response": [] + }, + { + "name": "02-Get localized", + "request": { + "method": "GET", + "header": [ + { + "key": "Content-Type", + "value": "application/json", + "type": "default" + }, + { + "key": "Accept", + "value": "application/json", + "type": "default" + } + ], + "url": { + "raw": "https://localhost:7151/api/ShopItems/404c8232-9048-4519-bfba-6e78dc7005ca?currentPage=2&itemsPerPage=2&locale=en-US", + "protocol": "https", + "host": [ + "localhost" + ], + "port": "7151", + "path": [ + "api", + "ShopItems", + "404c8232-9048-4519-bfba-6e78dc7005ca" + ], + "query": [ + { + "key": "currentPage", + "value": "2" + }, + { + "key": "itemsPerPage", + "value": "2" + }, + { + "key": "locale", + "value": "en-US" + } + ] + } + }, + "response": [] + }, + { + "name": "03-Delete", + "request": { + "method": "DELETE", + "header": [ + { + "key": "Content-Type", + "value": "application/json", + "type": "default" + }, + { + "key": "Accept", + "value": "application/json", + "type": "default" + } + ], + "url": { + "raw": "https://localhost:7151/api/ShopItems/404c8232-9048-4519-bfba-6e78dc7005ca", + "protocol": "https", + "host": [ + "localhost" + ], + "port": "7151", + "path": [ + "api", + "ShopItems", + "404c8232-9048-4519-bfba-6e78dc7005ca" + ] + } + }, + "response": [] } ] }, @@ -112,7 +665,7 @@ } ], "url": { - "raw": "https://localhost:7151/api/ShopCartItem/404c8232-9048-4519-bfba-6e78dc7005ca/fdc5aa50-ee68-4bae-a8e6-b8ae2c258f60/SKU-01?locale=en-US", + "raw": "https://localhost:7151/api/ShopCartItem/404c8232-9048-4519-bfba-6e78dc7005ca/fdc5aa50-ee68-4bae-a8e6-b8ae2c258f60/SKU-04?locale=en-US", "protocol": "https", "host": [ "localhost" @@ -123,7 +676,7 @@ "ShopCartItem", "404c8232-9048-4519-bfba-6e78dc7005ca", "fdc5aa50-ee68-4bae-a8e6-b8ae2c258f60", - "SKU-01" + "SKU-04" ], "query": [ { @@ -156,7 +709,7 @@ "raw": "{\r\n \"quantity\": 1\r\n}" }, "url": { - "raw": "https://localhost:7151/api/ShopCartItem/404c8232-9048-4519-bfba-6e78dc7005ca/fdc5aa50-ee68-4bae-a8e6-b8ae2c258f60/SKU-04", + "raw": "https://localhost:7151/api/ShopCartItem/404c8232-9048-4519-bfba-6e78dc7005ca/fdc5aa50-ee68-4bae-a8e6-b8ae2c258f60/SKU-05", "protocol": "https", "host": [ "localhost" @@ -167,7 +720,7 @@ "ShopCartItem", "404c8232-9048-4519-bfba-6e78dc7005ca", "fdc5aa50-ee68-4bae-a8e6-b8ae2c258f60", - "SKU-04" + "SKU-05" ] } }, @@ -248,50 +801,8 @@ ] }, { - "name": "ShopItems", + "name": "ShopCartItems", "item": [ - { - "name": "01-Get", - "request": { - "method": "GET", - "header": [ - { - "key": "Content-Type", - "value": "application/json", - "type": "default" - }, - { - "key": "Accept", - "value": "application/json", - "type": "default" - } - ], - "url": { - "raw": "https://localhost:7151/api/ShopItems/404c8232-9048-4519-bfba-6e78dc7005ca?currentPage=1&itemsPerPage=4", - "protocol": "https", - "host": [ - "localhost" - ], - "port": "7151", - "path": [ - "api", - "ShopItems", - "404c8232-9048-4519-bfba-6e78dc7005ca" - ], - "query": [ - { - "key": "currentPage", - "value": "1" - }, - { - "key": "itemsPerPage", - "value": "4" - } - ] - } - }, - "response": [] - }, { "name": "01-Get localized", "request": { @@ -309,7 +820,7 @@ } ], "url": { - "raw": "https://localhost:7151/api/ShopItems/404c8232-9048-4519-bfba-6e78dc7005ca?currentPage=2&itemsPerPage=2&locale=en-US", + "raw": "https://localhost:7151/api/ShopCartItems/404c8232-9048-4519-bfba-6e78dc7005ca/fdc5aa50-ee68-4bae-a8e6-b8ae2c258f60?locale=en-US", "protocol": "https", "host": [ "localhost" @@ -317,18 +828,11 @@ "port": "7151", "path": [ "api", - "ShopItems", - "404c8232-9048-4519-bfba-6e78dc7005ca" + "ShopCartItems", + "404c8232-9048-4519-bfba-6e78dc7005ca", + "fdc5aa50-ee68-4bae-a8e6-b8ae2c258f60" ], "query": [ - { - "key": "currentPage", - "value": "2" - }, - { - "key": "itemsPerPage", - "value": "2" - }, { "key": "locale", "value": "en-US" @@ -339,7 +843,7 @@ "response": [] }, { - "name": "01-Delete", + "name": "02-Delete", "request": { "method": "DELETE", "header": [ @@ -355,7 +859,7 @@ } ], "url": { - "raw": "https://localhost:7151/api/ShopItems/404c8232-9048-4519-bfba-6e78dc7005ca", + "raw": "https://localhost:7151/api/ShopCartItems/404c8232-9048-4519-bfba-6e78dc7005ca/fdc5aa50-ee68-4bae-a8e6-b8ae2c258f60", "protocol": "https", "host": [ "localhost" @@ -363,8 +867,9 @@ "port": "7151", "path": [ "api", - "ShopItems", - "404c8232-9048-4519-bfba-6e78dc7005ca" + "ShopCartItems", + "404c8232-9048-4519-bfba-6e78dc7005ca", + "fdc5aa50-ee68-4bae-a8e6-b8ae2c258f60" ] } }, @@ -373,7 +878,7 @@ ] }, { - "name": "ShopItem", + "name": "BlogItem", "item": [ { "name": "01-Get", @@ -392,7 +897,7 @@ } ], "url": { - "raw": "https://localhost:7151/api/ShopItem/404c8232-9048-4519-bfba-6e78dc7005ca/SKU-01", + "raw": "https://localhost:7151/api/BlogItem/404c8232-9048-4519-bfba-6e78dc7005ca/11f2f5f8-1270-4640-b082-c2e7ea8e60b4", "protocol": "https", "host": [ "localhost" @@ -400,16 +905,16 @@ "port": "7151", "path": [ "api", - "ShopItem", + "BlogItem", "404c8232-9048-4519-bfba-6e78dc7005ca", - "SKU-01" + "11f2f5f8-1270-4640-b082-c2e7ea8e60b4" ] } }, "response": [] }, { - "name": "01-GetSlug", + "name": "02-GetSlug", "request": { "method": "GET", "header": [ @@ -425,7 +930,7 @@ } ], "url": { - "raw": "https://localhost:7151/api/ShopItem/404c8232-9048-4519-bfba-6e78dc7005ca?slug=shop-catalog-item-01", + "raw": "https://localhost:7151/api/BlogItem/404c8232-9048-4519-bfba-6e78dc7005ca?slug=privacy-terms-and-conditions", "protocol": "https", "host": [ "localhost" @@ -433,13 +938,13 @@ "port": "7151", "path": [ "api", - "ShopItem", + "BlogItem", "404c8232-9048-4519-bfba-6e78dc7005ca" ], "query": [ { "key": "slug", - "value": "shop-catalog-item-01" + "value": "privacy-terms-and-conditions" } ] } @@ -447,7 +952,7 @@ "response": [] }, { - "name": "02-Post", + "name": "03-Post", "request": { "method": "POST", "header": [ @@ -464,10 +969,10 @@ ], "body": { "mode": "raw", - "raw": "{\r\n \"l10n\":[\r\n {\r\n \"locale\":\"en-US\",\r\n \"slug\":\"shop-catalog-item-05\",\r\n \"description\": \"\",\r\n \"title\":\"Shop item title\",\r\n \"shortText\":\"Lorem ipsum, dolor sit amet consectetur adipisicing elit. Eaque fugit ratione dicta mollitia. Officiis ad...\",\r\n \"text\":\"\",\r\n \"contentType\": \"HTML\",\r\n \"badges\":[\r\n \"sale\"\r\n ]\r\n }\r\n ],\r\n \r\n \"tags\":[\r\n \"react\",\r\n \"redux\",\r\n \"webapi\"\r\n ],\r\n \"categories\":[\r\n \"e154e33f-3cc7-468d-bb66-e0390ddb9ae0\"\r\n ],\r\n \"brandName\":\"Mongodb Brand & Name\",\r\n \"rating\":4.5,\r\n \"price\":20,\r\n \"newPrice\":10,\r\n \"quantity\":100\r\n}" + "raw": "{\r\n\r\n \"l10n\": [\r\n {\r\n \"locale\": \"en-US\",\r\n \"slug\": \"hello-world\",\r\n \"description\": \"Hello world description\",\r\n \"title\": \"Hello world title\",\r\n \"shortText\": \"Hello world short text\",\r\n \"text\": \"

Hello World

\",\r\n \"contentType\": \"HTML\",\r\n \"badges\": [\r\n \"post\"\r\n ]\r\n }\r\n ],\r\n \"images\": [\r\n {\r\n \"l10n\": [\r\n {\r\n \"locale\": \"en-US\",\r\n \"alt\": \"...\"\r\n },\r\n {\r\n \"locale\": \"it-IT\",\r\n \"alt\": \"...\"\r\n }\r\n ]\r\n }\r\n ],\r\n \"tags\": [\r\n \"privacy\",\r\n \"terms\"\r\n ],\r\n \"categories\": [\r\n \"e154e33f-3cc7-468d-bb66-e0390ddb9ae0\" \r\n ],\r\n \"familyFriendly\": true\r\n}" }, "url": { - "raw": "https://localhost:7151/api/ShopItem/404c8232-9048-4519-bfba-6e78dc7005ca/SKU-05", + "raw": "https://localhost:7151/api/BlogItem/404c8232-9048-4519-bfba-6e78dc7005ca", "protocol": "https", "host": [ "localhost" @@ -475,16 +980,15 @@ "port": "7151", "path": [ "api", - "ShopItem", - "404c8232-9048-4519-bfba-6e78dc7005ca", - "SKU-05" + "BlogItem", + "404c8232-9048-4519-bfba-6e78dc7005ca" ] } }, "response": [] }, { - "name": "03-Put", + "name": "04-Update", "request": { "method": "PUT", "header": [ @@ -501,10 +1005,10 @@ ], "body": { "mode": "raw", - "raw": "{\r\n \"slug\":\"shop-catalog-item-05\",\r\n \"l10n\":[\r\n {\r\n \"locale\":\"en-US\",\r\n \"slug\":\"shop-catalog-item-05\",\r\n \"description\": \"\",\r\n \"title\":\"Shop item title\",\r\n \"shortText\":\"Lorem ipsum, dolor sit amet consectetur adipisicing elit. Eaque fugit ratione dicta mollitia. Officiis ad...\",\r\n \"text\":\"\",\r\n \"contentType\": \"HTML\",\r\n \"badges\":[\r\n \"sale\"\r\n ]\r\n }\r\n ],\r\n \"images\":[\r\n {\r\n \"src\":\"https://dummyimage.com/450x300/dee2e6/6c757d.jpg\",\r\n \"l10n\":[\r\n {\r\n \"locale\":\"en-US\",\r\n \"alt\":\"...\"\r\n }\r\n ]\r\n }\r\n ],\r\n \"author\":\"fdc5aa50-ee68-4bae-a8e6-b8ae2c258f60\",\r\n \"tags\":[\r\n \"react\",\r\n \"redux\",\r\n \"webapi\"\r\n ],\r\n \"categories\":[\r\n \"e154e33f-3cc7-468d-bb66-e0390ddb9ae0\"\r\n ],\r\n \"brandName\":\"Mongodb Brand & Name\",\r\n \"rating\":4.5,\r\n \"price\":20,\r\n \"newPrice\":10,\r\n \"quantity\":200\r\n}" + "raw": "{\r\n\r\n \"l10n\": [\r\n {\r\n \"locale\": \"en-US\",\r\n \"slug\": \"hello-world\",\r\n \"description\": \"Hello world description\",\r\n \"title\": \"Hello world title\",\r\n \"shortText\": \"Hello world short text\",\r\n \"text\": \"

New Hello World

\",\r\n \"contentType\": \"HTML\",\r\n \"badges\": [\r\n \"post\"\r\n ]\r\n }\r\n ],\r\n \"images\": [\r\n {\r\n \"l10n\": [\r\n {\r\n \"locale\": \"en-US\",\r\n \"alt\": \"...\"\r\n },\r\n {\r\n \"locale\": \"it-IT\",\r\n \"alt\": \"...\"\r\n }\r\n ]\r\n }\r\n ],\r\n \"tags\": [\r\n \"privacy\",\r\n \"terms\"\r\n ],\r\n \"categories\": [\r\n \"e154e33f-3cc7-468d-bb66-e0390ddb9ae0\" \r\n ],\r\n \"familyFriendly\": true\r\n}" }, "url": { - "raw": "https://localhost:7151/api/ShopItem/404c8232-9048-4519-bfba-6e78dc7005ca/SKU-05", + "raw": "https://localhost:7151/api/BlogItem/404c8232-9048-4519-bfba-6e78dc7005ca/94feda90-ddf6-4717-8879-9c7139e7ff30", "protocol": "https", "host": [ "localhost" @@ -512,16 +1016,16 @@ "port": "7151", "path": [ "api", - "ShopItem", + "BlogItem", "404c8232-9048-4519-bfba-6e78dc7005ca", - "SKU-05" + "94feda90-ddf6-4717-8879-9c7139e7ff30" ] } }, "response": [] }, { - "name": "04-Delete", + "name": "05-Delete", "request": { "method": "DELETE", "header": [ @@ -537,7 +1041,7 @@ } ], "url": { - "raw": "https://localhost:7151/api/ShopItem/404c8232-9048-4519-bfba-6e78dc7005ca/SKU-05", + "raw": "https://localhost:7151/api/BlogItem/404c8232-9048-4519-bfba-6e78dc7005ca/94feda90-ddf6-4717-8879-9c7139e7ff30", "protocol": "https", "host": [ "localhost" @@ -545,9 +1049,9 @@ "port": "7151", "path": [ "api", - "ShopItem", + "BlogItem", "404c8232-9048-4519-bfba-6e78dc7005ca", - "SKU-05" + "94feda90-ddf6-4717-8879-9c7139e7ff30" ] } }, @@ -679,301 +1183,6 @@ "response": [] } ] - }, - { - "name": "BlogItem", - "item": [ - { - "name": "01-Get", - "request": { - "method": "GET", - "header": [ - { - "key": "Content-Type", - "value": "application/json", - "type": "default" - }, - { - "key": "Accept", - "value": "application/json", - "type": "default" - } - ], - "url": { - "raw": "https://localhost:7151/api/BlogItem/404c8232-9048-4519-bfba-6e78dc7005ca/11f2f5f8-1270-4640-b082-c2e7ea8e60b4", - "protocol": "https", - "host": [ - "localhost" - ], - "port": "7151", - "path": [ - "api", - "BlogItem", - "404c8232-9048-4519-bfba-6e78dc7005ca", - "11f2f5f8-1270-4640-b082-c2e7ea8e60b4" - ] - } - }, - "response": [] - } - ] - }, - { - "name": "CategoryItems", - "item": [ - { - "name": "01-Get", - "request": { - "method": "GET", - "header": [ - { - "key": "Content-Type", - "value": "application/json", - "type": "default" - }, - { - "key": "Accept", - "value": "application/json", - "type": "default" - } - ], - "url": { - "raw": "https://localhost:7151/api/CategoryItems/404c8232-9048-4519-bfba-6e78dc7005ca?locale=en-US", - "protocol": "https", - "host": [ - "localhost" - ], - "port": "7151", - "path": [ - "api", - "CategoryItems", - "404c8232-9048-4519-bfba-6e78dc7005ca" - ], - "query": [ - { - "key": "locale", - "value": "en-US" - } - ] - } - }, - "response": [] - }, - { - "name": "01-Get Copy", - "request": { - "method": "GET", - "header": [ - { - "key": "Content-Type", - "value": "application/json", - "type": "default" - }, - { - "key": "Accept", - "value": "application/json", - "type": "default" - } - ], - "url": { - "raw": "https://localhost:7151/api/CategoryItems/404c8232-9048-4519-bfba-6e78dc7005ca", - "protocol": "https", - "host": [ - "localhost" - ], - "port": "7151", - "path": [ - "api", - "CategoryItems", - "404c8232-9048-4519-bfba-6e78dc7005ca" - ] - } - }, - "response": [] - } - ] - }, - { - "name": "CategoryItem", - "item": [ - { - "name": "01-Get", - "request": { - "method": "GET", - "header": [ - { - "key": "Content-Type", - "value": "application/json", - "type": "default" - }, - { - "key": "Accept", - "value": "application/json", - "type": "default" - } - ], - "url": { - "raw": "https://localhost:7151/api/CategoryItem/404c8232-9048-4519-bfba-6e78dc7005ca/e154e33f-3cc7-468d-bb66-e0390ddb9ae0", - "protocol": "https", - "host": [ - "localhost" - ], - "port": "7151", - "path": [ - "api", - "CategoryItem", - "404c8232-9048-4519-bfba-6e78dc7005ca", - "e154e33f-3cc7-468d-bb66-e0390ddb9ae0" - ] - } - }, - "response": [] - }, - { - "name": "01-GetSlug", - "request": { - "method": "GET", - "header": [ - { - "key": "Content-Type", - "value": "application/json", - "type": "default" - }, - { - "key": "Accept", - "value": "application/json", - "type": "default" - } - ], - "url": { - "raw": "https://localhost:7151/api/CategoryItem/404c8232-9048-4519-bfba-6e78dc7005ca?slug=default", - "protocol": "https", - "host": [ - "localhost" - ], - "port": "7151", - "path": [ - "api", - "CategoryItem", - "404c8232-9048-4519-bfba-6e78dc7005ca" - ], - "query": [ - { - "key": "slug", - "value": "default" - } - ] - } - }, - "response": [] - }, - { - "name": "02-Post", - "request": { - "method": "POST", - "header": [ - { - "key": "Content-Type", - "value": "application/json", - "type": "default" - }, - { - "key": "Accept", - "value": "application/json", - "type": "default" - } - ], - "body": { - "mode": "raw", - "raw": "{\r\n \"l10n\": [\r\n {\r\n \"locale\": \"en-US\",\r\n \"slug\": \"guides\",\r\n \"text\": \"Guides\"\r\n }\r\n ]\r\n}" - }, - "url": { - "raw": "https://localhost:7151/api/CategoryItem/404c8232-9048-4519-bfba-6e78dc7005ca", - "protocol": "https", - "host": [ - "localhost" - ], - "port": "7151", - "path": [ - "api", - "CategoryItem", - "404c8232-9048-4519-bfba-6e78dc7005ca" - ] - } - }, - "response": [] - }, - { - "name": "03-Put", - "request": { - "method": "PUT", - "header": [ - { - "key": "Content-Type", - "value": "application/json", - "type": "default" - }, - { - "key": "Accept", - "value": "application/json", - "type": "default" - } - ], - "body": { - "mode": "raw", - "raw": "{\r\n \"l10n\": [\r\n {\r\n \"locale\": \"en-US\",\r\n \"slug\": \"guides\",\r\n \"text\": \"Guides\"\r\n },\r\n {\r\n \"locale\": \"it-IT\",\r\n \"slug\": \"guide\",\r\n \"text\": \"Guide\"\r\n }\r\n ]\r\n}" - }, - "url": { - "raw": "https://localhost:7151/api/CategoryItem/404c8232-9048-4519-bfba-6e78dc7005ca/2c50f4de-70f2-4414-aabc-7a0d2eb0e203", - "protocol": "https", - "host": [ - "localhost" - ], - "port": "7151", - "path": [ - "api", - "CategoryItem", - "404c8232-9048-4519-bfba-6e78dc7005ca", - "2c50f4de-70f2-4414-aabc-7a0d2eb0e203" - ] - } - }, - "response": [] - }, - { - "name": "04-Delete", - "request": { - "method": "DELETE", - "header": [ - { - "key": "Content-Type", - "value": "application/json", - "type": "default" - }, - { - "key": "Accept", - "value": "application/json", - "type": "default" - } - ], - "url": { - "raw": "https://localhost:7151/api/CategoryItem/404c8232-9048-4519-bfba-6e78dc7005ca/2c50f4de-70f2-4414-aabc-7a0d2eb0e203", - "protocol": "https", - "host": [ - "localhost" - ], - "port": "7151", - "path": [ - "api", - "CategoryItem", - "404c8232-9048-4519-bfba-6e78dc7005ca", - "2c50f4de-70f2-4414-aabc-7a0d2eb0e203" - ] - } - }, - "response": [] - } - ] } ] } \ No newline at end of file diff --git a/webapi/Core/DomainObjects/Documents/BlogItem.cs b/webapi/Core/DomainObjects/Documents/BlogItem.cs index 68ffcc9..1ed721b 100644 --- a/webapi/Core/DomainObjects/Documents/BlogItem.cs +++ b/webapi/Core/DomainObjects/Documents/BlogItem.cs @@ -17,8 +17,11 @@ namespace Core.DomainObjects.Documents { hash = hash * 23 + Images.Sum(x => x.GetHashCode()); hash = hash * 23 + Author.GetHashCode(); hash = hash * 23 + Created.GetHashCode(); - hash = hash * 23 + Tags.Sum(x => x.GetHashCode()); - hash = hash * 23 + Categories.Sum(x => x.GetHashCode()); + if(Tags != null) + hash = hash * 23 + Tags.Sum(x => x.GetHashCode()); + + if(Categories != null) + hash = hash * 23 + Categories.Sum(x => x.GetHashCode()); hash = hash * 23 + ReadTime.GetHashCode(); hash = hash * 23 + Likes.GetHashCode(); diff --git a/webapi/DataProviders/CategoryDataProvider.cs b/webapi/DataProviders/CategoryDataProvider.cs index d6e978f..e69f7a5 100644 --- a/webapi/DataProviders/CategoryDataProvider.cs +++ b/webapi/DataProviders/CategoryDataProvider.cs @@ -65,6 +65,6 @@ namespace DataProviders { DeleteWithPredicate(x => x.Id == id, _collectionName); public IDomainResult DeleteAll(Guid siteId) => - DeleteWithPredicate(x => x.SiteId == siteId, _collectionName); + DeleteManyWithPredicate(x => x.SiteId == siteId, _collectionName); } } diff --git a/webapi/DataProviders/ShopCartDataProvider.cs b/webapi/DataProviders/ShopCartDataProvider.cs index 0c82ed4..c61cd1f 100644 --- a/webapi/DataProviders/ShopCartDataProvider.cs +++ b/webapi/DataProviders/ShopCartDataProvider.cs @@ -50,6 +50,6 @@ namespace DataProviders { DeleteWithPredicate(x => x.Id == id, _collectionName); public IDomainResult DeleteAll(Guid siteId, Guid userId) => - DeleteWithPredicate(x => x.SiteId == siteId && x.UserId == userId, _collectionName); + DeleteManyWithPredicate(x => x.SiteId == siteId && x.UserId == userId, _collectionName); } } diff --git a/webapi/Tests/Extensions/IEquatableExtensionsTests.cs b/webapi/Tests/Extensions/IEquatableExtensionsTests.cs index 1433754..edac496 100644 --- a/webapi/Tests/Extensions/IEquatableExtensionsTests.cs +++ b/webapi/Tests/Extensions/IEquatableExtensionsTests.cs @@ -19,7 +19,14 @@ namespace ExtensionsTests { } public override int GetHashCode() { - return Test.GetHashCode(); + unchecked { + int hash = 17; + + if (Test != null) + hash = hash * 23 + Test.GetHashCode(); + + return hash; + } } } diff --git a/webapi/Tests/Extensions/ObjectExtensionsTests.cs b/webapi/Tests/Extensions/ObjectExtensionsTests.cs index 42128d4..bec3ef4 100644 --- a/webapi/Tests/Extensions/ObjectExtensionsTests.cs +++ b/webapi/Tests/Extensions/ObjectExtensionsTests.cs @@ -7,7 +7,7 @@ namespace ExtensionsTests { public class ObjectExtensions { private class DummyClass { - public string Test { get; set; } + public string? Test { get; set; } } [Fact] diff --git a/webapi/Tests/Extensions/StringExtensionsTests.cs b/webapi/Tests/Extensions/StringExtensionsTests.cs index a47b482..a1ed30d 100644 --- a/webapi/Tests/Extensions/StringExtensionsTests.cs +++ b/webapi/Tests/Extensions/StringExtensionsTests.cs @@ -17,12 +17,12 @@ using System.Threading.Tasks; namespace ExtensionsTests { public class StringExtensions : ServicesBase { - private readonly Configuration _configuration; + // private readonly Configuration _configuration; - public StringExtensions() { - _configuration = ServiceProvider.GetService>()?.Value - ?? throw new NullReferenceException("Configuration not found"); - } + // public StringExtensions() { + // _configuration = ServiceProvider.GetService>()?.Value + // ?? throw new NullReferenceException("Configuration not found"); + // } [Fact] @@ -93,7 +93,7 @@ namespace ExtensionsTests { [Fact] public void ToNullableInt() { var value = "0".ToNullableInt(); - Assert.True(value.GetType() == typeof(int) && value == 0 && "".ToNullableInt() == null); + Assert.True(value != null && value.GetType() == typeof(int?) && value == 0 && "".ToNullableInt() == null); } [Fact] @@ -105,7 +105,7 @@ namespace ExtensionsTests { [Fact] public void ToNullableUint() { var value = "0".ToNullableUint(); - Assert.True(value.GetType() == typeof(uint) && value == 0 && "".ToNullableUint() == null); + Assert.True(value != null && value.GetType() == typeof(uint) && value == 0 && "".ToNullableUint() == null); } [Fact] @@ -117,7 +117,7 @@ namespace ExtensionsTests { [Fact] public void ToNullableDecimal() { var value = "0".ToNullableDecimal(); - Assert.True(value.GetType() == typeof(decimal) && "".ToNullableDecimal() == null); + Assert.True(value != null && value.GetType() == typeof(decimal) && "".ToNullableDecimal() == null); } [Fact] @@ -129,7 +129,7 @@ namespace ExtensionsTests { [Fact] public void ToNullableDouble() { var value = "0".ToNullableDouble(); - Assert.True(value.GetType() == typeof(double) && value == 0 && "".ToNullableDouble() == null); + Assert.True(value != null && value.GetType() == typeof(double) && value == 0 && "".ToNullableDouble() == null); } [Fact] @@ -163,7 +163,12 @@ namespace ExtensionsTests { [Fact] public void ToNullalbeBool() { - Assert.True("true".ToNullableBool().Value && !"false".ToNullableBool().Value && "".ToNullableBool() == null); + var trueTest = "true".ToNullableBool(); + var falseTest = "false".ToNullableBool(); + if (trueTest == null || falseTest == null) + Assert.False(true); + else + Assert.True(trueTest.Value && !falseTest.Value && "".ToNullableBool() == null); } [Fact] @@ -195,13 +200,17 @@ namespace ExtensionsTests { public void Excerpt() => Assert.Equal("M...", "MAKS-IT".Excerpt(4)); private class DummyClass { - public string Test { get; set; } + public string? Test { get; set; } } [Fact] public void ToObject() { var obj = "{ \"Test\": \"Test\" }".ToObject(); - Assert.Equal("Test", obj.Test); + + if (obj == null) + Assert.False(true); + else + Assert.Equal("Test", obj.Test); } public static IEnumerable GetEmailValidationData() { diff --git a/webapi/WeatherForecast/Models/Abstractions/PageModelBase.cs b/webapi/WeatherForecast/Models/Abstractions/PageModelBase.cs index a1108fe..c578442 100644 --- a/webapi/WeatherForecast/Models/Abstractions/PageModelBase.cs +++ b/webapi/WeatherForecast/Models/Abstractions/PageModelBase.cs @@ -23,11 +23,6 @@ namespace WeatherForecast.Models.Abstractions { /// public TitleSectionModel? TitleSection { get; set; } - /// - /// - /// - public PageModelBase() { } - /// /// /// diff --git a/webapi/WeatherForecast/Models/Abstractions/PersonModelBase.cs b/webapi/WeatherForecast/Models/Abstractions/PersonModelBase.cs index 18c8eaa..23373c5 100644 --- a/webapi/WeatherForecast/Models/Abstractions/PersonModelBase.cs +++ b/webapi/WeatherForecast/Models/Abstractions/PersonModelBase.cs @@ -18,7 +18,7 @@ namespace WeatherForecast.Models.Abstractions { /// /// /// - public ImageModel? Image { get; set; } + public ImageResponseModel? Image { get; set; } /// /// diff --git a/webapi/WeatherForecast/Models/Abstractions/PostItemResponseModelBase.cs b/webapi/WeatherForecast/Models/Abstractions/PostItemResponseModelBase.cs index 5f7b751..cfeab7e 100644 --- a/webapi/WeatherForecast/Models/Abstractions/PostItemResponseModelBase.cs +++ b/webapi/WeatherForecast/Models/Abstractions/PostItemResponseModelBase.cs @@ -2,6 +2,7 @@ using Core.Abstractions.Models; using Core.DomainObjects; using Core.Enumerations; +using WeatherForecast.Models.Responses; using WeatherForecast.Models.Responses.L10n; namespace WeatherForecast.Models.Abstractions { @@ -73,7 +74,7 @@ namespace WeatherForecast.Models.Abstractions { /// /// /// - public List? Images { get; set; } + public List? Images { get; set; } /// /// @@ -93,7 +94,7 @@ namespace WeatherForecast.Models.Abstractions { /// /// /// - public List? Categories { get; set; } + public List? Categories { get; set; } /// /// @@ -121,8 +122,8 @@ namespace WeatherForecast.Models.Abstractions { /// public PostItemResponseModelBase(PostItemBase postItem, List categories) : this(postItem) { L10n = postItem.L10n.Select(x => new PostItemL10nModel(x)).ToList(); - Categories = categories.Select(x => new CategoryModel(x)).ToList(); - Images = postItem.Images?.Select(x => new ImageModel(x)).ToList(); + Categories = categories.Select(x => new CategoryItemResponseModel(x)).ToList(); + Images = postItem.Images?.Select(x => new ImageResponseModel(x)).ToList(); } /// @@ -133,7 +134,8 @@ namespace WeatherForecast.Models.Abstractions { /// public PostItemResponseModelBase(PostItemBase postItem, List categories, Locales locale) : this(postItem) { - var postItemL10n = postItem.L10n.SingleOrDefault(x => x.Locale == locale); + var postItemL10n = postItem.L10n.Single(x => x.Locale == locale); + if (postItemL10n != null) { Slug = postItemL10n.Slug; Badges = postItemL10n.Badges; @@ -144,8 +146,8 @@ namespace WeatherForecast.Models.Abstractions { Badges = postItemL10n.Badges; } - Categories = categories.Select(x => new CategoryModel(x, locale)).ToList(); - Images = postItem.Images?.Select(x => new ImageModel(x, locale)).ToList(); + Categories = categories.Select(x => new CategoryItemResponseModel(x, locale)).ToList(); + Images = postItem.Images?.Select(x => new ImageResponseModel(x, locale)).ToList(); } } } diff --git a/webapi/WeatherForecast/Models/CategoryModel.cs b/webapi/WeatherForecast/Models/CategoryModel.cs deleted file mode 100644 index 6a8bcf0..0000000 --- a/webapi/WeatherForecast/Models/CategoryModel.cs +++ /dev/null @@ -1,48 +0,0 @@ -using Core.DomainObjects; -using Core.Enumerations; -using WeatherForecast.Models.L10n; - -namespace WeatherForecast.Models { - - /// - /// - /// - public class CategoryModel { - - /// - /// - /// - public List? L10n { get; set; } - - /// - /// - /// - public string? Slug { get; set; } - - /// - /// - /// - public string? Text { get; set; } - - /// - /// - /// - /// - public CategoryModel(Category category) { - L10n = category.L10n.Select(x => new CategoryL10nModel(x)).ToList(); - } - - /// - /// - /// - /// - /// - public CategoryModel(Category category, Locales locale) { - var categoryL10n = category.L10n.SingleOrDefault(x => x.Locale == locale); - if (categoryL10n != null) { - Slug = categoryL10n.Slug; - Text = categoryL10n.Text; - } - } - } -} diff --git a/webapi/WeatherForecast/Models/CommentModel.cs b/webapi/WeatherForecast/Models/CommentModel.cs index 24b5b15..d4b55d0 100644 --- a/webapi/WeatherForecast/Models/CommentModel.cs +++ b/webapi/WeatherForecast/Models/CommentModel.cs @@ -1,4 +1,6 @@ -namespace WeatherForecast.Models { +using Core.DomainObjects; + +namespace WeatherForecast.Models { /// /// @@ -8,16 +10,18 @@ /// /// /// - public AuthorModel Author { get; set; } + public AuthorModel? Author { get; set; } /// /// /// - public string Comment { get; set; } + public string? Comment { get; set; } /// /// /// public List? Responses { get; set; } + + } } diff --git a/webapi/WeatherForecast/Models/PageSections/FeaturedBlogSectionModel.cs b/webapi/WeatherForecast/Models/PageSections/FeaturedBlogSectionModel.cs index cabf76a..af91f1b 100644 --- a/webapi/WeatherForecast/Models/PageSections/FeaturedBlogSectionModel.cs +++ b/webapi/WeatherForecast/Models/PageSections/FeaturedBlogSectionModel.cs @@ -13,11 +13,6 @@ namespace WeatherForecast.Models.PageSections { /// public string ReadTime { get; set; } - /// - /// - /// - public FeaturedBlogSectionModel() : base() { } - /// /// /// diff --git a/webapi/WeatherForecast/Models/PageSections/TitleSectionModel.cs b/webapi/WeatherForecast/Models/PageSections/TitleSectionModel.cs index 73a424a..f571c71 100644 --- a/webapi/WeatherForecast/Models/PageSections/TitleSectionModel.cs +++ b/webapi/WeatherForecast/Models/PageSections/TitleSectionModel.cs @@ -11,7 +11,7 @@ namespace WeatherForecast.Models.PageSections { /// /// /// - public ImageModel? Image { get; set; } + public ImageResponseModel? Image { get; set; } /// /// diff --git a/webapi/WeatherForecast/Models/Requests/BlogItemRequestModel.cs b/webapi/WeatherForecast/Models/Requests/BlogItemRequestModel.cs index 89f0ff5..ba13950 100644 --- a/webapi/WeatherForecast/Models/Requests/BlogItemRequestModel.cs +++ b/webapi/WeatherForecast/Models/Requests/BlogItemRequestModel.cs @@ -26,7 +26,8 @@ namespace WeatherForecast.Models.Requests { /// /// public override BlogItem ToDomainObject() { - if (HasValidationErrors(this) || HasValidationErrorsBase(HasValidationErrors(this))) throw new ValidationException(); + if (HasValidationErrors(this) || HasValidationErrorsBase(HasValidationErrors(this))) + throw new ValidationException(); return new BlogItem() { L10n = L10n.Select(x => x.ToDomainObject()).ToList(), @@ -48,16 +49,11 @@ namespace WeatherForecast.Models.Requests { /// /// /// - public override IEnumerable Validate( ValidationContext validationContext) { + public override IEnumerable Validate(ValidationContext validationContext) { if (L10n.IsNullOrEmpty()) - yield return new ValidationResult($"{validationContext.DisplayName} ${Errors.NullOrEmpty}"); - else { - foreach (var item in L10n) - foreach (var validationResult in item.Validate(validationContext)) - yield return validationResult; - } + yield return new ValidationResult($"{nameof(L10n)} ${Errors.NullOrEmpty}"); } - private bool HasValidationErrors(BlogItemRequestModel validationContext) => Validate(new ValidationContext(validationContext)).Any(); + private bool HasValidationErrors(BlogItemRequestModel model) => Validate(new ValidationContext(model)).Any(); } } diff --git a/webapi/WeatherForecast/Models/Requests/CategoryItemRequestModel.cs b/webapi/WeatherForecast/Models/Requests/CategoryItemRequestModel.cs index 0cbe680..4830564 100644 --- a/webapi/WeatherForecast/Models/Requests/CategoryItemRequestModel.cs +++ b/webapi/WeatherForecast/Models/Requests/CategoryItemRequestModel.cs @@ -38,12 +38,7 @@ namespace WeatherForecast.Models.Requests { /// public override IEnumerable Validate(ValidationContext validationContext) { if (L10n.IsNullOrEmpty()) - yield return new ValidationResult($"{validationContext.DisplayName} ${Errors.NullOrEmpty}"); - else { - foreach (var item in L10n) - foreach (var validationResult in item.Validate(validationContext)) - yield return validationResult; - } + yield return new ValidationResult($"{nameof(L10n)} ${Errors.NullOrEmpty}"); } [MemberNotNullWhen(false, new[] { nameof(L10n) })] diff --git a/webapi/WeatherForecast/Models/Requests/ImageRequestModel.cs b/webapi/WeatherForecast/Models/Requests/ImageRequestModel.cs new file mode 100644 index 0000000..3b5259a --- /dev/null +++ b/webapi/WeatherForecast/Models/Requests/ImageRequestModel.cs @@ -0,0 +1,56 @@ +using Core.Abstractions.Models; +using Core.DomainObjects; +using Core.Enumerations; +using Extensions; +using System.ComponentModel.DataAnnotations; +using WeatherForecast.Models.Requests.L10n; + +namespace WeatherForecast.Models { + + /// + /// + /// + public class ImageRequestModel : RequestModelBase { + + /// + /// + /// + public List? L10n { get; set; } + + /// + /// + /// + public string? Src { get; set; } + + /// + /// + /// + public string? Alt { get; set; } + + /// + /// + /// + /// + /// + public override Image ToDomainObject() { + throw new NotImplementedException(); + } + + /// + /// + /// + /// + /// + /// + public override IEnumerable Validate(ValidationContext validationContext) { + if (L10n.IsNullOrEmpty()) + yield return new ValidationResult($"{nameof(L10n)} ${Errors.NullOrEmpty}"); + + if (string.IsNullOrWhiteSpace(Src)) + yield return new ValidationResult($"{nameof(Src)} ${Errors.NullOrWhiteSpace}"); + + if (string.IsNullOrWhiteSpace(Alt)) + yield return new ValidationResult($"{nameof(Alt)} ${Errors.NullOrWhiteSpace}"); + } + } +} diff --git a/webapi/WeatherForecast/Models/Requests/L10n/CategoryL10nModel.cs b/webapi/WeatherForecast/Models/Requests/L10n/CategoryL10nModel.cs index e6bb2e0..3b295d5 100644 --- a/webapi/WeatherForecast/Models/Requests/L10n/CategoryL10nModel.cs +++ b/webapi/WeatherForecast/Models/Requests/L10n/CategoryL10nModel.cs @@ -50,15 +50,15 @@ namespace WeatherForecast.Models.Requests.L10n { /// public override IEnumerable Validate(ValidationContext validationContext) { if (string.IsNullOrWhiteSpace(Locale)) - yield return new ValidationResult($"{validationContext.DisplayName} {Errors.NullOrWhiteSpace.Name}"); + yield return new ValidationResult($"{nameof(Locale)} {Errors.NullOrWhiteSpace.Name}"); else if (Enumeration.FromDisplayName(Locale) == null) - yield return new ValidationResult($"{validationContext.DisplayName} {Errors.WrongOrNotManaged}"); + yield return new ValidationResult($"{nameof(Locale)} {Errors.WrongOrNotManaged}"); if (string.IsNullOrWhiteSpace(Slug)) - yield return new ValidationResult($"{validationContext.DisplayName} {Errors.NullOrWhiteSpace.Name}"); + yield return new ValidationResult($"{nameof(Slug)} {Errors.NullOrWhiteSpace.Name}"); if (string.IsNullOrWhiteSpace(Text)) - yield return new ValidationResult($"{validationContext.DisplayName} {Errors.NullOrWhiteSpace.Name}"); + yield return new ValidationResult($"{nameof(Text)} {Errors.NullOrWhiteSpace.Name}"); } // https://www.meziantou.net/csharp-8-nullable-reference-types.htm diff --git a/webapi/WeatherForecast/Models/Requests/L10n/ImageL10nModel.cs b/webapi/WeatherForecast/Models/Requests/L10n/ImageL10nModel.cs index 4ac9a78..07b1236 100644 --- a/webapi/WeatherForecast/Models/Requests/L10n/ImageL10nModel.cs +++ b/webapi/WeatherForecast/Models/Requests/L10n/ImageL10nModel.cs @@ -42,13 +42,12 @@ namespace WeatherForecast.Models.Requests.L10n { /// public override IEnumerable Validate(ValidationContext validationContext) { if (string.IsNullOrWhiteSpace(Locale)) - yield return new ValidationResult($"{validationContext.DisplayName} {Errors.NullOrWhiteSpace.Name}"); + yield return new ValidationResult($"{nameof(Locale)} {Errors.NullOrWhiteSpace.Name}"); else if (Enumeration.FromDisplayName(Locale) == null) - yield return new ValidationResult($"{validationContext.DisplayName} {Errors.WrongOrNotManaged}"); + yield return new ValidationResult($"{nameof(Locale)} {Errors.WrongOrNotManaged}"); if (string.IsNullOrWhiteSpace(Alt)) - yield return new ValidationResult($"{validationContext.DisplayName} {Errors.NullOrWhiteSpace.Name}"); - + yield return new ValidationResult($"{nameof(Alt)} {Errors.NullOrWhiteSpace.Name}"); } [MemberNotNullWhen(false, new[] { nameof(Locale), nameof(Alt) })] diff --git a/webapi/WeatherForecast/Models/Requests/L10n/PostItemL10nModel.cs b/webapi/WeatherForecast/Models/Requests/L10n/PostItemL10nModel.cs index deca5d9..bf723f4 100644 --- a/webapi/WeatherForecast/Models/Requests/L10n/PostItemL10nModel.cs +++ b/webapi/WeatherForecast/Models/Requests/L10n/PostItemL10nModel.cs @@ -87,29 +87,29 @@ namespace WeatherForecast.Models.Requests.L10n { /// public override IEnumerable Validate(ValidationContext validationContext) { if (string.IsNullOrWhiteSpace(Locale)) - yield return new ValidationResult($"{validationContext.DisplayName} {Errors.NullOrWhiteSpace.Name}"); + yield return new ValidationResult($"{nameof(Locale)} {Errors.NullOrWhiteSpace.Name}"); else if (Enumeration.FromDisplayName(Locale) == null) - yield return new ValidationResult($"{validationContext.DisplayName} {Errors.WrongOrNotManaged}"); + yield return new ValidationResult($"{nameof(Locale)} {Errors.WrongOrNotManaged}"); if (string.IsNullOrWhiteSpace(Slug)) - yield return new ValidationResult($"{validationContext.DisplayName} {Errors.NullOrWhiteSpace.Name}"); + yield return new ValidationResult($"{nameof(Slug)} {Errors.NullOrWhiteSpace.Name}"); if (string.IsNullOrWhiteSpace(Description)) - yield return new ValidationResult($"{validationContext.DisplayName} {Errors.NullOrWhiteSpace.Name}"); + yield return new ValidationResult($"{nameof(Description)} {Errors.NullOrWhiteSpace.Name}"); if (string.IsNullOrWhiteSpace(Title)) - yield return new ValidationResult($"{validationContext.DisplayName} {Errors.NullOrWhiteSpace.Name}"); + yield return new ValidationResult($"{nameof(Title)} {Errors.NullOrWhiteSpace.Name}"); if (string.IsNullOrWhiteSpace(Text)) - yield return new ValidationResult($"{validationContext.DisplayName} {Errors.NullOrWhiteSpace.Name}"); + yield return new ValidationResult($"{nameof(Text)} {Errors.NullOrWhiteSpace.Name}"); if (string.IsNullOrWhiteSpace(ShortText)) - yield return new ValidationResult($"{validationContext.DisplayName} {Errors.NullOrWhiteSpace.Name}"); + yield return new ValidationResult($"{nameof(ShortText)} {Errors.NullOrWhiteSpace.Name}"); if (string.IsNullOrWhiteSpace(ContentType)) - yield return new ValidationResult($"{validationContext.DisplayName} {Errors.NullOrWhiteSpace.Name}"); + yield return new ValidationResult($"{nameof(ContentType)} {Errors.NullOrWhiteSpace.Name}"); else if (Enumeration.FromDisplayName(ContentType) == null) - yield return new ValidationResult($"{validationContext.DisplayName} {Errors.WrongOrNotManaged}"); + yield return new ValidationResult($"{nameof(ContentType)} {Errors.WrongOrNotManaged}"); } [MemberNotNullWhen(false, new[] { nameof(Locale), nameof(Slug), nameof(Description), nameof(Title), nameof(Text), nameof(ShortText), nameof(ContentType) })] diff --git a/webapi/WeatherForecast/Models/Requests/ShopCartItemRequestModel.cs b/webapi/WeatherForecast/Models/Requests/ShopCartItemRequestModel.cs index e4ed187..9a75f7b 100644 --- a/webapi/WeatherForecast/Models/Requests/ShopCartItemRequestModel.cs +++ b/webapi/WeatherForecast/Models/Requests/ShopCartItemRequestModel.cs @@ -36,8 +36,8 @@ namespace WeatherForecast.Models.Requests { /// /// public override IEnumerable Validate(ValidationContext validationContext) { - if (Quantity == null) - yield return new ValidationResult($"{validationContext.DisplayName} ${Errors.NullOrEmpty}"); + if (Quantity == null || (Quantity != null && Quantity == 0)) + yield return new ValidationResult($"{nameof(Quantity)} ${Errors.NullOrEmpty}"); } [MemberNotNullWhen(false, new[] { nameof(Quantity) })] diff --git a/webapi/WeatherForecast/Models/Requests/ShopItemRequestModel.cs b/webapi/WeatherForecast/Models/Requests/ShopItemRequestModel.cs index 66f32c8..ff0c969 100644 --- a/webapi/WeatherForecast/Models/Requests/ShopItemRequestModel.cs +++ b/webapi/WeatherForecast/Models/Requests/ShopItemRequestModel.cs @@ -1,13 +1,11 @@ -using Core.Abstractions.Models; -using Core.DomainObjects; -using Core.DomainObjects.Documents; -using Core.DomainObjects.L10n; -using Core.Enumerations; -using ExtensionMethods; -using Extensions; -using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations; using System.Diagnostics.CodeAnalysis; + using WeatherForecast.Models.Abstractions; +using Core.DomainObjects.Documents; +using Core.Enumerations; +using Extensions; + namespace WeatherForecast.Models.Requests { @@ -46,7 +44,8 @@ namespace WeatherForecast.Models.Requests { /// /// public override ShopItem ToDomainObject() { - if (HasValidationErrors(this) || HasValidationErrorsBase(HasValidationErrors(this))) throw new ValidationException(); + if (HasValidationErrors(this) || HasValidationErrorsBase(HasValidationErrors(this))) + throw new ValidationException(); return new ShopItem() { L10n = L10n.Select(x => x.ToDomainObject()).ToList(), @@ -72,24 +71,19 @@ namespace WeatherForecast.Models.Requests { /// public override IEnumerable Validate(ValidationContext validationContext) { if (L10n.IsNullOrEmpty()) - yield return new ValidationResult($"{validationContext.DisplayName} ${Errors.NullOrEmpty}"); - else { - foreach (var item in L10n) - foreach (var validationResult in item.Validate(validationContext)) - yield return validationResult; - } + yield return new ValidationResult($"{nameof(L10n)} ${Errors.NullOrEmpty}"); if (string.IsNullOrWhiteSpace(BrandName)) - yield return new ValidationResult($"{validationContext.DisplayName} ${Errors.NullOrWhiteSpace}"); + yield return new ValidationResult($"{nameof(BrandName)} ${Errors.NullOrWhiteSpace}"); - if (Price == null) - yield return new ValidationResult($"{validationContext.DisplayName} {Errors.NullOrEmpty.Name}"); + if (Price == null || (Price != null && Price == 0)) + yield return new ValidationResult($"{nameof(Price)} {Errors.NullOrEmpty.Name}"); - if (Quantity == null) - yield return new ValidationResult($"{validationContext.DisplayName} ${Errors.NullOrEmpty}"); + if (Quantity == null || (Quantity != null && Quantity == 0)) + yield return new ValidationResult($"{nameof(Quantity)} ${Errors.NullOrEmpty}"); } [MemberNotNullWhen(false, new[] { nameof(BrandName), nameof(Price), nameof(Quantity) })] - private bool HasValidationErrors(ShopItemRequestModel validationContext) => Validate(new ValidationContext(validationContext)).Any(); + private bool HasValidationErrors(ShopItemRequestModel model) => Validate(new ValidationContext(model)).Any(); } } diff --git a/webapi/WeatherForecast/Models/Responses/BlogCategoriesResponseModel.cs b/webapi/WeatherForecast/Models/Responses/BlogCategoriesResponseModel.cs deleted file mode 100644 index a52b7a5..0000000 --- a/webapi/WeatherForecast/Models/Responses/BlogCategoriesResponseModel.cs +++ /dev/null @@ -1,15 +0,0 @@ -using Core.Abstractions.Models; - -namespace WeatherForecast.Models.Responses { - - /// - /// - /// - public class BlogCategoriesResponseModel : ResponseModelBase { - - /// - /// - /// - public List? Items { get; set; } - } -} diff --git a/webapi/WeatherForecast/Models/Responses/BlogFeaturedResponseModel.cs b/webapi/WeatherForecast/Models/Responses/BlogFeaturedResponseModel.cs deleted file mode 100644 index 524015e..0000000 --- a/webapi/WeatherForecast/Models/Responses/BlogFeaturedResponseModel.cs +++ /dev/null @@ -1,23 +0,0 @@ -using Core.Abstractions.Models; - -namespace WeatherForecast.Models.Responses { - - /// - /// - /// - public class BlogFeaturedResponseModel : ResponseModelBase { - - /// - /// - /// - public List Items { get; private set; } - - /// - /// - /// - /// - public BlogFeaturedResponseModel(List items) { - Items = items; - } - } -} diff --git a/webapi/WeatherForecast/Models/Responses/BlogItemsResponseModel.cs b/webapi/WeatherForecast/Models/Responses/BlogItemsResponseModel.cs index 0cf983a..07bfd6c 100644 --- a/webapi/WeatherForecast/Models/Responses/BlogItemsResponseModel.cs +++ b/webapi/WeatherForecast/Models/Responses/BlogItemsResponseModel.cs @@ -8,12 +8,6 @@ namespace WeatherForecast.Models.Responses { /// public class BlogItemsResponseModel : PaginationModelBase { - - //public BlogItemModel FeaturedBlog { get; set; } - - //public List Categories { get; set; } - - /// /// /// @@ -22,6 +16,5 @@ namespace WeatherForecast.Models.Responses { /// public BlogItemsResponseModel(int currentPage, int totalPages, List items) : base(currentPage, totalPages, items) { } - } } diff --git a/webapi/WeatherForecast/Models/Responses/CategoryItemResponseModel.cs b/webapi/WeatherForecast/Models/Responses/CategoryItemResponseModel.cs index c0ca54d..1d1af47 100644 --- a/webapi/WeatherForecast/Models/Responses/CategoryItemResponseModel.cs +++ b/webapi/WeatherForecast/Models/Responses/CategoryItemResponseModel.cs @@ -1,7 +1,7 @@ using Core.Abstractions.Models; using Core.DomainObjects; using Core.Enumerations; -using WeatherForecast.Models.L10n; +using WeatherForecast.Models.Responses.L10n; namespace WeatherForecast.Models.Responses { diff --git a/webapi/WeatherForecast/Models/ImageModel.cs b/webapi/WeatherForecast/Models/Responses/ImageResponseModel.cs similarity index 75% rename from webapi/WeatherForecast/Models/ImageModel.cs rename to webapi/WeatherForecast/Models/Responses/ImageResponseModel.cs index cb0092b..ca11b18 100644 --- a/webapi/WeatherForecast/Models/ImageModel.cs +++ b/webapi/WeatherForecast/Models/Responses/ImageResponseModel.cs @@ -1,13 +1,13 @@ using Core.DomainObjects; using Core.Enumerations; -using WeatherForecast.Models.L10n; +using WeatherForecast.Models.Responses.L10n; namespace WeatherForecast.Models { /// /// /// - public class ImageModel { + public class ImageResponseModel { /// /// @@ -28,7 +28,7 @@ namespace WeatherForecast.Models { /// /// /// - public ImageModel(Image image) { + public ImageResponseModel(Image image) { L10n = image.L10n.Select(x => new ImageL10nModel(x)).ToList(); } @@ -37,10 +37,11 @@ namespace WeatherForecast.Models { /// /// /// - public ImageModel(Image image, Locales locale) { + public ImageResponseModel(Image image, Locales locale) { Src = image.Src; - var l10n = image.L10n.SingleOrDefault(x => x.Locale == locale); + var l10n = image.L10n.Single(x => x.Locale == locale); + if (l10n != null) { Alt = l10n.Alt; } diff --git a/webapi/WeatherForecast/Models/Responses/ShopCartItemResponseModel.cs b/webapi/WeatherForecast/Models/Responses/ShopCartItemResponseModel.cs index fee0d43..fc31873 100644 --- a/webapi/WeatherForecast/Models/Responses/ShopCartItemResponseModel.cs +++ b/webapi/WeatherForecast/Models/Responses/ShopCartItemResponseModel.cs @@ -22,7 +22,7 @@ namespace WeatherForecast.Models.Responses { /// /// /// - public ImageModel Image { get; set; } + public ImageResponseModel? Image { get; set; } /// /// @@ -59,11 +59,6 @@ namespace WeatherForecast.Models.Responses { /// public uint? Quantity { get; set; } - /// - /// - /// - public ShopCartItemResponseModel() { } - /// /// /// @@ -79,13 +74,16 @@ namespace WeatherForecast.Models.Responses { NewPrice = shopItem.NewPrice; Quantity = shopCartItem.Quantity; + var l10n = shopItem.L10n.SingleOrDefault(x => x.Locale == locale); - var shopItemL10n = shopItem.L10n.Single(x => x.Locale == locale); - Slug = shopItemL10n.Slug; - Title = shopItemL10n.Title; - ShortText = shopItemL10n.ShortText; + if (l10n != null) { + Slug = l10n.Slug; + Title = l10n.Title; + ShortText = l10n.ShortText; + } - Image = new ImageModel(shopItem.Images.First(), locale); + if(shopItem.Images != null) + Image = new ImageResponseModel(shopItem.Images.First(), locale); } } diff --git a/webapi/WeatherForecast/Services/BlogItemService.cs b/webapi/WeatherForecast/Services/BlogItemService.cs index 0d3a1bd..cf40a73 100644 --- a/webapi/WeatherForecast/Services/BlogItemService.cs +++ b/webapi/WeatherForecast/Services/BlogItemService.cs @@ -1,7 +1,12 @@ -using Core.DomainObjects; -using Core.Enumerations; +using DomainResults.Common; + +using ExtensionMethods; using DataProviders; -using DomainResults.Common; + +using Core.DomainObjects; +using Core.DomainObjects.L10n; +using Core.Enumerations; + using WeatherForecast.Models.Requests; using WeatherForecast.Models.Responses; @@ -85,7 +90,44 @@ namespace WeatherForecast.Services { /// /// public (Guid?, IDomainResult) Post(Guid siteId, BlogItemRequestModel requestModel) { - throw new NotImplementedException(); + try { + var item = requestModel.ToDomainObject(); + item.SiteId = siteId; + + var (_, getResult) = _blogCatalogDataProvider.GetBySlugs(item.SiteId, item.L10n.Select(x => x.Slug).ToList()); + if (getResult.IsSuccess) + return IDomainResult.Failed(); + + // TODO: should be recovered from users by jwt + item.Author = "fdc5aa50-ee68-4bae-a8e6-b8ae2c258f60".ToGuid(); + + // TODO: should be placed to object storage + item.Images = new List() { + new Image { + Src = "https://dummyimage.com/450x300/dee2e6/6c757d.jpg", + L10n = new List { + new ImageL10n { + Locale = Locales.Us, + Alt = "..." + } + } + } + }; + + // TODO: default value shoud not be hardcoded by database id + item.Categories ??= new List { "e154e33f-3cc7-468d-bb66-e0390ddb9ae0".ToGuid() }; + + + var (id, insertResult) = _blogCatalogDataProvider.Insert(item); + + if (!insertResult.IsSuccess) + return IDomainResult.Failed(); + + return IDomainResult.Success(id); + } + catch (Exception ex) { + return IDomainResult.Failed(ex.Message); + } } /// @@ -95,21 +137,29 @@ namespace WeatherForecast.Services { /// /// public (BlogItemResponseModel?, IDomainResult) Get(Guid siteId, Guid blogId) { - var (item, result) = _blogCatalogDataProvider.Get(siteId, blogId); + try { + var (item, result) = _blogCatalogDataProvider.Get(siteId, blogId); - if (!result.IsSuccess || item == null) - return (null, result); + if (!result.IsSuccess || item == null) + return (null, result); - var categories = new List(); - foreach (var catId in item.Categories) { - var (category, getCategoryResult) = _categoryDataProvider.Get(siteId, catId); - if (!getCategoryResult.IsSuccess || category == null) - return (null, getCategoryResult); + var categories = new List(); - categories.Add(category); + if (item.Categories != null) { + foreach (var catId in item.Categories) { + var (category, getCategoryResult) = _categoryDataProvider.Get(siteId, catId); + if (!getCategoryResult.IsSuccess || category == null) + return (null, getCategoryResult); + + categories.Add(category); + } + } + + return IDomainResult.Success(new BlogItemResponseModel(item, categories)); + } + catch (Exception ex) { + return IDomainResult.Failed(ex.Message); } - - return IDomainResult.Success(new BlogItemResponseModel(item, categories)); } /// @@ -119,23 +169,31 @@ namespace WeatherForecast.Services { /// /// public (BlogItemResponseModel?, IDomainResult) GetSlug(Guid siteId, string slug) { - var (item, result) = _blogCatalogDataProvider.GetBySlug(siteId, slug); + try { + var (item, result) = _blogCatalogDataProvider.GetBySlug(siteId, slug); - if (!result.IsSuccess || item == null) - return (null, result); + if (!result.IsSuccess || item == null) + return (null, result); - var locale = item.L10n.SingleOrDefault(x => x.Slug == slug)?.Locale ?? Locales.Us; + var locale = item.L10n.SingleOrDefault(x => x.Slug == slug)?.Locale ?? Locales.Us; - var categories = new List(); - foreach (var catId in item.Categories) { - var (category, getCategoryResult) = _categoryDataProvider.Get(siteId, catId); - if (!getCategoryResult.IsSuccess || category == null) - return (null, getCategoryResult); + var categories = new List(); - categories.Add(category); + if (item.Categories != null) { + foreach (var catId in item.Categories) { + var (category, getCategoryResult) = _categoryDataProvider.Get(siteId, catId); + if (!getCategoryResult.IsSuccess || category == null) + return (null, getCategoryResult); + + categories.Add(category); + } + } + + return IDomainResult.Success(new BlogItemResponseModel(item, categories, locale)); + } + catch (Exception ex) { + return IDomainResult.Failed(ex.Message); } - - return IDomainResult.Success(new BlogItemResponseModel(item, categories, locale)); } /// @@ -147,7 +205,30 @@ namespace WeatherForecast.Services { /// /// public (Guid?, IDomainResult) Update(Guid siteId, Guid blogId, BlogItemRequestModel requestData) { - throw new NotImplementedException(); + try { + var (item, getResult) = _blogCatalogDataProvider.Get(siteId, blogId); + if (!getResult.IsSuccess || item == null) + return (null, getResult); + + // construct domain object from model + var newItem = requestData.ToDomainObject(); + newItem.Id = item.Id; + newItem.SiteId = siteId; + + newItem.Created = item.Created; + newItem.Author = item.Author; + + if (!item.Equals(newItem)) { + var (id, updateResult) = _blogCatalogDataProvider.Update(newItem); + if (!updateResult.IsSuccess || id == null) + return (null, updateResult); + } + + return IDomainResult.Success(item.Id); + } + catch (Exception ex) { + return IDomainResult.Failed(ex.Message); + } } /// @@ -158,7 +239,20 @@ namespace WeatherForecast.Services { /// /// public IDomainResult Delete(Guid siteId, Guid blogId) { - throw new NotImplementedException(); + try { + var (item, getResult) = _blogCatalogDataProvider.Get(siteId, blogId); + if (!getResult.IsSuccess || item == null) + return getResult; + + var result = _blogCatalogDataProvider.Delete(item.Id); + if (!result.IsSuccess) + return result; + + return IDomainResult.Success(); + } + catch (Exception ex) { + return IDomainResult.Failed(ex.Message); + } } } } diff --git a/webapi/WeatherForecast/Services/BlogItemsService.cs b/webapi/WeatherForecast/Services/BlogItemsService.cs index 147c0df..d75f890 100644 --- a/webapi/WeatherForecast/Services/BlogItemsService.cs +++ b/webapi/WeatherForecast/Services/BlogItemsService.cs @@ -1,8 +1,11 @@ -using Core.Abstractions; +using DomainResults.Common; + +using DataProviders; + +using Core.Abstractions; using Core.DomainObjects; using Core.Enumerations; -using DataProviders; -using DomainResults.Common; + using WeatherForecast.Models.Responses; namespace WeatherForecast.Services { @@ -68,30 +71,38 @@ namespace WeatherForecast.Services { /// /// public (BlogItemsResponseModel?, IDomainResult) Get(Guid siteId, Guid? category, int currentPage, int itemsPerPage, string? locale, string? searchText) { - var (items, result) = _blogCatalogDataProvider.GetAll(siteId, currentPage > 0 ? ((currentPage - 1) * itemsPerPage) : 0, itemsPerPage); - if (!result.IsSuccess || items == null) - return (null, result); + try { + var (items, result) = _blogCatalogDataProvider.GetAll(siteId, currentPage > 0 ? ((currentPage - 1) * itemsPerPage) : 0, itemsPerPage); + if (!result.IsSuccess || items == null) + return (null, result); - var blogItems = new List(); - foreach (var item in items) { - var categories = new List(); - foreach (var catId in item.Categories) { - var (cat, getCategoryResult) = _categoryDataProvider.Get(siteId, catId); - if (!getCategoryResult.IsSuccess || cat == null) - return (null, getCategoryResult); + var blogItems = new List(); + foreach (var item in items) { + var categories = new List(); - categories.Add(cat); + if (item.Categories != null) { + foreach (var catId in item.Categories) { + var (cat, getCategoryResult) = _categoryDataProvider.Get(siteId, catId); + if (!getCategoryResult.IsSuccess || cat == null) + return (null, getCategoryResult); + + categories.Add(cat); + } + } + + if (locale != null) + blogItems.Add(new BlogItemResponseModel(item, categories, Enumeration.FromDisplayName(locale) ?? Locales.Us)); + else + blogItems.Add(new BlogItemResponseModel(item, categories)); } - if(locale != null) - blogItems.Add(new BlogItemResponseModel(item, categories, Enumeration.FromDisplayName(locale) ?? Locales.Us)); - else - blogItems.Add(new BlogItemResponseModel(item, categories)); + return blogItems.Count > 0 + ? IDomainResult.Success(new BlogItemsResponseModel(currentPage, 0, blogItems)) + : IDomainResult.NotFound(); + } + catch (Exception ex) { + return IDomainResult.Failed(ex.Message); } - - return blogItems.Count > 0 - ? IDomainResult.Success(new BlogItemsResponseModel(currentPage, 0, blogItems)) - : IDomainResult.NotFound(); } /// @@ -99,6 +110,13 @@ namespace WeatherForecast.Services { /// /// /// - public IDomainResult Delete(Guid siteId) => _blogCatalogDataProvider.DeleteAll(siteId); + public IDomainResult Delete(Guid siteId) { + try { + return _blogCatalogDataProvider.DeleteAll(siteId); + } + catch (Exception ex) { + return IDomainResult.Failed(ex.Message); + } + } } } diff --git a/webapi/WeatherForecast/Services/CategoryItemService.cs b/webapi/WeatherForecast/Services/CategoryItemService.cs index a5b0acd..7837dc5 100644 --- a/webapi/WeatherForecast/Services/CategoryItemService.cs +++ b/webapi/WeatherForecast/Services/CategoryItemService.cs @@ -1,6 +1,9 @@ -using Core.Enumerations; +using DomainResults.Common; + using DataProviders; -using DomainResults.Common; + +using Core.Enumerations; + using WeatherForecast.Models.Requests; using WeatherForecast.Models.Responses; @@ -81,20 +84,24 @@ namespace WeatherForecast.Services { /// /// public (Guid?, IDomainResult) Post(Guid siteId, CategoryItemRequestModel requestModel) { - var (_, getResult) = _categoryDataProvider.GetBySlugs(siteId, requestModel.L10n.Select(x => x.Slug).ToList()); - if (getResult.IsSuccess) - return IDomainResult.Failed(); + try { + var item = requestModel.ToDomainObject(); + item.SiteId = siteId; - var item = requestModel.ToDomainObject(); - item.SiteId = siteId; + var (_, getResult) = _categoryDataProvider.GetBySlugs(item.SiteId, item.L10n.Select(x => x.Slug).ToList()); + if (getResult.IsSuccess) + return IDomainResult.Failed(); + var (id, insertResult) = _categoryDataProvider.Insert(item); - var (id, insertResult) = _categoryDataProvider.Insert(item); + if (!insertResult.IsSuccess) + return IDomainResult.Failed(); - if (!insertResult.IsSuccess) - return IDomainResult.Failed(); - - return IDomainResult.Success(id); + return IDomainResult.Success(id); + } + catch (Exception ex) { + return IDomainResult.Failed(ex.Message); + } } /// @@ -104,11 +111,16 @@ namespace WeatherForecast.Services { /// /// public (CategoryItemResponseModel?, IDomainResult) Get(Guid siteId, Guid categoryId) { - var (item, result) = _categoryDataProvider.Get(siteId, categoryId); - if (!result.IsSuccess || item == null) - return (null, result); + try { + var (item, result) = _categoryDataProvider.Get(siteId, categoryId); + if (!result.IsSuccess || item == null) + return (null, result); - return IDomainResult.Success(new CategoryItemResponseModel(item)); + return IDomainResult.Success(new CategoryItemResponseModel(item)); + } + catch (Exception ex) { + return IDomainResult.Failed(ex.Message); + } } /// @@ -118,13 +130,18 @@ namespace WeatherForecast.Services { /// /// public (CategoryItemResponseModel?, IDomainResult) GetSlug(Guid siteId, string slug) { - var (item, result) = _categoryDataProvider.GetBySlug(siteId, slug); - if (!result.IsSuccess || item == null) - return (null, result); + try { + var (item, result) = _categoryDataProvider.GetBySlug(siteId, slug); + if (!result.IsSuccess || item == null) + return (null, result); - var locale = item.L10n.SingleOrDefault(x => x.Slug == slug)?.Locale ?? Locales.Us; + var locale = item.L10n.SingleOrDefault(x => x.Slug == slug)?.Locale ?? Locales.Us; - return IDomainResult.Success(new CategoryItemResponseModel(item, locale)); + return IDomainResult.Success(new CategoryItemResponseModel(item, locale)); + } + catch (Exception ex) { + return IDomainResult.Failed(ex.Message); + } } /// @@ -135,22 +152,27 @@ namespace WeatherForecast.Services { /// /// public (Guid?, IDomainResult) Update(Guid siteId, Guid categoryId, CategoryItemRequestModel requestData) { - var (item, result) = _categoryDataProvider.Get(siteId, categoryId); - if (!result.IsSuccess || item == null) - return (null, result); + try { + var (item, result) = _categoryDataProvider.Get(siteId, categoryId); + if (!result.IsSuccess || item == null) + return (null, result); - // construct domain object from model - var newItem = requestData.ToDomainObject(); - newItem.Id = item.Id; - newItem.SiteId = item.SiteId; + // construct domain object from model + var newItem = requestData.ToDomainObject(); + newItem.Id = item.Id; + newItem.SiteId = item.SiteId; - if (!item.Equals(newItem)) { - var (id, updateResult) = _categoryDataProvider.Update(newItem); - if (!updateResult.IsSuccess || id == null) - return (null, updateResult); + if (!item.Equals(newItem)) { + var (id, updateResult) = _categoryDataProvider.Update(newItem); + if (!updateResult.IsSuccess || id == null) + return (null, updateResult); + } + + return IDomainResult.Success(item.Id); + } + catch (Exception ex) { + return IDomainResult.Failed(ex.Message); } - - return IDomainResult.Success(item.Id); } /// @@ -160,15 +182,20 @@ namespace WeatherForecast.Services { /// /// public IDomainResult Delete(Guid siteId, Guid categoryId) { - var (item, getResult) = _categoryDataProvider.Get(siteId, categoryId); - if (!getResult.IsSuccess || item == null) - return getResult; + try { + var (item, getResult) = _categoryDataProvider.Get(siteId, categoryId); + if (!getResult.IsSuccess || item == null) + return getResult; - var result = _categoryDataProvider.Delete(item.Id); - if (!result.IsSuccess) - return result; + var result = _categoryDataProvider.Delete(item.Id); + if (!result.IsSuccess) + return result; - return IDomainResult.Success(); + return IDomainResult.Success(); + } + catch (Exception ex) { + return IDomainResult.Failed(ex.Message); + } } } } diff --git a/webapi/WeatherForecast/Services/CategoryItemsService.cs b/webapi/WeatherForecast/Services/CategoryItemsService.cs index f472531..b12e24b 100644 --- a/webapi/WeatherForecast/Services/CategoryItemsService.cs +++ b/webapi/WeatherForecast/Services/CategoryItemsService.cs @@ -1,7 +1,10 @@ -using Core.Abstractions; -using Core.Enumerations; +using DomainResults.Common; + using DataProviders; -using DomainResults.Common; + +using Core.Abstractions; +using Core.Enumerations; + using WeatherForecast.Models.Responses; namespace WeatherForecast.Services { @@ -55,15 +58,20 @@ namespace WeatherForecast.Services { /// /// public (List?, IDomainResult) Get(Guid siteId, string? locale) { - var (items, result) = _categoryDataProvider.GetAll(siteId); - if (!result.IsSuccess || items == null) - return (null, result); + try { + var (items, result) = _categoryDataProvider.GetAll(siteId); + if (!result.IsSuccess || items == null) + return (null, result); - if (locale != null) { - return IDomainResult.Success(items.Select(x => new CategoryItemResponseModel(x, Enumeration.FromDisplayName(locale) ?? Locales.Us)).ToList()); + if (locale != null) { + return IDomainResult.Success(items.Select(x => new CategoryItemResponseModel(x, Enumeration.FromDisplayName(locale) ?? Locales.Us)).ToList()); + } + + return IDomainResult.Success(items.Select(x => new CategoryItemResponseModel(x)).ToList()); + } + catch (Exception ex) { + return IDomainResult.Failed?>(ex.Message); } - - return IDomainResult.Success(items.Select(x => new CategoryItemResponseModel(x)).ToList()); } /// @@ -71,6 +79,13 @@ namespace WeatherForecast.Services { /// /// /// - public IDomainResult Delete(Guid siteId) => _categoryDataProvider.DeleteAll(siteId); + public IDomainResult Delete(Guid siteId) { + try { + return _categoryDataProvider.DeleteAll(siteId); + } + catch (Exception ex) { + return IDomainResult.Failed(ex.Message); + } + } } } diff --git a/webapi/WeatherForecast/Services/ContentService.cs b/webapi/WeatherForecast/Services/ContentService.cs index bcabb8c..2659699 100644 --- a/webapi/WeatherForecast/Services/ContentService.cs +++ b/webapi/WeatherForecast/Services/ContentService.cs @@ -1,5 +1,6 @@ -using DataProviders; -using DomainResults.Common; +using DomainResults.Common; + +using DataProviders; using WeatherForecast.Models.Responses; @@ -47,12 +48,17 @@ namespace WeatherForecast.Services { /// /// public (ContentResponseModel?, IDomainResult) GetContent(Guid siteId, string locale) { - var (content, result) = _contentDataProvider.Get(siteId, locale); + try { + var (content, result) = _contentDataProvider.Get(siteId, locale); - if (!result.IsSuccess || content == null) - return (null, result); + if (!result.IsSuccess || content == null) + return (null, result); - return IDomainResult.Success(new ContentResponseModel(content)); + return IDomainResult.Success(new ContentResponseModel(content)); + } + catch (Exception ex) { + return IDomainResult.Failed(ex.Message); + } } } } diff --git a/webapi/WeatherForecast/Services/ShopCartItemService.cs b/webapi/WeatherForecast/Services/ShopCartItemService.cs index aa9870a..dc92d21 100644 --- a/webapi/WeatherForecast/Services/ShopCartItemService.cs +++ b/webapi/WeatherForecast/Services/ShopCartItemService.cs @@ -1,8 +1,9 @@ -using Core.Abstractions; -using Core.DomainObjects.Documents; -using Core.Enumerations; +using DomainResults.Common; + using DataProviders; -using DomainResults.Common; + +using Core.Abstractions; +using Core.Enumerations; using WeatherForecast.Models.Requests; using WeatherForecast.Models.Responses; @@ -32,7 +33,7 @@ namespace WeatherForecast.Services { /// /// /// - (ShopCartItemResponseModel?, IDomainResult) Get(Guid siteId, Guid userId, string sku, string locale = "en-US"); + (ShopCartItemResponseModel?, IDomainResult) Get(Guid siteId, Guid userId, string sku, string? locale); /// /// @@ -88,22 +89,27 @@ namespace WeatherForecast.Services { /// /// public (Guid?, IDomainResult) Post(Guid siteId, Guid userId, string sku, ShopCartItemRequestModel requestModel) { - var (_, getResult) = _shopCartDataProvider.Get(siteId, userId, sku); - if (getResult.IsSuccess) - return IDomainResult.Failed(); + try { + var (_, getResult) = _shopCartDataProvider.Get(siteId, userId, sku); + if (getResult.IsSuccess) + return IDomainResult.Failed(); - var item = requestModel.ToDomainObject(); + var item = requestModel.ToDomainObject(); - item.SiteId = siteId; - item.UserId = userId; - item.Sku = sku; + item.SiteId = siteId; + item.UserId = userId; + item.Sku = sku; - var (id, insertResult) = _shopCartDataProvider.Insert(item); + var (id, insertResult) = _shopCartDataProvider.Insert(item); - if (!insertResult.IsSuccess) - return IDomainResult.Failed(); + if (!insertResult.IsSuccess) + return IDomainResult.Failed(); - return IDomainResult.Success(id); + return IDomainResult.Success(id); + } + catch (Exception ex) { + return IDomainResult.Failed(ex.Message); + } } /// @@ -114,17 +120,26 @@ namespace WeatherForecast.Services { /// /// /// - public (ShopCartItemResponseModel?, IDomainResult) Get(Guid siteId, Guid userId, string sku, string locale = "en-US") { + public (ShopCartItemResponseModel?, IDomainResult) Get(Guid siteId, Guid userId, string sku, string? locale) { + try { + var (cartItem, getCartItemResult) = _shopCartDataProvider.Get(siteId, userId, sku); + if (!getCartItemResult.IsSuccess || cartItem == null) + return (null, getCartItemResult); - var (cartItem, getCartItemResult) = _shopCartDataProvider.Get(siteId, userId, sku); - if (!getCartItemResult.IsSuccess || cartItem == null) - return (null, getCartItemResult); + var (item, result) = _shopCatalogDataProvider.Get(siteId, cartItem.Sku); + if (!result.IsSuccess || item == null) { + var delteResult = _shopCartDataProvider.Delete(cartItem.Id); + if (!delteResult.IsSuccess) + return (null, delteResult); - var (item, result) = _shopCatalogDataProvider.Get(siteId, cartItem.Sku); - if (!result.IsSuccess || item == null) - return (null, result); - - return IDomainResult.Success(new ShopCartItemResponseModel(item, cartItem, Enumeration.FromDisplayName(locale))); + return IDomainResult.NotFound(); + } + else + return IDomainResult.Success(new ShopCartItemResponseModel(item, cartItem, Enumeration.FromDisplayName(locale ?? "en-US"))); + } + catch (Exception ex) { + return IDomainResult.Failed(ex.Message); + } } /// @@ -136,25 +151,30 @@ namespace WeatherForecast.Services { /// /// public (Guid?, IDomainResult) Update(Guid siteId, Guid userId, string sku, ShopCartItemRequestModel requestData) { - var (item, getResult) = _shopCartDataProvider.Get(siteId, userId, sku); - if (!getResult.IsSuccess || item == null) - return (null, getResult); + try { + var (item, getResult) = _shopCartDataProvider.Get(siteId, userId, sku); + if (!getResult.IsSuccess || item == null) + return (null, getResult); - // construct domain object from model - var newItem = requestData.ToDomainObject(); - newItem.Id = item.Id; - newItem.SiteId = siteId; - newItem.UserId = userId; - newItem.Sku = sku; - newItem.Created = item.Created; + // construct domain object from model + var newItem = requestData.ToDomainObject(); + newItem.Id = item.Id; + newItem.SiteId = siteId; + newItem.UserId = userId; + newItem.Sku = sku; + newItem.Created = item.Created; - if (!item.Equals(newItem)) { - var (id, updateResult) = _shopCartDataProvider.Update(newItem); - if (!updateResult.IsSuccess || id == null) - return (null, updateResult); + if (!item.Equals(newItem)) { + var (id, updateResult) = _shopCartDataProvider.Update(newItem); + if (!updateResult.IsSuccess || id == null) + return (null, updateResult); + } + + return IDomainResult.Success(item.Id); + } + catch (Exception ex) { + return IDomainResult.Failed(ex.Message); } - - return IDomainResult.Success(item.Id); } /// @@ -165,15 +185,20 @@ namespace WeatherForecast.Services { /// /// public IDomainResult Delete(Guid siteId, Guid userId, string sku) { - var (item, getResult) = _shopCartDataProvider.Get(siteId, userId, sku); - if (!getResult.IsSuccess || item == null) - return getResult; + try { + var (item, getResult) = _shopCartDataProvider.Get(siteId, userId, sku); + if (!getResult.IsSuccess || item == null) + return getResult; - var result = _shopCartDataProvider.Delete(item.Id); - if (!result.IsSuccess) - return result; + var result = _shopCartDataProvider.Delete(item.Id); + if (!result.IsSuccess) + return result; - return IDomainResult.Success(); + return IDomainResult.Success(); + } + catch (Exception ex) { + return IDomainResult.Failed(ex.Message); + } } } } diff --git a/webapi/WeatherForecast/Services/ShopCartItemsService.cs b/webapi/WeatherForecast/Services/ShopCartItemsService.cs index 1ce2802..efe5347 100644 --- a/webapi/WeatherForecast/Services/ShopCartItemsService.cs +++ b/webapi/WeatherForecast/Services/ShopCartItemsService.cs @@ -1,12 +1,12 @@ using DomainResults.Common; -using WeatherForecast.Models.Responses; - using DataProviders; using Core.Enumerations; using Core.Abstractions; +using WeatherForecast.Models.Responses; + namespace WeatherForecast.Services { /// @@ -21,7 +21,7 @@ namespace WeatherForecast.Services { /// /// /// - (List?, IDomainResult) Get(Guid siteId, Guid userId, string locale = "en-US"); + (List?, IDomainResult) Get(Guid siteId, Guid userId, string? locale); /// /// @@ -64,24 +64,31 @@ namespace WeatherForecast.Services { /// /// /// - public (List?, IDomainResult) Get(Guid siteId, Guid userId, string locale = "en-US") { + public (List?, IDomainResult) Get(Guid siteId, Guid userId, string? locale) { + try { + var (cartItems, getCartItemsResult) = _shopCartDataProvider.GetAll(siteId, userId); + if (!getCartItemsResult.IsSuccess || cartItems == null) + return (null, getCartItemsResult); - var (cartItems, getCartItemsResult) = _shopCartDataProvider.GetAll(siteId, userId); - if (!getCartItemsResult.IsSuccess || cartItems == null) - return (null, getCartItemsResult); + var items = new List(); + foreach (var cartItem in cartItems) { + var (item, result) = _shopCatalogDataProvider.Get(siteId, cartItem.Sku); + if (!result.IsSuccess || item == null) { + var delteResult = _shopCartDataProvider.Delete(cartItem.Id); + if (!delteResult.IsSuccess) + return (null, delteResult); + } + else + items.Add(new ShopCartItemResponseModel(item, cartItem, Enumeration.FromDisplayName(locale ?? "en-US"))); + } - var items = new List(); - foreach (var cartItem in cartItems) { - var (item, result) = _shopCatalogDataProvider.Get(siteId, cartItem.Sku); - if (!result.IsSuccess || item == null) - return (null, result); - - items.Add(new ShopCartItemResponseModel(item, cartItem, Enumeration.FromDisplayName(locale))); + return items.Count > 0 + ? IDomainResult.Success(items) + : IDomainResult.NotFound?>(); + } + catch (Exception ex) { + return IDomainResult.Failed?>(ex.Message); } - - return items.Count > 0 - ? IDomainResult.Success(items) - : IDomainResult.NotFound?>(); } /// @@ -90,6 +97,13 @@ namespace WeatherForecast.Services { /// /// /// - public IDomainResult Delete(Guid siteId, Guid userId) => _shopCartDataProvider.DeleteAll(siteId, userId); + public IDomainResult Delete(Guid siteId, Guid userId) { + try { + return _shopCartDataProvider.DeleteAll(siteId, userId); + } + catch (Exception ex) { + return IDomainResult.Failed(ex.Message); + } + } } } diff --git a/webapi/WeatherForecast/Services/ShopItemService.cs b/webapi/WeatherForecast/Services/ShopItemService.cs index 074b8bc..468cfca 100644 --- a/webapi/WeatherForecast/Services/ShopItemService.cs +++ b/webapi/WeatherForecast/Services/ShopItemService.cs @@ -1,9 +1,13 @@ -using Core.DomainObjects; +using DomainResults.Common; + +using DataProviders; + +using ExtensionMethods; + +using Core.DomainObjects; using Core.DomainObjects.L10n; using Core.Enumerations; -using DataProviders; -using DomainResults.Common; -using ExtensionMethods; + using WeatherForecast.Models; using WeatherForecast.Models.Requests; @@ -90,40 +94,47 @@ namespace WeatherForecast.Services { /// /// public (Guid?, IDomainResult) Post(Guid siteId, string sku, ShopItemRequestModel requestModel) { - var (_, getResult) = _shopCatalogDataProvider.Get(siteId, sku); - if (getResult.IsSuccess) - return IDomainResult.Failed(); + try { + var item = requestModel.ToDomainObject(); - var item = requestModel.ToDomainObject(); + item.SiteId = siteId; + item.Sku = sku; - item.SiteId = siteId; - item.Sku = sku; + var (_, getResult) = _shopCatalogDataProvider.Get(siteId, sku); + if (getResult.IsSuccess) + return IDomainResult.Failed(); - // TODO: should be recovered from users by jwt - item.Author = "fdc5aa50-ee68-4bae-a8e6-b8ae2c258f60".ToGuid(); + // TODO: should be recovered from users by jwt + item.Author = "fdc5aa50-ee68-4bae-a8e6-b8ae2c258f60".ToGuid(); - // TODO: should be placed to object storage - item.Images = new List() { - new Image { - Src = "https://dummyimage.com/450x300/dee2e6/6c757d.jpg", - L10n = new List { - new ImageL10n { - Locale = Locales.Us, - Alt = "..." + // TODO: should be placed to object storage + item.Images = new List() { + new Image { + Src = "https://dummyimage.com/450x300/dee2e6/6c757d.jpg", + L10n = new List { + new ImageL10n { + Locale = Locales.Us, + Alt = "..." + } } } - } - }; + }; - // TODO: default value shoud not be hardcoded by database id - item.Categories ??= new List { "e154e33f-3cc7-468d-bb66-e0390ddb9ae0".ToGuid() }; + // TODO: default value shoud not be hardcoded by database id + item.Categories ??= new List { "e154e33f-3cc7-468d-bb66-e0390ddb9ae0".ToGuid() }; - var (id, insertResult) = _shopCatalogDataProvider.Insert(item); + var (id, insertResult) = _shopCatalogDataProvider.Insert(item); - if (!insertResult.IsSuccess) - return IDomainResult.Failed(); + if (!insertResult.IsSuccess) + return IDomainResult.Failed(); - return IDomainResult.Success(id); + return IDomainResult.Success(id); + } + catch (Exception ex) { + return IDomainResult.Failed(ex.Message); + } + + } /// @@ -133,23 +144,28 @@ namespace WeatherForecast.Services { /// /// public (ShopItemResponseModel?, IDomainResult) Get(Guid siteId, string sku) { - var (item, result) = _shopCatalogDataProvider.Get(siteId, sku); + try { + var (item, result) = _shopCatalogDataProvider.Get(siteId, sku); - if (!result.IsSuccess || item == null) - return (null, result); + if (!result.IsSuccess || item == null) + return (null, result); - var categories = new List(); - if (item.Categories != null) { - foreach (var catId in item.Categories) { - var (category, getCategoryResult) = _categoryDataProvider.Get(siteId, catId); - if (!getCategoryResult.IsSuccess || category == null) - return (null, getCategoryResult); + var categories = new List(); + if (item.Categories != null) { + foreach (var catId in item.Categories) { + var (category, getCategoryResult) = _categoryDataProvider.Get(siteId, catId); + if (!getCategoryResult.IsSuccess || category == null) + return (null, getCategoryResult); - categories.Add(category); + categories.Add(category); + } } - } - return IDomainResult.Success(new ShopItemResponseModel(item, categories)); + return IDomainResult.Success(new ShopItemResponseModel(item, categories)); + } + catch (Exception ex) { + return IDomainResult.Failed(ex.Message); + } } /// @@ -159,25 +175,30 @@ namespace WeatherForecast.Services { /// /// public (ShopItemResponseModel?, IDomainResult) GetSlug(Guid siteId, string slug) { - var (item, result) = _shopCatalogDataProvider.GetBySlug(siteId, slug); + try { + var (item, result) = _shopCatalogDataProvider.GetBySlug(siteId, slug); - if (!result.IsSuccess || item == null) - return (null, result); + if (!result.IsSuccess || item == null) + return (null, result); - var locale = item.L10n.SingleOrDefault(x => x.Slug == slug)?.Locale ?? Locales.Us; + var locale = item.L10n.SingleOrDefault(x => x.Slug == slug)?.Locale ?? Locales.Us; - var categories = new List(); - if (item.Categories != null) { - foreach (var catId in item.Categories) { - var (category, getCategoryResult) = _categoryDataProvider.Get(siteId, catId); - if (!getCategoryResult.IsSuccess || category == null) - return (null, getCategoryResult); + var categories = new List(); + if (item.Categories != null) { + foreach (var catId in item.Categories) { + var (category, getCategoryResult) = _categoryDataProvider.Get(siteId, catId); + if (!getCategoryResult.IsSuccess || category == null) + return (null, getCategoryResult); - categories.Add(category); + categories.Add(category); + } } - } - return IDomainResult.Success(new ShopItemResponseModel(item, categories, locale)); + return IDomainResult.Success(new ShopItemResponseModel(item, categories, locale)); + } + catch (Exception ex) { + return IDomainResult.Failed(ex.Message); + } } /// @@ -188,25 +209,30 @@ namespace WeatherForecast.Services { /// /// public (Guid?, IDomainResult) Update(Guid siteId, string sku, ShopItemRequestModel requestData) { - var (item, getResult) = _shopCatalogDataProvider.Get(siteId, sku); - if (!getResult.IsSuccess || item == null) - return (null, getResult); + try { + var (item, getResult) = _shopCatalogDataProvider.Get(siteId, sku); + if (!getResult.IsSuccess || item == null) + return (null, getResult); - // construct domain object from model - var newItem = requestData.ToDomainObject(); - newItem.Id = item.Id; - newItem.SiteId = siteId; - newItem.Sku = sku; - newItem.Created = item.Created; - newItem.Author = item.Author; + // construct domain object from model + var newItem = requestData.ToDomainObject(); + newItem.Id = item.Id; + newItem.SiteId = siteId; + newItem.Sku = sku; + newItem.Created = item.Created; + newItem.Author = item.Author; - if (!item.Equals(newItem)) { - var (id, updateResult) = _shopCatalogDataProvider.Update(newItem); - if (!updateResult.IsSuccess || id == null) - return (null, updateResult); + if (!item.Equals(newItem)) { + var (id, updateResult) = _shopCatalogDataProvider.Update(newItem); + if (!updateResult.IsSuccess || id == null) + return (null, updateResult); + } + + return IDomainResult.Success(item.Id); + } + catch (Exception ex) { + return IDomainResult.Failed(ex.Message); } - - return IDomainResult.Success(item.Id); } /// @@ -216,16 +242,20 @@ namespace WeatherForecast.Services { /// /// public IDomainResult Delete(Guid siteId, string sku) { - var (item, getResult) = _shopCatalogDataProvider.Get(siteId, sku); - if (!getResult.IsSuccess || item == null) - return getResult; + try { + var (item, getResult) = _shopCatalogDataProvider.Get(siteId, sku); + if (!getResult.IsSuccess || item == null) + return getResult; - var result = _shopCatalogDataProvider.Delete(item.Id); - if (!result.IsSuccess) - return result; + var result = _shopCatalogDataProvider.Delete(item.Id); + if (!result.IsSuccess) + return result; - return IDomainResult.Success(); + return IDomainResult.Success(); + } + catch (Exception ex) { + return IDomainResult.Failed(ex.Message); + } } - } } diff --git a/webapi/WeatherForecast/Services/ShopItemsService.cs b/webapi/WeatherForecast/Services/ShopItemsService.cs index 629bf16..7a21bc4 100644 --- a/webapi/WeatherForecast/Services/ShopItemsService.cs +++ b/webapi/WeatherForecast/Services/ShopItemsService.cs @@ -1,8 +1,11 @@ -using Core.Abstractions; +using DomainResults.Common; + +using DataProviders; + +using Core.Abstractions; using Core.DomainObjects; using Core.Enumerations; -using DataProviders; -using DomainResults.Common; + using WeatherForecast.Models; using WeatherForecast.Models.Responses; @@ -68,35 +71,40 @@ namespace WeatherForecast.Services { /// /// public (ShopItemsResponseModel?, IDomainResult) Get(Guid siteId, Guid? category, int currentPage, int itemsPerPage, string? locale, string? searchText) { - var (items, result) = _shopCatalogDataProvider.GetAll(siteId, currentPage > 0 ? ((currentPage - 1) * itemsPerPage) : 0, itemsPerPage); + try { + var (items, result) = _shopCatalogDataProvider.GetAll(siteId, currentPage > 0 ? ((currentPage - 1) * itemsPerPage) : 0, itemsPerPage); - if (!result.IsSuccess || items == null) - return (null, result); + if (!result.IsSuccess || items == null) + return (null, result); - var shopItems = new List(); - foreach (var item in items) { + var shopItems = new List(); + foreach (var item in items) { - var categories = new List(); - if (item.Categories != null) { - foreach (var catId in item.Categories) { - var (cat, getCategoryResult) = _categoryDataProvider.Get(siteId, catId); - if (!getCategoryResult.IsSuccess || cat == null) - return (null, getCategoryResult); + var categories = new List(); + if (item.Categories != null) { + foreach (var catId in item.Categories) { + var (cat, getCategoryResult) = _categoryDataProvider.Get(siteId, catId); + if (!getCategoryResult.IsSuccess || cat == null) + return (null, getCategoryResult); - categories.Add(cat); + categories.Add(cat); + } } + + if(locale != null) + shopItems.Add(new ShopItemResponseModel(item, categories, Enumeration.FromDisplayName(locale) ?? Locales.Us)); + else + shopItems.Add(new ShopItemResponseModel(item, categories)); } - if(locale != null) - shopItems.Add(new ShopItemResponseModel(item, categories, Enumeration.FromDisplayName(locale) ?? Locales.Us)); - else - shopItems.Add(new ShopItemResponseModel(item, categories)); + return shopItems.Count > 0 + ? IDomainResult.Success(new ShopItemsResponseModel(currentPage, 0, shopItems)) + : IDomainResult.NotFound(); + } + catch (Exception ex) { + return IDomainResult.Failed(ex.Message); } - - return shopItems.Count > 0 - ? IDomainResult.Success(new ShopItemsResponseModel(currentPage, 0, shopItems)) - : IDomainResult.NotFound(); } /// @@ -104,6 +112,13 @@ namespace WeatherForecast.Services { /// /// /// - public IDomainResult Delete(Guid siteId) => _shopCatalogDataProvider.DeleteAll(siteId); + public IDomainResult Delete(Guid siteId) { + try { + return _shopCatalogDataProvider.DeleteAll(siteId); + } + catch (Exception ex) { + return IDomainResult.Failed(ex.Message); + } + } } }