private async Task <bool> GetOrListNewInventory(CreateInventoryRequest request, Inventory NewInventory) { StockXAccount tmp = new StockXAccount() { }; var List = await tmp.GetProductFromUrl(request.StockXUrl); if (List == null) { return(false); } if (List.Product == null) { } var Offers = List.Product.Children.Keys.Select(A => List.Product.Children[A]).ToList(); if (Offers == null || Offers.Count == 0) { return(false); } if (request.Size == "") { var Item = Offers[0]; NewInventory.Sku = Offers[0].Uuid; NewInventory.Size = ""; } else { var Sized = Offers.Where(A => A.ShoeSize == request.Size).FirstOrDefault(); if (Sized == null) { return(false); } NewInventory.Active = true; NewInventory.StockXAccountId = request.StockXAccountId; NewInventory.ParentSku = Sized.ParentUuid; NewInventory.Sku = Sized.Uuid; } foreach (var Offer in Offers) { if (!Db.Exists <StockXChildListing>(A => A.Uuid == NewInventory.Sku)) { Offer.Url = NewInventory.StockXUrl; Db.Insert(Offer); } } return(true); }
// [RequiresAnyRole("")] public async Task <CreateInventoryResponse> Post(CreateInventoryRequest request) { var Validation = new ValidateCreateInventory(); var Valid = Validation.Validate(request); if (!Valid.IsValid) { return(new CreateInventoryResponse() { Message = Valid.Errors.Select(A => A.ErrorMessage).Join("\n"), Success = false }); } var User = this.GetCurrentAppUser(); var ExistingStockXAccount = Db.Single <StockXAccount>(A => A.Id == request.StockXAccountId && A.UserId == User.Id); if (ExistingStockXAccount == null) { return(new CreateInventoryResponse() { Success = false, Message = "No Such StockXAccount" }); } request.StockXUrl = request.StockXUrl.ToLower(); var NewInventory = new Inventory() { StockXUrl = request.StockXUrl, Quantity = request.Quantity, MinSell = request.MinSell, UserId = User.Id, StartingAsk = request.StartingAsk, Size = request.Size, }; try { var Existing = Db.Single(Db.From <StockXChildListing>().Where(A => A.Url == request.StockXUrl && A.ShoeSize == request.Size)); if (Existing != null) { NewInventory.Active = true; NewInventory.StockXAccountId = request.StockXAccountId; NewInventory.ParentSku = Existing.ParentUuid; NewInventory.Sku = Existing.Uuid; } else { var Pass = await GetOrListNewInventory(request, NewInventory); if (!Pass) { return(new CreateInventoryResponse() { Success = false, Message = "Could not get product data" }); } } try { if (Db.Exists <Inventory>(A => A.Sku == NewInventory.Sku && A.StockXAccountId == request.StockXAccountId && A.UserId == User.Id)) { return(new CreateInventoryResponse() { Success = false, Message = "This Inventory Exists for this Account" }); } var InsertedId = Db.Insert(NewInventory, true); return(new CreateInventoryResponse() { InsertedId = InsertedId, Success = true, }); } catch (Exception ex) { return(new CreateInventoryResponse() { Success = false, Message = ex.Message }); } } catch (Exception ex) { return(new CreateInventoryResponse() { Success = false, Message = ex.Message }); } }