Exemple #1
0
        public async Task<IActionResult> ChangeAvatar(IFormFile avatar, string userName)
        {
            if(avatar != null && avatar.Length > 1)
            {
                UserDetailsViewModel user = UsersDetailsMapper.Map(_usersService.Get(User.Identity.Name ?? userName));

                string fileName = $"avatar{Path.GetExtension(ContentDispositionHeaderValue.Parse(avatar.ContentDisposition).FileName.Trim('"'))}";

                user.Avatar = fileName;

                fileName = Path.Combine(_environment.WebRootPath, "data/avatars") + $@"/{User.Identity.Name}/{fileName}";

                string dir = Path.Combine(_environment.WebRootPath, "data/avatars") + $@"/{User.Identity.Name}";
                if (!Directory.Exists(dir))
                    Directory.CreateDirectory(dir);

                using (FileStream fs = System.IO.File.Create(fileName))
                {
                    await avatar.CopyToAsync(fs);
                    await fs.FlushAsync();
                }

                await _usersService.EditAsync(user.UserName, user.RealName, user.About, user.WebSite, user.Gender, user.Avatar);
            }

            return RedirectToAction(nameof(Index));
        }
Exemple #2
0
        public ActionResult Details(string userName)
        {
            UserViewModel item = UsersDetailsMapper.Map(_usersService.Get(userName));

            if (User.Identity.IsAuthenticated)
            {
                ViewBag.CurrentUser = UsersMapper.Map(_currentUserService.GetDTO);
            }

            return(View(item));
        }
Exemple #3
0
        public IActionResult Index()
        {
            UserDetailsViewModel user = UsersDetailsMapper.Map(_usersService.Get(User.Identity.Name));
            if (user == null)
                throw new ApplicationException($"Unable to load user with ID '{_userManager.GetUserId(User)}'.");

            var model = new IndexViewModel
            {
                Avatar = user.Avatar,
                RealName = user.RealName,
                Username = user.UserName,
                About = user.About,
                WebSite = user.WebSite,
                Gender = user.Gender,
                StatusMessage = StatusMessage
            };

            return View(model);
        }
 public UserDetailsViewModel Get(string userName)
 {
     return(UsersDetailsMapper.Map(_usersService.Get(userName)));
 }
        /// <summary>
        /// Helps map user details entity to user details data transfer object.
        /// </summary>
        protected UserDetailsDTO MapUserDetails(User user)
        {
            User currentUser = _currentUserService.Get;

            var followings = new List <UserDTO>();
            var followers  = new List <UserDTO>();

            if (currentUser == null)
            {
                foreach (var following in _unitOfWork.Followings.Find(f => f.UserId == user.Id))
                {
                    followings.Add(UsersMapper.Map(
                                       following.FollowedUser,
                                       _unitOfWork.Confirmations.Find(c => c.UserId == following.FollowedUserId).FirstOrDefault() != null,
                                       false, false, false
                                       ));
                }

                foreach (var follower in _unitOfWork.Followings.Find(f => f.FollowedUserId == user.Id))
                {
                    followers.Add(UsersMapper.Map(
                                      follower.User,
                                      _unitOfWork.Confirmations.Find(c => c.UserId == follower.UserId).FirstOrDefault() != null,
                                      false, false, false
                                      ));
                }

                return(UsersDetailsMapper.Map(
                           user,
                           _unitOfWork.Confirmations.Find(c => c.UserId == user.Id).FirstOrDefault() != null,
                           false,
                           false,
                           false,
                           followings,
                           followers,
                           null
                           ));
            }
            else
            {
                foreach (var following in _unitOfWork.Followings.Find(f => f.UserId == user.Id))
                {
                    followings.Add(UsersMapper.Map(
                                       following.FollowedUser,
                                       _unitOfWork.Confirmations.Find(c => c.UserId == following.FollowedUserId).FirstOrDefault() != null,
                                       _unitOfWork.Blockings.Find(b => b.BlockedUserId == currentUser.Id && b.UserId == user.Id).FirstOrDefault() != null,
                                       _unitOfWork.Followings.Find(f => f.FollowedUserId == following.FollowedUserId && f.UserId == currentUser.Id).FirstOrDefault() != null,
                                       _unitOfWork.Blockings.Find(b => b.BlockedUserId == currentUser.Id && b.UserId == user.Id).FirstOrDefault() != null
                                       ));
                }

                foreach (var follower in _unitOfWork.Followings.Find(f => f.FollowedUserId == user.Id))
                {
                    followers.Add(UsersMapper.Map(
                                      follower.User,
                                      _unitOfWork.Confirmations.Find(c => c.UserId == follower.UserId).FirstOrDefault() != null,
                                      _unitOfWork.Followings.Find(f => f.FollowedUserId == follower.UserId && f.UserId == currentUser.Id).FirstOrDefault() != null,
                                      _unitOfWork.Blockings.Find(b => b.BlockedUserId == follower.UserId && b.UserId == currentUser.Id).FirstOrDefault() != null,
                                      _unitOfWork.Blockings.Find(b => b.BlockedUserId == currentUser.Id && b.UserId == user.Id).FirstOrDefault() != null
                                      ));
                }

                var mutuals = new List <UserDTO>();

                if (followers.Count > 0)
                {
                    foreach (var follower in _unitOfWork.Followings.Find(f => f.FollowedUserId == currentUser.Id))
                    {
                        if (follower.UserId != user.Id && _unitOfWork.Followings.Find(f => f.FollowedUserId == user.Id && f.UserId == follower.UserId).FirstOrDefault() != null)
                        {
                            mutuals.Add(UsersMapper.Map(
                                            follower.User,
                                            _unitOfWork.Confirmations.Find(c => c.UserId == follower.UserId).FirstOrDefault() != null,
                                            _unitOfWork.Followings.Find(f => f.FollowedUserId == follower.UserId && f.UserId == currentUser.Id).FirstOrDefault() != null,
                                            _unitOfWork.Blockings.Find(b => b.BlockedUserId == follower.UserId && b.UserId == currentUser.Id).FirstOrDefault() != null,
                                            _unitOfWork.Blockings.Find(b => b.BlockedUserId == currentUser.Id && b.UserId == user.Id).FirstOrDefault() != null
                                            ));
                        }
                    }
                }

                return(UsersDetailsMapper.Map(
                           user,
                           _unitOfWork.Confirmations.Find(c => c.UserId == user.Id).FirstOrDefault() != null,
                           _unitOfWork.Followings.Find(f => f.FollowedUserId == user.Id && f.UserId == currentUser.Id).FirstOrDefault() != null,
                           _unitOfWork.Blockings.Find(b => b.BlockedUserId == user.Id && b.UserId == currentUser.Id).FirstOrDefault() != null,
                           _unitOfWork.Blockings.Find(b => b.BlockedUserId == currentUser.Id && b.UserId == user.Id).FirstOrDefault() != null,
                           followings,
                           followers,
                           mutuals
                           ));
            }
        }