public async Task <CategoryGroupsModel> PostCategoryGroup(CategoryGroupsModel categoryGroupsModel, long userId, long categoryModelId)
        {
            var userHasCategory = await _userContext.Categories.AnyAsync(u =>
                                                                         u.Id == categoryModelId && u.LoginModelId == userId);

            if (!userHasCategory)
            {
                return(null);
            }

            await _userContext.CategoryGroups.AddAsync(categoryGroupsModel);

            if (await _userContext.SaveChangesAsync() == 0)
            {
                return(null);
            }

            return(categoryGroupsModel);
        }
Beispiel #2
0
        public async Task <ActionResult> PostCategoryGroup([FromBody] CategoryGroupPostedModel postedItem)
        {
            _iLogger.LogInformation("Called Categories.PostCategoryGroup");

            var userId = this.User.GetUserId();

            if (userId == null)
            {
                _iLogger.LogWarning("Claim identity could not be found");
                return(BadRequest("Bad request"));
            }

            CategoryGroupsModel categoryGroupsModel = new CategoryGroupsModel()
            {
                Name            = postedItem.Name,
                Order           = Convert.ToInt64(postedItem.Order),
                CategoryModelId = Convert.ToInt64(postedItem.CategoryId)
            };


            if (categoryGroupsModel == null)
            {
                _iLogger.LogWarning("Unable to create CategoryItemsModel from {0]", postedItem);
                return(BadRequest("Unable to parse data"));
            }


            var postResponse = await _categoriesRepository.PostCategoryGroup(categoryGroupsModel, (long)userId,
                                                                             Convert.ToInt64(postedItem.CategoryId));

            if (postResponse == null)
            {
                _iLogger.LogWarning("Error when posting");
                return(BadRequest("Unable to add item"));
            }

            _iLogger.LogInformation("Post successful");

            return(Ok(postResponse));
        }
Beispiel #3
0
        public void Seed()
        {
            var user         = _userContext.LoginModel.FirstOrDefault(m => m.UserName == "johndoe");
            var passwordSalt = _passwordHasher.GetSalt();

            if (user == null)
            {
                user = new LoginModel()
                {
                    UserName               = "******",
                    Password               = _passwordHasher.GetHashedSaltedPassword("JohnDoePassword", passwordSalt),
                    PasswordSalt           = passwordSalt,
                    RefreshToken           = _iTokenService.GenerateRefreshToken(),
                    RefreshTokenExpiryTime = _iTokenService.RefreshTokenTime
                };
                _userContext.Add(user);
                _userContext.SaveChanges();
            }

            var templateOwnerUser = _userContext.LoginModel.FirstOrDefault(m => m.UserName == "TemplateOwnerUser");
            var passwordSalt2     = _passwordHasher.GetSalt();

            if (templateOwnerUser == null)
            {
                templateOwnerUser = new LoginModel()
                {
                    UserName               = "******",
                    Password               = _passwordHasher.GetHashedSaltedPassword("TemplateOwnerUserPassword", passwordSalt2),
                    PasswordSalt           = passwordSalt2,
                    RefreshToken           = _iTokenService.GenerateRefreshToken(),
                    RefreshTokenExpiryTime = _iTokenService.RefreshTokenTime
                };
                _userContext.Add(templateOwnerUser);
                _userContext.SaveChanges();
            }
            var usercategory = _userContext.Categories
                               .Include(u => u.LoginModel)
                               .FirstOrDefault(u => u.LoginModelId == user.Id);

            if (usercategory == null)
            {
                usercategory = new CategoryModel()
                {
                    Name         = "films",
                    LoginModelId = user.Id
                };
                _userContext.Add(usercategory);
                _userContext.SaveChanges();

                var filmsId = usercategory.Id;


                var userCategoryGroups = _userContext.CategoryGroups
                                         .Include(u => u.CategoryModel)
                                         .FirstOrDefault(u => u.CategoryModelId == filmsId);

                long userCategoryGroupsId;

                if (userCategoryGroups == null)
                {
                    userCategoryGroups = new CategoryGroupsModel()
                    {
                        CategoryModel   = usercategory,
                        CategoryModelId = usercategory.Id,
                        Name            = "Horror",
                        Order           = 1
                    };

                    _userContext.Add(userCategoryGroups);
                    _userContext.SaveChanges();
                }

                userCategoryGroupsId = userCategoryGroups.Id;

                var usercategoryitems = _userContext.CategoryItems
                                        .Include(u => u.CategoryGroupsModel)
                                        .ThenInclude(u => u.CategoryModel)
                                        .FirstOrDefault(u => u.CategoryGroupsModel.CategoryModel.Id == filmsId);

                if (usercategoryitems == null)
                {
                    usercategoryitems = new CategoryItemsModel()
                    {
                        Name                = "IT",
                        Order               = 1,
                        Comment             = "Jump scares!",
                        CategoryGroupsId    = userCategoryGroupsId,
                        CategoryGroupsModel = userCategoryGroups
                    };
                    _userContext.Add(usercategoryitems);
                    //userContext.SaveChanges();

                    usercategoryitems = new CategoryItemsModel()
                    {
                        Name             = "Hereditary",
                        Order            = 2,
                        Comment          = "Psychological",
                        CategoryGroupsId = userCategoryGroupsId
                    };
                    _userContext.Add(usercategoryitems);
                    _userContext.SaveChanges();
                }
            }
        }