示例#1
0
        public async Task <OperationOutput <UserDetails> > GetUserByHandle(string userHandle)
        {
            var user = await dbContext.UserDetails.FirstOrDefaultAsync(u => u.UserHandle.Equals(userHandle));

            if (user == null)
            {
                return(OperationOutput <UserDetails> .Failed("User with handle not found"));
            }
            return(OperationOutput <UserDetails> .Success(user));
        }
示例#2
0
        public async Task <OperationOutput <UserDetails> > AddUserDetails(UserDetails userDetails)
        {
            if (dbContext.UserDetails.Any(ud => ud.UserHandle.Equals(userDetails.UserHandle)))
            {
                return(OperationOutput <UserDetails> .Failed("User exisist with the given handle"));
            }

            dbContext.UserDetails.Add(userDetails);
            await dbContext.SaveChangesAsync();

            return(OperationOutput <UserDetails> .Success(userDetails));
        }
示例#3
0
        /// <summary>
        /// Add a new user to the system
        /// </summary>
        /// <param name="userModel"> user data</param>
        /// <returns>Added user</returns>
        public async Task <OperationOutput <ApplicationUser> > AddUser(ApplicationUserModel userModel)
        {
            if (userModel == null)
            {
                return(OperationOutput <ApplicationUser> .Failed("Arguments Null"));
            }

            if (String.IsNullOrWhiteSpace(userModel.Email) || String.IsNullOrWhiteSpace(userModel.UserName))
            {
                return(OperationOutput <ApplicationUser> .Failed("Arguments Invalid"));
            }

            try
            {
                if (dbContext.Users.Any(u => u.Email.Equals(userModel.Email)))
                {
                    return(OperationOutput <ApplicationUser> .Failed("User Exists"));
                }

                var user = new ApplicationUser
                {
                    Id             = Guid.NewGuid(),
                    UserName       = userModel.UserName,
                    Email          = userModel.Email,
                    PasswordHash   = Encoding.UTF8.GetString(MySHA256.ComputeHash(Encoding.UTF8.GetBytes(userModel.Password + "MySecret"))),
                    Gender         = userModel.Gender,
                    ProfilePicture = new Image
                    {
                        Id  = Guid.NewGuid(),
                        Url = userModel.Gender == Gender.Male ? "/images/profile_m.png" : "/images/profile_f.png"
                    }
                };

                dbContext.Users.Add(user);
                await dbContext.SaveChangesAsync();

                return(OperationOutput <ApplicationUser> .Success(user));
            }
            catch (Exception ex)
            {
                return(OperationOutput <ApplicationUser> .Failed("Internal Server Error", ex.Message));
            }
        }