Exemple #1
0
        [DataRow("Bad Data")] //TODO: Try to create a more generic data driven unit test, then replace Bad Data
        public void TestUpdateUserWithData(string id)
        {
            //TODO: notice the parameterized test.
            //the id i want to find (our student account)
            // primary key id
            // int id = 7;//display ID

            //************How do I grab a specific student using the ID assigend above*************
            user = _dataAccess._context.ApplicationUser.FirstOrDefault(u => u.Id == id);


            //instance of dataAccess Layer
            SeeSharpLMS.DataAccessLayer.DataAccessClass data = new SeeSharpLMS.DataAccessLayer.DataAccessClass();

            //update user information with this stuff
            data.UpdateUser(user, "Jane1", "Deer", "*****@*****.**", "Hi, I am student!");

            //compare what is supposed to be with user from database
            Assert.AreEqual("Jane1", user.FirstName);
            Assert.AreEqual("Deer", user.LastName);
            Assert.AreEqual("*****@*****.**", user.Email);
            Assert.AreEqual("Hi, I am student!", user.Description);
        }
        public IActionResult OnPost(IFormFile file)
        {
            //var updateUser = _context.ApplicationUser.FirstOrDefault(x => x.Id == user.Id);
            var updateUser = _context.ApplicationUser.FirstOrDefault(u => u.Id == _userManager.GetUserId(User));

            //updateUser.FirstName = Request.Form["EditFirstName"].ToString();
            //updateUser.LastName = Request.Form["EditLastName"].ToString();
            //updateUser.Email = Request.Form["EditEmail"].ToString();
            //updateUser.UserName = Request.Form["EditEmail"].ToString();
            //updateUser.NormalizedEmail = Request.Form["EditEmail"].ToString().ToUpper();
            //updateUser.NormalizedUserName = Request.Form["EditEmail"].ToString().ToUpper();
            //updateUser.Description = Request.Form["EditBio"].ToString();
            //_context.SaveChanges();
            ////var updateLink = new ProfileLink();
            DataAccessLayer.DataAccessClass dbAccess = new DataAccessLayer.DataAccessClass();

            dbAccess.UpdateUser(updateUser, Request.Form["EditFirstName"].ToString(), Request.Form["EditLastName"].ToString(), Request.Form["EditEmail"].ToString(), Request.Form["EditBio"].ToString());


            Links = (_context.ProfileLink.Where(x => x.InstructorId == updateUser.Id)).ToList();

            //If the user has any links, creates a new profile link object
            //then fills that object with a link from the list
            //and updates the links in the database

            //Upload the file
            if (file != null)
            {
                _unitOfWork.UploadImage(file);
                updateUser.ImagePath = file.FileName;
            }
            _context.SaveChanges();


            if (Links.Count > 0)
            {
                //Creates the profile link object.
                var updateLink = new ProfileLink();

                //For each link in the list,
                foreach (var x in Links)
                {
                    //Fill the updatelink object with information from the list.
                    updateLink = _context.ProfileLink.FirstOrDefault(l => l.Id == x.Id);

                    //If the input that matches that link is empty the link is deleted.
                    if (Request.Form[x.Id.ToString()].ToString().Length == 0)
                    {
                        _context.ProfileLink.Remove(updateLink);
                    }
                    //Else the link is updated with new information.
                    else
                    {
                        updateLink.Link = Request.Form[x.Id.ToString()].ToString();
                    }
                    //Save changes to the links.
                    _context.SaveChanges();
                }
            }

            //For each new link input field, adds new links to the database.
            for (int i = 0; i < 5; i++)
            {
                //Create new ProfileLink to add to database.
                var updateLink = new ProfileLink();

                //If the input field is not empty,
                if (Request.Form["NewLinkId" + i].ToString().Length > 0)
                {
                    //assigns the user's id to the InstructorId,
                    updateLink.InstructorId = updateUser.Id;

                    //adds the input field's information to the ProfileLink,
                    updateLink.Link = Request.Form["NewLinkId" + i].ToString();

                    //and adds the new link to the datatable and saves the changes.
                    _context.ProfileLink.Add(updateLink);
                    _context.SaveChanges();
                }
            }
            return(RedirectToPage("/Profile"));
        }