Ejemplo n.º 1
0
        /// <inheritdoc />
        public async Task <IPipelineProcess <Group> > Process(IPipelineProcess <Group> input,
                                                              IMvcForumContext context)
        {
            _localizationService.RefreshContext(context);
            _groupService.RefreshContext(context);

            try
            {
                Guid?parentGroupGuid = null;
                if (input.ExtendedData.ContainsKey(Constants.ExtendedDataKeys.ParentGroup))
                {
                    parentGroupGuid = input.ExtendedData[Constants.ExtendedDataKeys.ParentGroup] as Guid?;
                }

                // Sort if this is a section
                Section section = null;
                if (input.ExtendedData.ContainsKey(Constants.ExtendedDataKeys.Section))
                {
                    if (input.ExtendedData[Constants.ExtendedDataKeys.Section] is Guid guid)
                    {
                        section = _groupService.GetSection(guid);
                    }
                }

                // Sort the section - If it's null remove it
                input.EntityToProcess.Section = section ?? null;

                var isEdit = false;
                if (input.ExtendedData.ContainsKey(Constants.ExtendedDataKeys.IsEdit))
                {
                    isEdit = input.ExtendedData[Constants.ExtendedDataKeys.IsEdit] as bool? == true;
                }

                if (isEdit)
                {
                    var parentCat = parentGroupGuid != null
                        ? _groupService.Get(parentGroupGuid.Value)
                        : null;

                    // Check they are not trying to add a subGroup of this Group as the parent or it will break
                    if (parentCat?.Path != null && parentGroupGuid != null)
                    {
                        var parentCats = parentCat.Path.Split(',').Where(x => !string.IsNullOrWhiteSpace(x))
                                         .Select(x => new Guid(x)).ToList();
                        if (parentCats.Contains(input.EntityToProcess.Id))
                        {
                            // Remove the parent Group, but still let them create the catgory
                            parentGroupGuid = null;
                        }
                    }

                    if (parentGroupGuid != null)
                    {
                        // Set the parent Group
                        var parentGroup = _groupService.Get(parentGroupGuid.Value);
                        input.EntityToProcess.Parent_GroupId = parentGroup;

                        // Append the path from the parent Group
                        _groupService.SortPath(input.EntityToProcess, parentGroup);
                    }
                    else
                    {
                        // Must access property (trigger lazy-loading) before we can set it to null (Entity Framework bug!!!)
                        var triggerEfLoad = input.EntityToProcess.Parent_GroupId;
                        input.EntityToProcess.Parent_GroupId = null;

                        // Also clear the path
                        input.EntityToProcess.Path = null;
                    }

                    Guid user = new Guid();
                    _groupService.UpdateSlugFromName(input.EntityToProcess, user);
                    try
                    {
                        await context.SaveChangesAsync();
                    }
                    catch (DbUpdateConcurrencyException)
                    {
                        return(null);
                    }
                }
                else
                {
                    if (parentGroupGuid != null)
                    {
                        var parentGroup = await context.Group.FirstOrDefaultAsync(x => x.Id == parentGroupGuid.Value);

                        input.EntityToProcess.Parent_GroupId = parentGroup;
                        _groupService.SortPath(input.EntityToProcess, parentGroup);
                    }


                    // url slug generator
                    input.EntityToProcess.Slug = ServiceHelpers.GenerateSlug(input.EntityToProcess.Name,
                                                                             _groupService.GetBySlugLike(ServiceHelpers.CreateUrl(input.EntityToProcess.Name)).Select(x => x.Slug).ToList(), null);

                    // Add the Group
                    context.Group.Add(input.EntityToProcess);
                }

                try
                {
                    await context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    return(null);
                }

                _cacheService.ClearStartsWith("GroupList");
            }
            catch (Exception ex)
            {
                input.AddError(ex.Message);
                _loggingService.Error(ex);
            }

            return(input);
        }