コード例 #1
0
        public async Task <IActionResult> Edit(InputModels.EditBoardInput input)
        {
            if (ModelState.IsValid)
            {
                var serviceResponse = BoardRepository.UpdateBoard(input);
                return(await ForumViewResult.RedirectFromService(this, serviceResponse, FailureCallback));
            }

            return(await FailureCallback());

            async Task <IActionResult> FailureCallback()
            {
                var boardRecord = BoardRepository.First(b => b.Id == input.Id);

                var viewModel = new PageViewModels.EditPage {
                    Id         = boardRecord.Id,
                    Categories = BoardRepository.CategoryPickList(),
                    Roles      = RoleRepository.PickList(boardRecord.Id)
                };

                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); }));
            }
        }
コード例 #2
0
        public async Task <ServiceModels.ServiceResponse> UpdateBoard(InputModels.EditBoardInput input)
        {
            var serviceResponse = new ServiceModels.ServiceResponse();

            var record = (await Records()).FirstOrDefault(b => b.Id == input.Id);

            if (record is null)
            {
                serviceResponse.Error($"A record does not exist with ID '{input.Id}'");
            }

            DataModels.Category newCategoryRecord = null;

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

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

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

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

                    DbContext.Categories.Add(newCategoryRecord);
                    DbContext.SaveChanges();
                }
            }
            else
            {
                try {
                    var newCategoryId = Convert.ToInt32(input.Category);
                    newCategoryRecord = (await Categories()).FirstOrDefault(c => c.Id == newCategoryId);

                    if (newCategoryRecord 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();
            }

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

            record.Name        = input.Name;
            record.Description = input.Description;

            var oldCategoryId = -1;

            if (record.CategoryId != newCategoryRecord.Id)
            {
                var categoryBoards = (await Records()).Where(r => r.CategoryId == record.CategoryId).ToList();

                if (categoryBoards.Count() <= 1)
                {
                    oldCategoryId = record.CategoryId;
                }

                record.CategoryId = newCategoryRecord.Id;
            }

            var boardRoles = (from role in await RoleRepository.BoardRoles()
                              where role.BoardId == record.Id
                              select role).ToList();

            foreach (var boardRole in boardRoles)
            {
                DbContext.BoardRoles.Remove(boardRole);
            }

            if (input.Roles != null)
            {
                var roleIds = (from role in await RoleRepository.SiteRoles()
                               select role.Id).ToList();

                foreach (var inputRole in input.Roles)
                {
                    if (roleIds.Contains(inputRole))
                    {
                        DbContext.BoardRoles.Add(new DataModels.BoardRole {
                            BoardId = record.Id,
                            RoleId  = inputRole
                        });
                    }
                    else
                    {
                        serviceResponse.Error($"Role does not exist with id '{inputRole}'");
                    }
                }
            }

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

            DbContext.Update(record);
            DbContext.SaveChanges();

            if (oldCategoryId >= 0)
            {
                var oldCategoryRecord = (await Categories()).FirstOrDefault(item => item.Id == oldCategoryId);

                if (oldCategoryRecord != null)
                {
                    DbContext.Categories.Remove(oldCategoryRecord);
                    DbContext.SaveChanges();
                }
            }

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

            return(serviceResponse);
        }