public async Task <ActionResult> SetAvatar([FromForm] SetAvatar command)
        {
            command.UserId            = User.GetUserId();
            command.ImageBaseHttpPath = string.Format("{0}://{1}{2}/UserAvatars", Request.Scheme, Request.Host, Request.PathBase);
            command.ImagesFolderPath  = Path.Combine(_hostEnvironment.ContentRootPath, "UserAvatars");

            await _userService.SetAvatar(command);

            return(Ok());
        }
        public async Task SetAvatar(SetAvatar command)
        {
            var user = await _userRepository.GetAsync(command.UserId);

            if (!string.IsNullOrWhiteSpace(user.ImageHttpPath))
            {
                throw new AppException("User already has an avatar. If you want to update it please use dedicated updating functionality.", AppErrorCode.ALREADY_EXISTS);
            }
            var imageName =
                user.Email + "_" +
                DateTime.Now.ToString("dd_mm_yy_HH_MM_ss") +
                Path.GetExtension(command.ImageFile.FileName);

            await SaveImage(command.ImageFile, command.ImagesFolderPath, imageName);

            user.SetImage(command.ImageBaseHttpPath + "/" + imageName);
            await _userRepository.SaveChangesAsync();
        }
Exemple #3
0
        public async Task <IActionResult> Post([FromBody] SetAvatar command)
        {
            await _userService.SetAvatar(UserId, command.AvatarUrl);

            return(NoContent());
        }