Esempio n. 1
0
        public override async Task<TResponse> UpdateAsync<TResponse>(Guid id, IRequest request, Guid userId)
        {
            if (request == null) throw new ArgumentNullException(nameof(request));
            if (!(request is AdUpdateRequest adRequest)) throw new InvalidCastException(nameof(adRequest));
            if (!IsAuthorized(id, userId)) throw new UnauthorizedAccessException();

            var ad = await Entries.SingleAsync(x => x.Id == id);

            _imageHelper.DeleteImage(ad.ImageName);
            ad = Mapper.Map(adRequest, ad);
            ad.ImageName = await _imageHelper.UploadImageAndGetNameAsync(adRequest.Image);

            await Context.SaveChangesAsync();

            return MapToResponse<TResponse>(ad);
        }
Esempio n. 2
0
        public async Task <IActionResult> OnPostAsync()
        {
            var user = await _userManager.GetUserAsync(User);

            if (user == null)
            {
                return(NotFound($"Unable to load user with ID '{_userManager.GetUserId(User)}'."));
            }

            if (IsOperationChange)
            {
                await _photoSNRepository.ChangeCurrentAvatarAsync(new AvatarDto
                {
                    UserId  = user.Id,
                    ImageId = ImageId
                });

                StatusMessage = "Your profile picture has been updated.";
            }
            else
            {
                await _photoSNRepository.DeleteAvatarAsync(new AvatarDto
                {
                    UserId  = user.Id,
                    ImageId = ImageId
                });

                var imageToDelete = await _photoSNRepository.GetImageAsync(ImageId);

                await _photoSNRepository.DeleteImageAsync(ImageId);

                _imageHelper.DeleteImage(imageToDelete.Guid);

                StatusMessage = "Your profile picture has been deleted.";
            }

            await _signInManager.RefreshSignInAsync(user);

            return(RedirectToPage());
        }