Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using Crypto.Compare.DataAccess;
using Crypto.Compare.Services.Queries.Symbols;
using Crypto.Compare.Services.Results.Providers;
using Crypto.Compare.Services.Results.Symbols;
using FluentResults;
using MediatR;
using Microsoft.EntityFrameworkCore;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
using Crypto.Compare.DataAccess;
using Crypto.Compare.Services.Queries.SymbolProviders;
using Crypto.Compare.Services.Results.SymbolProviders;
using FluentResults;
using MediatR;
using Microsoft.EntityFrameworkCore;

namespace Crypto.Compare.Services.Handlers.Queries.SymbolProviders;

public class GetSymbolProvidersQueryHandler : IRequestHandler<GetSymbolProvidersQuery, Result<GetSymbolProviderResult>>
{
private readonly CryptoCompareContext _cryptoCompareContext;

public GetSymbolProvidersQueryHandler(CryptoCompareContext cryptoCompareContext)
{
_cryptoCompareContext = cryptoCompareContext;
}

public async Task<Result<GetSymbolProviderResult>> Handle(GetSymbolProvidersQuery request, CancellationToken cancellationToken)
{
var query = _cryptoCompareContext
.SymbolProviders
.Include(x => x.Symbol)
.AsQueryable()
.AsNoTracking()
.Where(x => x.ProviderId == request.ProviderId);

if (!string.IsNullOrEmpty(request.Ticker))
{
query = query.Where(x => x.Symbol.Ticker == request.Ticker);
}


var symbols = await query
.Select(s => new SymbolProviderResult
{
Id = s.Symbol.Id,
Ticker = s.Ticker,
Symbol = $"{s.Symbol.BaseSymbol}/{s.Symbol.QuoteSymbol}",
ProviderId = s.ProviderId,
PriceSell = s.PriceSell,
PriceBuy = s.PriceBuy,
UpdatedAt = s.UpdatedAt
})
.Skip((int)request.Skip)
.Take(request.Rows)
.ToListAsync(cancellationToken);


return new GetSymbolProviderResult
{
Symbols = symbols
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ public GetSymbolsQueryHandler(CryptoCompareContext cryptoCompareContext)
public async Task<Result<GetSymbolsResult>> Handle(GetSymbolsQuery request, CancellationToken cancellationToken)
{
var query = _cryptoCompareContext
.SymbolProviders
.Symbols
.Include(x=>x.SymbolProviders)
.AsQueryable()
.AsNoTracking();

Expand All @@ -28,12 +29,30 @@ public async Task<Result<GetSymbolsResult>> Handle(GetSymbolsQuery request, Canc
query = query.Where(x => x.Ticker == request.Ticker);
}

if (request.ProviderId.HasValue)
{
query = query.Where(x => x.ProviderId == request.ProviderId);
}

var symbols = await query
.Select(symbol => new SymbolResult
{
Id = symbol.Id,
Ticker = symbol.Ticker,
Symbol = $"{symbol.BaseSymbol}/{symbol.QuoteSymbol}",
ProviderSellId = symbol.SymbolProviders
.OrderByDescending(sp => sp.PriceSell)
.Select(sp => sp.ProviderId)
.FirstOrDefault(),
PriceSell = symbol.SymbolProviders
.OrderByDescending(sp => sp.PriceSell)
.Select(sp => sp.PriceSell)
.FirstOrDefault(),
ProviderBuyId = symbol.SymbolProviders
.OrderBy(sp => sp.PriceBuy)
.Select(sp => sp.ProviderId)
.FirstOrDefault(),
PriceBuy = symbol.SymbolProviders
.OrderBy(sp => sp.PriceBuy)
.Select(sp => sp.PriceBuy)
.FirstOrDefault(),
UpdatedAt = symbol.UpdatedAt
})
.Skip((int)request.Skip)
.Take(request.Rows)
.ToListAsync(cancellationToken);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using Crypto.Compare.Services.Results.SymbolProviders;
using FluentResults;
using MediatR;

namespace Crypto.Compare.Services.Queries.SymbolProviders
{
public class GetSymbolProvidersQuery : IRequest<Result<GetSymbolProviderResult>>
{
private const int MaxRows = 50;
private const int DefaultRows = 10;

public GetSymbolProvidersQuery(int providerId, string? ticker = null, long? skip = 0, int? rows = DefaultRows)
{
Ticker = ticker?.ToLowerInvariant();
ProviderId = providerId;
Skip = skip ?? 0;
Rows = rows is null or < 10 or > MaxRows ? DefaultRows : rows.Value;
}

/// <summary>
/// Ticker symbol
/// </summary>
public string? Ticker { get; }
public int ProviderId { get; }


/// <summary>
/// Skip rows
/// </summary>
public long Skip { get; }

/// <summary>
/// Get rows
/// </summary>
public int Rows { get; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,12 @@ namespace Crypto.Compare.Services.Queries.Symbols;

public class GetSymbolsQuery : IRequest<Result<GetSymbolsResult>>
{
private const int MaxRows = 50;
private const int MaxRows = 30;
private const int DefaultRows = 10;

public GetSymbolsQuery(string? ticker = null, int? providerId = null, long? skip = 0, int? rows = DefaultRows)
public GetSymbolsQuery(string? ticker = null, long? skip = 0, int? rows = DefaultRows)
{
Ticker = ticker?.ToLowerInvariant();
ProviderId = providerId;
Skip = skip ?? 0;
Rows = rows is null or < 10 or > MaxRows ? DefaultRows : rows.Value;
}
Expand All @@ -22,11 +21,6 @@ public GetSymbolsQuery(string? ticker = null, int? providerId = null, long? skip
/// </summary>
public string? Ticker { get; }

/// <summary>
/// Provider for symbols
/// </summary>
public int? ProviderId { get; }

/// <summary>
/// Skip rows
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using Crypto.Compare.Data;

namespace Crypto.Compare.Services.Results.SymbolProviders;

public class GetSymbolProviderResult : IHandlerResult
{
public List<SymbolProviderResult> Symbols { get; set; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace Crypto.Compare.Services.Results.SymbolProviders;

public class SymbolProviderResult
{

/// <summary>
/// Uniq id
/// </summary>
public long Id { get; set; }

/// <summary>
/// Ticker name
/// </summary>
public string Ticker { get; set; }

/// <summary>
/// symbol or viewed name
/// </summary>
public string Symbol { get; set; }

/// <summary>
/// Id provider the best sell price
/// </summary>
public int ProviderId { get; set; }

/// <summary>
/// Price sell
/// </summary>
public decimal PriceSell { get; set; }

/// <summary>
/// Current sell price
/// </summary>
public decimal PriceBuy { get; set; }

/// <summary>
/// Last date get quotes
/// </summary>
public DateTime UpdatedAt { get; set; }

}
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
using Crypto.Compare.Data;

namespace Crypto.Compare.Services.Results.Symbols;

public class GetSymbolsResult : IHandlerResult
{
public List<SymbolProvider> Symbols { get; set; }
public List<SymbolResult> Symbols { get; set; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
namespace Crypto.Compare.Services.Results.Symbols;

public class SymbolResult
{
/// <summary>
/// Uniq id
/// </summary>
public long Id { get; set; }

/// <summary>
/// Ticker name
/// </summary>
public string Ticker { get; set; }

/// <summary>
/// symbol or viewed name
/// </summary>
public string Symbol { get; set; }

/// <summary>
/// Id provider the best sell price
/// </summary>
public int ProviderSellId { get; set; }

/// <summary>
/// Price sell
/// </summary>
public decimal PriceSell { get; set; }

/// <summary>
/// Id provider the best buy price
/// </summary>
public int ProviderBuyId { get; set; }

/// <summary>
/// Current sell price
/// </summary>
public decimal PriceBuy { get; set; }

/// <summary>
/// Last date get quotes
/// </summary>
public DateTime UpdatedAt { get; set; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,6 @@ public void Configure(EntityTypeBuilder<SymbolProvider> builder)
.HasForeignKey(p => p.SymbolId)
.IsRequired(false);

builder.Ignore(p => p.Symbol);
//builder.Ignore(p => p.Symbol);
}
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
using System.Net.Mime;
using Crypto.Compare.Data;
using Crypto.Compare.Data;
using Crypto.Compare.PublicApi.Mapping;
using Crypto.Compare.PublicApi.Responses;
using Crypto.Compare.PublicApi.Responses.v1.SymbolProvider;
using Crypto.Compare.PublicApi.Responses.v1.Symbols;
using Crypto.Compare.PublicApi.Swaggers.Responses;
using Crypto.Compare.PublicApi.Swaggers.Responses.v1.Symbols;
using Crypto.Compare.Services.Queries.SymbolProviders;
using Crypto.Compare.Services.Queries.Symbols;
using Crypto.Compare.Services.Results.SymbolProviders;
using Crypto.Compare.Services.Results.Symbols;
using MediatR;
using Microsoft.AspNetCore.Mvc;
using Swashbuckle.AspNetCore.Annotations;
using Swashbuckle.AspNetCore.Filters;
using System.Net.Mime;

namespace Crypto.Compare.PublicApi.Controllers;

Expand Down Expand Up @@ -64,12 +67,12 @@ public async Task<IActionResult> GetSymbols([FromQuery] int? skip, [FromQuery] i
[SwaggerResponseExample(StatusCodes.Status200OK, typeof(GetSymbolsResponseExample))]
public async Task<IActionResult> GetSymbolsByProvider([FromRoute] int providerId, [FromQuery] int? skip, [FromQuery] int? rows, CancellationToken cancellationToken)
{
var command = new GetSymbolsQuery(providerId: providerId,
var command = new GetSymbolProvidersQuery(providerId: providerId,
skip: skip,
rows: rows);
var result = await _mediator.Send(command, cancellationToken);

return _responseMapper.ToCustomResponse<GetSymbolsResponse, GetSymbolsResult>(result);
return _responseMapper.ToCustomResponse<GetSymbolsProviderResponse, GetSymbolProviderResult>(result);
}

/// <summary>
Expand Down
11 changes: 8 additions & 3 deletions sources/presentation/Crypto.Compare.PublicApi/Dtos/SymbolDto.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,20 @@ public class SymbolDto
public string Symbol { get; set; }

/// <summary>
/// Id provider
/// Id provider the best sell price
/// </summary>
public int ProviderId { get; set; }
public int ProviderSellId { get; set; }

/// <summary>
/// Price sell
/// </summary>
public decimal PriceSell { get; set; }


/// <summary>
/// Id provider the best buy price
/// </summary>
public int ProviderBuyId { get; set; }

/// <summary>
/// Current sell price
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
namespace Crypto.Compare.PublicApi.Dtos;

public class SymbolProviderDto
{
/// <summary>
/// Uniq id
/// </summary>
public long Id { get; set; }

/// <summary>
/// Ticker name
/// </summary>
public string Ticker { get; set; }

/// <summary>
/// symbol or viewed name
/// </summary>
public string Symbol { get; set; }

/// <summary>
/// Id provider
/// </summary>
public int ProviderId { get; set; }

/// <summary>
/// Price sell
/// </summary>
public decimal PriceSell { get; set; }

/// <summary>
/// Current sell price
/// </summary>
public decimal PriceBuy { get; set; }

/// <summary>
/// Last date get quotes
/// </summary>
public DateTime UpdatedAt { get; set; }
}
Loading
Loading