예제 #1
0
        public async Task Delete(Guid userId, Guid storyId)
        {
            var storyDB = await _storiesRepository.GetById(storyId);

            if (storyDB == null)
            {
                throw new ArgumentException("Story not found");
            }
            if (storyDB.UserId != userId)
            {
                throw new ArgumentException("Story doesn't belong to user");
            }

            await Task.WhenAll(_amazonS3Repository.Delete(storyId.ToString()),
                               _storiesRepository.Delete(storyId),
                               _feedService.DeleteStoryFromFeed(userId, storyDB));
        }
예제 #2
0
        public async Task Update(UserView userView, IFormFile avatar = null)
        {
            var user   = _mapper.Map <User>(userView);
            var userDB = await _usersRepository.GetUserById(user.Id);

            if (userDB == null)
            {
                throw new ArgumentException("User not found");
            }

            if (userDB.Username != user.Username)
            {
                if (await _usersRepository.GetUserByName(user.Username) != null)
                {
                    throw new ArgumentException("Username \"" + user.Username + "\" is already taken");
                }
            }
            if (userDB.Email != user.Email)
            {
                if (await _usersRepository.GetUserByEmail(user.Email) != null)
                {
                    throw new ArgumentException("Email \"" + user.Email + "\" is already taken");
                }
            }

            if (!string.IsNullOrWhiteSpace(userView.FirstName))
            {
                userDB.FirstName = userView.FirstName;
            }
            if (!string.IsNullOrWhiteSpace(userView.LastName))
            {
                userDB.LastName = userView.LastName;
            }
            if (!string.IsNullOrWhiteSpace(userView.Username))
            {
                userDB.Username = userView.Username;
            }
            userDB.Description = userView.Description;
            if (!string.IsNullOrWhiteSpace(userView.Email))
            {
                userDB.Email = userView.Email;
            }

            if (!string.IsNullOrWhiteSpace(userView.Password))
            {
                _cryptographyService.CreatePasswordHash(userView.Password, out var passwordHash, out var passwordSalt);
                userDB.PasswordHash = passwordHash;
                userDB.PasswordSalt = passwordSalt;
            }

            if (avatar != null)
            {
                if (!string.IsNullOrWhiteSpace(userDB.AvatarPath))
                {
                    await _amazonS3Repository.Delete(userView.Id);
                }
                await _amazonS3Repository.Upload(avatar, userView.Id);

                userDB.AvatarPath = $"{_s3Settings.ServiceURL}/{_s3Settings.BucketName}/{userView.Id}";
            }

            await _usersRepository.UpdateUser(userDB);
        }