public MarketAdViewModel(MarketAd ad) { this.Id = ad.Id; this.AdType = ad.Type.ToString(); this.Description = ad.Description; this.AuthorName = ad.Author.Name; var offerBundle = ad.Bundles.FirstOrDefault(b => (ad.Type == MarketAd.AdType.Sell || ad.Type == MarketAd.AdType.Lend) ? b.Type == MarketAdBundle.BundleType.Offer : b.Type == MarketAdBundle.BundleType.Request); this.ImageUrl = offerBundle == null ? "#" : offerBundle.Items.Count() > 0 ? offerBundle.Items.FirstOrDefault().Item.ImageUrl : "#"; this.Item = offerBundle == null || offerBundle.Items.Count() == 0 ? null : offerBundle.Items.FirstOrDefault().Item; this.AskingPrice = (ad.Type == MarketAd.AdType.Sell || ad.Type == MarketAd.AdType.Lend) ? ad.RequestString : ad.OfferString; this.Caption = (ad.Type == MarketAd.AdType.Sell || ad.Type == MarketAd.AdType.Lend) ? ad.OfferString : ad.RequestString; this.PlacementDate = ad.PlacementDate; this.OfferString = ad.OfferString; this.RequestString = ad.RequestString; }
public async Task <ActionResponseViewModel> EditAd(int id, int?playerId, int adType, string offer, string request, string description, bool active) { if (adType > byte.MaxValue || adType < byte.MinValue || !Enum.IsDefined(typeof(MarketAd.AdType), (byte)adType)) { return(ActionResponseViewModel.Error()); } var player = await _dbContext.Player.SingleOrDefaultAsync(p => p.Id == playerId); if (player == null) { return(ActionResponseViewModel.Error()); } var marketAd = await _dbContext.MarketAd.SingleOrDefaultAsync(ad => ad.Id == id && ad.AuthorId == player.Id); if (id > 0 && marketAd == null) { return(ActionResponseViewModel.Error("Ad doesn't exist or you don't have access to edit it.")); } if (id == 0) { marketAd = new MarketAd { Id = 0, AuthorId = player.Id, Bundles = new List <MarketAdBundle>(), PlacementDate = DateTime.Now, }; _dbContext.Add(marketAd); } marketAd.Type = (MarketAd.AdType)adType; marketAd.Active = active; marketAd.Description = description; var parsedOffer = await ParseMarketAdPart(offer); var parsedRequest = await ParseMarketAdPart(request); if (!parsedOffer.Success || !parsedRequest.Success) { if (id == 0) { _dbContext.Remove(marketAd); } return(ActionResponseViewModel.Error(parsedOffer.ParseMessage + " " + parsedRequest.ParseMessage)); } List <MarketAdBundle> allBundles = new List <MarketAdBundle>(); // processing offer marketAd.OfferType = MarketAd.TransactionElementType.Specific; if (parsedOffer.IsFree) { marketAd.OfferType = MarketAd.TransactionElementType.Nothing; } if (parsedOffer.IsAny) { marketAd.OfferType = MarketAd.TransactionElementType.Bid; } if (marketAd.OfferType == MarketAd.TransactionElementType.Specific) { var offerBundles = parsedOffer.ParsedBundles; offerBundles.ForEach(b => b.Type = MarketAdBundle.BundleType.Offer); allBundles.AddRange(offerBundles); } //processing request marketAd.RequestType = MarketAd.TransactionElementType.Specific; if (parsedRequest.IsFree) { marketAd.RequestType = MarketAd.TransactionElementType.Nothing; } if (parsedRequest.IsAny) { marketAd.RequestType = MarketAd.TransactionElementType.Bid; } if (marketAd.RequestType == MarketAd.TransactionElementType.Specific) { var requestBundles = parsedRequest.ParsedBundles; requestBundles.ForEach(b => b.Type = MarketAdBundle.BundleType.Request); allBundles.AddRange(requestBundles); } if (marketAd.Bundles != null && marketAd.Bundles.Count() > 0) { _dbContext.RemoveRange(marketAd.Bundles); marketAd.Bundles = null; } marketAd.Bundles = allBundles; await _dbContext.SaveChangesAsync(); return(ActionResponseViewModel.Success(id: marketAd.Id)); }