82 lines
2.7 KiB
C#
82 lines
2.7 KiB
C#
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<ShopCartItemService> _logger;
|
|
IShopCartDataProvider _shopCartDataProvider;
|
|
|
|
public ShopCartItemService(
|
|
ILogger<ShopCartItemService> 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<Guid?>();
|
|
|
|
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<Guid?>();
|
|
|
|
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();
|
|
}
|
|
}
|
|
}
|