public async Task<IHttpActionResult> PutUserPreference(UserPreferenceDTO userPrefDto)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }
            if (userPrefDto == null)
            {
                return BadRequest("Null object");
            }
            if (userPrefDto.Categories == null)
            {
                return BadRequest();    // DataAnnotations is not supported in PLC :-(
            }

            var userId = User.Identity.GetUserId();

            var categories = db.Categories
                .Where(x => userPrefDto.Categories.Contains(x.Name))
                .ToList();

            if (categories.Count != userPrefDto.Categories.Count)
            {
                // Client sent a bad category name
                return BadRequest("Invalid category name");
            }

            UserPreference userPreference = new UserPreference
            {
                ApplicationUser_Id = userId,
                ConversationLimit = userPrefDto.ConversationLimit,
                SortOrder = userPrefDto.SortOrder,
                UserCategory = new List<UserCategory>()
            };

            if (UserPreferenceExists(userId))
            {
                db.UserPreferences.Attach(userPreference);
                db.Entry(userPreference).State = EntityState.Modified;
                db.Entry(userPreference).Collection(x => x.UserCategory).Load();

                userPreference.UserCategory.Clear();
                foreach (var c in categories)
                {
                    userPreference.UserCategory.Add(new UserCategory { Category = c, ApplicationUser_Id = userId });
                }
            }
            else
            {
                foreach (var c in categories)
                {
                    userPreference.UserCategory.Add(new UserCategory { Category = c, ApplicationUser_Id = userId });
                }
                db.UserPreferences.Add(userPreference);
            }

            await db.SaveChangesAsync();

            return StatusCode(HttpStatusCode.NoContent);
        }
        public async Task GetReturnsUserPreferences()
        {
            var child = new UserCategory() { Category = new Category() { Name = "CAT1" } };
            var parent = new UserPreference
            {
                ApplicationUser_Id = "123",
                UserCategory = new List<UserCategory>()
            };
            parent.UserCategory.Add(child);

            var data = new List<UserPreference> 
            { 
                parent
            }.AsQueryable();

            var childData = new List<UserCategory>
            {
                child
            }.AsQueryable();

            // Mock DbSet
            var mockSet = Helpers.CreateMockSet(data);
            var mockChildSet = Helpers.CreateMockSet(childData);

            mockCtx.SetupGet(mc => mc.UserPreferences).Returns(mockSet.Object);
            mockCtx.SetupGet(mc => mc.UserCategories).Returns(mockChildSet.Object);

            IHttpActionResult actionResult = await controller.GetUserPreferences();
            var contentResult = actionResult as OkNegotiatedContentResult<UserPreferenceDTO>;

            Assert.IsNotNull(contentResult);
            Assert.IsNotNull(contentResult.Content);

            var dto = contentResult.Content;
            Assert.IsNotNull(dto.Categories);
            Assert.AreEqual("CAT1", dto.Categories.First());
        }