Esempio n. 1
0
        public async Task <IActionResult> Create(InputModels.CreateBoardInput input)
        {
            if (ModelState.IsValid)
            {
                var serviceResponse = BoardRepository.AddBoard(input);
                return(await ForumViewResult.RedirectFromService(this, serviceResponse, FailureCallback));
            }

            return(await FailureCallback());

            async Task <IActionResult> FailureCallback()
            {
                var viewModel = new PageViewModels.CreatePage()
                {
                    Categories = BoardRepository.CategoryPickList()
                };

                viewModel.Name        = input.Name;
                viewModel.Description = input.Description;

                if (!string.IsNullOrEmpty(input.Category))
                {
                    viewModel.Categories.First(item => item.Value == input.Category).Selected = true;
                }

                return(await Task.Run(() => { return ForumViewResult.ViewResult(this, viewModel); }));
            }
        }
Esempio n. 2
0
        public async Task <ServiceModels.ServiceResponse> AddBoard(InputModels.CreateBoardInput input)
        {
            var serviceResponse = new ServiceModels.ServiceResponse();

            if ((await Records()).Any(b => b.Name == input.Name))
            {
                serviceResponse.Error(nameof(input.Name), "A board with that name already exists");
            }

            DataModels.Category categoryRecord = null;

            if (!string.IsNullOrEmpty(input.NewCategory))
            {
                input.NewCategory = input.NewCategory.Trim();
            }

            if (!string.IsNullOrEmpty(input.NewCategory))
            {
                categoryRecord = (await Categories()).FirstOrDefault(c => c.Name == input.NewCategory);

                if (categoryRecord is null)
                {
                    var displayOrder = (await Categories()).DefaultIfEmpty().Max(c => c.DisplayOrder);

                    categoryRecord = new DataModels.Category {
                        Name         = input.NewCategory,
                        DisplayOrder = displayOrder + 1
                    };

                    DbContext.Categories.Add(categoryRecord);
                }
            }
            else
            {
                try {
                    var categoryId = Convert.ToInt32(input.Category);
                    categoryRecord = (await Categories()).First(c => c.Id == categoryId);

                    if (categoryRecord is null)
                    {
                        serviceResponse.Error(nameof(input.Category), "No category was found with this ID.");
                    }
                }
                catch (FormatException) {
                    serviceResponse.Error(nameof(input.Category), "Invalid category ID");
                }
            }

            if (!string.IsNullOrEmpty(input.Name))
            {
                input.Name = input.Name.Trim();
            }

            if (string.IsNullOrEmpty(input.Name))
            {
                serviceResponse.Error(nameof(input.Name), "Name is a required field.");
            }

            if (!string.IsNullOrEmpty(input.Description))
            {
                input.Description = input.Description.Trim();
            }

            var existingRecord = (await Records()).FirstOrDefault(b => b.Name == input.Name);

            if (existingRecord != null)
            {
                serviceResponse.Error(nameof(input.Name), "A board with that name already exists");
            }

            if (!serviceResponse.Success)
            {
                return(serviceResponse);
            }

            DbContext.SaveChanges();

            var record = new DataModels.Board {
                Name        = input.Name,
                Description = input.Description,
                CategoryId  = categoryRecord.Id
            };

            DbContext.Boards.Add(record);

            DbContext.SaveChanges();

            serviceResponse.RedirectPath = UrlHelper.Action(nameof(Controllers.Boards.Manage), nameof(Controllers.Boards), new { id = record.Id });

            return(serviceResponse);
        }