public async Task <ActionResult <ApiResultViewModel <AccountViewModel> > > UpdateAccount(string id, [FromBody] AccountInputModel inputModel, CancellationToken cancellationToken) { var account = await _accountManager.GetAsync(id.ToInt(), cancellationToken); if (account is null) { return(NotFound()); } if (account.Email != inputModel.Email && await _accountManager.FindByEmailAsync(inputModel.Email, cancellationToken) != null) { return(BadRequest("duplicate_email", "Account with entered email already exists.")); } account.Email = inputModel.Email; if (!string.IsNullOrWhiteSpace(inputModel.Password)) { account.PasswordHash = PasswordHash.CreateHash(inputModel.Password); } account.PhoneNumber = inputModel.PhoneNumber; account.FirstName = inputModel.FirstName; account.LastName = inputModel.LastName; account.Nickname = inputModel.Nickname; account.GenderId = inputModel.GenderTypeId.ToInt(); account.BirthDate = inputModel.BirthDate; account.StatusId = inputModel.StatusId.ToInt(); account.StatusNote = inputModel.StatusNote; account.IsEmailVerified = inputModel.IsEmailVerified; account.IsPhoneNumberVerified = inputModel.IsPhoneNumberVerified; account.Timezone = "Asia/Tehran"; account.CoverImageId = inputModel.CoverImageId; using (var transaction = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled)) { var selectedAvatarItemIds = inputModel.AvatarItemIds.Select(q => q.ToLong()).ToArray(); if (selectedAvatarItemIds.Any()) { var accountItems = await _accountItemManager.GetByAccountIdAsync(account.Id, cancellationToken); var imagesToCombine = new List <Stream>(); foreach (var selectedAvatarItemId in selectedAvatarItemIds) { var shopItem = await _shopItemManager.GetAsync(selectedAvatarItemId, cancellationToken); imagesToCombine.Add(_fileService.GetFile(shopItem.ImageFileId)); if (!accountItems.Any(q => q.ShopItemId == selectedAvatarItemId)) { // add item to user's items var newAccountItem = new AccountItem { AccountId = account.Id, ShopItemId = selectedAvatarItemId, ItemTypeId = shopItem.TypeId, Quantity = 1 }; newAccountItem = await _accountItemManager.SaveAsync(newAccountItem, cancellationToken); } } using (var avatarImg = _imageProcessingService.Combine(imagesToCombine)) { var fileId = await _fileService.SaveFileAsync(avatarImg, cancellationToken); var file = await _fileManager.SaveAsync(new UploadedFile { FileId = fileId, CreatorAccountId = AccountId, Extension = "png", MimeType = "image/png", MimeTypeCategoryId = UploadedFileMimeTypeCategoryIds.Image, Name = "avatar", Size = avatarImg.Length, TypeId = UploadedFileTypeIds.AccountAvatarImage }, cancellationToken); account.AvatarImageId = file.FileId; } } account.AvatarItemIds = JsonConvert.SerializeObject(selectedAvatarItemIds); account.RoleIds = JsonConvert.SerializeObject(inputModel.RoleIds?.Select(q => q.ToLong()) ?? new long[0]); account = await _accountManager.SaveAsync(account, cancellationToken); await _accountManager.UpdateRolesAsync(account, inputModel.RoleIds.Select(rid => rid.ToLong()), cancellationToken); transaction.Complete(); } return(OkData(AccountViewModel.Map(account))); }
private async Task SetDefaultAvatar(Account account, CancellationToken cancellationToken) { var avatarTypeId = account.GenderId == GenderIds.Male ? AvatarTypeIds.Male : AvatarTypeIds.Female; var defaultAvatarItems = await _dataContext.ShopItems.Where(q => q.IsArchived == false && q.TypeId == ShopItemTypeIds.AvatarItem && q.DefaultAvatar == true && q.AvatarTypeId == avatarTypeId) .GroupBy(q => q.AvatarCategoryId).Select(q => q.First()) .OrderBy(q => q.AvatarCategoryId) .ToListAsync(cancellationToken); if (defaultAvatarItems.Any()) { var imagesToCombine = new List <Stream>(); foreach (var item in defaultAvatarItems) { imagesToCombine.Add(_fileService.GetFile(item.ImageFileId)); _dataContext.AccountItems.Add(new AccountItem { AccountId = account.Id, ShopItemId = item.Id, ItemTypeId = item.TypeId, Quantity = 1, }); } using (var avatarImg = _imageProcessingService.Combine(imagesToCombine)) { var fileId = await _fileService.SaveFileAsync(avatarImg, cancellationToken); var file = await _fileManager.SaveAsync(new UploadedFile { FileId = fileId, CreatorAccountId = AccountId, Extension = "png", MimeType = "image/png", MimeTypeCategoryId = UploadedFileMimeTypeCategoryIds.Image, Name = "avatar", Size = avatarImg.Length, TypeId = UploadedFileTypeIds.AccountAvatarImage }, cancellationToken); account.AvatarImageId = file.FileId; var uploadedImage = Image.FromStream(avatarImg); var img = ImageResize.Crop(uploadedImage, 300, 250, TargetSpot.TopMiddle); var filePath = Path.Combine(_options.BaseStoragePath, $"{fileId}-h.png"); img.SaveAs($"{filePath}"); } List <Avatar> avatars = new List <Avatar>(); foreach (var item in defaultAvatarItems) { avatars.Add(new Avatar { Id = item.Id, AvatarCategoryId = item.AvatarCategoryId.Value }); } avatars.Add(new Avatar { Id = 0, AvatarCategoryId = 500 }); avatars.Add(new Avatar { Id = 0, AvatarCategoryId = 800 }); avatars.Add(new Avatar { Id = 0, AvatarCategoryId = 900 }); avatars.Add(new Avatar { Id = 0, AvatarCategoryId = 1200 }); avatars.Add(new Avatar { Id = 0, AvatarCategoryId = 1300 }); avatars.Add(new Avatar { Id = 0, AvatarCategoryId = 1400 }); account.AvatarItemIds = JsonConvert.SerializeObject(avatars); await _accountManager.SaveAsync(account, cancellationToken); } }