public int CreateOrUpdateUser([FromUri]User user)
        {
            using (var context = new TravelPlannerEntities())
            {
                var loggedUser = UserHelper.GetLoggedUser(Request);

                var loggedRole = context.Role.First(x => x.Id == loggedUser.RoleId);

                if (loggedRole.Name == RolesEnum.User.ToString())
                {
                    throw new WebException(Messages.Unauthorized);
                }

                var validationMessage = ValidateUserData(user);
                if (!string.IsNullOrEmpty(validationMessage))
                {
                    throw new WebException(validationMessage);
                }

                if (user.Id == 0)
                {
                    context.User.Add(user);
                }
                else
                {
                    context.Entry(user).State = EntityState.Modified;
                }
                context.SaveChanges();
                return user.Id;
            }
        }
        public static void ClassCleanup()
        {
            Mock<IUserHelper> UserHelper = new Mock<IUserHelper>(); ;
            UserHelper.Setup(x => x.GetLoggedUserRole(It.IsAny<HttpRequestMessage>())).Returns(new Role { Id = 1, Name = "Administrator" });

            var userController = new UserController(UserHelper.Object);

            using (var context = new TravelPlannerEntities())
            {
                var users = context.User.Where(x => x.Username == TestUserAdmnistratorName
                    || x.Username == TestUserManagerName
                    || x.Username == TestUserRegularUserName
                    || x.Username.Contains(RegisterUserTestName));

                /// Remove trips
                foreach (var user in users)
                {
                    var trips = context.Trip.Where(x => x.UserId == user.Id);
                    context.Trip.RemoveRange(trips);
                }

                /// Remove users
                context.User.RemoveRange(users);
                context.SaveChanges();
            }
        }
        public bool DeleteUser(int userId)
        {
            using (var context = new TravelPlannerEntities())
            {
                var user = GetUserIfAuthorized(userId, context);

                if (context.Trip.Any(x => x.UserId == userId))
                {
                    throw new WebException(Messages.UserWithTrips);
                }

                context.User.Remove(user);
                context.SaveChanges();
            }
            return true;
        }
        private TokenResponse CreateToken(User user, TravelPlannerEntities context)
        {
            var dateTime = DateTime.UtcNow;
            var time = BitConverter.GetBytes(dateTime.ToBinary());
            var key = Guid.NewGuid().ToByteArray();
            var token = Convert.ToBase64String(time.Concat(key).ToArray());

            user.Token = token;
            context.Entry(user).State = EntityState.Modified;
            context.SaveChanges();
            return new TokenResponse
            {
                Token = token,
                ExpirationDate = dateTime.AddHours(24).ToString("MM/dd/yyyy hh:mm:ss tt", CultureInfo.InvariantCulture),
                Role = user.Role.Name
            };
        }
        public TokenResponse RegisterUser([FromUri]User user)
        {
            using (var context = new TravelPlannerEntities())
            {
                var validationMessage = ValidateUserData(user);
                if (!string.IsNullOrEmpty(validationMessage))
                {
                    throw new WebException(validationMessage);
                }

                if (context.User.Any(x => x.Username == user.Username))
                {
                    throw new WebException(Messages.DuplicatedUser);
                }

                var role = context.Role.FirstOrDefault(x => x.Id == user.RoleId);
                if (role == null)
                {
                    throw new WebException(Messages.RoleNotFound);
                }

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

                return CreateToken(user, context);
            }
        }
 public bool Logout()
 {
     using (var context = new TravelPlannerEntities())
     {
         var loggedUserId = UserHelper.GetLoggedUser(Request).Id;
         context.User.First(x => x.Id == loggedUserId).Token = string.Empty;
         context.SaveChanges();
     }
     return true;
 }