コード例 #1
0
ファイル: CategoryAPI.cs プロジェクト: Coca162/Valour
        private static async Task InsertItem(HttpContext ctx, ValourDB db, ulong category_id,
                                             [FromHeader] string authorization)
        {
            AuthToken auth = await ServerAuthToken.TryAuthorize(authorization, db);

            if (auth == null)
            {
                ctx.Response.StatusCode = 401;
                await ctx.Response.WriteAsync($"Token is invalid [token: {authorization}]");

                return;
            }

            ServerPlanetCategory category = await db.PlanetCategories.Include(x => x.Planet)
                                            .ThenInclude(x => x.Members.Where(x => x.User_Id == auth.User_Id))
                                            .FirstOrDefaultAsync(x => x.Id == category_id);

            if (category == null)
            {
                ctx.Response.StatusCode = 400;
                await ctx.Response.WriteAsync($"Category not found [id: {category_id}]");

                return;
            }

            var member = category.Planet.Members.FirstOrDefault();

            if (member == null)
            {
                ctx.Response.StatusCode = 401;
                await ctx.Response.WriteAsync("Member not found");

                return;
            }

            if (!await category.HasPermission(member, CategoryPermissions.View, db))
            {
                ctx.Response.StatusCode = 401;
                await ctx.Response.WriteAsync("Member lacks CategoryPermissions.View");

                return;
            }

            if (!auth.HasScope(UserPermissions.PlanetManagement))
            {
                ctx.Response.StatusCode = 401;
                await ctx.Response.WriteAsync($"Token lacks UserPermissions.PlanetManagement");

                return;
            }

            if (!await category.HasPermission(member, CategoryPermissions.ManageCategory, db))
            {
                ctx.Response.StatusCode = 401;
                await ctx.Response.WriteAsync("Member lacks CategoryPermissions.ManageCategory");

                return;
            }

            ChannelListItem in_item = await JsonSerializer.DeserializeAsync <ChannelListItem>(ctx.Request.Body);

            if (in_item == null || in_item.Planet_Id == 0)
            {
                ctx.Response.StatusCode = 400;
                await ctx.Response.WriteAsync("Include item data.");

                return;
            }

            IServerChannelListItem item = await IServerChannelListItem.FindAsync(in_item.ItemType, in_item.Id, db);

            if (item == null)
            {
                ctx.Response.StatusCode = 400;
                await ctx.Response.WriteAsync($"Item not found [id: {in_item.Id}]");

                return;
            }

            ServerPlanet item_planet = await db.Planets.FindAsync(item.Planet_Id);

            if (item_planet == null)
            {
                ctx.Response.StatusCode = 400;
                await ctx.Response.WriteAsync($"Item planet not found [id: {in_item.Planet_Id}]");

                return;
            }

            if (item_planet.Id != category.Planet_Id)
            {
                ctx.Response.StatusCode = 400;
                await ctx.Response.WriteAsync($"Item belongs to different planet");

                return;
            }

            if (item.Parent_Id == category.Id)
            {
                ctx.Response.StatusCode = 200;
                await ctx.Response.WriteAsync($"No change");

                return;
            }

            // Ensure that if this is a category, it is not going into a category that contains itself!
            if (item.ItemType == ItemType.Category)
            {
                ulong?parent_id = category.Parent_Id;

                while (parent_id != null)
                {
                    // Recursion is a nono
                    if (parent_id == item.Id)
                    {
                        ctx.Response.StatusCode = 400;
                        await ctx.Response.WriteAsync("Operation would result in recursion.");

                        return;
                    }

                    parent_id = (await db.PlanetCategories.FindAsync(parent_id)).Parent_Id;
                }
            }

            item.Parent_Id = category.Id;
            item.Position  = in_item.Position;

            db.Update(item);
            await db.SaveChangesAsync();

            item.NotifyClientsChange();

            ctx.Response.StatusCode = 200;
            await ctx.Response.WriteAsync("Success");

            return;
        }
コード例 #2
0
ファイル: CategoryAPI.cs プロジェクト: Coca162/Valour
        private static async Task SetChildOrder(HttpContext ctx, ValourDB db, ulong category_id,
                                                [FromHeader] string authorization)
        {
            AuthToken auth = await ServerAuthToken.TryAuthorize(authorization, db);

            if (auth == null)
            {
                ctx.Response.StatusCode = 401;
                await ctx.Response.WriteAsync($"Token is invalid [token: {authorization}]");

                return;
            }

            ServerPlanetCategory category = await db.PlanetCategories.Include(x => x.Planet)
                                            .ThenInclude(x => x.Members.Where(x => x.User_Id == auth.User_Id))
                                            .FirstOrDefaultAsync(x => x.Id == category_id);

            if (category == null)
            {
                ctx.Response.StatusCode = 400;
                await ctx.Response.WriteAsync($"Category not found [id: {category_id}]");

                return;
            }

            var member = category.Planet.Members.FirstOrDefault();

            if (member == null)
            {
                ctx.Response.StatusCode = 401;
                await ctx.Response.WriteAsync("Member not found");

                return;
            }

            if (!await category.HasPermission(member, CategoryPermissions.View, db))
            {
                ctx.Response.StatusCode = 401;
                await ctx.Response.WriteAsync("Member lacks CategoryPermissions.View");

                return;
            }

            if (!auth.HasScope(UserPermissions.PlanetManagement))
            {
                ctx.Response.StatusCode = 401;
                await ctx.Response.WriteAsync($"Token lacks UserPermissions.PlanetManagement");

                return;
            }

            if (!await category.HasPermission(member, CategoryPermissions.ManageCategory, db))
            {
                ctx.Response.StatusCode = 401;
                await ctx.Response.WriteAsync("Member lacks CategoryPermissions.ManageCategory");

                return;
            }

            string body = await ctx.Request.ReadBodyStringAsync();

            if (string.IsNullOrEmpty(body))
            {
                ctx.Response.StatusCode = 400;
                await ctx.Response.WriteAsync("Include order data.");

                return;
            }

            List <CategoryContentData> orderData = JsonSerializer.Deserialize <List <CategoryContentData> >(body);

            if (orderData == null || orderData.Count == 0)
            {
                ctx.Response.StatusCode = 400;
                await ctx.Response.WriteAsync("Include order data.");

                return;
            }

            List <IServerChannelListItem> changed = new List <IServerChannelListItem>();

            foreach (CategoryContentData order in orderData)
            {
                IServerChannelListItem item = await IServerChannelListItem.FindAsync(order.ItemType, order.Id, db);

                if (item == null)
                {
                    ctx.Response.StatusCode = 400;
                    await ctx.Response.WriteAsync($"Item with id {order.Id} not found");

                    return;
                }

                if (item.Planet_Id != category.Planet_Id)
                {
                    ctx.Response.StatusCode = 400;
                    await ctx.Response.WriteAsync($"Item with id {order.Id} belongs to wrong planet {item.Planet_Id}");

                    return;
                }

                // Only act if there is a difference
                if (item.Parent_Id != category_id || item.Position != order.Position)
                {
                    // Prevent putting an item inside of itself
                    if (item.Id != category_id)
                    {
                        item.Parent_Id = category_id;
                        item.Position  = order.Position;
                        db.Update(item);
                        changed.Add(item);
                    }
                }
            }

            // If all is successful, save and send updates
            foreach (var item in changed)
            {
                // Send update to clients
                item.NotifyClientsChange();
            }

            await db.SaveChangesAsync();

            ctx.Response.StatusCode = 200;
            await ctx.Response.WriteAsync("Success");

            return;
        }