reactredux/webapi/WeatherForecast/Services/ShopItemService.cs

126 lines
4.1 KiB
C#

using Core.DomainObjects;
using Core.DomainObjects.L10n;
using Core.Enumerations;
using DataProviders;
using DomainResults.Common;
using ExtensionMethods;
using WeatherForecast.Models;
using WeatherForecast.Models.Requests;
namespace WeatherForecast.Services {
public interface IShopItemService {
(Guid?, IDomainResult) Post(Guid siteId, string sku, PostShopItemRequestModel requestModel);
(GetShopItemResponseModel, IDomainResult) Get(Guid siteId, string sku, Locales locale);
(Guid?, IDomainResult) Update(Guid siteId, string sku, PutShopItemRequestModel requestData);
IDomainResult Delete(Guid siteId, string sku);
}
public class ShopItemService : IShopItemService {
private readonly ILogger<ShopItemService> _logger;
private readonly IShopCatalogDataProvider _shopCatalogDataProvider;
private readonly ICategoryDataProvider _categoryDataProvider;
public ShopItemService(
ILogger<ShopItemService> logger,
IShopCatalogDataProvider shopCatalogDataProvider,
ICategoryDataProvider categoryDataProvider
) {
_logger = logger;
_shopCatalogDataProvider = shopCatalogDataProvider;
_categoryDataProvider = categoryDataProvider;
}
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;
// 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<Image>() {
new Image {
Src = "https://dummyimage.com/450x300/dee2e6/6c757d.jpg",
L10n = new List<ImageL10n> {
new ImageL10n {
Locale = Locales.Us,
Alt = "..."
}
}
}
};
// TODO: default value shoud not be hardcoded by database id
item.Categories ??= new List<Guid> { "e154e33f-3cc7-468d-bb66-e0390ddb9ae0".ToGuid() };
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, Locales locale) {
var (item, result) = _shopCatalogDataProvider.Get(siteId, sku);
if (!result.IsSuccess || item == null)
return (null, result);
var categories = new List<Category>();
foreach (var catId in item.Categories) {
var (category, getCategoryResult) = _categoryDataProvider.Get(catId, siteId);
if (!getCategoryResult.IsSuccess || category == null)
return (null, getCategoryResult);
categories.Add(category);
}
return IDomainResult.Success(new GetShopItemResponseModel(item, categories, locale));
}
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;
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);
}
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();
}
}
}