Esempio n. 1
0
 public UserCreationResultModel CreateUser([FromBody]UserCreationModel model)
 {
     if (!ModelState.IsValid)
     {
         return new UserCreationResultModel(false, ModelState);
     }
     // Check if user exists
     using (var db = new StatisticsDbContext())
     {
         var existingUsers = db.Users.Where(user => String.Equals(user.Username, model.Username));
         if (existingUsers.Any())
         {
             return new UserCreationResultModel(false, new[]
             {
                 String.Format("User with name '{0}' already exists.", model.Username)
             });
         }
         db.Users.Add(new User
         {
             Username = model.Username,
             Password = PasswordHash.CreateHashWithRandomSalt(model.Password),
             Country = model.Country,
             RegisteredDate = DateTime.Now,
             LastLoginDate = DateTime.Now
         });
         db.SaveChanges();
         return new UserCreationResultModel(true);
     }
 }
Esempio n. 2
0
 public bool AddGame([FromBody]Game game)
 {
     if (!ModelState.IsValid)
     {
         return false;
     }
     using (var db = new StatisticsDbContext())
     {
         var username = RequestContext.Principal.Identity.Name;
         var user = UserUtils.GetUserByUsername(username);
         game.GameID = 0;
         game.UserID = user.UserID;
         game.PlayedDate = DateTime.Now;
         var newGame = db.Games.Add(game);
         db.SaveChanges();
         Debug.WriteLine("GameID:{0}, UserID:{1}", newGame.GameID, newGame.UserID);
     }
     return true;
 }