86 lines
2.6 KiB
C#
86 lines
2.6 KiB
C#
using DataProviders;
|
|
using DomainResults.Common;
|
|
using WeatherForecast.Models;
|
|
using WeatherForecast.Models.Requests;
|
|
|
|
namespace WeatherForecast.Services {
|
|
|
|
public interface IShopItemService {
|
|
(GetShopItemResponseModel, IDomainResult) Get(Guid siteId, string sku);
|
|
}
|
|
|
|
public class ShopItemService : IShopItemService {
|
|
|
|
private readonly ILogger<ShopItemService> _logger;
|
|
private readonly IShopCatalogDataProvider _shopCatalogDataProvider;
|
|
|
|
public ShopItemService(
|
|
ILogger<ShopItemService> logger,
|
|
IShopCatalogDataProvider shopCatalogDataProvider
|
|
) {
|
|
_logger = logger;
|
|
_shopCatalogDataProvider = shopCatalogDataProvider;
|
|
}
|
|
|
|
public (Guid?, IDomainResult) Post(Guid siteId, string sku, PostShopItemRequestModel requestModel) {
|
|
var (_, getResult) = _shopCatalogDataProvider.Get(siteId, sku);
|
|
if (getResult.IsSuccess)
|
|
return IDomainResult.Failed<Guid?>();
|
|
|
|
var item = requestModel.ToDomainObject();
|
|
|
|
item.SiteId = siteId;
|
|
item.Sku = sku;
|
|
|
|
var (id, insertResult) = _shopCatalogDataProvider.Insert(item);
|
|
|
|
if (!insertResult.IsSuccess)
|
|
return IDomainResult.Failed<Guid?>();
|
|
|
|
return IDomainResult.Success(id);
|
|
}
|
|
|
|
public (GetShopItemResponseModel?, IDomainResult) Get(Guid siteId, string sku) {
|
|
var (item, result) = _shopCatalogDataProvider.Get(siteId, sku);
|
|
|
|
if (!result.IsSuccess || item == null)
|
|
return (null, result);
|
|
|
|
return IDomainResult.Success(new GetShopItemResponseModel(item));
|
|
}
|
|
|
|
public (Guid?, IDomainResult) Update(Guid siteId, string sku, PutShopItemRequestModel requestData) {
|
|
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;
|
|
|
|
if (!item.Equals(newItem)) {
|
|
var (id, updateResult) = _shopCatalogDataProvider.Update(newItem);
|
|
if (!updateResult.IsSuccess || id == null)
|
|
return (null, updateResult);
|
|
}
|
|
|
|
return IDomainResult.Success(item.Id);
|
|
}
|
|
|
|
public IDomainResult Delete(Guid siteId, string sku) {
|
|
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;
|
|
|
|
return IDomainResult.Success();
|
|
}
|
|
|
|
}
|
|
}
|