Skip to content

Commit

Permalink
Make Key object nullable in GetIdEndpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
YuriyDurov committed Sep 11, 2023
1 parent dd6f84e commit e457ada
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,22 @@ public static ICommunicatorRestEntityBuilder<TEntity, TKey> WithEndpoint<TEntity
return builder;
}

public static ICommunicatorRestEntityBuilder<TEntity> WithIdEndpoint<TEntity>(this ICommunicatorRestEntityBuilder<TEntity> builder, string endpoint)
where TEntity : class
{
builder.EntityOptions.GetIdEndpointAction = (key) => endpoint;

return builder;
}

public static ICommunicatorRestEntityBuilder<TEntity, TKey> WithIdEndpoint<TEntity, TKey>(this ICommunicatorRestEntityBuilder<TEntity, TKey> builder, string endpoint)
where TEntity : class
{
builder.EntityOptions.GetIdEndpointAction = (key) => endpoint;

return builder;
}

public static ICommunicatorRestEntityBuilder<TEntity> WithIdEndpoint<TEntity>(this ICommunicatorRestEntityBuilder<TEntity> builder, Func<string> getEndpoint)
where TEntity : class
{
Expand All @@ -34,15 +50,15 @@ public static ICommunicatorRestEntityBuilder<TEntity, TKey> WithIdEndpoint<TEnti
return builder;
}

public static ICommunicatorRestEntityBuilder<TEntity> WithIdEndpoint<TEntity>(this ICommunicatorRestEntityBuilder<TEntity> builder, Func<object, string> getEndpoint)
public static ICommunicatorRestEntityBuilder<TEntity> WithIdEndpoint<TEntity>(this ICommunicatorRestEntityBuilder<TEntity> builder, Func<object?, string> getEndpoint)
where TEntity : class
{
builder.EntityOptions.GetIdEndpointAction = getEndpoint;

return builder;
}

public static ICommunicatorRestEntityBuilder<TEntity, TKey> WithIdEndpoint<TEntity, TKey>(this ICommunicatorRestEntityBuilder<TEntity, TKey> builder, Func<TKey, string> getEndpoint)
public static ICommunicatorRestEntityBuilder<TEntity, TKey> WithIdEndpoint<TEntity, TKey>(this ICommunicatorRestEntityBuilder<TEntity, TKey> builder, Func<TKey?, string> getEndpoint)
where TEntity : class
{
builder.EntityOptions.GetIdEndpointAction = getEndpoint;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,13 +99,13 @@ public virtual async Task<PageResult<TEntity>> GetPageAsync(PageRequest pageRequ
return result;
}

public virtual async Task<TEntity> GetAsync(object id)
public virtual async Task<TEntity> GetAsync(object? id)
{
if (EntityOptions.GetIdEndpointAction is null) throw new KeyNotFoundException();

var idEndpoint = EntityOptions.GetIdEndpointAction(id);
var route = GetFullPath(idEndpoint);
_logger.LogInformation("Get {type}[{id}]: {route}", typeof(TEntity).Name, id.ToString(), route);
_logger.LogInformation("Get {type}[{id}]: {route}", typeof(TEntity).Name, id is not null ? id.ToString() : "_", route);

var message = new HttpRequestMessage(HttpMethod.Get, route);
var result = await HandleRequestAsync<TEntity>(message);
Expand All @@ -132,9 +132,9 @@ public CommunicatorRestEntityContext(HttpClient httpClient, CommunicatorRestServ
EntityOptions = entityOptions;
}

public override Task<TEntity> GetAsync(object id) => GetAsync((TKey)id);
public override Task<TEntity> GetAsync(object? id) => GetAsync((TKey?)id);

public async Task<TEntity> GetAsync(TKey id)
public async Task<TEntity> GetAsync(TKey? id)
{
string idEndpoint;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ public class CommunicatorRestEntityOptions<TEntity>
where TEntity : class
{
public string? Endpoint { get; set; }
protected Func<object, string>? _getIdEndpointAction;
public Func<object, string>? GetIdEndpointAction
protected Func<object?, string>? _getIdEndpointAction;
public Func<object?, string>? GetIdEndpointAction
{
get => _getIdEndpointAction;
set => _getIdEndpointAction = value;
Expand All @@ -20,7 +20,7 @@ public CommunicatorRestEntityOptions()
public class CommunicatorRestEntityOptions<TEntity, TKey> : CommunicatorRestEntityOptions<TEntity>
where TEntity : class
{
public new Func<TKey, string>? GetIdEndpointAction
public new Func<TKey?, string>? GetIdEndpointAction
{
get
{
Expand All @@ -34,7 +34,7 @@ public class CommunicatorRestEntityOptions<TEntity, TKey> : CommunicatorRestEnti
_getIdEndpointAction = null;
return;
}
_getIdEndpointAction = (key) => value!((TKey)key);
_getIdEndpointAction = (key) => value!((TKey?)key);
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/BitzArt.Communicator/Interfaces/ICommunicationContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ public interface ICommunicationContext<TEntity>
public Task<IEnumerable<TEntity>> GetAllAsync();
public Task<PageResult<TEntity>> GetPageAsync(int offset, int limit);
public Task<PageResult<TEntity>> GetPageAsync(PageRequest pageRequest);
public Task<TEntity> GetAsync(object id);
public Task<TEntity> GetAsync(object? id);
}

public interface ICommunicationContext<TEntity, TKey> : ICommunicationContext<TEntity>
where TEntity : class
{
public Task<TEntity> GetAsync(TKey id);
public Task<TEntity> GetAsync(TKey? id);
}

0 comments on commit e457ada

Please sign in to comment.