public override async Task <IActionResult> GetById(Guid id) { try { var item = await _context.Set <Item>().Include(x => x.ItemCategory) .Include(x => x.ItemImages).FirstOrDefaultAsync(x => x.ID == id); if (item == null) { return(NotFound("id is not realted to any Item")); } var json = new JsonResult(new ItemFullModel() { ID = item.ID, Name = item.Name, ShortDescription = item.ShortDescription, ItemCategoryID = item.ItemCategoryID, Price = item.Price, CashBack = item.CashBack, Quantity = item.Quantity, Description = item.Description, ThumbnailImagePath = item.ThumbnailImagePath, IsEnable = item.IsEnable, ItemCategory = item.ItemCategory?.Name, ItemImages = item.ItemImages.Select(y => y.ImagePath).ToList(), DaysToBeAvilable = item.DaysToBeAvailable, AccountID = item.AccountID, Location = item.Location, Mobile = item.Mobile, Owner = item.Owner }); #region Add Click History var clickContext = _context as IClickHistoryContext; if (clickContext != null) { var clickObject = clickContext.ObjectClicks.FirstOrDefault(x => x.ObjectType == nameof(Item) && x.ObjectID == id); if (clickObject == null) { clickObject = new SmartLifeLtd.Data.Tables.Shared.ObjectClick() { ObjectType = nameof(Item), ObjectID = id, ClicksCount = 0 }; clickContext.ObjectClicks.Add(clickObject); } clickObject.ClicksCount++; clickObject.LastClickedDate = DateTime.UtcNow; _context.SaveChanges(); } #endregion return(json); } catch (Exception ex) { return(BadRequest()); } }
public override async Task <IActionResult> GetById(Guid id) { try { var offer = await _context.Set <Offer>() .Include(x => x.Item) .Include(x => x.OfferItems) .ThenInclude(x => x.Item) .ThenInclude(x => x.ItemCategory) .Include(x => x.OfferImages) .FirstOrDefaultAsync(x => x.ID == id); if (offer == null) { return(NotFound("id is not related to any Offer")); } var json = new JsonResult(new OfferFullModel() { ID = offer.ID, Title = offer.Title, ItemName = offer.Item?.Name, Price = offer.Price ?? ((offer.Item?.Price ?? 0) - ((offer.Item?.Price ?? 0) * (offer.Discount ?? 0) / 100.0)), Discount = offer.Discount, UnitNetPrice = offer.Price ?? ((offer.Item?.Price ?? 0) - ((offer.Item?.Price ?? 0) * (offer.Discount ?? 0) / 100.0)), UnitPrice = offer.Item?.Price ?? 0, Description = offer.Description, ThumbnailImagePath = offer.Item?.ThumbnailImagePath, IsActive = offer.IsActive, ShortDescription = offer.ShortDescription, OfferType = offer.OfferType, ItemID = offer.ItemID, OfferImages = offer.OfferImages?.Select(y => y.ImagePath).ToList(), OfferItems = offer.OfferItems?.Select(y => new ItemBasicModel() { ID = y.ItemID ?? Guid.Empty, Name = y.Item?.Name, Price = y.Item?.Price, Quantity = y.Item?.Quantity, ShortDescription = y.Item?.ShortDescription, ItemCategoryID = y.Item?.ItemCategoryID, ThumbnailImagePath = y.Item?.ThumbnailImagePath, ItemCategory = y.Item?.ItemCategory?.Name }).ToList() }); #region Add Click History var clickContext = _context as IClickHistoryContext; if (clickContext != null) { var clickObject = clickContext.ObjectClicks.FirstOrDefault(x => x.ObjectType == nameof(offer) && x.ObjectID == id); if (clickObject == null) { clickObject = new SmartLifeLtd.Data.Tables.Shared.ObjectClick() { ObjectType = nameof(Offer), ObjectID = id, ClicksCount = 0 }; clickContext.ObjectClicks.Add(clickObject); } clickObject.ClicksCount++; clickObject.LastClickedDate = DateTime.UtcNow; _context.SaveChanges(); } #endregion return(json); } catch (Exception ex) { return(BadRequest()); } }