BililiveRecorder/BililiveRecorder.Web/Graphql/RecorderQuery.cs

79 lines
2.9 KiB
C#
Raw Normal View History

2021-05-30 19:16:20 +08:00
using System;
using System.Linq;
using BililiveRecorder.Core;
2022-04-03 20:43:38 +08:00
using BililiveRecorder.Core.Config.V3;
using BililiveRecorder.Web.Models;
using BililiveRecorder.Web.Models.Graphql;
2021-05-30 19:16:20 +08:00
using GraphQL;
using GraphQL.Types;
namespace BililiveRecorder.Web.Graphql
2021-05-30 19:16:20 +08:00
{
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()
{
2022-04-02 21:08:04 +08:00
this.Field<RecorderVersionType>("version", resolve: context => RecorderVersion.Instance);
2021-05-30 19:16:20 +08:00
this.Field<GlobalConfigType>("config", resolve: context => this.recorder.Config.Global);
this.Field<DefaultConfigType>("defaultConfig", resolve: context => DefaultConfig.Instance);
this.Field<ListGraphType<RoomType>>("rooms", arguments: new QueryArguments(
new QueryArgument<ListGraphType<IdGraphType>> { Name = "objectIds" },
new QueryArgument<ListGraphType<IntGraphType>> { Name = "roomIds" }
),
resolve: context =>
{
var objectIds = context.GetArgument<Guid[]>("objectIds");
var roomIds = context.GetArgument<int[]>("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)
);
});
2021-05-30 19:16:20 +08:00
this.Field<RoomType>("room",
arguments: new QueryArguments(
new QueryArgument<IdGraphType> { Name = "objectId" },
new QueryArgument<IntGraphType> { Name = "roomId" }
),
resolve: context =>
{
var objectId = context.GetArgument<Guid>("objectId");
var roomId = context.GetArgument<int>("roomId");
2021-05-30 19:16:20 +08:00
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;
2021-05-30 19:16:20 +08:00
return room;
}
);
}
}
}