public void Create() { var userGameRepository = new UserGameRepository(Database.Context.Instance); var game = new Game() { Id = 1, Name = "Game" }; var user = new User() { Id = 0, FirstName = "Daniel", LastName = "Cardoza", }; var userGame = new UserGame() { Id = 0, UserId = user.Id, User = user, GameId = game.Id, Game = game, Score = 1000 }; userGameRepository.Create(userGame); }
public UserDTO SubscribeUserToGame(UserGameDTO userGameDTO) { Logger.Trace("UserBusiness", "SubscribeUserToGame", "Starting the method"); UserDTO result = null; try { var unitOfWork = Database.Context.Instance; var userRepository = new UserRepository(unitOfWork); var userGameRepository = new UserGameRepository(unitOfWork); var user = userRepository.GetById(userGameDTO.UserId); if (user != null) { user.Games = user.Games ?? new List <UserGame>(); //This will be always game 1 because I'm not implementing several games, but the code is //prepared to support it with minimal changes. var game = new Game() { Id = userGameDTO.GameId, Name = "Game" }; var userGame = new UserGame() { UserId = user.Id, User = user, GameId = 1, Game = game, Score = 0 }; user.Games.Add(userGame); userGameRepository.Create(userGame); user = userRepository.Update(user); result = UserToUserDTO(user); } } catch (Exception e) { Logger.Error("UserBusiness", "SubscribeUserToGame", e.Message); throw e; } finally { Logger.Trace("UserBusiness", "SubscribeUserToGame", "Finishing method"); } return(result); }