Example #1
0
        public async Task <IActionResult> AddSection(string sectionName, int workspaceId)
        {
            var workspace = await _workspaceRepository.GetByIdAsync(workspaceId);

            if (User.FindFirst(Constants.SuperAdminClaim) is not null ||
                User.HasClaim(x => x.Type == Constants.AdminClaim && x.Value == workspace.Guid.ToString()))
            {
                if (string.IsNullOrWhiteSpace(sectionName))
                {
                    return(BadRequest("Name cannot be blank"));
                }

                bool nameCollision = workspace.Sections.Select(x => x.Name).Contains(sectionName);

                if (nameCollision)
                {
                    string errorMessage = $"Section named '{sectionName}' already exists";
                    return(BadRequest(errorMessage));
                }

                var section = new Section {
                    Name = sectionName, Workspace = workspace
                };

                await _sectionRepository.CreateAsync(section);

                return(Ok());
            }
            return(Unauthorized());
        }
        public async Task <ActionResult> CreateSection(CreateSectionViewModel createSectionViewModel)
        {
            var products = await productRepository.GetByIdsAsync(createSectionViewModel.ProductIds);

            if (products == null || products.Count != createSectionViewModel.ProductIds.Count)
            {
                return(BadRequest(new ValidationErrors("Not all products were found.")));
            }

            await sectionRepository.CreateAsync(createSectionViewModel.Title, products);

            return(NoContent());
        }