using System; using System.Linq; using BililiveRecorder.Core; using BililiveRecorder.Core.Config.V3; using BililiveRecorder.Web.Models; using BililiveRecorder.Web.Models.Graphql; using GraphQL; using GraphQL.Types; namespace BililiveRecorder.Web.Graphql { internal class RecorderQuery : ObjectGraphType { private readonly IRecorder recorder; public RecorderQuery(IRecorder recorder) { this.recorder = recorder ?? throw new ArgumentNullException(nameof(recorder)); this.SetupFields(); } private void SetupFields() { this.Field("version", resolve: context => RecorderVersion.Instance); this.Field("config", resolve: context => this.recorder.Config.Global); this.Field("defaultConfig", resolve: context => DefaultConfig.Instance); this.Field>("rooms", arguments: new QueryArguments( new QueryArgument> { Name = "objectIds" }, new QueryArgument> { Name = "roomIds" } ), resolve: context => { var objectIds = context.GetArgument("objectIds"); var roomIds = context.GetArgument("roomIds"); // If no arguments are provided, return all rooms if (objectIds == null && roomIds == null) return this.recorder.Rooms; // Remove any "0" from the roomIds roomIds = roomIds?.Where(x => x != 0).ToArray(); // Otherwise, filter the rooms return this.recorder.Rooms.Where(x => (objectIds?.Contains(x.ObjectId) ?? false) || (roomIds?.Contains(x.RoomConfig.RoomId) ?? false) || (roomIds?.Contains(x.ShortId) ?? false) ); }); this.Field("room", arguments: new QueryArguments( new QueryArgument { Name = "objectId" }, new QueryArgument { Name = "roomId" } ), resolve: context => { var objectId = context.GetArgument("objectId"); var roomId = context.GetArgument("roomId"); IRoom? room; if (objectId != default) room = this.recorder.Rooms.FirstOrDefault(x => x.ObjectId == objectId); else if (roomId != 0) room = this.recorder.Rooms.FirstOrDefault(x => x.RoomConfig.RoomId == roomId || x.ShortId == roomId); else room = null; return room; } ); } } }