public async Task AddMusicTrackAsync(MusicTrack musicTrack) { if (musicTrack?.MusicVersions == null) { throw new ArgumentNullException(nameof(musicTrack)); } using (var dataBase = new NameThatTuneDatabase()) { if (await dataBase.MusicTrack.AnyAsync(o => o.Id == musicTrack.Id)) { throw new Exception($"Track {musicTrack.NameArtist} - {musicTrack.NameTrack} already exists"); } ; var musicVersions = musicTrack.MusicVersions.ToArray(); musicTrack.MusicVersions = null; await dataBase.MusicTrack.AddAsync(musicTrack); await dataBase.SaveChangesAsync(); await dataBase.MusicVersions.AddRangeAsync(musicVersions); await dataBase.SaveChangesAsync(); Console.Out.WriteLine("DB: Track {0} added", musicTrack.NameArtist + "-" + musicTrack.NameTrack); } }
public static async Task UpdateUserStatistics(User user, bool answerType) { using (var dataBase = new NameThatTuneDatabase()) { if (!await dataBase.Users.AnyAsync(o => o.ChatId == user.ChatId && o.MessengerClass == user.MessengerClass)) { throw new Exception($"User {user.ChatId} - {user.MessengerClass} does not exists"); } var saveUser = await dataBase.Users.FirstOrDefaultAsync(u => u.ChatId == user.ChatId && u.MessengerClass == user.MessengerClass); var userStat = await dataBase.UserStatistics.FirstOrDefaultAsync(us => us.Id == saveUser.Id); if (answerType) { userStat.CorrectAnswers++; } else { userStat.WrongAnswers++; } await dataBase.SaveChangesAsync(); } }
public static async Task AddUserStatistics(User user) { using (var dataBase = new NameThatTuneDatabase()) { if (await dataBase.Users.AnyAsync(o => o.ChatId == user.ChatId && o.MessengerClass == user.MessengerClass)) { throw new Exception($"User {user.ChatId} - {user.MessengerClass} already exists"); } ; var newUser = user; await dataBase.Users.AddAsync(newUser); await dataBase.SaveChangesAsync(); var userStat = new UserStatistics { UserId = newUser.Id, Id = newUser.Id }; await dataBase.UserStatistics.AddAsync(userStat); await dataBase.SaveChangesAsync(); } }