public IQueryable <Movie> GetMovies(UserAccessToken user)
        {
            var result = _ctx.Movies.OrderByDescending(x => x.ReleaseDate).Take(10);

            if (user.UserName != null && user.UserName != "")
            {
                foreach (var item in result)
                {
                    int userId = _ctx.Users.Single(x => x.UserName == user.UserName).Id;
                    if (_ctx.UserRatings.Where(x => (x.UserId == userId && x.MovieId == item.Id)).Count() > 0)
                    {
                        var userRating = _ctx.UserRatings.Single(x => (x.UserId == userId && x.MovieId == item.Id));
                        if (userRating != null)
                        {
                            item.UserRating = userRating.Rating;
                        }
                    }
                }
            }
            return(result);
        }
        public UserAccessToken RegisterNewUser(User newUser)
        {
            using (MD5 md5Hash = MD5.Create())
            {
                newUser.UserGuid = Guid.NewGuid();
                newUser.Password = HelperClass.GetMd5Hash(md5Hash, newUser.Password + newUser.UserGuid.ToString());
            }

            try
            {
                _ctx.Users.Add(newUser);
                if (_ctx.SaveChanges() > 0)
                {
                    string          token = Convert.ToBase64String(Guid.NewGuid().ToByteArray());
                    UserAccessToken newUserAccessToken = new UserAccessToken();
                    newUserAccessToken.UserName       = newUser.UserName;
                    newUserAccessToken.AccessToken    = token;
                    newUserAccessToken.ExpirationDate = DateTime.Now.AddYears(1);
                    _ctx.UserAccessTokens.Add(newUserAccessToken);
                    if (_ctx.SaveChanges() > 0)
                    {
                        return(newUserAccessToken);
                    }
                    else
                    {
                        var res = new HttpResponseMessage(HttpStatusCode.NotFound)
                        {
                            Content      = new StringContent(string.Format("There was an error.")),
                            ReasonPhrase = "There was an error"
                        };
                        throw new HttpResponseException(res);
                    }
                }
                else
                {
                    throw new HttpResponseException(System.Net.HttpStatusCode.BadRequest);
                }
            }
            catch (DbUpdateException) { throw new HttpResponseException(System.Net.HttpStatusCode.BadRequest); }
        }
        public UserAccessToken ValidateExistingUser(User newUser)
        {
            try
            {
                bool IsUserValidated = false;

                User existingUser = _ctx.Users.Single(x => x.UserName == newUser.UserName);
                using (MD5 md5Hash = MD5.Create())
                {
                    IsUserValidated = HelperClass.VerifyMd5Hash(md5Hash, newUser.Password + existingUser.UserGuid, existingUser.Password);
                }

                if (IsUserValidated) {
                    string token = Convert.ToBase64String(Guid.NewGuid().ToByteArray());
                    UserAccessToken newUserAccessToken = new UserAccessToken();
                    newUserAccessToken.UserName = newUser.UserName;
                    newUserAccessToken.AccessToken = token;
                    newUserAccessToken.ExpirationDate = DateTime.Now.AddYears(1);
                    _ctx.UserAccessTokens.Add(newUserAccessToken);
                    if (_ctx.SaveChanges() > 0)
                        return newUserAccessToken;
                    else
                    {
                        var res = new HttpResponseMessage(HttpStatusCode.NotFound)
                        {
                            Content = new StringContent(string.Format("There was an error.")),
                            ReasonPhrase = "There was an error"
                        };
                        throw new HttpResponseException(res);
                    }
                }
                else
                {
                    var res = new HttpResponseMessage(HttpStatusCode.NotFound)
                    {
                        Content = new StringContent(string.Format("Invalid Username/Password Combination.")),
                        ReasonPhrase = "Invalid Username and Password"
                    };
                    throw new HttpResponseException(res);
                }
            }
            catch (InvalidOperationException)
            {
                var res = new HttpResponseMessage(HttpStatusCode.NotFound)
                {
                    Content = new StringContent(string.Format("User not found.")),
                    ReasonPhrase = "User not found."
                };
                throw new HttpResponseException(res);
            }
        }
        public UserAccessToken RegisterNewUser(User newUser)
        {
            using (MD5 md5Hash = MD5.Create())
            {
                newUser.UserGuid = Guid.NewGuid();
                newUser.Password = HelperClass.GetMd5Hash(md5Hash, newUser.Password + newUser.UserGuid.ToString());
            }

            try
            {
                _ctx.Users.Add(newUser);
                if (_ctx.SaveChanges() > 0) {
                    string token = Convert.ToBase64String(Guid.NewGuid().ToByteArray());
                    UserAccessToken newUserAccessToken = new UserAccessToken();
                    newUserAccessToken.UserName = newUser.UserName;
                    newUserAccessToken.AccessToken = token;
                    newUserAccessToken.ExpirationDate = DateTime.Now.AddYears(1);
                    _ctx.UserAccessTokens.Add(newUserAccessToken);
                    if (_ctx.SaveChanges() > 0)
                        return newUserAccessToken;
                    else
                    {
                        var res = new HttpResponseMessage(HttpStatusCode.NotFound)
                        {
                            Content = new StringContent(string.Format("There was an error.")),
                            ReasonPhrase = "There was an error"
                        };
                        throw new HttpResponseException(res);
                    }
                }
                else
                    throw new HttpResponseException(System.Net.HttpStatusCode.BadRequest);
            }
            catch (DbUpdateException) { throw new HttpResponseException(System.Net.HttpStatusCode.BadRequest); }
        }
        public IQueryable<Movie> GetMovies(UserAccessToken user)
        {
            var result = _ctx.Movies.OrderByDescending(x => x.ReleaseDate).Take(10);

            if (user.UserName != null && user.UserName != "") {
                foreach (var item in result)
                {
                    int userId = _ctx.Users.Single(x => x.UserName == user.UserName).Id;
                    if (_ctx.UserRatings.Where(x => (x.UserId == userId && x.MovieId == item.Id)).Count() > 0) {
                        var userRating = _ctx.UserRatings.Single(x => (x.UserId == userId && x.MovieId == item.Id));
                        if (userRating != null)
                        {
                            item.UserRating = userRating.Rating;
                        }
                    }

                }
            }
            return result;
        }
Пример #6
0
 public IEnumerable<Movie> Get(UserAccessToken user)
 {
     return _repo.GetMovies(user);
 }