Example #1
0
        public async Task <IActionResult> Index()
        {
            HomeViewModel     model  = new HomeViewModel();
            UserContextEntity entity = new UserContextEntity();

            if (HttpContext.User.Identity.IsAuthenticated)
            {
                entity.UserName    = HttpContext.User.Claims.Where(c => c.Type == "FullName").Select(c => c.Value).FirstOrDefault();
                entity.UserPicPath = HttpContext.User.Claims.Where(c => c.Type == "PicPath").Select(c => c.Value).FirstOrDefault();
                entity.UserId      = HttpContext.User.Claims.Where(c => c.Type == "Id").Select(c => c.Value).FirstOrDefault();
                model.LikedBlogs   = await _blogService.GetBlogsLikedByUser(entity);

                model.AllBlogs = await _blogService.GetAll();

                model.Popular = await _blogService.GetPopularBlogs(3);

                model.Recent = await _blogService.GetRecentBlogs(5);
            }
            else
            {
                model.Popular = await _blogService.GetPopularBlogs(3);

                model.Recent = await _blogService.GetRecentBlogs(5);

                model.Blogs = await _blogService.GetAll();

                model.AllBlogs = await _blogService.GetAll();
            }
            var x = await _blogService.GetBlogsCountByCategory();

            model.Categories = await _categoryService.GetAll();

            // throw new NullReferenceException();
            return(View(model));
        }
 public UserContext(UserContextEntity userContext)
     : base(userContext)
 {
     FirstName = userContext.FirstName;
     LastName  = userContext.LastName;
     Login     = userContext.Login;
     Role      = userContext.Role;
 }
Example #3
0
        public async Task <long> LikeBlog(string blogId, UserContextEntity user)
        {
            long count = 0;
            var  model = _mapper.Map <UserContextModel>(user);

            if (!await _blogRepository.IsBlogAlreadyLikedByUser(blogId, model))
            {
                count = (await _blogRepository.LikeBlog(blogId, model));
            }
            return(count);
        }
Example #4
0
        public async Task <List <BlogEntity> > GetBlogsLikedByUser(UserContextEntity user)
        {
            List <BlogEntity> entities = new List <BlogEntity>();
            var model = _mapper.Map <UserContextModel>(user);
            var blogs = (await _blogRepository.GetBlogsLikedByUser(model));

            if (blogs != null && blogs.Count > 0)
            {
                entities = _mapper.Map <List <BlogEntity> >(blogs.ToList());
            }
            return(entities);
        }
Example #5
0
        public async Task <long> DislikeBlog(string blogId, UserContextEntity user)
        {
            var model = _mapper.Map <UserContextModel>(user);

            return(await _blogRepository.DislikeBlog(blogId, model));
        }
Example #6
0
        public async Task <long> AddRating(string blogId, int rating, UserContextEntity user)
        {
            var model = _mapper.Map <UserContextModel>(user);

            return(await _blogRepository.AddRating(blogId, rating, model));
        }
Example #7
0
        public UserContext AddWaiter(int managerId, string firstName, string lastName, string login, string password)
        {
            if (!CheckHasUserRole(managerId, UserRole.Manager))
                throw new SecurityException(String.Format("User id = {0} is not logged in or is no manager", managerId));

            if (String.IsNullOrEmpty(firstName))
                throw new ArgumentNullException("firstName");
            if (String.IsNullOrEmpty(lastName))
                throw new ArgumentNullException("lastName");
            if (String.IsNullOrEmpty(login))
                throw new ArgumentNullException("login");
            if (String.IsNullOrEmpty(password))
                throw new ArgumentNullException("password");

            UserContextEntity newWaiterContextEntity = null;

            using (var db = new DataAccessProvider())
            {
                var waiterContextToAdd = new UserContextEntity() { FirstName = firstName, LastName = lastName, Login = login, Role = UserRole.Waiter};
                var usersSameLogin = db.Users.Where(u => u.Role == UserRole.Waiter && u.Login.Equals(login));

                if (usersSameLogin != null && usersSameLogin.Any())
                {
                    foreach (UserContextEntity userContextEntity in usersSameLogin)
                        if (userContextEntity.Equals(waiterContextToAdd))
                        {
                            if (userContextEntity.IsDeleted)
                                userContextEntity.IsDeleted = false;

                            newWaiterContextEntity = userContextEntity;
                            break;
                        }

                    //istnieją kelnerzy o tym samym loginie, ale nie są tacy sami jak ten co chcemy dodać.
                    if(newWaiterContextEntity == null)
                        throw new ArgumentException(String.Format("login = {0} already exists in database!", login));
                }

                if(newWaiterContextEntity == null)
                    newWaiterContextEntity = db.Users.Add(waiterContextToAdd);
                db.SaveChanges();

                PasswordEntity newWaiterPassword = db.Passwords.FirstOrDefault(p => p.UserId == newWaiterContextEntity.Id);
                if (newWaiterPassword == null)
                {
                    newWaiterPassword = new PasswordEntity()
                    {
                        UserId = newWaiterContextEntity.Id,
                        Hash = HashClass.CreateSecondHash(password)
                    };
                    db.Passwords.Add(newWaiterPassword);
                    db.SaveChanges();
                }
                else
                {
                    newWaiterPassword.Hash = HashClass.CreateSecondHash(password);
                    db.Entry(newWaiterPassword).State = EntityState.Detached;
                    db.Passwords.Attach(newWaiterPassword);
                    db.Entry(newWaiterPassword).State = EntityState.Modified;
                    db.SaveChanges();
                }

            }

            return new UserContext(newWaiterContextEntity);
        }
Example #8
0
        private UserContext AddUserToDatabase(string firstName, string lastName, string login, string password, UserRole role)
        {
            if (String.IsNullOrEmpty(firstName))
                throw new ArgumentNullException("firstName is null");
            if (String.IsNullOrEmpty(lastName))
                throw new ArgumentNullException("lastName is null");
            if (String.IsNullOrEmpty(login))
                throw new ArgumentNullException("login is null");
            if (String.IsNullOrEmpty(password))
                throw new ArgumentNullException("password is null");

            UserContextEntity newUser = null;

            using (var db = new DataAccessProvider())
            {
                //sprawdzenie czy dany login już istnieje
                var userSameLogin = db.Users.FirstOrDefault(u => u.Role == role && u.Login == login);

                if (userSameLogin != null)
                    throw new ArgumentException(String.Format("There already is an user with login = {0}.", login));

                newUser = new UserContextEntity()
                {
                    Login = login,
                    FirstName = firstName,
                    LastName = lastName,
                    Role = role
                };

                newUser = db.Users.Add(newUser);
                db.SaveChanges();

                var userPassword = new PasswordEntity() { UserId = newUser.Id, Hash = HashClass.CreateSecondHash(password) };
                db.Passwords.Add(userPassword);
                db.SaveChanges();
            }

            return new UserContext(newUser);
        }
Example #9
0
        public async Task <IActionResult> Create(BlogViewModel model)
        {
            string BUCKET_NAME = "srx-blog-images";

            if (ModelState.IsValid)
            {
                UserContextEntity user = new UserContextEntity()
                {
                    UserId = model.UserId, UserName = model.UserName, UserPicPath = model.UserPicPath
                };
                var    blog           = new Entities.BlogEntity();
                string uniqueFileName = null;

                if (model.ImageFile != null)
                {
                    var response = new PutObjectResponse();
                    var client   = new AmazonS3Client(RegionEndpoint.EUWest1);
                    uniqueFileName = Guid.NewGuid().ToString() + '_' + model.ImageFile.FileName;
                    using (var stream = new MemoryStream())
                    {
                        model.ImageFile.CopyTo(stream);

                        var request = new PutObjectRequest
                        {
                            BucketName  = BUCKET_NAME,
                            Key         = uniqueFileName,
                            InputStream = stream,
                            ContentType = model.ImageFile.ContentType,
                        };

                        response = await client.PutObjectAsync(request);
                    };
                }

                blog.BlogImage = uniqueFileName;
                blog.IsDeleted = false;
                blog.Like      = new List <UserContextEntity>();
                blog.DisLike   = new List <UserContextEntity>();
                blog.Ratings   = new List <string>();
                blog.SubTitle  = model.SubTitle;
                blog.Title     = model.Title;
                List <string> list = model.Tags.Split(",").ToList();
                blog.Tags = new List <string>();
                foreach (string str in list)
                {
                    blog.Tags.Add(str.Trim());
                }
                blog.Category  = model.Category;
                blog.CreatedBy = user;
                blog.CreatedOn = DateTime.Now;
                blog.Content   = model.Content;
                await _blogService.Create(blog);

                return(RedirectToAction("Index", "Home"));
            }
            else
            {
                await model.Initialize(_categoryCollection, HttpContext);

                return(View(model));
            }
        }
 public UserContext(UserContextEntity userContext)
     : base(userContext)
 {
     FirstName = userContext.FirstName;
     LastName = userContext.LastName;
     Login = userContext.Login;
     Role = userContext.Role;
 }