Skip to content

Commit

Permalink
code refact
Browse files Browse the repository at this point in the history
  • Loading branch information
EdiWang committed Mar 5, 2024
1 parent 83ffff3 commit d9422ba
Show file tree
Hide file tree
Showing 13 changed files with 33 additions and 34 deletions.
10 changes: 5 additions & 5 deletions src/API/Controllers/LinkController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class LinkController(
IMediator mediator) : ControllerBase
{
[HttpPost("create")]
[ProducesResponseType(typeof(string), StatusCodes.Status204NoContent)]
[ProducesResponseType(StatusCodes.Status204NoContent)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
public async Task<IActionResult> Create(LinkEditModel model)
{
Expand Down Expand Up @@ -68,7 +68,7 @@ public async Task<IActionResult> SetEnable(int id, bool isEnabled)
}

[HttpGet("list")]
[ProducesResponseType(typeof(PagedLinkResult), StatusCodes.Status200OK)]
[ProducesResponseType<PagedLinkResult>(StatusCodes.Status200OK)]
public async Task<IActionResult> List(
string term,
[Range(1, int.MaxValue)] int take,
Expand All @@ -87,7 +87,7 @@ public async Task<IActionResult> List(
}

[HttpPost("list/tags")]
[ProducesResponseType(typeof(PagedLinkResult), StatusCodes.Status200OK)]
[ProducesResponseType<PagedLinkResult>(StatusCodes.Status200OK)]
public async Task<IActionResult> ListByTags(ListByTagsRequest request)
{
var (links, totalRows) = await mediator.Send(new ListByTagsCommand(request));
Expand All @@ -103,7 +103,7 @@ public async Task<IActionResult> ListByTags(ListByTagsRequest request)
}

[HttpGet("{id:int}")]
[ProducesResponseType(typeof(LinkEditModel), StatusCodes.Status200OK)]
[ProducesResponseType<LinkEditModel>(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
public async Task<IActionResult> Get(int id)
{
Expand All @@ -114,7 +114,7 @@ public async Task<IActionResult> Get(int id)
}

[HttpDelete("{id:int}")]
[ProducesResponseType(typeof(LinkEditModel), StatusCodes.Status200OK)]
[ProducesResponseType<LinkEditModel>(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
public async Task<IActionResult> Delete(int id)
{
Expand Down
8 changes: 4 additions & 4 deletions src/API/Controllers/ReportController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace Elf.Api.Controllers;
public class ReportController(IConfiguration configuration, IMediator mediator) : ControllerBase
{
[HttpGet("requests")]
[ProducesResponseType(typeof(PagedRequestTrack), StatusCodes.Status200OK)]
[ProducesResponseType<PagedRequestTrack>(StatusCodes.Status200OK)]
public async Task<IActionResult> Requests(
[Range(1, int.MaxValue)] int take,
[Range(0, int.MaxValue)] int offset)
Expand All @@ -28,15 +28,15 @@ public async Task<IActionResult> Requests(
}

[HttpPost("requests/link")]
[ProducesResponseType(typeof(IReadOnlyList<MostRequestedLinkCount>), StatusCodes.Status200OK)]
[ProducesResponseType<List<MostRequestedLinkCount>>(StatusCodes.Status200OK)]
public async Task<IActionResult> MostRequestedLinks(DateRangeRequest request)
{
var linkCounts = await mediator.Send(new GetMostRequestedLinkCountQuery(request));
return Ok(linkCounts);
}

[HttpPost("requests/clienttype")]
[ProducesResponseType(typeof(IReadOnlyList<ClientTypeCount>), StatusCodes.Status200OK)]
[ProducesResponseType<List<ClientTypeCount>>(StatusCodes.Status200OK)]
public async Task<IActionResult> ClientType(DateRangeRequest request)
{
var types = await mediator.Send(new GetClientTypeCountsQuery(request,
Expand All @@ -45,7 +45,7 @@ public async Task<IActionResult> ClientType(DateRangeRequest request)
}

[HttpPost("tracking")]
[ProducesResponseType(typeof(IReadOnlyList<LinkTrackingDateCount>), StatusCodes.Status200OK)]
[ProducesResponseType<List<LinkTrackingDateCount>>(StatusCodes.Status200OK)]
public async Task<IActionResult> TrackingCount(DateRangeRequest request)
{
var dateCounts = await mediator.Send(new GetLinkTrackingDateCountQuery(request));
Expand Down
2 changes: 1 addition & 1 deletion src/API/Controllers/TagController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace Elf.Api.Controllers;
public class TagController(IMediator mediator) : ControllerBase
{
[HttpGet("list")]
[ProducesResponseType(typeof(List<TagEntity>), StatusCodes.Status200OK)]
[ProducesResponseType<List<TagEntity>>(StatusCodes.Status200OK)]
public async Task<IActionResult> List()
{
var list = await mediator.Send(new GetTagsQuery());
Expand Down
4 changes: 2 additions & 2 deletions src/API/Data/LinkTrackingEntity.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System.ComponentModel.DataAnnotations;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using System.ComponentModel.DataAnnotations;

namespace Elf.Api.Data;

Expand Down
8 changes: 4 additions & 4 deletions src/API/Features/GetClientTypeCountsQuery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@

namespace Elf.Api.Features;

public record GetClientTypeCountsQuery(DateRangeRequest Request, int TopTypes) : IRequest<IReadOnlyList<ClientTypeCount>>;
public record GetClientTypeCountsQuery(DateRangeRequest Request, int TopTypes) : IRequest<List<ClientTypeCount>>;

public class GetClientTypeCountsQueryHandler(ElfDbContext dbContext) : IRequestHandler<GetClientTypeCountsQuery, IReadOnlyList<ClientTypeCount>>
public class GetClientTypeCountsQueryHandler(ElfDbContext dbContext) : IRequestHandler<GetClientTypeCountsQuery, List<ClientTypeCount>>
{
public async Task<IReadOnlyList<ClientTypeCount>> Handle(GetClientTypeCountsQuery request, CancellationToken ct)
public async Task<List<ClientTypeCount>> Handle(GetClientTypeCountsQuery request, CancellationToken ct)
{
var uaParser = Parser.GetDefault();

Expand All @@ -21,7 +21,6 @@ string GetClientTypeName(string userAgent)
return $"{c.OS.Family}-{c.UA.Family}";
}

var utc = DateTime.UtcNow;
var uac = await dbContext.LinkTracking
.Where(p =>
p.RequestTimeUtc <= request.Request.EndDateUtc.Date &&
Expand All @@ -46,6 +45,7 @@ string GetClientTypeName(string userAgent)
if (request.TopTypes > 0) q = q.OrderByDescending(p => p.Count).Take(request.TopTypes);
return q.ToList();
}

return new List<ClientTypeCount>();
}
}
6 changes: 3 additions & 3 deletions src/API/Features/GetLinkTrackingDateCountQuery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@

namespace Elf.Api.Features;

public record GetLinkTrackingDateCountQuery(DateRangeRequest Request) : IRequest<IReadOnlyList<LinkTrackingDateCount>>;
public record GetLinkTrackingDateCountQuery(DateRangeRequest Request) : IRequest<List<LinkTrackingDateCount>>;

public class GetLinkTrackingDateCountQueryHandler(ElfDbContext dbContext) :
IRequestHandler<GetLinkTrackingDateCountQuery, IReadOnlyList<LinkTrackingDateCount>>
IRequestHandler<GetLinkTrackingDateCountQuery, List<LinkTrackingDateCount>>
{
public async Task<IReadOnlyList<LinkTrackingDateCount>> Handle(GetLinkTrackingDateCountQuery request, CancellationToken ct)
public async Task<List<LinkTrackingDateCount>> Handle(GetLinkTrackingDateCountQuery request, CancellationToken ct)
{
var data = await dbContext.LinkTracking
.Where(lt =>
Expand Down
6 changes: 3 additions & 3 deletions src/API/Features/GetMostRequestedLinkCountQuery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@

namespace Elf.Api.Features;

public record GetMostRequestedLinkCountQuery(DateRangeRequest Request) : IRequest<IReadOnlyList<MostRequestedLinkCount>>;
public record GetMostRequestedLinkCountQuery(DateRangeRequest Request) : IRequest<List<MostRequestedLinkCount>>;

public class GetMostRequestedLinkCountQueryHandler(ElfDbContext dbContext) :
IRequestHandler<GetMostRequestedLinkCountQuery, IReadOnlyList<MostRequestedLinkCount>>
IRequestHandler<GetMostRequestedLinkCountQuery, List<MostRequestedLinkCount>>
{
public async Task<IReadOnlyList<MostRequestedLinkCount>> Handle(GetMostRequestedLinkCountQuery request, CancellationToken ct)
public async Task<List<MostRequestedLinkCount>> Handle(GetMostRequestedLinkCountQuery request, CancellationToken ct)
{
var utc = DateTime.UtcNow;

Expand Down
6 changes: 3 additions & 3 deletions src/API/Features/GetRecentRequestsQuery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@

namespace Elf.Api.Features;

public record GetRecentRequestsQuery(int Offset, int Take) : IRequest<(IReadOnlyList<RequestTrack>, int TotalRows)>;
public record GetRecentRequestsQuery(int Offset, int Take) : IRequest<(List<RequestTrack>, int TotalRows)>;

public class GetRecentRequestsQueryHandler(ElfDbContext dbContext) : IRequestHandler<GetRecentRequestsQuery, (IReadOnlyList<RequestTrack>, int TotalRows)>
public class GetRecentRequestsQueryHandler(ElfDbContext dbContext) : IRequestHandler<GetRecentRequestsQuery, (List<RequestTrack>, int TotalRows)>
{
public async Task<(IReadOnlyList<RequestTrack>, int TotalRows)> Handle(GetRecentRequestsQuery request, CancellationToken ct)
public async Task<(List<RequestTrack>, int TotalRows)> Handle(GetRecentRequestsQuery request, CancellationToken ct)
{
var (offset, take) = request;
var query = dbContext.LinkTracking;
Expand Down
6 changes: 3 additions & 3 deletions src/API/Features/ListByTagsCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@

namespace Elf.Api.Features;

public record ListByTagsCommand(ListByTagsRequest Payload) : IRequest<(IReadOnlyList<LinkModel> Links, int TotalRows)>;
public record ListByTagsCommand(ListByTagsRequest Payload) : IRequest<(List<LinkModel> Links, int TotalRows)>;

public class ListByTagsCommandHandler(ElfDbContext dbContext) : IRequestHandler<ListByTagsCommand, (IReadOnlyList<LinkModel> Links, int TotalRows)>
public class ListByTagsCommandHandler(ElfDbContext dbContext) : IRequestHandler<ListByTagsCommand, (List<LinkModel> Links, int TotalRows)>
{
public async Task<(IReadOnlyList<LinkModel> Links, int TotalRows)> Handle(ListByTagsCommand request, CancellationToken ct)
public async Task<(List<LinkModel> Links, int TotalRows)> Handle(ListByTagsCommand request, CancellationToken ct)
{
var query = from l in dbContext.Link.Include(l => l.Tags)
where l.Tags.Any(t => request.Payload.TagIds.Contains(t.Id))
Expand Down
6 changes: 3 additions & 3 deletions src/API/Features/ListLinkQuery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
namespace Elf.Api.Features;

public record ListLinkQuery(int Offset, int Take, string NoteKeyword = null) :
IRequest<(IReadOnlyList<LinkModel> Links, int TotalRows)>;
IRequest<(List<LinkModel> Links, int TotalRows)>;

public class ListLinkQueryHandler(ElfDbContext dbContext) : IRequestHandler<ListLinkQuery, (IReadOnlyList<LinkModel> Links, int TotalRows)>
public class ListLinkQueryHandler(ElfDbContext dbContext) : IRequestHandler<ListLinkQuery, (List<LinkModel> Links, int TotalRows)>
{
public async Task<(IReadOnlyList<LinkModel> Links, int TotalRows)> Handle(ListLinkQuery request, CancellationToken ct)
public async Task<(List<LinkModel> Links, int TotalRows)> Handle(ListLinkQuery request, CancellationToken ct)
{
var query = from l in dbContext.Link.Include(l => l.LinkTrackings)
select l;
Expand Down
2 changes: 1 addition & 1 deletion src/API/Models/PagedLinkResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ namespace Elf.Api.Models;

public class PagedLinkResult : PagedResult
{
public IReadOnlyList<LinkModel> Links { get; set; }
public List<LinkModel> Links { get; set; }
}
1 change: 0 additions & 1 deletion src/Admin/Models/LinkModels.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using Elf.Shared;
using System.ComponentModel.DataAnnotations;
using System.Text.Json.Serialization;

namespace ElfAdmin.Models;
Expand Down
2 changes: 1 addition & 1 deletion src/Elf.Shared/RequestTrack.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,5 @@ public class RequestTrack

public class PagedRequestTrack : PagedResult
{
public IReadOnlyList<RequestTrack> RequestTracks { get; set; }
public List<RequestTrack> RequestTracks { get; set; }
}

0 comments on commit d9422ba

Please sign in to comment.