Пример #1
0
        public LoginResponseDTO GoogleLogin(UserLoginViewModel user)
        {
            LoginResponseDTO result = null;

            #region Check Input
            if (user == null)
            {
                result = new LoginResponseDTO
                {
                    Status  = 1,
                    Message = "User Login information must not be null",
                    Result  = null
                };
                return(result);
            }

            if (user.RoleName != "mentor" && user.RoleName != "mentee")
            {
                result = new LoginResponseDTO
                {
                    Status  = 2,
                    Message = "User missing required role",
                    Result  = null
                };
                return(result);
            }
            #endregion

            #region Check User
            User loggedUser = _uow
                              .GetRepository <User>()
                              .GetAll()
                              .SingleOrDefault(u => u.Email == user.Email);

            if (loggedUser == null)
            {
                result = new LoginResponseDTO
                {
                    Status  = 3,
                    Result  = null,
                    Message = "Email does not exist ! Please Register !"
                };
                return(result);
            }
            #endregion

            #region Check Role

            if (user.RoleName == "mentor")
            {
                Mentor mentor = _uow
                                .GetRepository <Mentor>()
                                .GetAll()
                                .SingleOrDefault(m => m.UserId == loggedUser.UserId);
                if (mentor == null)
                {
                    result = new LoginResponseDTO
                    {
                        Status  = 4,
                        Result  = null,
                        Message = "This user is not a Mentor."
                    };
                    return(result);
                }
            }
            else if (user.RoleName == "mentee")
            {
                Mentee mentee = _uow
                                .GetRepository <Mentee>()
                                .GetAll()
                                .SingleOrDefault(m => m.UserId == loggedUser.UserId);
                if (mentee == null)
                {
                    result = new LoginResponseDTO
                    {
                        Status  = 4,
                        Result  = null,
                        Message = "This user is not a Mentee."
                    };
                    return(result);
                }
            }

            #endregion

            string jwtToken = tokenManager.CreateUserAccessToken(new UserAuthModel
            {
                Id       = loggedUser.UserId,
                Email    = loggedUser.Email,
                RoleName = user.RoleName
            });

            result = new LoginResponseDTO
            {
                Status  = 0,
                Message = "Login Successfully.",
                Result  = jwtToken
            };

            return(result);
        }
Пример #2
0
        public BaseResponseDto Insert(MentorInsertModel mentorInsertModel)
        {
            BaseResponseDto responseDto = null;

            #region Check input
            if (mentorInsertModel == null)
            {
                responseDto = new BaseResponseDto
                {
                    Status  = 1,
                    Message = "Faulthy mentor info"
                };
                return(responseDto);
            }
            #endregion

            #region Insert mentor into database
            Guid userId   = Guid.NewGuid();
            Guid mentorId = Guid.NewGuid();
            try
            {
                Mentor newMentor = new Mentor
                {
                    MentorId = mentorId,
                    UserId   = userId,
                    User     = new User
                    {
                        UserId      = userId,
                        Email       = mentorInsertModel.User.Email,
                        Fullname    = mentorInsertModel.User.Fullname,
                        YearOfBirth = mentorInsertModel.User.YearOfBirth,
                        AvatarUrl   = mentorInsertModel.User.AvatarUrl,
                        Balance     = 0,
                        Phone       = mentorInsertModel.User.Phone,
                        Description = ""
                    },
                    Rating = new Rating
                    {
                        MentorId    = mentorId,
                        RatingCount = 0,
                        RatingScore = 0
                    },
                    IsDisable = false
                };

                _uow.GetRepository <Mentor>().Insert(newMentor);
                _uow.Commit();
            }
            catch (Exception e)
            {
                throw e;
            }
            #endregion


            string jwtToken = tokenManager.CreateUserAccessToken(new UserAuthModel
            {
                Id       = mentorId,
                Email    = mentorInsertModel.User.Email,
                RoleName = "mentor"
            });


            responseDto = new BaseResponseDto
            {
                Status  = 0,
                Message = jwtToken
            };

            return(responseDto);
        }