public IHttpActionResult GetById(int?Id)
        {
            if (Id is null)
            {
                return(BadRequest("Id is invalid"));
            }

            var category = DbContext.Categories.FirstOrDefault(p => p.Id == Id);

            if (category is null)
            {
                return(NotFound());
            }

            var categoryView = HouseholdHelpers.MapCategoryToView(category);

            return(Ok(categoryView));
        }
        public IHttpActionResult GetAll(int?Id)
        {
            var userId = User.Identity.GetUserId();

            if (userId is null)
            {
                return(Unauthorized());
            }

            var categories = DbContext.Households.FirstOrDefault(p => p.Id == Id).Categories.ToList();

            if (categories is null)
            {
                return(BadRequest("No Categories for this household"));
            }

            var categoriesView = categories.Select(p => HouseholdHelpers.MapCategoryToView(p));

            return(Ok(categoriesView));
        }
        public IHttpActionResult GetAll()
        {
            var userId = User.Identity.GetUserId();

            if (userId is null)
            {
                return(NotFound());
            }

            var categories = DbContext.Categories.Where(p => p.Household.Members.Any(i => i.Id == userId)).ToList();

            if (categories is null)
            {
                return(NotFound());
            }

            var categoriesView = categories.Select(p => HouseholdHelpers.MapCategoryToView(p));

            return(Ok(categoriesView));
        }
        public IHttpActionResult Create(int?Id, CategoryBindingModel categoryBinding)
        {
            // Id being the Id of the Household
            if (ModelState is null || !ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (DbContext.Households.FirstOrDefault(p => p.Id == Id).Categories.Any(p => p.Name == categoryBinding.Name))
            {
                return(BadRequest("That name is already in use, please choose another."));
            }

            var household = DbContext.Households.FirstOrDefault(p => p.Id == Id);

            if (household is null)
            {
                return(NotFound());
            }

            var category = new Category
            {
                DateCreated = DateTime.Now,
                DateUpdated = null,
                Name        = categoryBinding.Name,
                Description = categoryBinding.Description
            };

            household.Categories.Add(category);
            DbContext.SaveChanges();

            var categoryView = HouseholdHelpers.MapCategoryToView(category);

            return(Created(Url.Link(
                               "GetCategoryById",
                               new { category.Id }),
                           categoryView
                           ));
        }
        public IHttpActionResult Edit(int?Id, CategoryBindingModel categoryBinding)
        {
            if (ModelState is null || !ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var category = DbContext.Categories.FirstOrDefault(p => p.Id == Id);

            if (category is null)
            {
                return(NotFound());
            }

            category.DateUpdated = DateTime.Now;
            category.Name        = categoryBinding.Name;
            category.Description = categoryBinding.Description;
            DbContext.SaveChanges();

            var categoryView = HouseholdHelpers.MapCategoryToView(category);

            return(Ok(categoryView));
        }