/// <summary> /// save the battle result to the db /// </summary> /// <param name="userHeroId"></param> /// <param name="opponentHeroId"></param> /// <param name="winnerId"></param> public void SaveDuelBattelog(int userHeroId, int opponentHeroId, int?winnerId, string userId) { var newLog = new BattleLog(); try { //mapping newLog.UserHeroId = userHeroId; newLog.OpponentHeroId = opponentHeroId; newLog.WinnerHeroId = winnerId; newLog.UserId = userId; //insert record to the db using (SuperHeroDBEntities db = new SuperHeroDBEntities()) { using (var transaction = db.Database.BeginTransaction()) { db.BattleLog.Add(newLog); db.SaveChanges(); transaction.Commit(); } } } catch (Exception) { throw; } }
public void SaveHeroToDb(FavouriteSuperHero newFavouriteHero, string userId) { using (SuperHeroDBEntities db = new SuperHeroDBEntities()) { var user = AspNetUsersDb.GetUserIncludeFavouriteHeroesByIdAndDb(userId, db); using (DbContextTransaction tran = db.Database.BeginTransaction()) { newFavouriteHero.AspNetUsers.Add(user); db.FavouriteSuperHero.Add(newFavouriteHero); db.SaveChanges(); tran.Commit(); } } }
public void RemoveHeroFromUserFavouriteList(string userId, int heroId) { using (SuperHeroDBEntities db = new SuperHeroDBEntities()) { var user = GetUserIncludeFavouriteHeroesByIdAndDb(userId, db); var userFavouriteHero = FavouriteSuperHeroDb.GetFavouriteHeroById(heroId, db); using (DbContextTransaction tran = db.Database.BeginTransaction()) { user.FavouriteSuperHero.Remove(userFavouriteHero); db.Entry(user).State = EntityState.Modified; db.SaveChanges(); tran.Commit(); } } }
public void SaveHeroToUserFavHeroList(int apiId, string userId) { using (SuperHeroDBEntities db = new SuperHeroDBEntities()) { var user = GetUserIncludeFavouriteHeroesByIdAndDb(userId, db); var heroToSave = db.FavouriteSuperHero.Where(h => h.ApiId == apiId).FirstOrDefault(); using (DbContextTransaction tran = db.Database.BeginTransaction()) { user.FavouriteSuperHero.Add(heroToSave); db.Entry(user).State = EntityState.Modified; db.SaveChanges(); tran.Commit(); } } }