Created
October 30, 2023 04:43
-
-
Save duydang2311/b6e1a023c14879adffadad4ec3b2a55e to your computer and use it in GitHub Desktop.
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
using AltV.Net; | |
namespace Server.Roleplay.Models; | |
public class RpcAnswer : IWritable | |
{ | |
private readonly IWritable? data; | |
private LinkedList<ValidationError>? errors; | |
private RpcAnswer(IWritable? data) | |
{ | |
this.data = data; | |
} | |
private RpcAnswer(ValidationError error) | |
{ | |
errors = new(new[] { error }); | |
} | |
public static RpcAnswer Fail(ValidationError error) | |
{ | |
return new RpcAnswer(error); | |
} | |
public static RpcAnswer Ok(IWritable? value = default) | |
{ | |
return new RpcAnswer(value); | |
} | |
public void OnWrite(IMValueWriter writer) | |
{ | |
writer.BeginObject(); | |
if (errors is { Count: > 0 }) | |
{ | |
writer.Name("errors"); | |
writer.BeginArray(); | |
foreach (var error in errors) | |
{ | |
error.OnWrite(writer); | |
} | |
writer.EndArray(); | |
} | |
if (data is not null) | |
{ | |
writer.Name("data"); | |
writer.Value(data); | |
} | |
writer.EndObject(); | |
} | |
public RpcAnswer Error(ValidationError error) | |
{ | |
errors ??= new(); | |
errors.AddLast(error); | |
return this; | |
} | |
} |
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
using AltV.Net; | |
namespace Server.Roleplay.Models; | |
public class ValidationError : IWritable | |
{ | |
public string Name { get; set; } | |
public string Code { get; set; } | |
public ValidationError(string name, string code) | |
{ | |
Name = name; | |
Code = code; | |
} | |
public void OnWrite(IMValueWriter writer) | |
{ | |
writer.BeginObject(); | |
writer.Name("name"); | |
writer.Value(Name); | |
writer.Name("code"); | |
writer.Value(Code); | |
writer.EndObject(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment