예제 #1
0
        public async Task <ActionResult <IEnumerable <ConnectionDTO> > > GetUsers([FromQuery] UserParams userParams)
        {
            //to include interested in as well
            var user = await _userRepository.GetUserByUsernameAsync(User.GetUsername());

            userParams.CurrentUsername = user.UserName;
            if (string.IsNullOrEmpty(userParams.Gender))
            {
                userParams.Gender = user.Gender == "female" ? "female" : "male";
            }

            var users = await _userRepository.GetConnectionsAsync(userParams);

            //now the users variiable is now a paged list of type connection dto
            Response.AddPaginationHeader(users.CurrentPage, users.PageSize, users.TotalCount, users.TotalPages);
            return(Ok(users));
        }
예제 #2
0
        public async Task <ActionResult> UpdateUser(UpdateMemberDto updateMemberDto)
        {
            //var username = User.FindFirst(ClaimTypes.NameIdentifier)?.Value;
            //var username = User.GetUsername(); // vadi iz tokena username
            var user = await userRepository.GetUserByUsernameAsync(User.GetUsername());

            mapper.Map(updateMemberDto, user);

            userRepository.Update(user);

            if (await userRepository.SaveAllAsync())
            {
                return(NoContent());
            }

            return(BadRequest("Failed to update user."));
        }
예제 #3
0
        public async Task <ActionResult <IEnumerable <MemberDto> > > GetUsers([FromQuery] UserParams userParams)
        {
            // var users = await _userRepository.GetUsersAsync();
            // var usersToReturn = _mapper.Map<IEnumerable<MemberDto>>(users);
            // return Ok(usersToReturn);
            var user = await _userRepository.GetUserByUsernameAsync(User.GetUsername());

            userParams.CurrentUsername = user.UserName;
            if (string.IsNullOrEmpty(userParams.Gender))
            {
                userParams.Gender = user.Gender == "male" ? "female" : "male";
            }
            var users = await _userRepository.GetMembersAsync(userParams);

            Response.AddPaginationHeader(users.CurrentPage, users.PageSize, users.TotalCount, users.TotalPages);
            return(Ok(users));
        }
예제 #4
0
        public async Task <ActionResult> UpdateUser(MemberUpdateDto memberUpdateDto)
        {
            // grab username from jwt sent from client
            var username = User.GetUsername();
            // get matching user from db
            var user = await _userRepository.GetUserByUsernameAsync(username);

            // map updated info to existing user
            _mapper.Map(memberUpdateDto, user);
            _userRepository.Update(user);
            if (await _userRepository.SaveAllAsync())
            {
                return(NoContent());
            }

            return(BadRequest("Failed to update user"));
        }
예제 #5
0
        public async Task<ActionResult> SetMainPhoto(int photoId)
        {
            var user = await _unitOfWork.UserRepository.GetUserByUsernameAsync(User.GetUsername());

            var photo = user.Photos.FirstOrDefault(x => x.Id == photoId);

            if (photo.IsMain) return BadRequest("The photo is already a main photo");

            var currentMain = user.Photos.FirstOrDefault(x => x.IsMain);
            if (currentMain != null) currentMain.IsMain = false;
            photo.IsMain = true;

            if (await _unitOfWork.Complete()) return NoContent();

            return BadRequest("Failed to set the image as mail");

        }
예제 #6
0
        public async Task<ActionResult> DeletePhoto(int photoId)
        {
            var user = await _unitOfWork.UserRepository.GetUserByUsernameAsync(User.GetUsername());
            var photo = user.Photos.FirstOrDefault(x => x.Id == photoId);
            if (photo == null) return NotFound();
            if (photo.IsMain) return BadRequest("You cannot delete your main photo");
            if (photo.PublicId != null)
            {
                var result = await _photoService.DeletePhotoAsync(photo.PublicId);
                if (result.Error != null) return BadRequest(result.Error.Message);
            }

            user.Photos.Remove(photo);
            if (await _unitOfWork.Complete()) return Ok();

            return BadRequest("Failed to delete the photo");
        }
예제 #7
0
        public async Task <ActionResult> UpdateUser(MemberUpdateDto memberUpdateDto)
        {
            AppUser user = await _unitOfWork.UserRepository.GetUserByUsernameAsync(User.GetUsername());

            _mapper.Map(memberUpdateDto, user);

            _unitOfWork.UserRepository.Update(user);

            if (await _unitOfWork.Complete())
            {
                return(NoContent());
            }
            else
            {
                return(BadRequest("Failed to update user!"));
            }
        }
예제 #8
0
        public async Task <ActionResult> UpdateUser(MemberUpdateDto memberUpdateDto)
        {
            //var username = User.FindFirst(ClaimTypes.NameIdentifier)?.Value;    //This will find the user's username from the token in the header
            var username = User.GetUsername();
            var user     = await _userRepository.GetUserByUsernameAsync(username);

            _mapper.Map(memberUpdateDto, user);

            _userRepository.Update(user);

            if (await _userRepository.SaveAllAsync())
            {
                return(NoContent());
            }

            return(BadRequest("Failed to update user"));
        }
예제 #9
0
        public async Task <ActionResult <PagedList <MemberDto> > > GetUsers([FromQuery] UserParams userParams)
        {
            var gender = await _unitOfWork.UserRepository.GetUserGender(User.GetUsername());

            userParams.CurrentUsername = User.GetUsername();

            if (string.IsNullOrEmpty(userParams.Gender))
            {
                userParams.Gender = gender == "male" ? "female" : "male";
            }

            var users = await _unitOfWork.UserRepository.GetMembersAsync(userParams);

            Response.AddPaginationHeader(users.CurrentPage, users.PageSize, users.TotalCount, users.TotalPages);

            return(Ok(users));
        }
예제 #10
0
        public async Task <ActionResult> EditUserRoles(string username, EditUserRolesDto editUserRolesDto)
        {
            bool currentUser = username.ToLower() == User.GetUsername();

            if (currentUser && !editUserRolesDto.Roles.Contains("Admin"))
            {
                return(BadRequest("You cannot remove admin role from current user"));
            }

            var user = await _userManager.Users
                       .Include(u => u.UserRoles).ThenInclude(ur => ur.Role)
                       .Where(u => u.NormalizedUserName == username.ToUpper())
                       .SingleOrDefaultAsync();

            if (user == null)
            {
                return(NotFound("Could not find user"));
            }

            var userRoles = user.UserRoles.Select(ur => ur.Role.Name);

            var result = await _userManager.AddToRolesAsync(user, editUserRolesDto.Roles.Except(userRoles));

            if (!result.Succeeded)
            {
                return(BadRequest("Failed to add to roles"));
            }

            result = await _userManager.RemoveFromRolesAsync(user, userRoles.Except(editUserRolesDto.Roles));

            if (!result.Succeeded)
            {
                return(BadRequest("Failed to remove from roles"));
            }

            if (currentUser)
            {
                return(Ok(new UserDto
                {
                    Username = user.UserName,
                    Token = await _tokenService.CreateToken(user)
                }));
            }

            return(NoContent());
        }
예제 #11
0
        public async Task <ActionResult> UpdateUser(MemberUpdateDTO memberUpdateDTO)
        {
            var username     = User.GetUsername();
            var userToUpdate = await _userRepository.GetUserByUsernameAsync(username);

            if (userToUpdate != null)
            {
                var updatedAppUser = _mapper.Map(memberUpdateDTO, userToUpdate);
                _userRepository.Update(updatedAppUser);
                if (await _userRepository.SaveAllAsync())
                {
                    return(Ok());
                }
            }

            return(BadRequest("An error with the update user process has been occurred"));
        }
예제 #12
0
        public async Task <ActionResult> UpdateUser(MemberUpdateDto memberUpdateDto)
        {
            // var username = User.FindFirst(ClaimTypes.NameIdentifier)?.Value;;
            // var user = await _unitOfWork.UserRepository.GetUserByUsernameAsync(username);

            var user = await _unitOfWork.UserRepository.GetUserByUsernameAsync(User.GetUsername());

            _mapper.Map(memberUpdateDto, user);
            _unitOfWork.UserRepository.Update(user);

            if (await _unitOfWork.Complete())
            {
                return(NoContent());
            }

            return(BadRequest("Failed to update user"));
        }
예제 #13
0
        public async Task <ActionResult> UpdateUser(MemberUpdateDto memberUpdateDto)
        {
            // this should give to us username from API TOKEN.
            var user = await _unitOfWork.UserRepository.GetUserByUsernameAsync(User.GetUsername());

            _mapper.Map(memberUpdateDto, user);
            // it will map properties automatically

            _unitOfWork.UserRepository.Update(user);

            if (await _unitOfWork.Complete())
            {
                return(NoContent());
            }

            return(BadRequest("Failed to update user"));
        }
예제 #14
0
        public async Task <ActionResult> UpdateUser(MemberUpdateDto memberUpdateDto)
        {
            var user = await _userRepository.GetUserByUsername(User.GetUsername());



            _mapper.Map(memberUpdateDto, user);

            _userRepository.Update(user);

            if (await _userRepository.SaveAllAsync())
            {
                return(NoContent());
            }

            return(BadRequest("Failed to update user"));
        }
        public async Task <ActionResult <IEnumerable <MemberDto> > > GetUsers([FromQuery] UserParams userParams)
        {
            var user = await _userRepository.GetUserByUsernameAsync(User.GetUsername());

            userParams.CurrentUsername = user.UserName;

            if (string.IsNullOrEmpty(userParams.Gender))
            {
                userParams.Gender = user.Gender == "male" ? "female" : "male";
            }
            // It goes to datacontext class, access the user table and then access the data inside
            // ToListAsync is async method comes from EF Core
            var users = await _userRepository.GetMembersAsync(userParams);

            Response.AddPaginationHeader(users.CurrentPage, users.PageSize, users.TotalCount, users.TotalPages);
            return(Ok(users));
        }
예제 #16
0
        public async Task <ActionResult <IEnumerable <LikeDTO> > > GetUserLikes([FromQuery] UserParams userParams)
        {
            var user = await _userRepository.GetUserByUsernameAsync(User.GetUsername());

            userParams.CurrentUsername = user.UserName;

            var users = await _likesRepository.GetUserLikes(userParams, User.GetUserId());

            Response.AddPaginationHeader(users.CurrentPage, users.PageSize, users.TotalCount, users.TotalPages);

            if (users != null)
            {
                return(Ok(users));
            }

            return(BadRequest("Bad request"));
        }
예제 #17
0
        public async Task <ActionResult <IEnumerable <UserDetailsDto> > > GetUsers([FromQuery] UserParams userParams)
        {
            var user = await repo.GetUserByUsernameAsync(User.GetUsername());

            userParams.CurrentUsername = user.UserName;

            if (string.IsNullOrEmpty(userParams.Gender))
            {
                userParams.Gender = (user.Gender == "male") ? "female" : "male";
            }

            var users = await repo.GetUserDetailsAsync(userParams);

            Response.AddPaginationHeader(users.CurrentPage, users.PageSize, users.TotalCount, users.TotalPages);

            return(Ok(users));
        }
        public async Task <ActionResult> DeleteMessage(int id)
        {
            var username = User.GetUsername();


            var message = await _unitOfWork.MessageRepository.GetMessage(id);

            if (message.Sender.UserName != username && message.Recipient.UserName != username)
            {
                if (message.Sender.UserName != username && message.Recipient.UserName != username)
                {
                    return(Unauthorized());
                }
            }

            if (message.Sender.UserName == username)
            {
                message.SenderDeleted = true;
            }

            if (message.Recipient.UserName == username)
            {
                message.RecipientDeleted = true;
            }

            if (message.SenderDeleted && message.RecipientDeleted)
            {
                _unitOfWork.MessageRepository.DeleteMessage(message);
            }
            if (message.SenderDeleted && message.RecipientDeleted)
            {
                _unitOfWork.MessageRepository.DeleteMessage(message);
            }

            if (await _unitOfWork.Complete())
            {
                return(Ok());
            }
            if (await _unitOfWork.Complete())
            {
                return(Ok());
            }

            return(BadRequest("Problem deleting the message"));
        }
예제 #19
0
        // [AllowAnonymous]
        public async Task <ActionResult <IEnumerable <MemberDto> > > GetUsers([FromQuery] UserParams userParams)
        {
            var gender = await _unitOfWork.UserRepository.GetUserGender(User.GetUsername());

            userParams.CurrentUsername = User.GetUsername();

            if (string.IsNullOrEmpty(userParams.Gender))
            {
                userParams.Gender = gender == "male" ? "female" : "male";
            }

            var users = await _unitOfWork.UserRepository.GetMembersAsync(userParams);

            // in controllers we always have access to our Http response
            Response.AddPaginationHeader(users.CurrentPage, users.PageSize, users.TotalCount, users.TotalPages);

            return(Ok(users));
        }
예제 #20
0
        // public async Task<ActionResult<MessageDto>> CreateCourse(CreateCourseDto createCourseDto)
        public async Task <ActionResult <Semester> > DeleteCourse(int id)
        {
            var username = User.GetUsername();

            var course = await this.unitOfWork.CoursesRepository.GetCourseAsync(id);

            if (course != null)
            {
                this.unitOfWork.CoursesRepository.DeleteCourse(course);
            }

            if (await this.unitOfWork.Complete())
            {
                return(Ok());
            }

            return(BadRequest("Failed to delete course"));
        }
예제 #21
0
        public async Task <ActionResult> updatePassword(NewPassDto newPassDto)
        {
            var user = await _unitOfWork.UserRepository.GetUserByUsernameAsync(User.GetUsername());

            var result = await _userManager.ChangePasswordAsync(user, newPassDto.CurrentPassword, newPassDto.NewPassword);

            if (!result.Succeeded)
            {
                return(Unauthorized("Current password was wrong"));
            }

            if (result.Succeeded)
            {
                return(NoContent());
            }

            return(BadRequest("Failed to update password"));
        }
예제 #22
0
        public async Task <ActionResult <IEnumerable <MemberDto> > > GetUers([FromQuery] UserParams userParams)
        {
            var user = await _unitOfWork.UserRepository.GetUserByUsernameAsync(User.GetUsername());  // get current user, User?

            userParams.CurrentUsername = user.UserName;

            if (string.IsNullOrEmpty(userParams.Gender))
            {
                userParams.Gender = user.Gender == "male" ? "female" : "male";
            }

            var users = await _unitOfWork.UserRepository.GetMembersAsync(userParams);

            // add Pagination info to response header
            Response.AddPaginationHeader(users.CurrentPage, users.PageSize, users.TotalCount, users.TotalPages);

            return(Ok(users));
        }
예제 #23
0
        public async Task <ActionResult <IEnumerable <SearchResultDto> > > Search(string queryString)
        {
            queryString = queryString.Replace(@"%", "");

            var user = await _unitOfWork.UserRepository.GetUserByUsernameAsync(User.GetUsername());

            // Get libraries user has access to
            var libraries = (await _unitOfWork.LibraryRepository.GetLibrariesForUserIdAsync(user.Id)).ToList();

            if (!libraries.Any())
            {
                return(BadRequest("User does not have access to any libraries"));
            }

            var series = await _unitOfWork.SeriesRepository.SearchSeries(libraries.Select(l => l.Id).ToArray(), queryString);

            return(Ok(series));
        }
예제 #24
0
        public async Task <ActionResult> UpdateUser(MemberUpdateDto memberUpdateDto)
        {
            // var claimsIdentity = User.Identity as System.Security.Claims.ClaimsIdentity;
            //var username = claimsIdentity.Name;

            var user = await _userRepository.GetUserByUsernameAsync(User.GetUsername());

            _mapper.Map(memberUpdateDto, user);

            _userRepository.Update(user);

            if (await _userRepository.SaveAllAsync())
            {
                return(NoContent());
            }

            return(BadRequest("Failed to update User"));
        }
        public async Task <ActionResult <IEnumerable <MemberDto> > > GetUsers([FromQuery] UserParams userParams)
        {
            var gender = await _unitOfWork.UserRepository.GetUserGender(User.GetUsername());

            userParams.CurrentUsername = User.GetUsername();

            // to get opposite gender if logged as opposite gender
            // if (string.IsNullOrEmpty(userParams.Gender))
            //     userParams.Gender = gender == student ? all : all;

            // Query is inside this method in usersRepo
            var users = await _unitOfWork.UserRepository.GetMembersAsync(userParams);

            Response.AddPaginationHeader(
                users.CurrentPage, users.PageSize, users.TotalCount, users.TotalPages);

            return(Ok(users));
        }
예제 #26
0
        public async Task <ActionResult <InstrumentDto> > AddInstrument(string newInstrument)
        {
            var user = await _userRepository.GetUserByUsernameAsync(User.GetUsername());

            var instrument = new Instrument
            {
                Name = newInstrument,
            };

            user.PreferredInstruments.Add(instrument);

            if (await _userRepository.SaveAllAsync())
            {
                return(Ok());
            }

            return(BadRequest("There was an error adding this instrument"));
        }
예제 #27
0
        [HttpGet] // get all members
        //cái userParams bên dưới :
        // là object .net tự tạo, với các giá trị ban mặc định, sau đó set các properties với giá trị
        // trong request param.
        // nếu param k có gtri => các giá trị mặc định như trong class UserParams sẽ đc sd
        // vd : k có query => mặc định userParams.PageNumber = 1, userParams.PageSize = 10
        public async Task <ActionResult <IEnumerable <MemberDto> > > GetUsers([FromQuery] UserParams userParams) // formquery : => userParam lay tu query string
        {
            var user = await _userRepository.GetUserByUsernameAsync(User.GetUsername());

            userParams.CurrentUsername = user.UserName;  // gán curentUserName cho userParam object = username trong token lấy ra

            if (string.IsNullOrEmpty(userParams.Gender)) // k cung cap gender trong param
            {
                userParams.Gender = user.Gender == "male" ? "female" : "male";
            }

            var users = await _userRepository.GetMembersAsync(userParams);

            Response.AddPaginationHeader(users.CurrentPage, users.PageSize,
                                         users.TotalCount, users.TotalPages);

            return(Ok(users));
        }
예제 #28
0
        public async Task <ActionResult <IEnumerable <MemberDto> > > GetUsers([FromQuery] UserParams userParams)
        {
            var gender = await _unitOfWork.UserRepository.GetUserGender(User.GetUsername());

            userParams.CurrentUsername = User.GetUsername();
            // this to get the logged in user gender
            if (string.IsNullOrEmpty(userParams.Gender))
            {
                userParams.Gender = gender == "معلم" ? "معلمة" : "معلم";
            }

            var users = await _unitOfWork.UserRepository.GetMembersAsync(userParams);

            Response.AddPaginationHeader(
                users.CurrentPage, users.PageSize, users.TotalCount, users.TotalPages);

            return(Ok(users));
        }
예제 #29
0
        public async Task <ActionResult <MemberDto> > GetUser(string username)
        {
            MemberDto result = null;

            if (User.GetUsername() != username)
            {
                result = await _unitOfWork.UserRepository.GetMemberAsync(username);
            }
            else
            {
                result = await _unitOfWork.UserRepository.GetMyselfAsync(username);
            }
            if (User.GetUsername() != username)
            {
                result.Photos = result.Photos?.ApprovedOnly();
            }
            return(result);
        }
예제 #30
0
        public async Task <ActionResult> UpdateUser(UserUpdateDto userUpdateDto)
        {
            var username = User.GetUsername();
            var user     = await repo.GetUserByUsernameAsync(username);

            mapper.Map(userUpdateDto, user);

            repo.Update(user);

            if (await repo.SaveAllAsync())
            {
                return(NoContent());
            }
            else
            {
                return(BadRequest("Unable to update user."));
            }
        }