예제 #1
0
        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)));
        }
예제 #2
0
        public async Task <ActionResult> SetAvatarByAccountId([FromRoute] long accountId,
                                                              [FromBody] SetAvatarInputModel input, CancellationToken cancellationToken)
        {
            if (AccountId != accountId)
            {
                return(Forbidden());
            }

            var account = await _accountManager.GetAsync(accountId, cancellationToken);

            var           selectedAvatarItemIds = input.AvatarItemIds;
            List <Avatar> avatars = new List <Avatar>();

            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.Id, cancellationToken);

                    if (shopItem != null)
                    {
                        avatars.Add(selectedAvatarItemId);

                        imagesToCombine.Add(_fileService.GetFile(shopItem.ImageFileId));

                        if (!accountItems.Any(q => q.ShopItemId == selectedAvatarItemId.Id))
                        {
                            if (shopItem.DefaultAvatar == true)
                            {
                                // add item to user's items
                                var newAccountItem = new AccountItem
                                {
                                    AccountId  = account.Id,
                                    ShopItemId = selectedAvatarItemId.Id,
                                    ItemTypeId = shopItem.TypeId,
                                    Quantity   = 1
                                };
                                await _accountItemManager.SaveAsync(newAccountItem, cancellationToken);
                            }
                            else
                            {
                                return(BadRequest("invalid_itemId", "AvatarItem not in AccountItems"));
                            }
                        }
                    }
                }

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

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

                    account.AvatarImageId = file.FileId;
                }
            }

            account.AvatarItemIds = JsonConvert.SerializeObject(selectedAvatarItemIds);
            await _accountManager.SaveAsync(account, cancellationToken);

            return(Ok());
        }