public async Task Should_UpdateExistUserSetting_When_PassValidParameters() { // Arrange var entity = new SettingsDb() { SettingKey = "key_1", DefaultValue = "default_value_1", SettingType = Domain.Enums.SettingType.User }; entity.UserSettings.Add(new UserSettingsDb() { UserId = 1, SettingValue = "user1_key1_value" }); entity.UserSettings.Add(new UserSettingsDb() { UserId = 2, SettingValue = "user2_key1_value" }); _fixture.Context.Settings.Attach(entity); entity = new SettingsDb() { SettingKey = "key_2", DefaultValue = "default_value_2", SettingType = Domain.Enums.SettingType.User }; entity.UserSettings.Add(new UserSettingsDb() { UserId = 1, SettingValue = "user1_key2_value" }); _fixture.Context.Settings.Attach(entity); _fixture.Context.SaveChanges(); IRepository <SettingsDb> settingRepository = new Repository <SettingsDb>(_fixture.Context); IRepository <UserSettingsDb> repository = new Repository <UserSettingsDb>(_fixture.Context); IUnitOfWork unitOfWork = new UnitOfWork(_fixture.Context); var handler = new UpdateUserSettingHandler(unitOfWork, repository, settingRepository, _userLogger, _mapper); var dtos = new List <SettingsDto>(); dtos.Add(new SettingsDto() { Key = "key_1", Value = "user1_key1_updated_value" }); var request = new UpdateUserSettingCommand(userId: 1, dtos); // Act var tcs = new CancellationToken(); var result = await handler.Handle(request, tcs); // Assert Assert.Equal("user1_key1_updated_value", result.SingleOrDefault(x => x.Key == "key_1").Value); Assert.Equal("user1_key2_value", result.SingleOrDefault(x => x.Key == "key_2").Value); }
public async Task <IEnumerable <SettingsDto> > Handle(UpdateUserSettingCommand request, CancellationToken cancellationToken) { var settings = await _settingRepository.Queryable() .Where(x => x.SettingType == Domain.Enums.SettingType.User) .ToListAsync(); var userSettings = await _repository.Queryable() .Where(x => x.UserId == request.UserId) .ToListAsync(); Dictionary <string, SettingsDto> result = settings.Select(x => new SettingsDto() { Key = x.SettingKey, SettingId = x.SettingId, Value = userSettings.Any(cs => cs.SettingId == x.SettingId) ? userSettings.SingleOrDefault(cs => cs.SettingId == x.SettingId).SettingValue : x.DefaultValue }).ToDictionary(x => x.Key, x => x); foreach (var setting in request.SettingsDtos) { var existSetting = settings.SingleOrDefault(x => x.SettingKey == setting.Key); if (existSetting == null) { throw new VacationTrackingException(ExceptionMessages.ItemNotFound, $"Setting not found by key: {setting.Key}", 400); } var companySetting = userSettings.SingleOrDefault(x => x.SettingId == existSetting.SettingId); if (companySetting == null) { _repository.Insert(new UserSettingDb() { UserId = request.UserId, SettingId = existSetting.SettingId, SettingValue = setting.Value }); } else { companySetting.SettingValue = setting.Value; _repository.Update(companySetting); } result[setting.Key].Value = setting.Value; } var affectedRow = await _unitOfWork.SaveChangesAsync(); return(result.Values.AsEnumerable()); }