From f20da5eb0f23146ced70bd75b86e759d36667b5d Mon Sep 17 00:00:00 2001 From: Maksym Sadovnychyy Date: Sun, 14 Aug 2022 22:27:55 +0200 Subject: [PATCH] (feat): shop cart item controller --- .../Abstractions/Models/RequestModelBase.cs | 1 + .../Core/DomainObjects/Documents/ShopCart.cs | 27 ------ .../DomainObjects/Documents/ShopCartItem.cs | 41 +++++++++ .../{ShopItem.cs => ShopCatalogItem.cs} | 2 +- webapi/Core/DomainObjects/Image.cs | 5 +- webapi/DataProviders/Mappings.cs | 4 +- webapi/DataProviders/ShopCartDataProvider.cs | 31 ++++++- .../Controllers/LoginController.cs | 32 +++---- .../Controllers/ShopCartItemController.cs | 84 +++++++++++++++++++ ...ntroller.cs => ShopCartItemsController.cs} | 19 ++--- .../Requests/PostShopCartItemRequestModel.cs | 37 ++++++++ .../Requests/PutShopCartItemRequestModel.cs | 37 ++++++++ .../Requests/PutShopCatalogRequestModel.cs | 37 -------- .../Responses/GetShopCartItemResponseModel.cs | 33 ++++++++ .../GetShopCartItemsResponseModel.cs | 14 ++++ .../Responses/GetShopCartResponseModel.cs | 14 ---- .../WeatherForecast/Models/ShopCartModel.cs | 33 -------- .../Services/ShopCartItemService.cs | 81 ++++++++++++++++++ .../Services/ShopCartItemsService.cs | 36 ++++++++ .../Services/ShopCartService.cs | 46 ---------- webapi/WeatherForecast/Startup.cs | 3 +- 21 files changed, 423 insertions(+), 194 deletions(-) delete mode 100644 webapi/Core/DomainObjects/Documents/ShopCart.cs create mode 100644 webapi/Core/DomainObjects/Documents/ShopCartItem.cs rename webapi/Core/DomainObjects/Documents/{ShopItem.cs => ShopCatalogItem.cs} (84%) create mode 100644 webapi/WeatherForecast/Controllers/ShopCartItemController.cs rename webapi/WeatherForecast/Controllers/{ShopCartController.cs => ShopCartItemsController.cs} (55%) create mode 100644 webapi/WeatherForecast/Models/Requests/PostShopCartItemRequestModel.cs create mode 100644 webapi/WeatherForecast/Models/Requests/PutShopCartItemRequestModel.cs delete mode 100644 webapi/WeatherForecast/Models/Requests/PutShopCatalogRequestModel.cs create mode 100644 webapi/WeatherForecast/Models/Responses/GetShopCartItemResponseModel.cs create mode 100644 webapi/WeatherForecast/Models/Responses/GetShopCartItemsResponseModel.cs delete mode 100644 webapi/WeatherForecast/Models/Responses/GetShopCartResponseModel.cs delete mode 100644 webapi/WeatherForecast/Models/ShopCartModel.cs create mode 100644 webapi/WeatherForecast/Services/ShopCartItemService.cs create mode 100644 webapi/WeatherForecast/Services/ShopCartItemsService.cs delete mode 100644 webapi/WeatherForecast/Services/ShopCartService.cs diff --git a/webapi/Core/Abstractions/Models/RequestModelBase.cs b/webapi/Core/Abstractions/Models/RequestModelBase.cs index 6259ec4..e387c3b 100644 --- a/webapi/Core/Abstractions/Models/RequestModelBase.cs +++ b/webapi/Core/Abstractions/Models/RequestModelBase.cs @@ -7,6 +7,7 @@ using System.Threading.Tasks; namespace Core.Abstractions.Models { public abstract class RequestModelBase : ModelBase { + public abstract T ApplyTo(T obj); public abstract T ToDomainObject(); } diff --git a/webapi/Core/DomainObjects/Documents/ShopCart.cs b/webapi/Core/DomainObjects/Documents/ShopCart.cs deleted file mode 100644 index 4a968d9..0000000 --- a/webapi/Core/DomainObjects/Documents/ShopCart.cs +++ /dev/null @@ -1,27 +0,0 @@ -using Core.Abstractions.DomainObjects; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace Core.DomainObjects.Documents { - public class ShopCart : DomainObjectDocumentBase { - public Guid SiteId { get; set; } - public Guid UserId { get; set; } - public string Slug { get; set; } - public string Sku { get; set; } - public Image Image { get; set; } - public string Title { get; set; } - public string BrandName { get; set; } - public string ShortText { get; set; } - public DateTime Created { get; set; } - public double Price { get; set; } - public double NewPrice { get; set; } - public uint Quantity { get; set; } - - public override int GetHashCode() { - throw new NotImplementedException(); - } - } -} diff --git a/webapi/Core/DomainObjects/Documents/ShopCartItem.cs b/webapi/Core/DomainObjects/Documents/ShopCartItem.cs new file mode 100644 index 0000000..47c58db --- /dev/null +++ b/webapi/Core/DomainObjects/Documents/ShopCartItem.cs @@ -0,0 +1,41 @@ +using Core.Abstractions.DomainObjects; + +namespace Core.DomainObjects.Documents { + public class ShopCartItem : DomainObjectDocumentBase { + public Guid SiteId { get; set; } + public Guid UserId { get; set; } + public string Slug { get; set; } + public string Sku { get; set; } + public Image Image { get; set; } + public string Title { get; set; } + public string BrandName { get; set; } + public string ShortText { get; set; } + public DateTime Created { get; set; } + public double Price { get; set; } + public double NewPrice { get; set; } + public uint Quantity { get; set; } + + public override int GetHashCode() { + // https://stackoverflow.com/questions/263400/what-is-the-best-algorithm-for-overriding-gethashcode + // Overflow is fine, just wrap + unchecked { + + int hash = 17; + // Suitable nullity checks etc, of course :) + hash = hash * 23 + SiteId.GetHashCode(); + hash = hash * 23 + UserId.GetHashCode(); + hash = hash * 23 + Slug.GetHashCode(); + hash = hash * 23 + Sku.GetHashCode(); + hash = hash * 23 + Image.GetHashCode(); + hash = hash * 23 + Title.GetHashCode(); + hash = hash * 23 + BrandName.GetHashCode(); + hash = hash * 23 + ShortText.GetHashCode(); + hash = hash * 23 + Created.GetHashCode(); + hash = hash * 23 + Price.GetHashCode(); + hash = hash * 23 + NewPrice.GetHashCode(); + hash = hash * 23 + Quantity.GetHashCode(); + return hash; + } + } + } +} diff --git a/webapi/Core/DomainObjects/Documents/ShopItem.cs b/webapi/Core/DomainObjects/Documents/ShopCatalogItem.cs similarity index 84% rename from webapi/Core/DomainObjects/Documents/ShopItem.cs rename to webapi/Core/DomainObjects/Documents/ShopCatalogItem.cs index a92ecf9..be4d314 100644 --- a/webapi/Core/DomainObjects/Documents/ShopItem.cs +++ b/webapi/Core/DomainObjects/Documents/ShopCatalogItem.cs @@ -1,7 +1,7 @@ using Core.Abstractions.DomainObjects; namespace Core.DomainObjects.Documents { - public class ShopItem : PostItemBase { + public class ShopCatalogItem : PostItemBase { public string Sku { get; set; } public int Rating { get; set; } diff --git a/webapi/Core/DomainObjects/Image.cs b/webapi/Core/DomainObjects/Image.cs index 01aaf2c..37896e7 100644 --- a/webapi/Core/DomainObjects/Image.cs +++ b/webapi/Core/DomainObjects/Image.cs @@ -6,7 +6,10 @@ namespace Core.DomainObjects { public string Alt { get; set; } public override int GetHashCode() { - throw new NotImplementedException(); + int hash = 17; + hash = hash * 23 + Src.GetHashCode(); + hash = hash * 23 + Alt.GetHashCode(); + return hash; } } } diff --git a/webapi/DataProviders/Mappings.cs b/webapi/DataProviders/Mappings.cs index 912b5ba..aaeabbc 100644 --- a/webapi/DataProviders/Mappings.cs +++ b/webapi/DataProviders/Mappings.cs @@ -290,8 +290,8 @@ namespace DataProviders { #endregion #region ShopItem - if (!BsonClassMap.IsClassMapRegistered(typeof(ShopItem))) { - BsonClassMap.RegisterClassMap(cm => { + if (!BsonClassMap.IsClassMapRegistered(typeof(ShopCatalogItem))) { + BsonClassMap.RegisterClassMap(cm => { cm.AutoMap(); }); } diff --git a/webapi/DataProviders/ShopCartDataProvider.cs b/webapi/DataProviders/ShopCartDataProvider.cs index 6fb76a6..92c1bbe 100644 --- a/webapi/DataProviders/ShopCartDataProvider.cs +++ b/webapi/DataProviders/ShopCartDataProvider.cs @@ -11,19 +11,42 @@ using Core.DomainObjects.Documents; namespace DataProviders { public interface IShopCartDataProvider { - (List?, IDomainResult) Get(Guid siteId, Guid userId); + (Guid?, IDomainResult) Insert(ShopCartItem obj); + (List?, IDomainResult) GetAll(Guid siteId, Guid userId); + (ShopCartItem?, IDomainResult) Get(Guid siteId, Guid userId, string sku); + (Guid?, IDomainResult) Update(ShopCartItem shopCart); + IDomainResult Delete(Guid siteId, Guid userId, string sku); } - public class ShopCartDataProvider : DataProviderBase, IShopCartDataProvider { + public class ShopCartDataProvider : DataProviderBase, IShopCartDataProvider { private const string _collectionName = "shopcart"; public ShopCartDataProvider( - ILogger> logger, + ILogger> logger, IMongoClient client, IIdGenerator idGenerator, ISessionService sessionService) : base(logger, client, idGenerator, sessionService) { } - public (List?, IDomainResult) Get(Guid siteId, Guid userId) => + public (Guid?, IDomainResult) Insert(ShopCartItem obj) => + Insert(obj, _collectionName); + + public (List?, IDomainResult) GetAll(Guid siteId, Guid userId) => GetWithPredicate(x => x.SiteId == siteId && x.UserId == userId, _collectionName); + + public (ShopCartItem?, IDomainResult) Get(Guid siteId, Guid userId, string sku) { + var (list, result) = GetWithPredicate(x => x.SiteId == siteId && x.UserId == userId && x.Sku == sku, _collectionName); + + if (!result.IsSuccess || list == null || list.Count == 0) + return (null, result); + + return (list.First(), result); + } + + public (Guid?, IDomainResult) Update(ShopCartItem shopCart) => + UpdateWithPredicate(shopCart, x => x.Id == shopCart.Id, _collectionName); + + public IDomainResult Delete(Guid siteId, Guid userId, string sku) { + throw new NotImplementedException(); + } } } diff --git a/webapi/WeatherForecast/Controllers/LoginController.cs b/webapi/WeatherForecast/Controllers/LoginController.cs index 2bd3df5..a76ea2b 100644 --- a/webapi/WeatherForecast/Controllers/LoginController.cs +++ b/webapi/WeatherForecast/Controllers/LoginController.cs @@ -3,29 +3,29 @@ using Microsoft.AspNetCore.Mvc; using WeatherForecast.Models; -namespace WeatherForecast.Controllers; +namespace WeatherForecast.Controllers { -public class PostLoginRequest : RequestModelBase { - public string Username { get; set; } - public string Password { get; set; } +//public class PostLoginRequest : RequestModelBase { +// public string Username { get; set; } +// public string Password { get; set; } -} +//} -[ApiController] -[Route("[controller]")] -public class LoginController : ControllerBase { +//[ApiController] +//[Route("[controller]")] +//public class LoginController : ControllerBase { - private readonly ILogger _logger; +// private readonly ILogger _logger; - public LoginController(ILogger logger) { - _logger = logger; - } +// public LoginController(ILogger logger) { +// _logger = logger; +// } - [HttpPost(Name = "Login")] - public IActionResult Post([FromBody] PostLoginRequest requestBody) { - return BadRequest(); - } +// [HttpPost(Name = "Login")] +// public IActionResult Post([FromBody] PostLoginRequest requestBody) { +// return BadRequest(); +// } } diff --git a/webapi/WeatherForecast/Controllers/ShopCartItemController.cs b/webapi/WeatherForecast/Controllers/ShopCartItemController.cs new file mode 100644 index 0000000..167c401 --- /dev/null +++ b/webapi/WeatherForecast/Controllers/ShopCartItemController.cs @@ -0,0 +1,84 @@ +using DomainResults.Mvc; +using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Mvc; +using WeatherForecast.Models.Requests; +using WeatherForecast.Services; + +namespace WeatherForecast.Controllers { + + + /// + /// + /// + [ApiController] + [AllowAnonymous] + [Route("api/[controller]")] + public class ShopCartItemController : ControllerBase { + + private readonly ILogger _logger; + private readonly IShopCartItemService _shopCartItemService; + + /// + /// + /// + /// + public ShopCartItemController( + ILogger logger, + IShopCartItemService shopCartItemService + ) { + _logger = logger; + _shopCartItemService = shopCartItemService; + } + + /// + /// + /// + /// + /// + /// + /// + /// + [HttpPost("{siteId}/{userId}/{sku}")] + public IActionResult Post([FromRoute] Guid siteId, [FromRoute] Guid userId, [FromRoute] string sku, [FromBody] PostShopCartItemRequestModel requestData) { + var result = _shopCartItemService.Post(siteId, userId, sku, requestData); + return result.ToActionResult(); + } + + /// + /// + /// + /// + [HttpGet("{siteId}/{userId}/{sku}")] + public IActionResult Get([FromRoute] Guid siteId, [FromRoute] Guid userId, [FromRoute] string sku) { + var result = _shopCartItemService.Get(siteId, userId, sku); + return result.ToActionResult(); + } + + /// + /// + /// + /// + /// + /// + /// + /// + [HttpPut("{siteId}/{userId}/{sku}")] + public IActionResult Update([FromRoute] Guid siteId, [FromRoute] Guid userId, [FromRoute] string sku, [FromBody] PutShopCartItemRequestModel requestData) { + var result = _shopCartItemService.Update(siteId, userId, sku, requestData); + return result.ToActionResult(); + } + + /// + /// + /// + /// + /// + /// + /// + [HttpDelete("{siteId}/{userId}/{sku}")] + public IActionResult Delete([FromRoute] Guid siteId, [FromRoute] Guid userId, [FromRoute] string sku) { + var result = _shopCartItemService.Delete(siteId, userId, sku); + return result.ToActionResult(); + } + } +} diff --git a/webapi/WeatherForecast/Controllers/ShopCartController.cs b/webapi/WeatherForecast/Controllers/ShopCartItemsController.cs similarity index 55% rename from webapi/WeatherForecast/Controllers/ShopCartController.cs rename to webapi/WeatherForecast/Controllers/ShopCartItemsController.cs index 9f9131b..fb92188 100644 --- a/webapi/WeatherForecast/Controllers/ShopCartController.cs +++ b/webapi/WeatherForecast/Controllers/ShopCartItemsController.cs @@ -13,21 +13,21 @@ namespace WeatherForecast.Controllers { [ApiController] [AllowAnonymous] [Route("api/[controller]")] - public class ShopCartController : ControllerBase { + public class ShopCartItemsController : ControllerBase { private readonly ILogger _logger; - private readonly IShopCartService _shopCartService; + private readonly IShopCartItemsService _shopCartItemsService; /// /// /// /// - public ShopCartController( + public ShopCartItemsController( ILogger logger, - IShopCartService shopCartService + IShopCartItemsService shopCartItemsService ) { _logger = logger; - _shopCartService = shopCartService; + _shopCartItemsService = shopCartItemsService; } /// @@ -36,14 +36,9 @@ namespace WeatherForecast.Controllers { /// [HttpGet("{siteId}/{userId}")] public IActionResult Get([FromRoute] Guid siteId, [FromRoute] Guid userId) { - var result = _shopCartService.GetShopCart(siteId, userId); - return result.ToActionResult(); - } - - [HttpPut("{siteId}/{userId}/{shopCartId}")] - public IActionResult Update([FromRoute] Guid siteId, [FromRoute] Guid userId, [FromRoute] Guid shopCartId, [FromBody] PutShopCatalogRequestModel requestData) { - var result = _shopCartService.UpdateShopCart(siteId, userId, shopCartId, requestData); + var result = _shopCartItemsService.Get(siteId, userId); return result.ToActionResult(); } + } } diff --git a/webapi/WeatherForecast/Models/Requests/PostShopCartItemRequestModel.cs b/webapi/WeatherForecast/Models/Requests/PostShopCartItemRequestModel.cs new file mode 100644 index 0000000..d25cb83 --- /dev/null +++ b/webapi/WeatherForecast/Models/Requests/PostShopCartItemRequestModel.cs @@ -0,0 +1,37 @@ +using Core.Abstractions.Models; +using Core.DomainObjects; +using Core.DomainObjects.Documents; + +namespace WeatherForecast.Models.Requests { + public class PostShopCartItemRequestModel : RequestModelBase { + + public string Slug { get; set; } + public ImageModel Image { get; set; } + public string Title { get; set; } + public string BrandName { get; set; } + public string ShortText { get; set; } + public DateTime Created { get; set; } + public double Price { get; set; } + public double NewPrice { get; set; } + public uint Quantity { get; set; } + + public override ShopCartItem ApplyTo(ShopCartItem shopCart) { + shopCart.Slug = Slug; + shopCart.Image = new Image { + Src = Image.Src, + Alt = Image.Alt + }; + shopCart.Title = Title; + shopCart.BrandName = BrandName; + shopCart.ShortText = ShortText; + shopCart.Created = Created; + shopCart.Price = Price; + shopCart.NewPrice = NewPrice; + shopCart.Quantity = Quantity; + + return shopCart; + } + + public override ShopCartItem ToDomainObject() => ApplyTo(new ShopCartItem()); + } +} diff --git a/webapi/WeatherForecast/Models/Requests/PutShopCartItemRequestModel.cs b/webapi/WeatherForecast/Models/Requests/PutShopCartItemRequestModel.cs new file mode 100644 index 0000000..c058f0a --- /dev/null +++ b/webapi/WeatherForecast/Models/Requests/PutShopCartItemRequestModel.cs @@ -0,0 +1,37 @@ +using Core.Abstractions.Models; +using Core.DomainObjects; +using Core.DomainObjects.Documents; + +namespace WeatherForecast.Models.Requests { + public class PutShopCartItemRequestModel : RequestModelBase { + + public string Slug { get; set; } + public ImageModel Image { get; set; } + public string Title { get; set; } + public string BrandName { get; set; } + public string ShortText { get; set; } + public DateTime Created { get; set; } + public double Price { get; set; } + public double NewPrice { get; set; } + public uint Quantity { get; set; } + + public override ShopCartItem ApplyTo(ShopCartItem shopCart) { + shopCart.Slug = Slug; + shopCart.Image = new Image { + Src = Image.Src, + Alt = Image.Alt + }; + shopCart.Title = Title; + shopCart.BrandName = BrandName; + shopCart.ShortText = ShortText; + shopCart.Created = Created; + shopCart.Price = Price; + shopCart.NewPrice = NewPrice; + shopCart.Quantity = Quantity; + + return shopCart; + } + + public override ShopCartItem ToDomainObject() => ApplyTo(new ShopCartItem()); + } +} diff --git a/webapi/WeatherForecast/Models/Requests/PutShopCatalogRequestModel.cs b/webapi/WeatherForecast/Models/Requests/PutShopCatalogRequestModel.cs deleted file mode 100644 index 917eefb..0000000 --- a/webapi/WeatherForecast/Models/Requests/PutShopCatalogRequestModel.cs +++ /dev/null @@ -1,37 +0,0 @@ -using Core.Abstractions.Models; -using Core.DomainObjects; -using Core.DomainObjects.Documents; - -namespace WeatherForecast.Models.Requests { - public class PutShopCatalogRequestModel : RequestModelBase { - - public string Slug { get; set; } - public string Sku { get; set; } - public ImageModel Image { get; set; } - public string Title { get; set; } - public string BrandName { get; set; } - public string ShortText { get; set; } - public DateTime Created { get; set; } - public double Price { get; set; } - public double NewPrice { get; set; } - public uint Quantity { get; set; } - - public override ShopCart ToDomainObject() { - return new ShopCart { - Slug = Slug, - Sku = Sku, - Image = new Image { - Src = Image.Src, - Alt = Image.Alt - }, - Title = Title, - BrandName = BrandName, - ShortText = ShortText, - Created = Created, - Price = Price, - NewPrice = NewPrice, - Quantity = Quantity - }; - } - } -} diff --git a/webapi/WeatherForecast/Models/Responses/GetShopCartItemResponseModel.cs b/webapi/WeatherForecast/Models/Responses/GetShopCartItemResponseModel.cs new file mode 100644 index 0000000..416fcb6 --- /dev/null +++ b/webapi/WeatherForecast/Models/Responses/GetShopCartItemResponseModel.cs @@ -0,0 +1,33 @@ +using Core.Abstractions.Models; +using Core.DomainObjects.Documents; + +namespace WeatherForecast.Models.Responses { + public class GetShopCartItemResponseModel : ResponseModelBase { + public string Slug { get; set; } + public string Sku { get; set; } + public ImageModel Image { get; set; } + public string Title { get; set; } + public string BrandName { get; set; } + public string ShortText { get; set; } + public DateTime Created { get; set; } + public double Price { get; set; } + public double NewPrice { get; set; } + public uint Quantity { get; set; } + + public GetShopCartItemResponseModel() { } + + public GetShopCartItemResponseModel(ShopCartItem shopCartItem) { + Slug = shopCartItem.Slug; + Sku = shopCartItem.Sku; + Image = new ImageModel(shopCartItem.Image); + Title = shopCartItem.Title; + BrandName = shopCartItem.BrandName; + ShortText = shopCartItem.ShortText; + Created = shopCartItem.Created; + Price = shopCartItem.Price; + NewPrice = shopCartItem.NewPrice; + Quantity = shopCartItem.Quantity; + + } + } +} diff --git a/webapi/WeatherForecast/Models/Responses/GetShopCartItemsResponseModel.cs b/webapi/WeatherForecast/Models/Responses/GetShopCartItemsResponseModel.cs new file mode 100644 index 0000000..e9d3946 --- /dev/null +++ b/webapi/WeatherForecast/Models/Responses/GetShopCartItemsResponseModel.cs @@ -0,0 +1,14 @@ +using Core.Abstractions.Models; +using Core.DomainObjects.Documents; + +namespace WeatherForecast.Models.Responses { + public class GetShopCartItemsResponseModel : ResponseModelBase { + + public List Items { get; set; } + + public GetShopCartItemsResponseModel(List items) { + Items = items.Select(x => new GetShopCartItemResponseModel(x)).ToList(); + + } + } +} diff --git a/webapi/WeatherForecast/Models/Responses/GetShopCartResponseModel.cs b/webapi/WeatherForecast/Models/Responses/GetShopCartResponseModel.cs deleted file mode 100644 index 246b03a..0000000 --- a/webapi/WeatherForecast/Models/Responses/GetShopCartResponseModel.cs +++ /dev/null @@ -1,14 +0,0 @@ -using Core.Abstractions.Models; -using Core.DomainObjects.Documents; - -namespace WeatherForecast.Models.Responses { - public class GetShopCartResponseModel : ResponseModelBase { - - public List Items { get; set; } - - public GetShopCartResponseModel(List items) { - Items = items.Select(x => new ShopCartModel(x)).ToList(); - - } - } -} diff --git a/webapi/WeatherForecast/Models/ShopCartModel.cs b/webapi/WeatherForecast/Models/ShopCartModel.cs deleted file mode 100644 index a3dc710..0000000 --- a/webapi/WeatherForecast/Models/ShopCartModel.cs +++ /dev/null @@ -1,33 +0,0 @@ -using Core.Abstractions.Models; -using Core.DomainObjects.Documents; - -namespace WeatherForecast.Models { - public class ShopCartModel : ModelBase { - - public string Slug { get; set; } - public string Sku { get; set; } - public ImageModel Image { get; set; } - public string Title { get; set; } - public string BrandName { get; set; } - public string ShortText { get; set; } - public DateTime Created { get; set; } - public double Price { get; set; } - public double NewPrice { get; set; } - public uint Quantity { get; set; } - - public ShopCartModel() { } - - public ShopCartModel(ShopCart shopCart) { - Slug = shopCart.Slug; - Sku = shopCart.Sku; - Image = new ImageModel(shopCart.Image); - Title = shopCart.Title; - BrandName = shopCart.BrandName; - ShortText = shopCart.ShortText; - Created = shopCart.Created; - Price = shopCart.Price; - NewPrice = shopCart.NewPrice; - Quantity = shopCart.Quantity; - } - } -} diff --git a/webapi/WeatherForecast/Services/ShopCartItemService.cs b/webapi/WeatherForecast/Services/ShopCartItemService.cs new file mode 100644 index 0000000..a6b2571 --- /dev/null +++ b/webapi/WeatherForecast/Services/ShopCartItemService.cs @@ -0,0 +1,81 @@ +using DataProviders; +using DomainResults.Common; + +using WeatherForecast.Models.Requests; +using WeatherForecast.Models.Responses; + +namespace WeatherForecast.Services { + + public interface IShopCartItemService { + (Guid?, IDomainResult) Post(Guid siteId, Guid userId, string sku, PostShopCartItemRequestModel requestModel); + (GetShopCartItemResponseModel?, IDomainResult) Get(Guid siteId, Guid userId, string sku); + (Guid?, IDomainResult) Update(Guid siteId, Guid userId, string sku, PutShopCartItemRequestModel requestData); + IDomainResult Delete(Guid siteId, Guid userId, string sku); + } + + public class ShopCartItemService : IShopCartItemService { + + ILogger _logger; + IShopCartDataProvider _shopCartDataProvider; + + public ShopCartItemService( + ILogger logger, + IShopCartDataProvider shopCartDataprovider + ) { + _logger = logger; + _shopCartDataProvider = shopCartDataprovider; + } + + public (Guid?, IDomainResult) Post(Guid siteId, Guid userId, string sku, PostShopCartItemRequestModel requestModel) { + var (_, getResult) = _shopCartDataProvider.Get(siteId, userId, sku); + if (getResult.IsSuccess) + return IDomainResult.Failed(); + + var obj = requestModel.ToDomainObject(); + + obj.SiteId = siteId; + obj.UserId = userId; + obj.Sku = sku; + + var (id, insertResult) = _shopCartDataProvider.Insert(obj); + + if (!insertResult.IsSuccess) + return IDomainResult.Failed(); + + return IDomainResult.Success(id); + } + + public (GetShopCartItemResponseModel?, IDomainResult) Get(Guid siteId, Guid userId, string sku) { + var (item, result) = _shopCartDataProvider.Get(siteId, userId, sku); + + if (!result.IsSuccess || item == null) + return (null, result); + + return IDomainResult.Success(new GetShopCartItemResponseModel(item)); + } + + public (Guid?, IDomainResult) Update(Guid siteId, Guid userId, string sku, PutShopCartItemRequestModel requestData) { + var (item, getResult) = _shopCartDataProvider.Get(siteId, userId, sku); + if (!getResult.IsSuccess || item == null) + return (null, getResult); + + var newShopCart = requestData.ApplyTo(item); + + if (!item.Equals(newShopCart)) { + var (id, updateResult) = _shopCartDataProvider.Update(item); + if (!updateResult.IsSuccess || id == null) + return (null, updateResult); + } + + return IDomainResult.Success(item.Id); + } + + public IDomainResult Delete(Guid siteId, Guid userId, string sku) { + var result = _shopCartDataProvider.Delete(siteId, userId, sku); + if (!result.IsSuccess) + return result; + + return IDomainResult.Success(); + } + } +} diff --git a/webapi/WeatherForecast/Services/ShopCartItemsService.cs b/webapi/WeatherForecast/Services/ShopCartItemsService.cs new file mode 100644 index 0000000..c6d5c1b --- /dev/null +++ b/webapi/WeatherForecast/Services/ShopCartItemsService.cs @@ -0,0 +1,36 @@ +using DataProviders; +using DomainResults.Common; + +using WeatherForecast.Models.Requests; +using WeatherForecast.Models.Responses; + +namespace WeatherForecast.Services { + + public interface IShopCartItemsService { + (GetShopCartItemsResponseModel?, IDomainResult) Get(Guid siteId, Guid userId); + } + + public class ShopCartItemsService : IShopCartItemsService { + + ILogger _logger; + IShopCartDataProvider _shopCartDataProvider; + + public ShopCartItemsService( + ILogger logger, + IShopCartDataProvider shopCartDataprovider + ) { + _logger = logger; + _shopCartDataProvider = shopCartDataprovider; + } + + public (GetShopCartItemsResponseModel?, IDomainResult) Get(Guid siteId, Guid userId) { + + var (items, result) = _shopCartDataProvider.GetAll(siteId, userId); + + if (!result.IsSuccess || items == null) + return (null, result); + + return IDomainResult.Success(new GetShopCartItemsResponseModel(items)); + } + } +} diff --git a/webapi/WeatherForecast/Services/ShopCartService.cs b/webapi/WeatherForecast/Services/ShopCartService.cs deleted file mode 100644 index 53fe7ce..0000000 --- a/webapi/WeatherForecast/Services/ShopCartService.cs +++ /dev/null @@ -1,46 +0,0 @@ -using DataProviders; -using DomainResults.Common; - -using WeatherForecast.Models.Requests; -using WeatherForecast.Models.Responses; - -namespace WeatherForecast.Services { - - public interface IShopCartService { - (GetShopCartResponseModel?, IDomainResult) GetShopCart(Guid siteId, Guid userId); - (Guid?, IDomainResult) UpdateShopCart(Guid siteId, Guid userId, Guid shopCartId, PutShopCatalogRequestModel requestData); - } - - public class ShopCartService : IShopCartService { - - ILogger _logger; - IShopCartDataProvider _shopCartDataProvider; - - public ShopCartService( - ILogger logger, - IShopCartDataProvider shopCartDataprovider - ) { - _logger = logger; - _shopCartDataProvider = shopCartDataprovider; - } - - public (GetShopCartResponseModel?, IDomainResult) GetShopCart(Guid siteId, Guid userId) { - var (items, result) = _shopCartDataProvider.Get(siteId, userId); - - if (!result.IsSuccess || items == null) - return (null, result); - - return IDomainResult.Success(new GetShopCartResponseModel(items)); - } - - public (Guid?, IDomainResult) UpdateShopCart(Guid siteId, Guid userId, Guid shopCartId, PutShopCatalogRequestModel requestData) { - var shopCart = requestData.ToDomainObject(); - - shopCart.Id = shopCartId; - shopCart.SiteId = siteId; - shopCart.UserId = userId; - - return IDomainResult.Failed(); - } - } -} diff --git a/webapi/WeatherForecast/Startup.cs b/webapi/WeatherForecast/Startup.cs index 3f72035..4041fa9 100644 --- a/webapi/WeatherForecast/Startup.cs +++ b/webapi/WeatherForecast/Startup.cs @@ -58,7 +58,8 @@ namespace WeatherForecast { services.AddHttpContextAccessor(); services.AddScoped(); - services.AddScoped(); + services.AddScoped(); + services.AddScoped(); services.RegisterDataproviders(appSettings);