Exemplo n.º 1
0
        public HttpResponseMessage PostLoginUser(User user)
        {
            BankSystemEntities context = new BankSystemEntities();

            using (context)
            {
                context.Users.Add(user);
                context.SaveChanges();

                user.AuthKey = this.GenerateSessionKey(user.Id);
                context.SaveChanges();

                var response =
                    this.Request.CreateResponse(HttpStatusCode.Created,
                                    user);
                return response;
            }
        }
Exemplo n.º 2
0
        public HttpResponseMessage PostLoginUser(UserModel model)
        {
            var context = new BankSystemEntities();
            using (context)
            {

                var user = context.Users.FirstOrDefault(
                    u => u.Username == model.Username.ToLower()
                    && u.Password == model.Password);

                if (user == null)
                {
                    throw new InvalidOperationException("Invalid username or password");
                }
                if (user.AuthKey == null)
                {
                    user.AuthKey = this.GenerateSessionKey(user.Id);
                    context.SaveChanges();
                }

                var loggedModel = new LoggedUserModel()
                {
                    Id = user.Id,
                    Username = user.Username,
                    SessionKey = user.AuthKey
                };

                var response =
               this.Request.CreateResponse(HttpStatusCode.Created,
                               loggedModel);
                return response;
            }
        }
Exemplo n.º 3
0
        public HttpResponseMessage PutLogoutUser(int money,string sessionKey)
        {
            var context = new BankSystemEntities();
            using (context)
            {

                var user = context.Users.FirstOrDefault(u => u.AuthKey == sessionKey);
                if (user==null)
                {
                    throw new InvalidOperationException("There is no such user loged");
                }
                user.AvelableMoney = money;
                context.SaveChanges();

                var model = new UserMoneyModel()
                {
                    Id = user.Id,
                    AvelableMoney = user.AvelableMoney
                };

                var response =
                this.Request.CreateResponse(HttpStatusCode.OK,model);

                return response;
            }
        }
Exemplo n.º 4
0
        public HttpResponseMessage PutLogoutUser(string sessionKey)
        {
            var context = new BankSystemEntities();
            using (context)
            {

                var user = context.Users.FirstOrDefault(
                    u => u.AuthKey == sessionKey);

                if (user == null)
                {
                    throw new InvalidOperationException("No user with such sessionKey logged");
                }
                user.AuthKey = null;
                context.SaveChanges();

                var response =
                this.Request.CreateResponse(HttpStatusCode.OK);

                return response;
            }
        }