From 62467b2250a5a7545de938ab77a1441b9a344d01 Mon Sep 17 00:00:00 2001 From: Tr00d Date: Thu, 6 Jun 2024 12:33:22 +0200 Subject: [PATCH] feat: refactor builders for Conversations --- .../CreateConversationRequestBuilder.cs | 54 +++----- .../GetConversationsRequestBuilder.cs | 42 +----- .../GetMembers/GetMembersRequestBuilder.cs | 50 +++---- .../GetUserConversationsRequestBuilder.cs | 124 ++++-------------- .../UpdateConversationRequestBuilder.cs | 62 +++------ 5 files changed, 90 insertions(+), 242 deletions(-) diff --git a/Vonage/Conversations/CreateConversation/CreateConversationRequestBuilder.cs b/Vonage/Conversations/CreateConversation/CreateConversationRequestBuilder.cs index bb5debe17..59e042cbd 100644 --- a/Vonage/Conversations/CreateConversation/CreateConversationRequestBuilder.cs +++ b/Vonage/Conversations/CreateConversation/CreateConversationRequestBuilder.cs @@ -9,7 +9,7 @@ namespace Vonage.Conversations.CreateConversation; -internal class CreateConversationRequestBuilder : IBuilderForOptional +internal struct CreateConversationRequestBuilder : IBuilderForOptional { private const int CallbackEventMask = 200; private const int DisplayNameMaxLength = 50; @@ -17,13 +17,17 @@ internal class CreateConversationRequestBuilder : IBuilderForOptional private const int PropertiesCustomSortKeyMaxLength = 200; private const int PropertiesTypeMaxLength = 200; - private readonly IEnumerable allowedMethods = new[] {HttpMethod.Get, HttpMethod.Post}; + private static readonly IEnumerable AllowedMethods = new[] {HttpMethod.Get, HttpMethod.Post}; private readonly List numbers = new(); - private Maybe callback; - private Maybe properties; - private Maybe name; - private Maybe displayName; - private Maybe uri; + private Maybe callback = default; + private Maybe properties = default; + private Maybe name = default; + private Maybe displayName = default; + private Maybe uri = default; + + public CreateConversationRequestBuilder() + { + } /// public Result Create() => Result.FromSuccess( @@ -42,38 +46,22 @@ public Result Create() => Result - public IBuilderForOptional WithCallback(Callback value) - { - this.callback = value; - return this; - } + public IBuilderForOptional WithCallback(Callback value) => this with {callback = value}; /// - public IBuilderForOptional WithDisplayName(string value) - { - this.displayName = value; - return this; - } + public IBuilderForOptional WithDisplayName(string value) => this with {displayName = value}; /// - public IBuilderForOptional WithImageUrl(Uri value) - { - this.uri = value; - return this; - } + public IBuilderForOptional WithImageUrl(Uri value) => this with {uri = value}; /// - public IBuilderForOptional WithName(string value) - { - this.name = value; - return this; - } + public IBuilderForOptional WithName(string value) => this with {name = value}; /// public IBuilderForOptional WithNumber(INumber value) @@ -83,11 +71,7 @@ public IBuilderForOptional WithNumber(INumber value) } /// - public IBuilderForOptional WithProperties(Properties value) - { - this.properties = value; - return this; - } + public IBuilderForOptional WithProperties(Properties value) => this with {properties = value}; private static Result VerifyCallbackEventMaskLength(CreateConversationRequest request) => request.Callback.Match( @@ -95,9 +79,9 @@ private static Result VerifyCallbackEventMaskLength(C $"{nameof(request.Callback)} {nameof(some.EventMask)}"), () => request); - private Result VerifyCallbackHttpMethod(CreateConversationRequest request) => + private static Result VerifyCallbackHttpMethod(CreateConversationRequest request) => request.Callback.Match( - some => this.allowedMethods.Contains(some.Method) + some => AllowedMethods.Contains(some.Method) ? Result.FromSuccess(request) : ResultFailure.FromErrorMessage("Callback HttpMethod must be GET or POST.") .ToResult(), diff --git a/Vonage/Conversations/GetConversations/GetConversationsRequestBuilder.cs b/Vonage/Conversations/GetConversations/GetConversationsRequestBuilder.cs index 627c097f2..0de73d5d6 100644 --- a/Vonage/Conversations/GetConversations/GetConversationsRequestBuilder.cs +++ b/Vonage/Conversations/GetConversations/GetConversationsRequestBuilder.cs @@ -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 cursor; - private Maybe endDate; private FetchOrder fetchOrder = FetchOrder.Ascending; private int pageSize = 10; + private Maybe endDate; private Maybe startDate; + private readonly Maybe cursor; internal GetConversationsRequestBuilder(Maybe cursor) => this.cursor = cursor; @@ -31,44 +31,16 @@ public Result Create() => Result evaluation.WithRules(VerifyMinimumPageSize, VerifyMaximumPageSize)); /// - 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}; /// - 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}; /// - 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}; /// - 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 VerifyMaximumPageSize(GetConversationsRequest request) => InputValidation.VerifyLowerOrEqualThan(request, request.PageSize, MaximumPageSize, nameof(request.PageSize)); diff --git a/Vonage/Conversations/GetMembers/GetMembersRequestBuilder.cs b/Vonage/Conversations/GetMembers/GetMembersRequestBuilder.cs index 2c02cd7d1..c1728b040 100644 --- a/Vonage/Conversations/GetMembers/GetMembersRequestBuilder.cs +++ b/Vonage/Conversations/GetMembers/GetMembersRequestBuilder.cs @@ -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 cursor; - private string conversationId; private FetchOrder fetchOrder = FetchOrder.Ascending; private int pageSize = 10; - + private readonly Maybe cursor; + private string conversationId; + internal GetMembersRequestBuilder(Maybe cursor) => this.cursor = cursor; - - /// - public IBuilderForOptional WithConversationId(string value) => - new GetMembersRequestBuilder(this.cursor) - { - pageSize = this.pageSize, - fetchOrder = this.fetchOrder, - conversationId = value, - }; - + /// public Result Create() => Result.FromSuccess(new GetMembersRequest { @@ -34,31 +25,22 @@ public Result Create() => Result.FromSucce }) .Map(InputEvaluation.Evaluate) .Bind(evaluation => evaluation.WithRules(VerifyConversationId, VerifyMinimumPageSize, VerifyMaximumPageSize)); - + /// - 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}; + /// - 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}; + + /// + public IBuilderForOptional WithPageSize(int value) => this with {pageSize = value}; + private static Result VerifyConversationId(GetMembersRequest request) => InputValidation.VerifyNotEmpty(request, request.ConversationId, nameof(request.ConversationId)); - + private static Result VerifyMaximumPageSize(GetMembersRequest request) => InputValidation.VerifyLowerOrEqualThan(request, request.PageSize, MaximumPageSize, nameof(request.PageSize)); - + private static Result VerifyMinimumPageSize(GetMembersRequest request) => InputValidation.VerifyHigherOrEqualThan(request, request.PageSize, MinimumPageSize, nameof(request.PageSize)); } @@ -74,7 +56,7 @@ public interface IBuilderForOptional : IVonageRequestBuilder /// The order. /// The builder. IBuilderForOptional WithOrder(FetchOrder value); - + /// /// Sets the page size on the builder. /// diff --git a/Vonage/Conversations/GetUserConversations/GetUserConversationsRequestBuilder.cs b/Vonage/Conversations/GetUserConversations/GetUserConversationsRequestBuilder.cs index 862cf9903..78290bf5d 100644 --- a/Vonage/Conversations/GetUserConversations/GetUserConversationsRequestBuilder.cs +++ b/Vonage/Conversations/GetUserConversations/GetUserConversationsRequestBuilder.cs @@ -5,18 +5,18 @@ namespace Vonage.Conversations.GetUserConversations; -internal class GetUserConversationsRequestBuilder : IBuilderForUserId, IBuilderForOptional +internal struct GetUserConversationsRequestBuilder : IBuilderForUserId, IBuilderForOptional { - private const string DefaultOrderBy = "created"; private const int MaximumPageSize = 100; private const int MinimumPageSize = 1; - private readonly Maybe cursor; + private const string DefaultOrderBy = "created"; private bool includeCustomData; private FetchOrder order = FetchOrder.Ascending; - private Maybe orderBy; private int pageSize = 10; private Maybe startDate; private Maybe state; + private readonly Maybe cursor; + private Maybe orderBy; private string userId; internal GetUserConversationsRequestBuilder(Maybe cursor) => this.cursor = cursor; @@ -40,104 +40,34 @@ public Result Create() => Result - public IBuilderForOptional WithOrder(FetchOrder value) => - new GetUserConversationsRequestBuilder(this.cursor) - { - includeCustomData = this.includeCustomData, - orderBy = this.orderBy, - order = value, - state = this.state, - pageSize = this.pageSize, - startDate = this.startDate, - userId = this.userId, - }; + public IBuilderForOptional IncludeCustomData() => this with {includeCustomData = true}; /// - public IBuilderForOptional WithOrderBy(string value) => - new GetUserConversationsRequestBuilder(this.cursor) - { - includeCustomData = this.includeCustomData, - orderBy = value, - order = this.order, - state = this.state, - pageSize = this.pageSize, - startDate = this.startDate, - userId = this.userId, - }; + public IBuilderForOptional WithOrder(FetchOrder value) => this with {order = value}; /// - public IBuilderForOptional WithState(State value) => - new GetUserConversationsRequestBuilder(this.cursor) - { - includeCustomData = this.includeCustomData, - orderBy = this.orderBy, - order = this.order, - state = value, - pageSize = this.pageSize, - startDate = this.startDate, - userId = this.userId, - }; + public IBuilderForOptional WithOrderBy(string value) => this with {orderBy = value}; /// - public IBuilderForOptional IncludeCustomData() => - new GetUserConversationsRequestBuilder(this.cursor) - { - includeCustomData = true, - orderBy = this.orderBy, - order = this.order, - state = this.state, - pageSize = this.pageSize, - startDate = this.startDate, - userId = this.userId, - }; + public IBuilderForOptional WithPageSize(int value) => this with {pageSize = value}; /// - public IBuilderForOptional WithPageSize(int value) => - new GetUserConversationsRequestBuilder(this.cursor) - { - includeCustomData = this.includeCustomData, - orderBy = this.orderBy, - order = this.order, - state = this.state, - pageSize = value, - startDate = this.startDate, - userId = this.userId, - }; + public IBuilderForOptional WithStartDate(DateTimeOffset value) => this with {startDate = value}; /// - public IBuilderForOptional WithStartDate(DateTimeOffset value) => - new GetUserConversationsRequestBuilder(this.cursor) - { - includeCustomData = this.includeCustomData, - orderBy = this.orderBy, - order = this.order, - state = this.state, - pageSize = this.pageSize, - startDate = value, - userId = this.userId, - }; + public IBuilderForOptional WithState(State value) => this with {state = value}; /// - public IBuilderForOptional WithUserId(string value) => - new GetUserConversationsRequestBuilder(this.cursor) - { - includeCustomData = this.includeCustomData, - orderBy = this.orderBy, - order = this.order, - state = this.state, - pageSize = this.pageSize, - startDate = this.startDate, - userId = value, - }; + public IBuilderForOptional WithUserId(string value) => this with {userId = value}; private static Result VerifyMaximumPageSize(GetUserConversationsRequest request) => InputValidation.VerifyLowerOrEqualThan(request, request.PageSize, MaximumPageSize, nameof(request.PageSize)); - private static Result VerifyUserId(GetUserConversationsRequest request) => - InputValidation.VerifyNotEmpty(request, request.UserId, nameof(request.UserId)); - private static Result VerifyMinimumPageSize(GetUserConversationsRequest request) => InputValidation.VerifyHigherOrEqualThan(request, request.PageSize, MinimumPageSize, nameof(request.PageSize)); + + private static Result VerifyUserId(GetUserConversationsRequest request) => + InputValidation.VerifyNotEmpty(request, request.UserId, nameof(request.UserId)); } /// @@ -158,6 +88,12 @@ public interface IBuilderForUserId /// public interface IBuilderForOptional : IVonageRequestBuilder { + /// + /// Sets the request to include custom data. + /// + /// The builder. + IBuilderForOptional IncludeCustomData(); + /// /// Sets the order on the builder. /// @@ -172,19 +108,6 @@ public interface IBuilderForOptional : IVonageRequestBuilderThe builder. IBuilderForOptional WithOrderBy(string value); - /// - /// Sets the state on the builder. - /// - /// The state. - /// The builder. - IBuilderForOptional WithState(State value); - - /// - /// Sets the request to include custom data. - /// - /// The builder. - IBuilderForOptional IncludeCustomData(); - /// /// Sets the page size on the builder. /// @@ -198,4 +121,11 @@ public interface IBuilderForOptional : IVonageRequestBuilderThe start date. /// The builder. IBuilderForOptional WithStartDate(DateTimeOffset value); + + /// + /// Sets the state on the builder. + /// + /// The state. + /// The builder. + IBuilderForOptional WithState(State value); } \ No newline at end of file diff --git a/Vonage/Conversations/UpdateConversation/UpdateConversationRequestBuilder.cs b/Vonage/Conversations/UpdateConversation/UpdateConversationRequestBuilder.cs index 87ba74897..e452693b6 100644 --- a/Vonage/Conversations/UpdateConversation/UpdateConversationRequestBuilder.cs +++ b/Vonage/Conversations/UpdateConversation/UpdateConversationRequestBuilder.cs @@ -9,7 +9,7 @@ namespace Vonage.Conversations.UpdateConversation; -internal class UpdateConversationRequestBuilder : IBuilderForConversationId, IBuilderForOptional +internal struct UpdateConversationRequestBuilder : IBuilderForConversationId, IBuilderForOptional { private const int CallbackEventMask = 200; private const int DisplayNameMaxLength = 50; @@ -17,14 +17,18 @@ internal class UpdateConversationRequestBuilder : IBuilderForConversationId, IBu private const int PropertiesCustomSortKeyMaxLength = 200; private const int PropertiesTypeMaxLength = 200; - private readonly IEnumerable allowedMethods = new[] {HttpMethod.Get, HttpMethod.Post}; + private static readonly IEnumerable AllowedMethods = new[] {HttpMethod.Get, HttpMethod.Post}; private readonly List numbers = new(); - private Maybe callback; - private Maybe properties; - private Maybe name; - private Maybe displayName; - private Maybe uri; - private string conversationId; + private Maybe callback = default; + private Maybe properties = default; + private Maybe name = default; + private Maybe displayName = default; + private Maybe uri = default; + private string conversationId = default; + + public UpdateConversationRequestBuilder() + { + } /// public Result Create() => Result.FromSuccess( @@ -45,45 +49,25 @@ public Result Create() => Result - public IBuilderForOptional WithCallback(Callback value) - { - this.callback = value; - return this; - } + public IBuilderForOptional WithCallback(Callback value) => this with {callback = value}; /// - public IBuilderForOptional WithConversationId(string value) - { - this.conversationId = value; - return this; - } + public IBuilderForOptional WithConversationId(string value) => this with {conversationId = value}; /// - public IBuilderForOptional WithDisplayName(string value) - { - this.displayName = value; - return this; - } + public IBuilderForOptional WithDisplayName(string value) => this with {displayName = value}; /// - public IBuilderForOptional WithImageUrl(Uri value) - { - this.uri = value; - return this; - } + public IBuilderForOptional WithImageUrl(Uri value) => this with {uri = value}; /// - public IBuilderForOptional WithName(string value) - { - this.name = value; - return this; - } + public IBuilderForOptional WithName(string value) => this with {name = value}; /// public IBuilderForOptional WithNumber(INumber value) @@ -93,11 +77,7 @@ public IBuilderForOptional WithNumber(INumber value) } /// - public IBuilderForOptional WithProperties(Properties value) - { - this.properties = value; - return this; - } + public IBuilderForOptional WithProperties(Properties value) => this with {properties = value}; private static Result VerifyCallbackEventMaskLength(UpdateConversationRequest request) => request.Callback.Match( @@ -105,9 +85,9 @@ private static Result VerifyCallbackEventMaskLength(U $"{nameof(request.Callback)} {nameof(some.EventMask)}"), () => request); - private Result VerifyCallbackHttpMethod(UpdateConversationRequest request) => + private static Result VerifyCallbackHttpMethod(UpdateConversationRequest request) => request.Callback.Match( - some => this.allowedMethods.Contains(some.Method) + some => AllowedMethods.Contains(some.Method) ? Result.FromSuccess(request) : ResultFailure.FromErrorMessage("Callback HttpMethod must be GET or POST.") .ToResult(),