private async Task <bool> UploadAssetIconAsync(int assetId, string url) { if (string.IsNullOrEmpty(url)) { return(false); } if (!url.ToLower().StartsWith("http")) { return(false); } if (url.ToLower().Contains("/large/")) { url = url.Replace("/large/", "/small/"); } var result = await AzureStorageBusiness.UploadAssetFromUrlAsync($"{assetId}.png", url); if (!result && url.Contains("/small/")) { url = url.Replace("/small/", "/large/"); result = await AzureStorageBusiness.UploadAssetFromUrlAsync($"{assetId}.png", url); } return(result); }
public async Task <Guid> EditAdvisorAsync(int id, string name, string description, bool changePicture, Stream pictureStream, string pictureExtension) { if (string.IsNullOrWhiteSpace(name)) { throw new BusinessException("Name must be filled."); } if (name.Length > 50) { throw new BusinessException("Name cannot have more than 50 characters."); } if (!string.IsNullOrEmpty(description) && description.Length > 160) { throw new BusinessException("Description cannot have more than 160 characters."); } byte[] picture = null; if (changePicture && pictureStream != null) { picture = GetPictureBytes(pictureStream, pictureExtension); } var advisor = Data.GetAdvisor(id); if (advisor == null || !advisor.Enabled) { throw new NotFoundException("Trader not found"); } if (advisor.Email.ToLower() != LoggedEmail.ToLower()) { throw new UnauthorizedException("Invalid credentials"); } var previousData = $"(Previous) Name: {advisor.Name} - Change Picture: {changePicture} - Url Guid: {advisor.UrlGuid} - Description: {advisor.Description}"; if (changePicture) { var previousGuid = advisor.UrlGuid; advisor.UrlGuid = Guid.NewGuid(); if (await AzureStorageBusiness.UploadUserPictureFromBytesAsync($"{advisor.UrlGuid}.png", picture ?? GetNoUploadedImageForAdvisor(advisor))) { await AzureStorageBusiness.DeleteUserPicture($"{previousGuid}.png"); } } advisor.Name = name; advisor.Description = description; Update(advisor); ActionBusiness.InsertEditAdvisor(advisor.Id, previousData); var cachedAdvisor = AdvisorRankingBusiness.ListAdvisorsFullData().FirstOrDefault(c => c.Id == advisor.Id); if (cachedAdvisor != null) { cachedAdvisor.Name = advisor.Name; cachedAdvisor.Description = advisor.Description; cachedAdvisor.UrlGuid = advisor.UrlGuid; } return(advisor.UrlGuid); }
public async Task <LoginResponse> CreateAsync(string email, string password, string name, string description, string referralCode, bool changePicture, Stream pictureStream, string pictureExtension) { if (string.IsNullOrWhiteSpace(name)) { throw new BusinessException("Name must be filled."); } if (name.Length > 50) { throw new BusinessException("Name cannot have more than 50 characters."); } if (!string.IsNullOrWhiteSpace(description) && description.Length > 160) { throw new BusinessException("Short description cannot have more than 160 characters."); } byte[] picture = null; if (changePicture && pictureStream != null) { picture = AdvisorBusiness.GetPictureBytes(pictureStream, pictureExtension); } User user = null; var updateUser = false; if (LoggedEmail != null) { if (!string.IsNullOrWhiteSpace(email) && email != LoggedEmail) { throw new BusinessException("Invalid email."); } if (!string.IsNullOrWhiteSpace(password)) { throw new BusinessException("Invalid password."); } user = UserBusiness.GetForLoginByEmail(LoggedEmail); if (UserBusiness.IsValidAdvisor(user)) { throw new BusinessException("User already registered."); } if (!user.ReferredId.HasValue) { var referredUser = UserBusiness.GetReferredUser(referralCode); if (referredUser != null) { updateUser = true; user.ReferralStatus = ReferralStatusType.InProgress.Value; user.ReferredId = referredUser.Id; user.BonusToReferred = UserBusiness.GetBonusToReferredUser(referredUser); } } else if (!string.IsNullOrEmpty(referralCode)) { throw new BusinessException("User already has a referral code defined."); } } else { user = UserBusiness.GetValidUserToRegister(email, password, referralCode); } var advisorsCount = AdvisorRankingBusiness.ListAdvisorsFullData().Count; Guid urlGuid = Guid.NewGuid(); var creationDate = Data.GetDateTimeNow(); using (var transaction = TransactionalDapperCommand) { if (LoggedEmail == null) { transaction.Insert(user); } else if (updateUser) { transaction.Update(user); } var advisor = new DomainObjects.Advisor.Advisor() { Id = user.Id, Name = name, Description = description, BecameAdvisorDate = creationDate, Enabled = true, UrlGuid = urlGuid }; transaction.Insert(advisor); var virtualDolar = new Order() { AssetId = AssetUSDId, CreationDate = creationDate, Price = 1, Quantity = VirtualMoney, RemainingQuantity = VirtualMoney, Status = OrderStatusType.Executed.Value, StatusDate = creationDate, Type = OrderType.Buy.Value, UserId = user.Id, ActionType = OrderActionType.Automated.Value, Fee = 0 }; transaction.Insert(virtualDolar); var rating = 2.5; var ranking = advisorsCount + 1; transaction.Insert(new AdvisorRanking() { Id = user.Id, UpdateDate = creationDate, Rating = rating, Ranking = ranking }); transaction.Insert(new AdvisorRankingHistory() { UserId = user.Id, ReferenceDate = creationDate, Rating = rating, Ranking = ranking }); var baseProfit = AdvisorProfitBusiness.GetBaseUsdAdvisorProfit(user.Id, creationDate); transaction.Insert(baseProfit); transaction.Insert(new AdvisorProfitHistory() { AssetId = baseProfit.AssetId, OrderCount = baseProfit.OrderCount, ReferenceDate = baseProfit.UpdateDate, Status = baseProfit.Status, SuccessCount = baseProfit.SuccessCount, SummedProfitDollar = baseProfit.SummedProfitDollar, SummedProfitPercentage = baseProfit.SummedProfitPercentage, SummedTradeMinutes = baseProfit.SummedTradeMinutes, TotalDollar = baseProfit.TotalDollar, TotalQuantity = baseProfit.TotalQuantity, Type = baseProfit.Type, UserId = baseProfit.UserId, TotalFee = baseProfit.TotalFee }); transaction.Commit(); } await AzureStorageBusiness.UploadUserPictureFromBytesAsync($"{urlGuid}.png", picture ?? AdvisorBusiness.GetNoUploadedImageForAdvisor(user)); if (LoggedEmail == null || !user.ConfirmationDate.HasValue) { await UserBusiness.SendEmailConfirmationAsync(user.Email, user.ConfirmationCode); } UserBusiness.ClearUserCache(user.Email); AdvisorRankingBusiness.AppendNewAdvisorToCache(user.Id); return(new LoginResponse() { Id = user.Id, Email = user.Email, PendingConfirmation = !user.ConfirmationDate.HasValue, AdvisorName = name, ProfileUrlGuid = urlGuid.ToString(), HasInvestment = false, IsAdvisor = true, RequestedToBeAdvisor = false }); }
public async Task UpdateAssetEventsAsync() { var startDate = Data.GetDateTimeNow().AddDays(-1).Date; List <DomainObjects.Asset.Asset> assets = null; List <AssetEventCategory> categories = null; List <AssetEvent> assetEvents = null; List <Record> events = null; Parallel.Invoke(() => assets = AssetBusiness.ListAssets(true), () => categories = AssetEventCategoryBusiness.ListCategories(), () => assetEvents = Data.ListAssetEventsWithPagination(startDate, null, null, null, null, null), () => events = CoinMarketCalBusiness.ListEvents(startDate)); events = events.OrderBy(c => c.FormattedCreatedDate).ThenBy(c => c.FormattedEventDate).ToList(); foreach (var e in events) { bool updateProofImage = false; bool isNewEvent = false; var linkCategoryToDelete = new List <LinkEventCategory>(); var linkCategoryToInsert = new List <LinkEventCategory>(); var linkAssetToDelete = new List <LinkEventAsset>(); var linkAssetToInsert = new List <LinkEventAsset>(); var coinsId = e.Coins.Select(c => c.Id).Distinct().ToHashSet(); var eventAssets = assets.Where(c => coinsId.Contains(c.CoinMarketCalId)).ToList(); var categoriesId = e.Categories.Select(c => c.Id).Distinct().ToHashSet(); var eventCategories = categories.Where(c => categoriesId.Contains(c.Id)).ToList(); if (eventAssets.Any()) { var workingEvent = assetEvents.FirstOrDefault(c => c.ExternalId == e.Id.ToString()); if (workingEvent == null) { isNewEvent = true; updateProofImage = true; workingEvent = new AssetEvent() { ExternalId = e.Id.ToString(), CreationDate = Data.GetDateTimeNow() }; linkCategoryToInsert = eventCategories.Select(c => new LinkEventCategory() { AssetEventCategoryId = c.Id }).ToList(); linkAssetToInsert = eventAssets.Select(c => new LinkEventAsset() { AssetId = c.Id }).ToList(); } else { linkCategoryToDelete = workingEvent.LinkEventCategory.Where(c => !eventCategories.Any(a => a.Id == c.AssetEventCategoryId)).ToList(); linkAssetToDelete = workingEvent.LinkEventAsset.Where(c => !eventAssets.Any(a => a.Id == c.AssetId)).ToList(); linkCategoryToInsert = eventCategories.Where(c => !workingEvent.LinkEventCategory.Any(a => a.AssetEventCategoryId == c.Id)).Select(c => new LinkEventCategory() { AssetEventCategoryId = c.Id, AssetEventId = workingEvent.Id }).ToList(); linkAssetToInsert = eventAssets.Where(c => !workingEvent.LinkEventAsset.Any(a => a.AssetId == c.Id)).Select(c => new LinkEventAsset() { AssetId = c.Id, AssetEventId = workingEvent.Id }).ToList(); if (workingEvent.Title == e.Title && workingEvent.Description == e.Description && workingEvent.EventDate == e.FormattedEventDate && workingEvent.ExternalCreationDate == e.FormattedCreatedDate && workingEvent.CanOccurBefore == e.CanOccurBefore && workingEvent.ReliablePercentage == e.Percentage && workingEvent.Source == e.Source && workingEvent.Proof == e.Proof && linkCategoryToDelete.Count == 0 && linkAssetToDelete.Count == 0 && linkCategoryToInsert.Count == 0 && linkAssetToInsert.Count == 0) { continue; } updateProofImage = workingEvent.Proof != e.Proof; } UpdateAssetEventData(workingEvent, e); try { using (var transaction = TransactionalDapperCommand) { if (isNewEvent) { transaction.Insert(workingEvent); } else { transaction.Update(workingEvent); } foreach (var linkCategory in linkCategoryToDelete) { transaction.Delete(linkCategory); } foreach (var linkCategory in linkCategoryToInsert) { linkCategory.AssetEventId = workingEvent.Id; transaction.Insert(linkCategory); } foreach (var linkAsset in linkAssetToDelete) { transaction.Delete(linkAsset); } foreach (var linkAsset in linkAssetToInsert) { linkAsset.AssetEventId = workingEvent.Id; transaction.Insert(linkAsset); } transaction.Commit(); } if (updateProofImage) { var fileName = $"{workingEvent.Id}.png"; var contentType = workingEvent.Proof.ToLower().EndsWith("png") ? "image/png" : workingEvent.Proof.ToLower().EndsWith("pdf") ? "application/pdf" : "image/jpeg"; byte[] file; using (var client = new WebClient()) { file = client.DownloadData(workingEvent.Proof); } if (!await AzureStorageBusiness.UploadAssetEventFromByteAsync(fileName, file, contentType)) { throw new BusinessException($"Error on upload asset event proof image. (Id {workingEvent.Id} - Url {workingEvent.Proof}"); } } } catch (Exception ex) { var telemetry = new TelemetryClient(); telemetry.TrackEvent($"AssetEventBusiness.UpdateAssetEvents.Id.{e?.Id}"); telemetry.TrackException(ex); } } } }