Пример #1
0
        public void UpdatePreferences(Guid userId, PreferencesDto preferencesDto)
        {
            var preferences = this.PreferencesRepository.GetAll().FirstOrDefault(p => p.UserId == userId);

            var exists = true;

            if (preferences == null)
            {
                exists      = false;
                preferences = new PreferencesEntity
                {
                    UserId = userId
                };
            }

            preferences.MinTemperature     = preferencesDto.MinTemperature;
            preferences.MaxTemperature     = preferencesDto.MaxTemperature;
            preferences.TransportationType = preferencesDto.TransportationType;
            preferences.RainyTrip          = preferencesDto.RainyTrip;
            preferences.SnowyTrip          = preferencesDto.SnowyTrip;

            if (exists)
            {
                this.PreferencesRepository.Update(preferences);
            }
            else
            {
                this.PreferencesRepository.Insert(preferences);
            }
        }
        public async Task <bool> SetUserPreferencesAsync(PreferencesEntity preferences)
        {
            if (preferences == null)
            {
                throw new ArgumentNullException($"{nameof(preferences)}");
            }

            using (MySqlConnection conn = new MySqlConnection(this.connectionStringFactory.GetConnectionString()))
            {
                var changed = await conn.ExecuteAsync(@"
					INSERT INTO preferences (UserID, SoonDays, ImminentDays)
						VALUES (@UserID, @SoonDays, @ImminentDays)
					ON DUPLICATE KEY UPDATE
						SoonDays=VALUES(SoonDays),
						ImminentDays=VALUES(ImminentDays);"                        ,
                                                      new
                {
                    preferences.UserID,
                    preferences.SoonDays,
                    preferences.ImminentDays
                });

                return(changed > 0);
            }
        }