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 }); }