public Result AddItem(AddItemRequest request) { var currency = _languageAndCurrencyAccessor.GetCurrencyByCode(request.SelectedItemCurrency).Payload; var donateItem = new DonateItem { ItemType = request.SelectedItemType, Need = request.SelectedItemType == "direct" ? Math.Min(request.CurrentAmount.Value / request.GoalAmount.Value, 1) : request.Need.Value, Price = request.Price, CurrencyID = currency.CurrencyID }; var donateItemResult = _donateAccessor.AddItem(donateItem); if (!donateItemResult.IsSuccess) { return(new Result(false, donateItemResult.Message)); } donateItem = donateItemResult.Payload; var engTitle = new DonateItemTitle { ItemID = donateItem.ItemID, LanguageID = 1, Title = request.EnglishName }; _donateAccessor.AddItemTitle(engTitle); var swaTitle = new DonateItemTitle { ItemID = donateItem.ItemID, LanguageID = 2, Title = request.SwahiliName }; _donateAccessor.AddItemTitle(swaTitle); var engDescription = new DonateItemDescription { ItemID = donateItem.ItemID, LanguageID = 1, Description = request.EnglishDescription }; _donateAccessor.AddItemDescription(engDescription); var swaDescription = new DonateItemDescription { ItemID = donateItem.ItemID, LanguageID = 2, Description = request.SwahiliDescription }; _donateAccessor.AddItemDescription(swaDescription); if (request.SelectedItemType == "direct") { var invItem = new InventoryItem { ItemID = donateItem.ItemID, ActualAmount = request.CurrentAmount.Value, GoalAmount = (int)request.GoalAmount.Value, AutoDecrement = request.AutoDecrement, DecrementPerDay = request.DecrementPerDay }; _inventoryAccessor.AddInventoryItem(invItem); } else { var campaignItem = new CampaignItem { ItemID = donateItem.ItemID, GoalAmount = (int)Math.Ceiling(request.Price), ActualAmount = 0, CurrencyID = currency.CurrencyID }; _donateAccessor.AddCampaignItem(campaignItem); } var imageBase = request.ImageBase.Substring(request.ImageBase.IndexOf("base64,") + "base64,".Length); var imageBytes = Convert.FromBase64String(imageBase); //Add image here using (var stream = new MemoryStream()) { using (Image <Rgba32> image = Image.Load(imageBytes)) { var width = Math.Min(image.Width, image.Height); image.Mutate(x => x.Crop(width, width)); image.SaveAsJpeg(stream); } var bytes = stream.ToArray(); var itemImage = new DonateItemImage { ItemImageID = 0, ItemID = donateItem.ItemID, ImageData = bytes }; _donateAccessor.AddImage(itemImage); } return(new Result(true)); }
public Result <DonateItemTitle> AddItemTitle(DonateItemTitle title) { _dBContext.DonateItemTitle.Add(title); _dBContext.SaveChanges(); return(new Result <DonateItemTitle>(title)); }