Ejemplo n.º 1
0
        public HttpResponseMessage RegisterUser(UserRegisterModel model)
        {
            var responseMsg = this.ExceptionHandler(
                () =>
            {
                var context = new AutoMorgueContext();

                UserDataPersister.ValidateUsername(model.Username);
                UserDataPersister.ValidateNickname(model.DisplayName);
                UserDataPersister.ValidateAuthCode(model.AuthCode);

                var usernameToLower    = model.Username.ToLower();
                var displayNameToLower = model.DisplayName.ToLower();

                var users = context.Users;
                var user  = users.FirstOrDefault(usr => usr.Username == usernameToLower || usr.DisplayName.ToLower() == displayNameToLower);

                var role = context.Roles.FirstOrDefault(r => r.Name == "user");
                if (role == null)
                {
                    role = new Role
                    {
                        Name = "user"
                    };
                }

                if (user != null)
                {
                    throw new InvalidOperationException("Invalid Username or Password");
                }

                var newUser = new User
                {
                    Username    = usernameToLower,
                    DisplayName = model.DisplayName,
                    AuthCode    = model.AuthCode,
                    Role        = role
                };

                var userInDb = context.Users.Add(newUser);
                context.SaveChanges();

                userInDb.SessionKey = UserDataPersister.GenerateSessionKey(userInDb.Id);
                context.SaveChanges();

                var loggedModel = new UserLoggedModel
                {
                    DisplayName = userInDb.DisplayName,
                    SessionKey  = userInDb.SessionKey
                };

                var response = this.Request.CreateResponse(HttpStatusCode.Created, loggedModel);
                response.Headers.Location = new Uri(Url.Link("DefaultApi", new { id = userInDb.Id }));

                return(response);
            });

            return(responseMsg);
        }
Ejemplo n.º 2
0
 public HttpResponseMessage StartGame(string sessionKey, int gameId)
 {
     var responseMsg = this.PerformOperation(() =>
     {
         var userId = UserDataPersister.LoginUser(sessionKey);
         GameDataPersister.StartGame(gameId);
     });
     return responseMsg;
 }
Ejemplo n.º 3
0
        public HttpResponseMessage LogoutUser(string sessionKey)
        {
            var responseMsg = this.PerformOperation(() =>
            {
                UserDataPersister.LogoutUser(sessionKey);
            });

            return(responseMsg);
        }
Ejemplo n.º 4
0
 public HttpResponseMessage GetActiveGames(string sessionKey)
 {
     var responseMsg = this.PerformOperation(() =>
     {
         var userId = UserDataPersister.LoginUser(sessionKey);
         IEnumerable<OpenGameModel> games = GameDataPersister.GetActiveGames(userId);
         return games;
     });
     return responseMsg;
 }
Ejemplo n.º 5
0
 public HttpResponseMessage JoinGame(string sessionKey, [FromBody]
                                     GameModel gameModel)
 {
     var responseMsg = this.PerformOperation(() =>
     {
         var userId = UserDataPersister.LoginUser(sessionKey);
         GameDataPersister.JoinGame(userId, gameModel.Id, gameModel.Password);
     });
     return responseMsg;
 }
Ejemplo n.º 6
0
        public HttpResponseMessage GetAllUsers(string sessionKey)
        {
            var responseMsg = this.PerformOperation(() =>
            {
                UserDataPersister.LoginUser(sessionKey);
                IEnumerable <UserModel> users = UserDataPersister.GetAllUsers();
                return(users);
            });

            return(responseMsg);
        }
Ejemplo n.º 7
0
        public HttpResponseMessage PerformAttack(string sessionKey, int gameId, [FromBody]
                                                 MoveModel move)
        {
            var responseMsg = this.PerformOperation(() =>
            {
                var userId = UserDataPersister.LoginUser(sessionKey);
                BattleDataPersister.PerformAttack(userId, gameId, move.UnitId, move.Position.X, move.Position.Y);
            });

            return(responseMsg);
        }
Ejemplo n.º 8
0
        public HttpResponseMessage GetUnreadMessages(string sessionKey)
        {
            var responseMsg = this.PerformOperation(() =>
            {
                var userId   = UserDataPersister.LoginUser(sessionKey);
                var messages = MessagesDataPersister.GetUnreadMessages(userId);
                return(messages);
            });

            return(responseMsg);
        }
Ejemplo n.º 9
0
        public HttpResponseMessage PerformDefend(string sessionKey, int gameId, [FromBody]
                                                 int unitId)
        {
            var responseMsg = this.PerformOperation(() =>
            {
                var userId = UserDataPersister.LoginUser(sessionKey);
                BattleDataPersister.PerformDefend(userId, gameId, unitId);
            });

            return(responseMsg);
        }
Ejemplo n.º 10
0
        public HttpResponseMessage GetBattleField(string sessionKey, int gameId)
        {
            var responseMsg = this.PerformOperation(() =>
            {
                UserDataPersister.LoginUser(sessionKey);

                BattleFieldModel gameField = GameDataPersister.GetBattleField(gameId);

                return gameField;
            });
            return responseMsg;
        }
        public HttpResponseMessage LogoutUser(string sessionKey)
        {
            var responseMsg = this.PerformOperation(() =>
            {
                UserDataPersister.LogoutUser(sessionKey);
            });

            if (responseMsg.StatusCode == HttpStatusCode.OK)
            {
                responseMsg = Request.CreateResponse(HttpStatusCode.NoContent);
            }
            return(responseMsg);
        }
Ejemplo n.º 12
0
        public HttpResponseMessage StartGame(string sessionKey, int gameId)
        {
            var responseMsg = this.PerformOperation(() =>
            {
                var userId = UserDataPersister.LoginUser(sessionKey);
                GameDataPersister.StartGame(gameId);
            });

            if (responseMsg.StatusCode == HttpStatusCode.OK)
            {
                responseMsg.StatusCode = HttpStatusCode.NoContent;
            }
            return(responseMsg);
        }
        public HttpResponseMessage LoginUser(UserLoginModel user)
        {
            var responseMsg = this.PerformOperation(() =>
            {
                string nickname = string.Empty;
                var sessionKey  = UserDataPersister.LoginUser(user.Username, user.AuthCode, out nickname);
                return(new UserLoggedModel()
                {
                    Nickname = nickname,
                    SessionKey = sessionKey
                });
            });

            return(responseMsg);
        }
Ejemplo n.º 14
0
        public HttpResponseMessage JoinGame(string sessionKey, [FromBody]
                                            GameModel gameModel)
        {
            var responseMsg = this.PerformOperation(() =>
            {
                var userId = UserDataPersister.LoginUser(sessionKey);
                GameDataPersister.JoinGame(userId, gameModel.Id, gameModel.Password);
            });

            if (responseMsg.StatusCode == HttpStatusCode.OK)
            {
                responseMsg.StatusCode = HttpStatusCode.NoContent;
            }
            return(responseMsg);
        }
Ejemplo n.º 15
0
        public HttpResponseMessage LogoutUser(string sessionKey)
        {
            var responseMsg = this.PerformOperation(() =>
            {
                UserDataPersister.LogoutUser(sessionKey);
            });

            if (responseMsg.StatusCode == HttpStatusCode.OK)
            {
                responseMsg.StatusCode = HttpStatusCode.NoContent;
            }
            //responseMsg.Content = new StringContent("");

            return(responseMsg);
        }
Ejemplo n.º 16
0
        public HttpResponseMessage LoginUser(UserLoginModel model)
        {
            var responseMsg = this.ExceptionHandler(
                () =>
            {
                var context = new AutoMorgueContext();

                UserDataPersister.ValidateUsername(model.Username);
                UserDataPersister.ValidateAuthCode(model.AuthCode);

                var usernameToLower = model.Username.ToLower();

                var users = context.Users;
                var user  = users.FirstOrDefault(
                    usr => usr.Username == usernameToLower && usr.AuthCode == model.AuthCode);

                if (user == null)
                {
                    throw new InvalidOperationException("Invalid Username or Password");
                }

                user.SessionKey = UserDataPersister.GenerateSessionKey(user.Id);
                context.SaveChanges();

                var loggedModel = new UserLoggedModel
                {
                    DisplayName = user.DisplayName,
                    SessionKey  = user.SessionKey
                };

                var response = this.Request.CreateResponse(HttpStatusCode.Created, loggedModel);
                response.Headers.Location = new Uri(Url.Link("DefaultApi", new { id = user.Id }));

                return(response);
            });

            return(responseMsg);
        }
Ejemplo n.º 17
0
 public UsersController(UserDataPersister data)
 {
     this.data = data;
 }
Ejemplo n.º 18
0
 public DbDependencyResolver(RecipeAppContext context)
 {
     this.context     = context;
     recipeRepository = new RecipeRepository(context);
     data             = new UserDataPersister(context);
 }
 public RecipiesController(IRepository <Recipe> recipeRepository, UserDataPersister data)
 {
     this.recipeRepository = recipeRepository;
     this.persister        = data;
 }