Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main'
Browse files Browse the repository at this point in the history
  • Loading branch information
Tr00d committed Jun 10, 2024
2 parents 0ddedaa + a081eec commit 13e1477
Show file tree
Hide file tree
Showing 7 changed files with 119 additions and 276 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,25 @@

namespace Vonage.Conversations.CreateConversation;

internal class CreateConversationRequestBuilder : IBuilderForOptional
internal struct CreateConversationRequestBuilder : IBuilderForOptional
{
private const int CallbackEventMask = 200;
private const int DisplayNameMaxLength = 50;
private const int NameMaxLength = 100;
private const int PropertiesCustomSortKeyMaxLength = 200;
private const int PropertiesTypeMaxLength = 200;

private readonly IEnumerable<HttpMethod> allowedMethods = new[] {HttpMethod.Get, HttpMethod.Post};
private static readonly IEnumerable<HttpMethod> AllowedMethods = new[] {HttpMethod.Get, HttpMethod.Post};
private readonly List<INumber> numbers = new();
private Maybe<Callback> callback;
private Maybe<Properties> properties;
private Maybe<string> name;
private Maybe<string> displayName;
private Maybe<Uri> uri;
private Maybe<Callback> callback = default;
private Maybe<Properties> properties = default;
private Maybe<string> name = default;
private Maybe<string> displayName = default;
private Maybe<Uri> uri = default;

public CreateConversationRequestBuilder()
{
}

/// <inheritdoc />
public Result<CreateConversationRequest> Create() => Result<CreateConversationRequest>.FromSuccess(
Expand All @@ -42,38 +46,22 @@ internal class CreateConversationRequestBuilder : IBuilderForOptional
VerifyNameLength,
VerifyDisplayName,
VerifyDisplayNameLength,
this.VerifyCallbackHttpMethod,
VerifyCallbackHttpMethod,
VerifyPropertiesTypeLength,
VerifyPropertiesCustomSortKeyLength,
VerifyCallbackEventMaskLength));

/// <inheritdoc />
public IBuilderForOptional WithCallback(Callback value)
{
this.callback = value;
return this;
}
public IBuilderForOptional WithCallback(Callback value) => this with {callback = value};

/// <inheritdoc />
public IBuilderForOptional WithDisplayName(string value)
{
this.displayName = value;
return this;
}
public IBuilderForOptional WithDisplayName(string value) => this with {displayName = value};

/// <inheritdoc />
public IBuilderForOptional WithImageUrl(Uri value)
{
this.uri = value;
return this;
}
public IBuilderForOptional WithImageUrl(Uri value) => this with {uri = value};

/// <inheritdoc />
public IBuilderForOptional WithName(string value)
{
this.name = value;
return this;
}
public IBuilderForOptional WithName(string value) => this with {name = value};

/// <inheritdoc />
public IBuilderForOptional WithNumber(INumber value)
Expand All @@ -83,21 +71,17 @@ public IBuilderForOptional WithNumber(INumber value)
}

/// <inheritdoc />
public IBuilderForOptional WithProperties(Properties value)
{
this.properties = value;
return this;
}
public IBuilderForOptional WithProperties(Properties value) => this with {properties = value};

private static Result<CreateConversationRequest> VerifyCallbackEventMaskLength(CreateConversationRequest request) =>
request.Callback.Match(
some => InputValidation.VerifyLengthLowerOrEqualThan(request, some.EventMask, CallbackEventMask,
$"{nameof(request.Callback)} {nameof(some.EventMask)}"),
() => request);

private Result<CreateConversationRequest> VerifyCallbackHttpMethod(CreateConversationRequest request) =>
private static Result<CreateConversationRequest> VerifyCallbackHttpMethod(CreateConversationRequest request) =>
request.Callback.Match(
some => this.allowedMethods.Contains(some.Method)
some => AllowedMethods.Contains(some.Method)
? Result<CreateConversationRequest>.FromSuccess(request)
: ResultFailure.FromErrorMessage("Callback HttpMethod must be GET or POST.")
.ToResult<CreateConversationRequest>(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@

namespace Vonage.Conversations.GetConversations;

internal class GetConversationsRequestBuilder : IBuilderForOptional
internal struct GetConversationsRequestBuilder : IBuilderForOptional
{
private const int MaximumPageSize = 100;
private const int MinimumPageSize = 1;
private readonly Maybe<string> cursor;
private Maybe<DateTimeOffset> endDate;
private FetchOrder fetchOrder = FetchOrder.Ascending;
private int pageSize = 10;
private Maybe<DateTimeOffset> endDate;
private Maybe<DateTimeOffset> startDate;
private readonly Maybe<string> cursor;

internal GetConversationsRequestBuilder(Maybe<string> cursor) => this.cursor = cursor;

Expand All @@ -31,44 +31,16 @@ internal class GetConversationsRequestBuilder : IBuilderForOptional
.Bind(evaluation => evaluation.WithRules(VerifyMinimumPageSize, VerifyMaximumPageSize));

/// <inheritdoc />
public IBuilderForOptional WithEndDate(DateTimeOffset value) =>
new GetConversationsRequestBuilder(this.cursor)
{
startDate = this.startDate,
pageSize = this.pageSize,
endDate = value,
fetchOrder = this.fetchOrder,
};
public IBuilderForOptional WithEndDate(DateTimeOffset value) => this with {endDate = value};

/// <inheritdoc />
public IBuilderForOptional WithOrder(FetchOrder value) =>
new GetConversationsRequestBuilder(this.cursor)
{
startDate = this.startDate,
pageSize = this.pageSize,
endDate = this.endDate,
fetchOrder = value,
};
public IBuilderForOptional WithOrder(FetchOrder value) => this with {fetchOrder = value};

/// <inheritdoc />
public IBuilderForOptional WithPageSize(int value) =>
new GetConversationsRequestBuilder(this.cursor)
{
startDate = this.startDate,
pageSize = value,
endDate = this.endDate,
fetchOrder = this.fetchOrder,
};
public IBuilderForOptional WithPageSize(int value) => this with {pageSize = value};

/// <inheritdoc />
public IBuilderForOptional WithStartDate(DateTimeOffset value) =>
new GetConversationsRequestBuilder(this.cursor)
{
startDate = value,
pageSize = this.pageSize,
endDate = this.endDate,
fetchOrder = this.fetchOrder,
};
public IBuilderForOptional WithStartDate(DateTimeOffset value) => this with {startDate = value};

private static Result<GetConversationsRequest> VerifyMaximumPageSize(GetConversationsRequest request) =>
InputValidation.VerifyLowerOrEqualThan(request, request.PageSize, MaximumPageSize, nameof(request.PageSize));
Expand Down
50 changes: 16 additions & 34 deletions Vonage/Conversations/GetMembers/GetMembersRequestBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,17 @@

namespace Vonage.Conversations.GetMembers;

internal class GetMembersRequestBuilder : IBuilderForConversationId, IBuilderForOptional
internal struct GetMembersRequestBuilder : IBuilderForConversationId, IBuilderForOptional
{
private const int MaximumPageSize = 100;
private const int MinimumPageSize = 1;
private readonly Maybe<string> cursor;
private string conversationId;
private FetchOrder fetchOrder = FetchOrder.Ascending;
private int pageSize = 10;

private readonly Maybe<string> cursor;
private string conversationId;

internal GetMembersRequestBuilder(Maybe<string> cursor) => this.cursor = cursor;

/// <inheritdoc />
public IBuilderForOptional WithConversationId(string value) =>
new GetMembersRequestBuilder(this.cursor)
{
pageSize = this.pageSize,
fetchOrder = this.fetchOrder,
conversationId = value,
};


/// <inheritdoc />
public Result<GetMembersRequest> Create() => Result<GetMembersRequest>.FromSuccess(new GetMembersRequest
{
Expand All @@ -34,31 +25,22 @@ internal class GetMembersRequestBuilder : IBuilderForConversationId, IBuilderFor
})
.Map(InputEvaluation<GetMembersRequest>.Evaluate)
.Bind(evaluation => evaluation.WithRules(VerifyConversationId, VerifyMinimumPageSize, VerifyMaximumPageSize));

/// <inheritdoc />
public IBuilderForOptional WithOrder(FetchOrder value) =>
new GetMembersRequestBuilder(this.cursor)
{
pageSize = this.pageSize,
fetchOrder = value,
conversationId = this.conversationId,
};

public IBuilderForOptional WithConversationId(string value) => this with {conversationId = value};

/// <inheritdoc />
public IBuilderForOptional WithPageSize(int value) =>
new GetMembersRequestBuilder(this.cursor)
{
pageSize = value,
fetchOrder = this.fetchOrder,
conversationId = this.conversationId,
};

public IBuilderForOptional WithOrder(FetchOrder value) => this with {fetchOrder = value};

/// <inheritdoc />
public IBuilderForOptional WithPageSize(int value) => this with {pageSize = value};

private static Result<GetMembersRequest> VerifyConversationId(GetMembersRequest request) =>
InputValidation.VerifyNotEmpty(request, request.ConversationId, nameof(request.ConversationId));

private static Result<GetMembersRequest> VerifyMaximumPageSize(GetMembersRequest request) =>
InputValidation.VerifyLowerOrEqualThan(request, request.PageSize, MaximumPageSize, nameof(request.PageSize));

private static Result<GetMembersRequest> VerifyMinimumPageSize(GetMembersRequest request) =>
InputValidation.VerifyHigherOrEqualThan(request, request.PageSize, MinimumPageSize, nameof(request.PageSize));
}
Expand All @@ -74,7 +56,7 @@ public interface IBuilderForOptional : IVonageRequestBuilder<GetMembersRequest>
/// <param name="value">The order.</param>
/// <returns>The builder.</returns>
IBuilderForOptional WithOrder(FetchOrder value);

/// <summary>
/// Sets the page size on the builder.
/// </summary>
Expand Down
Loading

0 comments on commit 13e1477

Please sign in to comment.