Exemplo n.º 1
0
        /// <summary>
        /// Tries to delete the category while respecting constraints
        /// </summary>
        public async Task <TaskResult> TryDeleteAsync(ValourDB db)
        {
            var planet = await GetPlanetAsync();

            if (await db.PlanetCategories.CountAsync(x => x.Planet_Id == Planet_Id) < 2)
            {
                return(new TaskResult(false, "Last category cannot be deleted"));
            }

            var childCategoryCount = await db.PlanetCategories.CountAsync(x => x.Parent_Id == Id);

            var childChannelCount = await db.PlanetChatChannels.CountAsync(x => x.Parent_Id == Id);

            if (childCategoryCount != 0 || childChannelCount != 0)
            {
                return(new TaskResult(false, "Category must be empty"));
            }

            // Remove permission nodes

            db.CategoryPermissionsNodes.RemoveRange(
                db.CategoryPermissionsNodes.Where(x => x.Category_Id == Id)
                );

            // Remove category
            db.PlanetCategories.Remove(
                await db.PlanetCategories.FindAsync(Id)
                );

            // Save changes
            await db.SaveChangesAsync();

            // Notify of update
            await PlanetHub.NotifyCategoryDeletion(this);

            return(new TaskResult(true, "Success"));
        }