-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
18 changed files
with
298 additions
and
64 deletions.
There are no files selected for viewing
5 changes: 1 addition & 4 deletions
5
src/Serein.Core/Models/Network/WebApi/WebSocketBroadcastPacket.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,8 @@ | ||
using System.Text.Json.Serialization; | ||
|
||
namespace Serein.Core.Models.Network.WebApi; | ||
|
||
public class WebSocketBroadcastPacket(WebSocketBroadcastType type, string? data = null) | ||
{ | ||
public string? Data { get; init; } = data; | ||
|
||
[JsonConverter(typeof(JsonStringEnumConverter<WebSocketBroadcastType>))] | ||
public WebSocketBroadcastType Type { get; init; } = type; | ||
public string Type { get; init; } = type.ToString().ToLowerInvariant(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,5 +14,5 @@ public enum WebSocketBroadcastType | |
|
||
Error, | ||
|
||
Information, | ||
Info, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using EmbedIO; | ||
using EmbedIO.Routing; | ||
using Force.DeepCloner; | ||
using Serein.Core.Models.Commands; | ||
|
||
namespace Serein.Core.Services.Network.WebApi.Apis; | ||
|
||
internal partial class ApiMap | ||
{ | ||
[Route(HttpVerbs.Get, "/matches")] | ||
public async Task GetMatches() | ||
{ | ||
await HttpContext.SendPacketAsync(_matchesProvider.Value); | ||
} | ||
|
||
[Route(HttpVerbs.Post, "/matches")] | ||
public async Task AddMatch() | ||
{ | ||
var match = await HttpContext.ConvertRequestAs<Match>(); | ||
_matchesProvider.Value.Add(match); | ||
_matchesProvider.SaveAsyncWithDebounce(); | ||
|
||
await HttpContext.SendPacketAsync(match.GetHashCode()); | ||
} | ||
|
||
[Route(HttpVerbs.Delete, "/matches/{id}")] | ||
public async Task DeleteMatch(int id) | ||
{ | ||
var fisrt = _matchesProvider.Value.FirstOrDefault((m) => m.GetHashCode() == id); | ||
if (fisrt is not null) | ||
{ | ||
_matchesProvider.Value.Remove(fisrt); | ||
_matchesProvider.SaveAsyncWithDebounce(); | ||
await HttpContext.SendPacketAsync(); | ||
} | ||
else | ||
{ | ||
throw HttpException.NotFound("未找到指定的匹配"); | ||
} | ||
} | ||
|
||
[Route(HttpVerbs.Put, "/matches/{id}")] | ||
public async Task UpdateMatch(int id) | ||
{ | ||
var match = await HttpContext.ConvertRequestAs<Match>(); | ||
var fisrt = _matchesProvider.Value.FirstOrDefault((m) => m.GetHashCode() == id); | ||
if (fisrt is not null) | ||
{ | ||
match.DeepCloneTo(fisrt); | ||
|
||
_matchesProvider.SaveAsyncWithDebounce(); | ||
await HttpContext.SendPacketAsync(fisrt.GetHashCode()); | ||
} | ||
else | ||
{ | ||
throw HttpException.NotFound("未找到指定的匹配"); | ||
} | ||
} | ||
} |
61 changes: 61 additions & 0 deletions
61
src/Serein.Core/Services/Network/WebApi/Apis/ScheduleApi.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using EmbedIO; | ||
using EmbedIO.Routing; | ||
using Force.DeepCloner; | ||
using Serein.Core.Models.Commands; | ||
|
||
namespace Serein.Core.Services.Network.WebApi.Apis; | ||
|
||
internal partial class ApiMap | ||
{ | ||
[Route(HttpVerbs.Get, "/schedules")] | ||
public async Task GetSchedules() | ||
{ | ||
await HttpContext.SendPacketAsync(_scheduleProvider.Value); | ||
} | ||
|
||
[Route(HttpVerbs.Post, "/schedules")] | ||
public async Task CreateMatch() | ||
{ | ||
var schedule = await HttpContext.ConvertRequestAs<Schedule>(); | ||
_scheduleProvider.Value.Add(schedule); | ||
_scheduleProvider.SaveAsyncWithDebounce(); | ||
|
||
await HttpContext.SendPacketAsync(schedule.GetHashCode()); | ||
} | ||
|
||
[Route(HttpVerbs.Delete, "/schedules/{id}")] | ||
public async Task DeleteSchedules(int id) | ||
{ | ||
var fisrt = _scheduleProvider.Value.FirstOrDefault((m) => m.GetHashCode() == id); | ||
if (fisrt is not null) | ||
{ | ||
_scheduleProvider.Value.Remove(fisrt); | ||
_scheduleProvider.SaveAsyncWithDebounce(); | ||
await HttpContext.SendPacketAsync(); | ||
} | ||
else | ||
{ | ||
throw HttpException.NotFound("未找到指定的定时任务"); | ||
} | ||
} | ||
|
||
[Route(HttpVerbs.Put, "/schedules/{id}")] | ||
public async Task UpdateSchedule(int id) | ||
{ | ||
var schedule = await HttpContext.ConvertRequestAs<Schedule>(); | ||
var fisrt = _scheduleProvider.Value.FirstOrDefault((m) => m.GetHashCode() == id); | ||
if (fisrt is not null) | ||
{ | ||
schedule.DeepCloneTo(fisrt); | ||
|
||
_scheduleProvider.SaveAsyncWithDebounce(); | ||
await HttpContext.SendPacketAsync(fisrt.GetHashCode()); | ||
} | ||
else | ||
{ | ||
throw HttpException.NotFound("未找到指定的匹配"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.