public HttpResponseMessage PostRegisterAdmin([FromBody]UserModel model, [ValueProvider(typeof(HeaderValueProviderFactory<string>))]string sessionKey)
        {
            var responseMsg = this.PerformOperationAndHandleExceptions<HttpResponseMessage>(
               () =>
               {
                   using (var context = new StoreContext())
                   {
                       this.ValidateSessionKey(sessionKey);

                       var admin = context.Users.FirstOrDefault(u => u.SessionKey == sessionKey);
                       if (admin == null)
                       {
                           throw new ArgumentException("Invalid SessionKey or user is already logouted");
                       }
                       else if (admin.IsAdmin != true)
                       {
                           throw new ArgumentException("Unauthorized Access");
                       }

                       // trqbva da validirame userite
                       this.ValidateUsername(model.Username);
                       this.ValidateDisplayNam(model.DisplayName);
                       this.ValidateAuthCode(model.AuthCode);

                       //sravnqvame usernames ToLower(), taka gi vkarvame i v bazata danni
                       // dokato nicknames pak gi sravnqvame v ToLower() no v bazata si ostavat
                       // nepromeneni za da moje da se pishat taka naprimer PyMeH
                       var usernameToLower = model.Username.ToLower();
                       var displayNameToLower = model.DisplayName.ToLower();
                       var user = context.Users.FirstOrDefault(
                           u => u.Username == usernameToLower ||
                               u.DisplayName == displayNameToLower);

                       if (user != null)
                       {
                           throw new InvalidOperationException("User with this username or displayname exists");
                       }

                       user = new User
                       {
                           Username = usernameToLower,
                           DisplayName = model.DisplayName,
                           AuthCode = model.AuthCode,
                           IsAdmin = model.IsAdmin
                       };

                       if (model.IsAdmin == true)
                       {
                           user.IsAdmin = true;
                       }
                       else
                       {
                           user.IsAdmin = false;
                       }

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

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

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

                       var response =
                            this.Request.CreateResponse(HttpStatusCode.Created,
                                            loggedModel);
                       return response;
                   }
               });

            return responseMsg;
        }
        public HttpResponseMessage PostLogin(UserModel model)
        {
            var responseMsg = this.PerformOperationAndHandleExceptions<HttpResponseMessage>(
             () =>
             {
                 using (var context = new StoreContext())
                 {
                     // trqbva da validirame userite
                     this.ValidateUsername(model.Username);
                     this.ValidateAuthCode(model.AuthCode);

                     //sravnqvame usernames ToLower(), taka gi vkarvame i v bazata danni
                     // dokato nicknames pak gi sravnqvame v ToLower() no v bazata si ostavat
                     // nepromeneni za da moje da se pishat taka naprimer PyMeH
                     var usernameToLower = model.Username.ToLower();
                     var user = context.Users.FirstOrDefault(
                         usr => usr.Username == usernameToLower
                         && usr.AuthCode == model.AuthCode);

                     if (user == null)
                     {
                         throw new InvalidOperationException("Wrong username or password");
                     }

                     if (user.SessionKey == null)
                     {
                         user.SessionKey = this.GenerateSessionKey(user.Id);
                         context.SaveChanges();
                     }

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

                     var response =
                          this.Request.CreateResponse(HttpStatusCode.OK,
                                          loggedModel);
                     return response;
                 }
             });

            return responseMsg;
        }
        public HttpResponseMessage PostRegister([FromBody]UserModel model)
        {
            var responseMsg = this.PerformOperationAndHandleExceptions<HttpResponseMessage>(
               () =>
               {
                   using (var context = new StoreContext())
                   {
                       // trqbva da validirame userite
                       model.DisplayName = model.Username;
                       this.ValidateUsername(model.Username);
                       this.ValidateDisplayNam(model.DisplayName);
                       this.ValidateAuthCode(model.AuthCode);

                       //sravnqvame usernames ToLower(), taka gi vkarvame i v bazata danni
                       // dokato nicknames pak gi sravnqvame v ToLower() no v bazata si ostavat
                       // nepromeneni za da moje da se pishat taka naprimer PyMeH
                       var usernameToLower = model.Username.ToLower();
                       var displayNameToLower = model.DisplayName.ToLower();
                       var user = context.Users.FirstOrDefault(
                           u => u.Username == usernameToLower ||
                               u.DisplayName == displayNameToLower);

                       if (user != null)
                       {
                           throw new InvalidOperationException("User with this username or displayname exists");
                       }

                       user = new User
                       {
                           Username = usernameToLower,
                           DisplayName = model.DisplayName,
                           AuthCode = model.AuthCode
                       };

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

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

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

                       var response =
                            this.Request.CreateResponse(HttpStatusCode.Created,
                                            loggedModel);
                       return response;
                   }
               });

            return responseMsg;
        }