/// <summary> /// Retrieve all applications by specified receiver /// </summary> /// <param name="receiverId"></param> /// <returns></returns> public IQueryable <ApplicationDTO> Read(int receiverId) { Func <ApplicationStatusEnum, bool> checkStatus = status => status == ApplicationStatusEnum.Completed || status == ApplicationStatusEnum.Pending;; var entities = from a in _context.Applications where a.UserId == receiverId orderby a.Created descending select new ApplicationDTO { ApplicationId = a.Id, ReceiverId = a.UserId, ReceiverName = $"{a.User.FirstName} {a.User.SurName}", Country = a.User.Country, Thumbnail = ImageHelper.GetRelativeStaticFolderImagePath(a.User.Thumbnail), ProductId = a.Product.Id, ProductTitle = a.Product.Title, ProductPrice = a.Product.Price, ProducerId = a.Product.UserId, Motivation = a.Motivation, Bytes = checkStatus(a.Status) ? (from c in _context.Contracts where a.Id == c.ApplicationId select c.Bytes ).FirstOrDefault() : 0, BytesInCurrentDollars = checkStatus(a.Status) ? BytesToUSDConverter.BytesToUSD( (from c in _context.Contracts where a.Id == c.ApplicationId select c.Bytes ).FirstOrDefault(), (from b in _context.ByteExchangeRate where b.Id == 1 select b.GBYTE_USD).FirstOrDefault() ) : 0, ContractSharedAddress = checkStatus(a.Status) ? (from c in _context.Contracts where c.ApplicationId == a.Id select c.SharedAddress ).FirstOrDefault() : null, Status = a.Status, DateOfDonation = a.DateOfDonation.ToString("yyyy-MM-dd"), CreationDate = a.Created.ToString("yyyy-MM-dd HH:mm:ss"), }; return(entities); }
/// <summary> /// Retrieve all applications by specified receiver /// </summary> /// <param name="receiverId"></param> /// <returns></returns> public IQueryable <ApplicationDTO> ReadWithdrawableByProducer(int producerId) { Func <ApplicationStatusEnum, bool> checkStatus = status => status == ApplicationStatusEnum.Completed || status == ApplicationStatusEnum.Pending; var applications = from a in (from p in _context.Products where p.UserId == producerId select p.Applications).SelectMany(aps => aps) where a.Status == ApplicationStatusEnum.Completed where _context.Contracts.Where(c => c.ApplicationId == a.Id).Select(c => c.Bytes).FirstOrDefault() > 0 orderby a.Id ascending select new ApplicationDTO { ApplicationId = a.Id, ReceiverId = a.UserId, ReceiverName = $"{a.User.FirstName} {a.User.SurName}", Country = a.User.Country, Thumbnail = ImageHelper.GetRelativeStaticFolderImagePath(a.User.Thumbnail), ProductId = a.Product.Id, ProductTitle = a.Product.Title, ProductPrice = a.Product.Price, ProducerId = a.Product.UserId, Motivation = a.Motivation, Bytes = (from c in _context.Contracts where a.Id == c.ApplicationId select c.Bytes).FirstOrDefault(), BytesInCurrentDollars = BytesToUSDConverter.BytesToUSD( (from c in _context.Contracts where a.Id == c.ApplicationId select c.Bytes).FirstOrDefault(), (from b in _context.ByteExchangeRate where b.Id == 1 select b.GBYTE_USD).FirstOrDefault()), ContractSharedAddress = (from c in _context.Contracts where c.ApplicationId == a.Id select c.SharedAddress).FirstOrDefault(), Status = a.Status, DateOfDonation = a.DateOfDonation.ToString("yyyy-MM-dd"), CreationDate = a.Created.ToString("yyyy-MM-dd HH:mm:ss"), }; return(applications); }
/// <summary> /// Retrieve all products by specified producer /// </summary> /// <param name="producerId"></param> /// <returns></returns> public IQueryable <ProductDTO> Read(int producerId) { var weekAgo = DateTime.UtcNow.Subtract(new TimeSpan(7, 0, 0, 0)); var monthAgo = DateTime.UtcNow.Subtract(new TimeSpan(30, 0, 0, 0)); Func <ApplicationStatusEnum, bool> checkStatus = status => status == ApplicationStatusEnum.Completed || status == ApplicationStatusEnum.Pending;; var entities = from p in _context.Products where p.UserId == producerId orderby p.Rank descending orderby p.Created descending select new ProductDTO { ProductId = p.Id, Title = p.Title, UserId = p.UserId, Price = p.Price, Country = p.Country, Thumbnail = ImageHelper.GetRelativeStaticFolderImagePath(p.Thumbnail), Description = p.Description, Location = p.Location, Available = p.Available, Rank = p.Rank, // Stats DateLastDonation = p.Applications.Count == 0 ? null : p.Applications.Select(a => a.DateOfDonation).DefaultIfEmpty(DateTime.MinValue).Max().Equals(DateTime.MinValue) ? null : p.Applications.Select(a => a.DateOfDonation).DefaultIfEmpty(DateTime.MinValue).Max().ToString("yyyy-MM-dd HH':'mm"), CompletedDonationsPastWeek = (from a in p.Applications where a.Status == ApplicationStatusEnum.Completed && a.LastModified >= weekAgo select new { ApplicationId = a.Id }).Count(), CompletedDonationsPastMonth = (from a in p.Applications where a.Status == ApplicationStatusEnum.Completed && a.LastModified >= monthAgo select new { ApplicationId = a.Id }).Count(), CompletedDonationsAllTime = (from a in p.Applications where a.Status == ApplicationStatusEnum.Completed select new { ApplicationId = a.Id }).Count(), PendingDonationsPastWeek = (from a in p.Applications where (a.Status == ApplicationStatusEnum.Pending) && a.LastModified >= weekAgo select new { ApplicationId = a.Id }).Count(), PendingDonationsPastMonth = (from a in p.Applications where a.Status == ApplicationStatusEnum.Pending && a.LastModified >= monthAgo select new { ApplicationId = a.Id }).Count(), PendingDonationsAllTime = (from a in p.Applications where a.Status == ApplicationStatusEnum.Pending select new { ApplicationId = a.Id }).Count(), // Applications for the product of status x OpenApplications = from a in p.Applications where a.Status == ApplicationStatusEnum.Open select new ApplicationDTO { ApplicationId = a.Id, ReceiverId = a.UserId, ReceiverName = $"{a.User.FirstName} {a.User.SurName}", Country = a.User.Country, Thumbnail = ImageHelper.GetRelativeStaticFolderImagePath(a.User.Thumbnail), ProductId = a.Product.Id, ProductTitle = a.Product.Title, ProductPrice = a.Product.Price, ProducerId = a.Product.UserId, Motivation = a.Motivation, Status = a.Status, }, PendingApplications = from a in p.Applications where a.Status == ApplicationStatusEnum.Pending orderby a.User.SurName select new ApplicationDTO { ApplicationId = a.Id, ReceiverId = a.UserId, ReceiverName = $"{a.User.FirstName} {a.User.SurName}", Country = a.User.Country, Thumbnail = ImageHelper.GetRelativeStaticFolderImagePath(a.User.Thumbnail), ProductId = a.Product.Id, ProductTitle = a.Product.Title, ProductPrice = a.Product.Price, ProducerId = a.Product.UserId, Motivation = a.Motivation, Bytes = checkStatus(a.Status) ? (from c in _context.Contracts where a.Id == c.ApplicationId select c.Bytes ).FirstOrDefault() : 0, BytesInCurrentDollars = checkStatus(a.Status) ? BytesToUSDConverter.BytesToUSD( (from c in _context.Contracts where a.Id == c.ApplicationId select c.Bytes ).FirstOrDefault(), (from b in _context.ByteExchangeRate where b.Id == 1 select b.GBYTE_USD).FirstOrDefault() ) : 0, ContractSharedAddress = checkStatus(a.Status) ? (from c in _context.Contracts where c.ApplicationId == a.Id select c.SharedAddress ).FirstOrDefault() : null, Status = a.Status, }, ClosedApplications = from a in p.Applications where a.Status == ApplicationStatusEnum.Unavailable orderby a.LastModified select new ApplicationDTO { ApplicationId = a.Id, ReceiverId = a.UserId, ReceiverName = $"{a.User.FirstName} {a.User.SurName}", Country = a.User.Country, Thumbnail = ImageHelper.GetRelativeStaticFolderImagePath(a.User.Thumbnail), ProductId = a.Product.Id, ProductTitle = a.Product.Title, ProductPrice = a.Product.Price, ProducerId = a.Product.UserId, Motivation = a.Motivation, Bytes = checkStatus(a.Status) ? (from c in _context.Contracts where a.Id == c.ApplicationId select c.Bytes ).FirstOrDefault() : 0, BytesInCurrentDollars = checkStatus(a.Status) ? BytesToUSDConverter.BytesToUSD( (from c in _context.Contracts where a.Id == c.ApplicationId select c.Bytes ).FirstOrDefault(), (from b in _context.ByteExchangeRate where b.Id == 1 select b.GBYTE_USD).FirstOrDefault() ) : 0, ContractSharedAddress = checkStatus(a.Status) ? (from c in _context.Contracts where c.ApplicationId == a.Id select c.SharedAddress ).FirstOrDefault() : null, Status = a.Status, }, CompletedApplications = from a in p.Applications where a.Status == ApplicationStatusEnum.Completed where a.DateOfDonation >= monthAgo orderby a.User.SurName select new ApplicationDTO { ApplicationId = a.Id, ReceiverId = a.UserId, ReceiverName = $"{a.User.FirstName} {a.User.SurName}", Country = a.User.Country, Thumbnail = ImageHelper.GetRelativeStaticFolderImagePath(a.User.Thumbnail), ProductId = a.Product.Id, ProductTitle = a.Product.Title, ProductPrice = a.Product.Price, ProducerId = a.Product.UserId, Motivation = a.Motivation, Bytes = (from c in _context.Contracts where a.Id == c.ApplicationId select c.Bytes ).FirstOrDefault(), BytesInCurrentDollars = BytesToUSDConverter.BytesToUSD( (from c in _context.Contracts where a.Id == c.ApplicationId select c.Bytes ).FirstOrDefault(), (from b in _context.ByteExchangeRate where b.Id == 1 select b.GBYTE_USD).FirstOrDefault() ), Status = a.Status, }, }; return(entities); }
/// <summary> /// Find a product by id /// </summary> /// <param name="productId"></param> /// <returns name="ProductDTO"></returns> public async Task <ProductDTO> FindAsync(int productId) { var weekAgo = DateTime.UtcNow.Subtract(new TimeSpan(7, 0, 0, 0)); var monthAgo = DateTime.UtcNow.Subtract(new TimeSpan(30, 0, 0, 0)); var here = (from c in _context.Contracts where 479 == c.ApplicationId select c.Bytes ).FirstOrDefault(); var product = await(from p in _context.Products where p.Id == productId select new ProductDTO { ProductId = p.Id, Title = p.Title, UserId = p.UserId, Price = p.Price, Description = p.Description, Country = p.Country, Thumbnail = ImageHelper.GetRelativeStaticFolderImagePath(p.Thumbnail), Location = p.Location, Available = p.Available, Rank = p.Rank, // Stats DateLastDonation = p.Applications.Count == 0 ? null : p.Applications.Select(a => a.DateOfDonation).DefaultIfEmpty(DateTime.MinValue).Max().Equals(DateTime.MinValue) ? null : p.Applications.Select(a => a.DateOfDonation).DefaultIfEmpty(DateTime.MinValue).Max().ToString("yyyy-MM-dd HH':'mm"), CompletedDonationsPastWeek = (from a in p.Applications where a.Status == ApplicationStatusEnum.Completed && a.LastModified >= weekAgo select new { ApplicationId = a.Id }).Count(), CompletedDonationsPastMonth = (from a in p.Applications where a.Status == ApplicationStatusEnum.Completed && a.LastModified >= monthAgo select new { ApplicationId = a.Id }).Count(), CompletedDonationsAllTime = (from a in p.Applications where a.Status == ApplicationStatusEnum.Completed select new { ApplicationId = a.Id }).Count(), PendingDonationsPastWeek = (from a in p.Applications where (a.Status == ApplicationStatusEnum.Pending) && a.LastModified >= weekAgo select new { ApplicationId = a.Id }).Count(), PendingDonationsPastMonth = (from a in p.Applications where a.Status == ApplicationStatusEnum.Pending && a.LastModified >= monthAgo select new { ApplicationId = a.Id }).Count(), PendingDonationsAllTime = (from a in p.Applications where a.Status == ApplicationStatusEnum.Pending select new { ApplicationId = a.Id }).Count(), // Lists of applications of status x OpenApplications = from a in p.Applications where a.Status == ApplicationStatusEnum.Open select new ApplicationDTO { ApplicationId = a.Id, ReceiverId = a.UserId, ReceiverName = $"{a.User.FirstName} {a.User.SurName}", Country = a.User.Country, Thumbnail = ImageHelper.GetRelativeStaticFolderImagePath(a.User.Thumbnail), ProductId = a.Product.Id, ProductTitle = a.Product.Title, ProductPrice = a.Product.Price, ProducerId = a.Product.UserId, Motivation = a.Motivation, Status = a.Status, }, PendingApplications = from a in p.Applications where a.Status == ApplicationStatusEnum.Pending select new ApplicationDTO { ApplicationId = a.Id, ReceiverId = a.UserId, ReceiverName = $"{a.User.FirstName} {a.User.SurName}", Country = a.User.Country, Thumbnail = ImageHelper.GetRelativeStaticFolderImagePath(a.User.Thumbnail), ProductId = a.Product.Id, ProductTitle = a.Product.Title, ProductPrice = a.Product.Price, ProducerId = a.Product.UserId, Motivation = a.Motivation, Bytes = (from c in _context.Contracts where a.Id == c.ApplicationId select c.Bytes ).FirstOrDefault(), BytesInCurrentDollars = BytesToUSDConverter.BytesToUSD( (from c in _context.Contracts where a.Id == c.ApplicationId select c.Bytes ).FirstOrDefault(), (from b in _context.ByteExchangeRate where b.Id == 1 select b.GBYTE_USD).FirstOrDefault() ), ContractSharedAddress = (from c in _context.Contracts where c.ApplicationId == a.Id select c.SharedAddress ).FirstOrDefault(), Status = a.Status, }, ClosedApplications = from a in p.Applications where a.Status == ApplicationStatusEnum.Unavailable select new ApplicationDTO { ApplicationId = a.Id, ReceiverId = a.UserId, ReceiverName = $"{a.User.FirstName} {a.User.SurName}", Country = a.User.Country, Thumbnail = ImageHelper.GetRelativeStaticFolderImagePath(a.User.Thumbnail), ProductId = a.Product.Id, ProductTitle = a.Product.Title, ProductPrice = a.Product.Price, ProducerId = a.Product.UserId, Motivation = a.Motivation, Bytes = (from c in _context.Contracts where a.Id == c.ApplicationId select c.Bytes ).FirstOrDefault(), BytesInCurrentDollars = BytesToUSDConverter.BytesToUSD( (from c in _context.Contracts where a.Id == c.ApplicationId select c.Bytes ).FirstOrDefault(), (from b in _context.ByteExchangeRate where b.Id == 1 select b.GBYTE_USD).FirstOrDefault() ), ContractSharedAddress = (from c in _context.Contracts where c.ApplicationId == a.Id select c.SharedAddress ).FirstOrDefault(), Status = a.Status, }, }).SingleOrDefaultAsync(); if (product == null) { return(null); } return(product); }