From e8fbab2721720dea6ccc1977c58eab99dd3bac43 Mon Sep 17 00:00:00 2001 From: Vzart Date: Fri, 28 Jul 2023 22:20:40 +0700 Subject: [PATCH 1/8] add: metric for import document count --- src/Api/Controllers/DashboardController.cs | 21 +++++ .../GetImportedDocumentsMetricsRequest.cs | 9 +++ .../Dashboards/Queries/GetImportDocuments.cs | 81 +++++++++++++++++++ 3 files changed, 111 insertions(+) create mode 100644 src/Api/Controllers/Payload/Requests/Dashboard/GetImportedDocumentsMetricsRequest.cs create mode 100644 src/Application/Dashboards/Queries/GetImportDocuments.cs diff --git a/src/Api/Controllers/DashboardController.cs b/src/Api/Controllers/DashboardController.cs index 9d2421fc..377271d2 100644 --- a/src/Api/Controllers/DashboardController.cs +++ b/src/Api/Controllers/DashboardController.cs @@ -1,12 +1,33 @@ +using Api.Controllers.Payload.Requests.Dashboard; using Application.Common.Interfaces; +using Application.Common.Models; +using Application.Dashboards.Queries; +using Application.Identity; +using Infrastructure.Identity.Authorization; +using Microsoft.AspNetCore.Mvc; namespace Api.Controllers; public class DashboardController : ApiControllerBase { private readonly ICurrentUserService _currentUserService; + public DashboardController(ICurrentUserService currentUserService) { _currentUserService = currentUserService; } + + [RequiresRole(IdentityData.Roles.Admin)] + [HttpPost("import-documents")] + public async Task>>> GetImportDocuments(GetImportedDocumentsMetricsRequest request) + { + var query = new GetImportDocuments.Query() + { + StartDate = request.StartDate, + EndDate = request.EndDate + }; + + var result = await Mediator.Send(query); + return Ok(Result>.Succeed(result)); + } } \ No newline at end of file diff --git a/src/Api/Controllers/Payload/Requests/Dashboard/GetImportedDocumentsMetricsRequest.cs b/src/Api/Controllers/Payload/Requests/Dashboard/GetImportedDocumentsMetricsRequest.cs new file mode 100644 index 00000000..95283540 --- /dev/null +++ b/src/Api/Controllers/Payload/Requests/Dashboard/GetImportedDocumentsMetricsRequest.cs @@ -0,0 +1,9 @@ +namespace Api.Controllers.Payload.Requests.Dashboard; + +public class GetImportedDocumentsMetricsRequest +{ + + public DateTime StartDate { get; set; } + + public DateTime EndDate { get; set; } +} \ No newline at end of file diff --git a/src/Application/Dashboards/Queries/GetImportDocuments.cs b/src/Application/Dashboards/Queries/GetImportDocuments.cs new file mode 100644 index 00000000..68c3c864 --- /dev/null +++ b/src/Application/Dashboards/Queries/GetImportDocuments.cs @@ -0,0 +1,81 @@ +using Application.Common.Interfaces; +using Domain.Statuses; +using FluentValidation; +using MediatR; +using Microsoft.EntityFrameworkCore; +using NodaTime; + +namespace Application.Dashboards.Queries; + +public class GetImportDocuments +{ + public class Validator : AbstractValidator + { + public Validator() + { + RuleLevelCascadeMode = CascadeMode.Stop; + + RuleFor(x => x.StartDate) + .NotEmpty().WithMessage("Start date can not be empty."); + + RuleFor(x => x.EndDate) + .NotEmpty().WithMessage("End date can not be empty."); + } + } + public class Result + { + public string Label { get; set; } + public int Value { get; set; } + } + + public record Query : IRequest> + { + public DateTime StartDate { get; init; } + public DateTime EndDate { get; init; } + + } + + public class QueryHandler : IRequestHandler> + { + private readonly IApplicationDbContext _context; + private readonly IDateTimeProvider _dateTimeProvider; + + public QueryHandler(IApplicationDbContext context, IDateTimeProvider dateTimeProvider) + { + _context = context; + _dateTimeProvider = dateTimeProvider; + } + + public async Task> Handle(Query request, CancellationToken cancellationToken) + { + var result = new List(); + + var currentDate = request.StartDate.Date; + var endDate = request.EndDate.Date; + + while (currentDate <= endDate) + { + var nextDate = currentDate.AddDays(1); + var label = $"{currentDate:dd-MM} to {nextDate:dd-MM}"; + if (nextDate <= endDate) + { + var date = currentDate; + var importedDocumentsCount = await _context.Documents + .Where(x => x.Created >= LocalDateTime.FromDateTime(date) + && x.Created <= LocalDateTime.FromDateTime(nextDate) + && x.Status != DocumentStatus.Issued) + .CountAsync(cancellationToken); + + result.Add(new Result() + { + Label = label, + Value = importedDocumentsCount + }); + } + currentDate = nextDate; + } + + return result; + } + } +} \ No newline at end of file From 256c8ea9bbb2c0382b40ceec4779b93448243493 Mon Sep 17 00:00:00 2001 From: Vzart Date: Fri, 28 Jul 2023 23:21:19 +0700 Subject: [PATCH 2/8] log fucked up --- src/Api/Controllers/AuthController.cs | 13 ++++++++++++- src/Api/Controllers/DashboardController.cs | 2 ++ .../Commands/ApproveOrRejectBorrowRequest.cs | 4 ++-- src/Application/Borrows/Commands/BorrowDocument.cs | 2 +- .../Borrows/Commands/CancelBorrowRequest.cs | 2 +- .../Borrows/Commands/CheckoutDocument.cs | 2 +- src/Application/Borrows/Commands/ReturnDocument.cs | 2 +- src/Application/Borrows/Commands/UpdateBorrow.cs | 2 +- .../Extensions/Logging}/BorrowLogExtensions.cs | 2 +- .../Extensions/Logging}/DocumentLogExtensions.cs | 2 +- .../Extensions/Logging}/EntryLogExtension.cs | 2 +- .../Extensions/Logging}/FolderLogExtension.cs | 2 +- .../Logging}/ImportRequestLogExtensions.cs | 3 +-- .../Extensions/Logging}/LockerLogExtension.cs | 2 +- .../Common/Extensions/Logging/LoginLogExtension.cs | 11 +++++++++++ .../Extensions/Logging}/RoomLogExtension.cs | 2 +- .../Extensions/Logging}/StaffLogExtensions.cs | 2 +- .../Extensions/Logging}/UserLogExtensions.cs | 2 +- src/Application/Common/Messages/LoginLogMessages.cs | 6 ++++++ .../Documents/Commands/DeleteDocument.cs | 1 + .../Documents/Commands/ImportDocument.cs | 1 + src/Application/Documents/Commands/ShareDocument.cs | 1 + .../Documents/Commands/UpdateDocument.cs | 1 + src/Application/Entries/Commands/CreateEntry.cs | 1 + .../Entries/Commands/CreateSharedEntry.cs | 1 + src/Application/Entries/Commands/DeleteBinEntry.cs | 1 + .../Entries/Commands/DownloadDigitalFile.cs | 1 + src/Application/Entries/Commands/MoveEntryToBin.cs | 1 + src/Application/Entries/Commands/RestoreBinEntry.cs | 1 + src/Application/Entries/Commands/ShareEntry.cs | 1 + src/Application/Entries/Commands/UpdateEntry.cs | 1 + src/Application/Folders/Commands/AddFolder.cs | 1 + src/Application/Folders/Commands/RemoveFolder.cs | 1 + src/Application/Folders/Commands/UpdateFolder.cs | 1 + .../Commands/ApproveOrRejectDocument.cs | 1 + .../ImportRequests/Commands/AssignDocument.cs | 1 + .../ImportRequests/Commands/CheckinDocument.cs | 1 + .../Commands/RequestImportDocument.cs | 1 + src/Application/Lockers/Commands/AddLocker.cs | 1 + src/Application/Lockers/Commands/RemoveLocker.cs | 1 + src/Application/Lockers/Commands/UpdateLocker.cs | 1 + src/Application/Rooms/Commands/AddRoom.cs | 1 + src/Application/Rooms/Commands/RemoveRoom.cs | 1 + src/Application/Rooms/Commands/UpdateRoom.cs | 1 + src/Application/Staffs/Commands/AssignStaff.cs | 1 + .../Staffs/Commands/RemoveStaffFromRoom.cs | 1 + src/Application/Users/Commands/AddUser.cs | 1 + src/Application/Users/Commands/UpdateUser.cs | 1 + 48 files changed, 76 insertions(+), 18 deletions(-) rename src/Application/{Borrows => Common/Extensions/Logging}/BorrowLogExtensions.cs (97%) rename src/Application/{Documents => Common/Extensions/Logging}/DocumentLogExtensions.cs (96%) rename src/Application/{Entries => Common/Extensions/Logging}/EntryLogExtension.cs (97%) rename src/Application/{Folders => Common/Extensions/Logging}/FolderLogExtension.cs (94%) rename src/Application/{ImportRequests => Common/Extensions/Logging}/ImportRequestLogExtensions.cs (97%) rename src/Application/{Lockers => Common/Extensions/Logging}/LockerLogExtension.cs (94%) create mode 100644 src/Application/Common/Extensions/Logging/LoginLogExtension.cs rename src/Application/{Rooms => Common/Extensions/Logging}/RoomLogExtension.cs (94%) rename src/Application/{Staffs => Common/Extensions/Logging}/StaffLogExtensions.cs (92%) rename src/Application/{Users => Common/Extensions/Logging}/UserLogExtensions.cs (90%) create mode 100644 src/Application/Common/Messages/LoginLogMessages.cs diff --git a/src/Api/Controllers/AuthController.cs b/src/Api/Controllers/AuthController.cs index 51c3d93c..c4e980c8 100644 --- a/src/Api/Controllers/AuthController.cs +++ b/src/Api/Controllers/AuthController.cs @@ -2,7 +2,9 @@ using System.Security.Authentication; using Api.Controllers.Payload.Requests.Auth; using Api.Controllers.Payload.Responses; +using Application.Common.Extensions.Logging; using Application.Common.Interfaces; +using Application.Common.Logging; using Application.Common.Models; using Application.Common.Models.Dtos; using Application.Users.Queries; @@ -18,10 +20,14 @@ namespace Api.Controllers; public class AuthController : ControllerBase { private readonly IIdentityService _identityService; + private readonly ILogger _logger; + private readonly IDateTimeProvider _dateTimeProvider; - public AuthController(IIdentityService identityService) + public AuthController(IIdentityService identityService, ILogger logger, IDateTimeProvider dateTimeProvider) { _identityService = identityService; + _logger = logger; + _dateTimeProvider = dateTimeProvider; } /// @@ -54,6 +60,11 @@ public async Task Login([FromBody] LoginModel loginModel) LastName = loginSuccess.UserCredentials.LastName, }; + var dateTimeNow = _dateTimeProvider.DateTimeNow; + using (Logging.PushProperties("Login", loginResult.Id, loginResult.Id)) + { + _logger.LogLogin(loginResult.Username, dateTimeNow.ToString("yyyy-MM-dd HH:mm:ss")); + } return Ok(Result.Succeed(loginResult)); }, token => diff --git a/src/Api/Controllers/DashboardController.cs b/src/Api/Controllers/DashboardController.cs index 377271d2..2a2be550 100644 --- a/src/Api/Controllers/DashboardController.cs +++ b/src/Api/Controllers/DashboardController.cs @@ -19,6 +19,8 @@ public DashboardController(ICurrentUserService currentUserService) [RequiresRole(IdentityData.Roles.Admin)] [HttpPost("import-documents")] + [ProducesResponseType(StatusCodes.Status200OK)] + [ProducesResponseType(StatusCodes.Status401Unauthorized)] public async Task>>> GetImportDocuments(GetImportedDocumentsMetricsRequest request) { var query = new GetImportDocuments.Query() diff --git a/src/Application/Borrows/Commands/ApproveOrRejectBorrowRequest.cs b/src/Application/Borrows/Commands/ApproveOrRejectBorrowRequest.cs index ec9397ac..60a9e295 100644 --- a/src/Application/Borrows/Commands/ApproveOrRejectBorrowRequest.cs +++ b/src/Application/Borrows/Commands/ApproveOrRejectBorrowRequest.cs @@ -120,7 +120,7 @@ is BorrowRequestStatus.Approved using (Logging.PushProperties("BorrowRequest", borrowRequest.Id, request.CurrentUserId)) { - _logger.LogApproveBorrowRequest(borrowRequest.Document.Id.ToString(), borrowRequest.Id.ToString()); + Common.Extensions.Logging.BorrowLogExtensions.LogApproveBorrowRequest(_logger, borrowRequest.Document.Id.ToString(), borrowRequest.Id.ToString()); } } @@ -130,7 +130,7 @@ is BorrowRequestStatus.Approved using (Logging.PushProperties("BorrowRequest", borrowRequest.Id, request.CurrentUserId)) { - _logger.LogRejectBorrowRequest(borrowRequest.Document.Id.ToString(), borrowRequest.Id.ToString()); + Common.Extensions.Logging.BorrowLogExtensions.LogRejectBorrowRequest(_logger, borrowRequest.Document.Id.ToString(), borrowRequest.Id.ToString()); } } diff --git a/src/Application/Borrows/Commands/BorrowDocument.cs b/src/Application/Borrows/Commands/BorrowDocument.cs index 4ef3dbaf..6b2896a4 100644 --- a/src/Application/Borrows/Commands/BorrowDocument.cs +++ b/src/Application/Borrows/Commands/BorrowDocument.cs @@ -161,7 +161,7 @@ is BorrowRequestStatus.Approved await _context.SaveChangesAsync(cancellationToken); using (Logging.PushProperties("Request", document.Id, user.Id)) { - _logger.LogBorrowDocument(document.Id.ToString(), result.Entity.Id.ToString()); + Common.Extensions.Logging.BorrowLogExtensions.LogBorrowDocument(_logger, document.Id.ToString(), result.Entity.Id.ToString()); } return _mapper.Map(result.Entity); } diff --git a/src/Application/Borrows/Commands/CancelBorrowRequest.cs b/src/Application/Borrows/Commands/CancelBorrowRequest.cs index 6f9ffd79..135d05ef 100644 --- a/src/Application/Borrows/Commands/CancelBorrowRequest.cs +++ b/src/Application/Borrows/Commands/CancelBorrowRequest.cs @@ -65,7 +65,7 @@ public async Task Handle(Command request, CancellationToken cancellat await _context.SaveChangesAsync(cancellationToken); using (Logging.PushProperties("BorrowRequest", borrowRequest.Id, request.CurrentUserId)) { - _logger.LogCancelBorrowRequest(borrowRequest.Document.Id.ToString(), borrowRequest.Id.ToString()); + Common.Extensions.Logging.BorrowLogExtensions.LogCancelBorrowRequest(_logger, borrowRequest.Document.Id.ToString(), borrowRequest.Id.ToString()); } return _mapper.Map(result.Entity); } diff --git a/src/Application/Borrows/Commands/CheckoutDocument.cs b/src/Application/Borrows/Commands/CheckoutDocument.cs index 275b344f..0dd95d0a 100644 --- a/src/Application/Borrows/Commands/CheckoutDocument.cs +++ b/src/Application/Borrows/Commands/CheckoutDocument.cs @@ -90,7 +90,7 @@ public async Task Handle(Command request, CancellationToken cancellat await _context.SaveChangesAsync(cancellationToken); using (Logging.PushProperties("BorrowRequest", borrowRequest.Id, request.CurrentStaff.Id)) { - _logger.LogCheckoutDocument(borrowRequest.Document.Id.ToString(), borrowRequest.Id.ToString()); + Common.Extensions.Logging.BorrowLogExtensions.LogCheckoutDocument(_logger, borrowRequest.Document.Id.ToString(), borrowRequest.Id.ToString()); } return _mapper.Map(result.Entity); } diff --git a/src/Application/Borrows/Commands/ReturnDocument.cs b/src/Application/Borrows/Commands/ReturnDocument.cs index d2e3cc37..2ee6da97 100644 --- a/src/Application/Borrows/Commands/ReturnDocument.cs +++ b/src/Application/Borrows/Commands/ReturnDocument.cs @@ -91,7 +91,7 @@ public async Task Handle(Command request, CancellationToken cancellat await _context.SaveChangesAsync(cancellationToken); using (Logging.PushProperties("BorrowRequest", borrowRequest.Id, request.CurrentUser.Id)) { - _logger.LogReturnDocument(borrowRequest.Document.Id.ToString(), borrowRequest.Id.ToString()); + Common.Extensions.Logging.BorrowLogExtensions.LogReturnDocument(_logger, borrowRequest.Document.Id.ToString(), borrowRequest.Id.ToString()); } return _mapper.Map(result.Entity); } diff --git a/src/Application/Borrows/Commands/UpdateBorrow.cs b/src/Application/Borrows/Commands/UpdateBorrow.cs index 0d1d9512..cdbf0d9e 100644 --- a/src/Application/Borrows/Commands/UpdateBorrow.cs +++ b/src/Application/Borrows/Commands/UpdateBorrow.cs @@ -115,7 +115,7 @@ is BorrowRequestStatus.Approved using (Logging.PushProperties("BorrowRequest", borrowRequest.Id, request.CurrentUser.Id)) { - _logger.LogUpdateBorrow(borrowRequest.Document.Id.ToString(), borrowRequest.Id.ToString()); + Common.Extensions.Logging.BorrowLogExtensions.LogUpdateBorrow(_logger, borrowRequest.Document.Id.ToString(), borrowRequest.Id.ToString()); } return _mapper.Map(result.Entity); diff --git a/src/Application/Borrows/BorrowLogExtensions.cs b/src/Application/Common/Extensions/Logging/BorrowLogExtensions.cs similarity index 97% rename from src/Application/Borrows/BorrowLogExtensions.cs rename to src/Application/Common/Extensions/Logging/BorrowLogExtensions.cs index 04df6443..cd33de16 100644 --- a/src/Application/Borrows/BorrowLogExtensions.cs +++ b/src/Application/Common/Extensions/Logging/BorrowLogExtensions.cs @@ -1,7 +1,7 @@ using Application.Common.Messages; using Microsoft.Extensions.Logging; -namespace Application.Borrows; +namespace Application.Common.Extensions.Logging; public static partial class BorrowLogExtensions { diff --git a/src/Application/Documents/DocumentLogExtensions.cs b/src/Application/Common/Extensions/Logging/DocumentLogExtensions.cs similarity index 96% rename from src/Application/Documents/DocumentLogExtensions.cs rename to src/Application/Common/Extensions/Logging/DocumentLogExtensions.cs index 790b5479..13e8a07b 100644 --- a/src/Application/Documents/DocumentLogExtensions.cs +++ b/src/Application/Common/Extensions/Logging/DocumentLogExtensions.cs @@ -2,7 +2,7 @@ using Microsoft.Extensions.Logging; using EventId = Application.Common.Logging.EventId; -namespace Application.Documents; +namespace Application.Common.Extensions.Logging; public static partial class DocumentLogExtensions { diff --git a/src/Application/Entries/EntryLogExtension.cs b/src/Application/Common/Extensions/Logging/EntryLogExtension.cs similarity index 97% rename from src/Application/Entries/EntryLogExtension.cs rename to src/Application/Common/Extensions/Logging/EntryLogExtension.cs index e2f850b4..d99b237e 100644 --- a/src/Application/Entries/EntryLogExtension.cs +++ b/src/Application/Common/Extensions/Logging/EntryLogExtension.cs @@ -2,7 +2,7 @@ using Microsoft.Extensions.Logging; using EventId = Application.Common.Logging.EventId; -namespace Application.Entries; +namespace Application.Common.Extensions.Logging; public static partial class EntryLogExtension { diff --git a/src/Application/Folders/FolderLogExtension.cs b/src/Application/Common/Extensions/Logging/FolderLogExtension.cs similarity index 94% rename from src/Application/Folders/FolderLogExtension.cs rename to src/Application/Common/Extensions/Logging/FolderLogExtension.cs index aca354d6..e5da414c 100644 --- a/src/Application/Folders/FolderLogExtension.cs +++ b/src/Application/Common/Extensions/Logging/FolderLogExtension.cs @@ -2,7 +2,7 @@ using Microsoft.Extensions.Logging; using EventId = Application.Common.Logging.EventId; -namespace Application.Folders; +namespace Application.Common.Extensions.Logging; public static partial class FolderLogExtension { diff --git a/src/Application/ImportRequests/ImportRequestLogExtensions.cs b/src/Application/Common/Extensions/Logging/ImportRequestLogExtensions.cs similarity index 97% rename from src/Application/ImportRequests/ImportRequestLogExtensions.cs rename to src/Application/Common/Extensions/Logging/ImportRequestLogExtensions.cs index 6719dde6..5143dc19 100644 --- a/src/Application/ImportRequests/ImportRequestLogExtensions.cs +++ b/src/Application/Common/Extensions/Logging/ImportRequestLogExtensions.cs @@ -1,9 +1,8 @@ using Application.Common.Messages; -using Domain.Entities.Physical; using Microsoft.Extensions.Logging; using EventId = Application.Common.Logging.EventId; -namespace Application.ImportRequests; +namespace Application.Common.Extensions.Logging; public static partial class ImportRequestLogExtensions { // Approve or reject document diff --git a/src/Application/Lockers/LockerLogExtension.cs b/src/Application/Common/Extensions/Logging/LockerLogExtension.cs similarity index 94% rename from src/Application/Lockers/LockerLogExtension.cs rename to src/Application/Common/Extensions/Logging/LockerLogExtension.cs index f38951ce..db6d4505 100644 --- a/src/Application/Lockers/LockerLogExtension.cs +++ b/src/Application/Common/Extensions/Logging/LockerLogExtension.cs @@ -2,7 +2,7 @@ using Microsoft.Extensions.Logging; using EventId = Application.Common.Logging.EventId; -namespace Application.Lockers; +namespace Application.Common.Extensions.Logging; public static partial class LockerLogExtension { diff --git a/src/Application/Common/Extensions/Logging/LoginLogExtension.cs b/src/Application/Common/Extensions/Logging/LoginLogExtension.cs new file mode 100644 index 00000000..0bd03761 --- /dev/null +++ b/src/Application/Common/Extensions/Logging/LoginLogExtension.cs @@ -0,0 +1,11 @@ +using Application.Common.Messages; +using Microsoft.Extensions.Logging; +using EventId = Application.Common.Logging.EventId; + +namespace Application.Common.Extensions.Logging; + +public static partial class LoginLogExtension +{ + [LoggerMessage(Level = LogLevel.Information, Message = LoginLogMessages.Login, EventId = EventId.Approve)] + public static partial void LogLogin(this ILogger logger, string username, string time); +} \ No newline at end of file diff --git a/src/Application/Rooms/RoomLogExtension.cs b/src/Application/Common/Extensions/Logging/RoomLogExtension.cs similarity index 94% rename from src/Application/Rooms/RoomLogExtension.cs rename to src/Application/Common/Extensions/Logging/RoomLogExtension.cs index 13d5f9fe..d1cd8f9f 100644 --- a/src/Application/Rooms/RoomLogExtension.cs +++ b/src/Application/Common/Extensions/Logging/RoomLogExtension.cs @@ -2,7 +2,7 @@ using Microsoft.Extensions.Logging; using EventId = Application.Common.Logging.EventId; -namespace Application.Rooms; +namespace Application.Common.Extensions.Logging; public static partial class RoomLogExtension { [LoggerMessage(Level = LogLevel.Information, Message = RoomLogMessage.Add, EventId = EventId.Add)] diff --git a/src/Application/Staffs/StaffLogExtensions.cs b/src/Application/Common/Extensions/Logging/StaffLogExtensions.cs similarity index 92% rename from src/Application/Staffs/StaffLogExtensions.cs rename to src/Application/Common/Extensions/Logging/StaffLogExtensions.cs index 8577103e..071fd548 100644 --- a/src/Application/Staffs/StaffLogExtensions.cs +++ b/src/Application/Common/Extensions/Logging/StaffLogExtensions.cs @@ -2,7 +2,7 @@ using Microsoft.Extensions.Logging; using EventId = Application.Common.Logging.EventId; -namespace Application.Staffs; +namespace Application.Common.Extensions.Logging; public static partial class StaffLogExtensions { [LoggerMessage(Level = LogLevel.Information, Message = UserLogMessages.Staff.AssignStaff, EventId = EventId.Add)] diff --git a/src/Application/Users/UserLogExtensions.cs b/src/Application/Common/Extensions/Logging/UserLogExtensions.cs similarity index 90% rename from src/Application/Users/UserLogExtensions.cs rename to src/Application/Common/Extensions/Logging/UserLogExtensions.cs index 7607bc02..7dd99cc7 100644 --- a/src/Application/Users/UserLogExtensions.cs +++ b/src/Application/Common/Extensions/Logging/UserLogExtensions.cs @@ -1,7 +1,7 @@ using Application.Common.Messages; using Microsoft.Extensions.Logging; -namespace Application.Users; +namespace Application.Common.Extensions.Logging; public static partial class UserLogExtensions { diff --git a/src/Application/Common/Messages/LoginLogMessages.cs b/src/Application/Common/Messages/LoginLogMessages.cs new file mode 100644 index 00000000..d2bead8a --- /dev/null +++ b/src/Application/Common/Messages/LoginLogMessages.cs @@ -0,0 +1,6 @@ +namespace Application.Common.Messages; + +public class LoginLogMessages +{ + public const string Login = "User with id {Username} Log in at {Time}"; +} \ No newline at end of file diff --git a/src/Application/Documents/Commands/DeleteDocument.cs b/src/Application/Documents/Commands/DeleteDocument.cs index 176a6845..6331ff18 100644 --- a/src/Application/Documents/Commands/DeleteDocument.cs +++ b/src/Application/Documents/Commands/DeleteDocument.cs @@ -1,3 +1,4 @@ +using Application.Common.Extensions.Logging; using Application.Common.Interfaces; using Application.Common.Logging; using Application.Common.Models.Dtos.Physical; diff --git a/src/Application/Documents/Commands/ImportDocument.cs b/src/Application/Documents/Commands/ImportDocument.cs index b105ff93..9a3ac6ee 100644 --- a/src/Application/Documents/Commands/ImportDocument.cs +++ b/src/Application/Documents/Commands/ImportDocument.cs @@ -1,4 +1,5 @@ using Application.Common.Exceptions; +using Application.Common.Extensions.Logging; using Application.Common.Interfaces; using Application.Common.Logging; using Application.Common.Models.Dtos.Physical; diff --git a/src/Application/Documents/Commands/ShareDocument.cs b/src/Application/Documents/Commands/ShareDocument.cs index c1359f80..9fd5e7bb 100644 --- a/src/Application/Documents/Commands/ShareDocument.cs +++ b/src/Application/Documents/Commands/ShareDocument.cs @@ -1,4 +1,5 @@ using Application.Common.Exceptions; +using Application.Common.Extensions.Logging; using Application.Common.Interfaces; using Application.Common.Logging; using Application.Common.Models.Dtos.Physical; diff --git a/src/Application/Documents/Commands/UpdateDocument.cs b/src/Application/Documents/Commands/UpdateDocument.cs index fc8e5aea..c67a6cdd 100644 --- a/src/Application/Documents/Commands/UpdateDocument.cs +++ b/src/Application/Documents/Commands/UpdateDocument.cs @@ -1,5 +1,6 @@ using Application.Common.Exceptions; using Application.Common.Extensions; +using Application.Common.Extensions.Logging; using Application.Common.Interfaces; using Application.Common.Logging; using Application.Common.Models.Dtos.Physical; diff --git a/src/Application/Entries/Commands/CreateEntry.cs b/src/Application/Entries/Commands/CreateEntry.cs index 1a9ddb4c..7062d294 100644 --- a/src/Application/Entries/Commands/CreateEntry.cs +++ b/src/Application/Entries/Commands/CreateEntry.cs @@ -1,4 +1,5 @@ using Application.Common.Exceptions; +using Application.Common.Extensions.Logging; using Application.Common.Interfaces; using Application.Common.Logging; using Application.Common.Models.Dtos.Digital; diff --git a/src/Application/Entries/Commands/CreateSharedEntry.cs b/src/Application/Entries/Commands/CreateSharedEntry.cs index 5fe9d8f0..ebefb8b7 100644 --- a/src/Application/Entries/Commands/CreateSharedEntry.cs +++ b/src/Application/Entries/Commands/CreateSharedEntry.cs @@ -1,4 +1,5 @@ using Application.Common.Exceptions; +using Application.Common.Extensions.Logging; using Application.Common.Interfaces; using Application.Common.Logging; using Application.Common.Models.Dtos.Digital; diff --git a/src/Application/Entries/Commands/DeleteBinEntry.cs b/src/Application/Entries/Commands/DeleteBinEntry.cs index 10379f82..0b4ff8cb 100644 --- a/src/Application/Entries/Commands/DeleteBinEntry.cs +++ b/src/Application/Entries/Commands/DeleteBinEntry.cs @@ -1,4 +1,5 @@ using Application.Common.Exceptions; +using Application.Common.Extensions.Logging; using Application.Common.Interfaces; using Application.Common.Logging; using Application.Common.Models.Dtos.Digital; diff --git a/src/Application/Entries/Commands/DownloadDigitalFile.cs b/src/Application/Entries/Commands/DownloadDigitalFile.cs index 85048f97..88ecfe3f 100644 --- a/src/Application/Entries/Commands/DownloadDigitalFile.cs +++ b/src/Application/Entries/Commands/DownloadDigitalFile.cs @@ -1,4 +1,5 @@ using Application.Common.Exceptions; +using Application.Common.Extensions.Logging; using Application.Common.Interfaces; using Application.Common.Logging; using Application.Common.Models.Operations; diff --git a/src/Application/Entries/Commands/MoveEntryToBin.cs b/src/Application/Entries/Commands/MoveEntryToBin.cs index e83c245b..6d30a93c 100644 --- a/src/Application/Entries/Commands/MoveEntryToBin.cs +++ b/src/Application/Entries/Commands/MoveEntryToBin.cs @@ -1,4 +1,5 @@ using Application.Common.Exceptions; +using Application.Common.Extensions.Logging; using Application.Common.Interfaces; using Application.Common.Logging; using Application.Common.Models.Dtos.Digital; diff --git a/src/Application/Entries/Commands/RestoreBinEntry.cs b/src/Application/Entries/Commands/RestoreBinEntry.cs index da924ade..fb3d74ea 100644 --- a/src/Application/Entries/Commands/RestoreBinEntry.cs +++ b/src/Application/Entries/Commands/RestoreBinEntry.cs @@ -1,4 +1,5 @@ using Application.Common.Exceptions; +using Application.Common.Extensions.Logging; using Application.Common.Interfaces; using Application.Common.Logging; using Application.Common.Models.Dtos.Digital; diff --git a/src/Application/Entries/Commands/ShareEntry.cs b/src/Application/Entries/Commands/ShareEntry.cs index 320454b6..8fd8b3a2 100644 --- a/src/Application/Entries/Commands/ShareEntry.cs +++ b/src/Application/Entries/Commands/ShareEntry.cs @@ -1,5 +1,6 @@ using System.Configuration; using Application.Common.Exceptions; +using Application.Common.Extensions.Logging; using Application.Common.Interfaces; using Application.Common.Logging; using Application.Common.Models.Dtos.Digital; diff --git a/src/Application/Entries/Commands/UpdateEntry.cs b/src/Application/Entries/Commands/UpdateEntry.cs index 7f463cb7..2d412646 100644 --- a/src/Application/Entries/Commands/UpdateEntry.cs +++ b/src/Application/Entries/Commands/UpdateEntry.cs @@ -1,4 +1,5 @@ using Application.Common.Exceptions; +using Application.Common.Extensions.Logging; using Application.Common.Interfaces; using Application.Common.Logging; using Application.Common.Models.Dtos.Digital; diff --git a/src/Application/Folders/Commands/AddFolder.cs b/src/Application/Folders/Commands/AddFolder.cs index 2810e5f0..59bc069c 100644 --- a/src/Application/Folders/Commands/AddFolder.cs +++ b/src/Application/Folders/Commands/AddFolder.cs @@ -1,5 +1,6 @@ using Application.Common.Exceptions; using Application.Common.Extensions; +using Application.Common.Extensions.Logging; using Application.Common.Interfaces; using Application.Common.Logging; using Application.Common.Models.Dtos.Physical; diff --git a/src/Application/Folders/Commands/RemoveFolder.cs b/src/Application/Folders/Commands/RemoveFolder.cs index d9d24596..557fe4c4 100644 --- a/src/Application/Folders/Commands/RemoveFolder.cs +++ b/src/Application/Folders/Commands/RemoveFolder.cs @@ -1,5 +1,6 @@ using Application.Common.Exceptions; using Application.Common.Extensions; +using Application.Common.Extensions.Logging; using Application.Common.Interfaces; using Application.Common.Logging; using Application.Common.Models.Dtos.Physical; diff --git a/src/Application/Folders/Commands/UpdateFolder.cs b/src/Application/Folders/Commands/UpdateFolder.cs index ce5c058d..8338cebb 100644 --- a/src/Application/Folders/Commands/UpdateFolder.cs +++ b/src/Application/Folders/Commands/UpdateFolder.cs @@ -1,5 +1,6 @@ using Application.Common.Exceptions; using Application.Common.Extensions; +using Application.Common.Extensions.Logging; using Application.Common.Interfaces; using Application.Common.Logging; using Application.Common.Models.Dtos.Physical; diff --git a/src/Application/ImportRequests/Commands/ApproveOrRejectDocument.cs b/src/Application/ImportRequests/Commands/ApproveOrRejectDocument.cs index 9beb97d5..f93c2dfa 100644 --- a/src/Application/ImportRequests/Commands/ApproveOrRejectDocument.cs +++ b/src/Application/ImportRequests/Commands/ApproveOrRejectDocument.cs @@ -1,5 +1,6 @@ using Application.Common.Exceptions; using Application.Common.Extensions; +using Application.Common.Extensions.Logging; using Application.Common.Interfaces; using Application.Common.Logging; using Application.Common.Models.Dtos.ImportDocument; diff --git a/src/Application/ImportRequests/Commands/AssignDocument.cs b/src/Application/ImportRequests/Commands/AssignDocument.cs index 1397f4b5..e134f927 100644 --- a/src/Application/ImportRequests/Commands/AssignDocument.cs +++ b/src/Application/ImportRequests/Commands/AssignDocument.cs @@ -1,4 +1,5 @@ using Application.Common.Exceptions; +using Application.Common.Extensions.Logging; using Application.Common.Interfaces; using Application.Common.Logging; using Application.Common.Models.Dtos.ImportDocument; diff --git a/src/Application/ImportRequests/Commands/CheckinDocument.cs b/src/Application/ImportRequests/Commands/CheckinDocument.cs index 39b78f6c..fa30b592 100644 --- a/src/Application/ImportRequests/Commands/CheckinDocument.cs +++ b/src/Application/ImportRequests/Commands/CheckinDocument.cs @@ -1,4 +1,5 @@ using Application.Common.Exceptions; +using Application.Common.Extensions.Logging; using Application.Common.Interfaces; using Application.Common.Logging; using Application.Common.Models.Dtos.Physical; diff --git a/src/Application/ImportRequests/Commands/RequestImportDocument.cs b/src/Application/ImportRequests/Commands/RequestImportDocument.cs index 2e768164..ef1b088e 100644 --- a/src/Application/ImportRequests/Commands/RequestImportDocument.cs +++ b/src/Application/ImportRequests/Commands/RequestImportDocument.cs @@ -1,4 +1,5 @@ using Application.Common.Exceptions; +using Application.Common.Extensions.Logging; using Application.Common.Interfaces; using Application.Common.Logging; using Application.Common.Models.Dtos.ImportDocument; diff --git a/src/Application/Lockers/Commands/AddLocker.cs b/src/Application/Lockers/Commands/AddLocker.cs index 41c558e6..f2123351 100644 --- a/src/Application/Lockers/Commands/AddLocker.cs +++ b/src/Application/Lockers/Commands/AddLocker.cs @@ -1,4 +1,5 @@ using Application.Common.Exceptions; +using Application.Common.Extensions.Logging; using Application.Common.Interfaces; using Application.Common.Logging; using Application.Common.Models.Dtos.Physical; diff --git a/src/Application/Lockers/Commands/RemoveLocker.cs b/src/Application/Lockers/Commands/RemoveLocker.cs index 9860aea9..f5c3cb55 100644 --- a/src/Application/Lockers/Commands/RemoveLocker.cs +++ b/src/Application/Lockers/Commands/RemoveLocker.cs @@ -1,4 +1,5 @@ using Application.Common.Exceptions; +using Application.Common.Extensions.Logging; using Application.Common.Interfaces; using Application.Common.Logging; using Application.Common.Models.Dtos.Physical; diff --git a/src/Application/Lockers/Commands/UpdateLocker.cs b/src/Application/Lockers/Commands/UpdateLocker.cs index 45b5aed9..d4db5580 100644 --- a/src/Application/Lockers/Commands/UpdateLocker.cs +++ b/src/Application/Lockers/Commands/UpdateLocker.cs @@ -1,4 +1,5 @@ using Application.Common.Exceptions; +using Application.Common.Extensions.Logging; using Application.Common.Interfaces; using Application.Common.Logging; using Application.Common.Models.Dtos.Physical; diff --git a/src/Application/Rooms/Commands/AddRoom.cs b/src/Application/Rooms/Commands/AddRoom.cs index 9dff5688..e6692789 100644 --- a/src/Application/Rooms/Commands/AddRoom.cs +++ b/src/Application/Rooms/Commands/AddRoom.cs @@ -1,4 +1,5 @@ using Application.Common.Exceptions; +using Application.Common.Extensions.Logging; using Application.Common.Interfaces; using Application.Common.Logging; using Application.Common.Models.Dtos.Physical; diff --git a/src/Application/Rooms/Commands/RemoveRoom.cs b/src/Application/Rooms/Commands/RemoveRoom.cs index 4619d011..73d7579c 100644 --- a/src/Application/Rooms/Commands/RemoveRoom.cs +++ b/src/Application/Rooms/Commands/RemoveRoom.cs @@ -1,4 +1,5 @@ using Application.Common.Exceptions; +using Application.Common.Extensions.Logging; using Application.Common.Interfaces; using Application.Common.Logging; using Application.Common.Models.Dtos.Physical; diff --git a/src/Application/Rooms/Commands/UpdateRoom.cs b/src/Application/Rooms/Commands/UpdateRoom.cs index b630cdaa..0cdea5ff 100644 --- a/src/Application/Rooms/Commands/UpdateRoom.cs +++ b/src/Application/Rooms/Commands/UpdateRoom.cs @@ -1,4 +1,5 @@ using Application.Common.Exceptions; +using Application.Common.Extensions.Logging; using Application.Common.Interfaces; using Application.Common.Logging; using Application.Common.Models.Dtos.Physical; diff --git a/src/Application/Staffs/Commands/AssignStaff.cs b/src/Application/Staffs/Commands/AssignStaff.cs index e8dd03ef..32f99ee9 100644 --- a/src/Application/Staffs/Commands/AssignStaff.cs +++ b/src/Application/Staffs/Commands/AssignStaff.cs @@ -1,4 +1,5 @@ using Application.Common.Exceptions; +using Application.Common.Extensions.Logging; using Application.Common.Interfaces; using Application.Common.Logging; using Application.Common.Models.Dtos.Physical; diff --git a/src/Application/Staffs/Commands/RemoveStaffFromRoom.cs b/src/Application/Staffs/Commands/RemoveStaffFromRoom.cs index c87aec9f..fdd95573 100644 --- a/src/Application/Staffs/Commands/RemoveStaffFromRoom.cs +++ b/src/Application/Staffs/Commands/RemoveStaffFromRoom.cs @@ -1,4 +1,5 @@ using Application.Common.Exceptions; +using Application.Common.Extensions.Logging; using Application.Common.Interfaces; using Application.Common.Logging; using Application.Common.Models.Dtos.Physical; diff --git a/src/Application/Users/Commands/AddUser.cs b/src/Application/Users/Commands/AddUser.cs index 269c3d91..17dd5b03 100644 --- a/src/Application/Users/Commands/AddUser.cs +++ b/src/Application/Users/Commands/AddUser.cs @@ -1,5 +1,6 @@ using Application.Common.Exceptions; using Application.Common.Extensions; +using Application.Common.Extensions.Logging; using Application.Common.Interfaces; using Application.Common.Logging; using Application.Helpers; diff --git a/src/Application/Users/Commands/UpdateUser.cs b/src/Application/Users/Commands/UpdateUser.cs index 805b0202..584f57d2 100644 --- a/src/Application/Users/Commands/UpdateUser.cs +++ b/src/Application/Users/Commands/UpdateUser.cs @@ -1,4 +1,5 @@ using Application.Common.Extensions; +using Application.Common.Extensions.Logging; using Application.Common.Interfaces; using Application.Common.Logging; using Application.Common.Messages; From 5b1d863652916b27f401bd8a7bdc46c6f6a5ff14 Mon Sep 17 00:00:00 2001 From: Vzart Date: Sat, 29 Jul 2023 14:07:01 +0700 Subject: [PATCH 3/8] minor stuff --- src/Application/Common/Messages/LoginLogMessages.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Application/Common/Messages/LoginLogMessages.cs b/src/Application/Common/Messages/LoginLogMessages.cs index d2bead8a..102e3e4b 100644 --- a/src/Application/Common/Messages/LoginLogMessages.cs +++ b/src/Application/Common/Messages/LoginLogMessages.cs @@ -2,5 +2,5 @@ public class LoginLogMessages { - public const string Login = "User with id {Username} Log in at {Time}"; + public const string Login = "User with username {Username} Log in at {Time}"; } \ No newline at end of file From 2ed29801ab0b6ede9827acf217fc84cb6d2828c1 Mon Sep 17 00:00:00 2001 From: Vzart Date: Sat, 29 Jul 2023 19:09:18 +0700 Subject: [PATCH 4/8] get login users metric --- src/Api/Controllers/DashboardController.cs | 22 +++++++++- .../Dashboard/GetLoggedInUsersRequest.cs | 6 +++ src/Api/appsettings.Development.json | 2 +- .../Models/Dtos/DashBoard/MetricResultDto.cs | 7 +++ .../Dashboards/Queries/GetImportDocuments.cs | 21 +++------ .../Dashboards/Queries/GetLoggedInUser.cs | 44 +++++++++++++++++++ 6 files changed, 85 insertions(+), 17 deletions(-) create mode 100644 src/Api/Controllers/Payload/Requests/Dashboard/GetLoggedInUsersRequest.cs create mode 100644 src/Application/Common/Models/Dtos/DashBoard/MetricResultDto.cs create mode 100644 src/Application/Dashboards/Queries/GetLoggedInUser.cs diff --git a/src/Api/Controllers/DashboardController.cs b/src/Api/Controllers/DashboardController.cs index 2a2be550..f4e26c12 100644 --- a/src/Api/Controllers/DashboardController.cs +++ b/src/Api/Controllers/DashboardController.cs @@ -1,6 +1,7 @@ using Api.Controllers.Payload.Requests.Dashboard; using Application.Common.Interfaces; using Application.Common.Models; +using Application.Common.Models.Dtos.DashBoard; using Application.Dashboards.Queries; using Application.Identity; using Infrastructure.Identity.Authorization; @@ -21,7 +22,8 @@ public DashboardController(ICurrentUserService currentUserService) [HttpPost("import-documents")] [ProducesResponseType(StatusCodes.Status200OK)] [ProducesResponseType(StatusCodes.Status401Unauthorized)] - public async Task>>> GetImportDocuments(GetImportedDocumentsMetricsRequest request) + public async Task>>> GetImportDocuments( + [FromBody] GetImportedDocumentsMetricsRequest request) { var query = new GetImportDocuments.Query() { @@ -30,6 +32,22 @@ public DashboardController(ICurrentUserService currentUserService) }; var result = await Mediator.Send(query); - return Ok(Result>.Succeed(result)); + return Ok(Result>.Succeed(result)); + } + + [RequiresRole(IdentityData.Roles.Admin)] + [HttpPost("logins")] + [ProducesResponseType(StatusCodes.Status200OK)] + [ProducesResponseType(StatusCodes.Status401Unauthorized)] + public async Task>> GetLoggedInUsers( + [FromBody] DateTime date) + { + var query = new GetLoggedInUser.Query() + { + Date = date + }; + + var result = await Mediator.Send(query); + return Ok(Result.Succeed(result)); } } \ No newline at end of file diff --git a/src/Api/Controllers/Payload/Requests/Dashboard/GetLoggedInUsersRequest.cs b/src/Api/Controllers/Payload/Requests/Dashboard/GetLoggedInUsersRequest.cs new file mode 100644 index 00000000..2b83b388 --- /dev/null +++ b/src/Api/Controllers/Payload/Requests/Dashboard/GetLoggedInUsersRequest.cs @@ -0,0 +1,6 @@ +namespace Api.Controllers.Payload.Requests.Dashboard; + +public class GetLoggedInUsersRequest +{ + public DateTime Date { get; set; } +} \ No newline at end of file diff --git a/src/Api/appsettings.Development.json b/src/Api/appsettings.Development.json index b547eab4..6ede2e2e 100644 --- a/src/Api/appsettings.Development.json +++ b/src/Api/appsettings.Development.json @@ -36,7 +36,7 @@ { "Name": "PostgreSQL", "Args": { - "connectionString": "Server=postgres;Port=5432;Database=mydb;User ID=profiletest;Password=supasecured", + "connectionString": "Server=localhost;Port=5432;Database=mydb;User ID=profiletest;Password=supasecured", "tableName": "Logs", "schemaName": null, "needAutoCreateTable": true, diff --git a/src/Application/Common/Models/Dtos/DashBoard/MetricResultDto.cs b/src/Application/Common/Models/Dtos/DashBoard/MetricResultDto.cs new file mode 100644 index 00000000..0c98f1e2 --- /dev/null +++ b/src/Application/Common/Models/Dtos/DashBoard/MetricResultDto.cs @@ -0,0 +1,7 @@ +namespace Application.Common.Models.Dtos.DashBoard; + +public class MetricResultDto +{ + public string Label { get; set; } + public int Value { get; set; } +} \ No newline at end of file diff --git a/src/Application/Dashboards/Queries/GetImportDocuments.cs b/src/Application/Dashboards/Queries/GetImportDocuments.cs index 68c3c864..1c4b12cd 100644 --- a/src/Application/Dashboards/Queries/GetImportDocuments.cs +++ b/src/Application/Dashboards/Queries/GetImportDocuments.cs @@ -1,4 +1,5 @@ using Application.Common.Interfaces; +using Application.Common.Models.Dtos.DashBoard; using Domain.Statuses; using FluentValidation; using MediatR; @@ -22,33 +23,25 @@ public Validator() .NotEmpty().WithMessage("End date can not be empty."); } } - public class Result - { - public string Label { get; set; } - public int Value { get; set; } - } - - public record Query : IRequest> + public record Query : IRequest> { public DateTime StartDate { get; init; } public DateTime EndDate { get; init; } } - public class QueryHandler : IRequestHandler> + public class QueryHandler : IRequestHandler> { private readonly IApplicationDbContext _context; - private readonly IDateTimeProvider _dateTimeProvider; - public QueryHandler(IApplicationDbContext context, IDateTimeProvider dateTimeProvider) + public QueryHandler(IApplicationDbContext context) { _context = context; - _dateTimeProvider = dateTimeProvider; } - public async Task> Handle(Query request, CancellationToken cancellationToken) + public async Task> Handle(Query request, CancellationToken cancellationToken) { - var result = new List(); + var result = new List(); var currentDate = request.StartDate.Date; var endDate = request.EndDate.Date; @@ -66,7 +59,7 @@ public async Task> Handle(Query request, CancellationToken cancella && x.Status != DocumentStatus.Issued) .CountAsync(cancellationToken); - result.Add(new Result() + result.Add(new MetricResultDto() { Label = label, Value = importedDocumentsCount diff --git a/src/Application/Dashboards/Queries/GetLoggedInUser.cs b/src/Application/Dashboards/Queries/GetLoggedInUser.cs new file mode 100644 index 00000000..92ae64a7 --- /dev/null +++ b/src/Application/Dashboards/Queries/GetLoggedInUser.cs @@ -0,0 +1,44 @@ +using Application.Common.Interfaces; +using Application.Common.Models.Dtos.DashBoard; +using Domain.Statuses; +using MediatR; +using Microsoft.EntityFrameworkCore; +using NodaTime; + +namespace Application.Dashboards.Queries; + +public class GetLoggedInUser +{ + public record Query : IRequest + { + public DateTime Date { get; init; } + } + + public class QueryHandler : IRequestHandler + { + private readonly IApplicationDbContext _context; + + public QueryHandler(IApplicationDbContext context) + { + _context = context; + } + + public async Task Handle(Query request, CancellationToken cancellationToken) + { + var currentDate = request.Date.Date; + + var label = $"{currentDate:dd-MM}"; + var loggedinUsersCount = await _context.Logs + .Where(x => x.ObjectType!.Equals("Login") + && x.Time >= Instant.FromDateTimeUtc(currentDate) + && x.Time < Instant.FromDateTimeUtc(currentDate.AddDays(1))) + .CountAsync(cancellationToken); + + return new MetricResultDto() + { + Label = label, + Value = loggedinUsersCount + }; + } + } +} \ No newline at end of file From ae024e336d804eca2595a8def85d0a6368714467 Mon Sep 17 00:00:00 2001 From: Vzart Date: Sat, 29 Jul 2023 19:09:55 +0700 Subject: [PATCH 5/8] stoopid 2 --- .../Payload/Requests/Dashboard/GetLoggedInUsersRequest.cs | 6 ------ 1 file changed, 6 deletions(-) delete mode 100644 src/Api/Controllers/Payload/Requests/Dashboard/GetLoggedInUsersRequest.cs diff --git a/src/Api/Controllers/Payload/Requests/Dashboard/GetLoggedInUsersRequest.cs b/src/Api/Controllers/Payload/Requests/Dashboard/GetLoggedInUsersRequest.cs deleted file mode 100644 index 2b83b388..00000000 --- a/src/Api/Controllers/Payload/Requests/Dashboard/GetLoggedInUsersRequest.cs +++ /dev/null @@ -1,6 +0,0 @@ -namespace Api.Controllers.Payload.Requests.Dashboard; - -public class GetLoggedInUsersRequest -{ - public DateTime Date { get; set; } -} \ No newline at end of file From fb40026ba0106f116b235aeb7d8d25cfe14d6140 Mon Sep 17 00:00:00 2001 From: Vzart Date: Sat, 29 Jul 2023 19:19:01 +0700 Subject: [PATCH 6/8] minor changes --- .../Dashboards/Queries/GetLoggedInUser.cs | 11 ++++++ .../Dashboards/Queries/GetTheTopDriveUser.cs | 39 +++++++++++++++++++ 2 files changed, 50 insertions(+) create mode 100644 src/Application/Dashboards/Queries/GetTheTopDriveUser.cs diff --git a/src/Application/Dashboards/Queries/GetLoggedInUser.cs b/src/Application/Dashboards/Queries/GetLoggedInUser.cs index 92ae64a7..0abd755e 100644 --- a/src/Application/Dashboards/Queries/GetLoggedInUser.cs +++ b/src/Application/Dashboards/Queries/GetLoggedInUser.cs @@ -1,6 +1,7 @@ using Application.Common.Interfaces; using Application.Common.Models.Dtos.DashBoard; using Domain.Statuses; +using FluentValidation; using MediatR; using Microsoft.EntityFrameworkCore; using NodaTime; @@ -9,6 +10,16 @@ namespace Application.Dashboards.Queries; public class GetLoggedInUser { + public class Validator : AbstractValidator + { + public Validator() + { + RuleLevelCascadeMode = CascadeMode.Stop; + + RuleFor(x => x.Date) + .NotEmpty().WithMessage("Date can not be empty."); + } + } public record Query : IRequest { public DateTime Date { get; init; } diff --git a/src/Application/Dashboards/Queries/GetTheTopDriveUser.cs b/src/Application/Dashboards/Queries/GetTheTopDriveUser.cs new file mode 100644 index 00000000..66c66bac --- /dev/null +++ b/src/Application/Dashboards/Queries/GetTheTopDriveUser.cs @@ -0,0 +1,39 @@ +using Application.Common.Interfaces; +using Application.Common.Models.Dtos.DashBoard; +using FluentValidation; +using MediatR; + +namespace Application.Dashboards.Queries; + +public class GetTheTopDriveUser +{ + public class Validator : AbstractValidator + { + public Validator() + { + RuleLevelCascadeMode = CascadeMode.Stop; + + RuleFor(x => x.Date) + .NotEmpty().WithMessage("Date can not be empty."); + } + } + public record Query : IRequest + { + public DateTime Date { get; init; } + } + + public class QueryHandler : IRequestHandler + { + private readonly IApplicationDbContext _context; + + public QueryHandler(IApplicationDbContext context) + { + _context = context; + } + + public Task Handle(Query request, CancellationToken cancellationToken) + { + throw new NotImplementedException(); + } + } +} \ No newline at end of file From 99bdda54bded69b443d1a9a870151b4933408a26 Mon Sep 17 00:00:00 2001 From: Vzart Date: Sat, 29 Jul 2023 21:48:22 +0700 Subject: [PATCH 7/8] add: get user with largest drive --- src/Api/Controllers/DashboardController.cs | 16 +++++ .../Models/Dtos/DashBoard/LargestDriveDto.cs | 10 ++++ .../Dashboards/Queries/GetTheTopDriveUser.cs | 39 ------------ .../Queries/GetUserWithLargestDriveData.cs | 60 +++++++++++++++++++ 4 files changed, 86 insertions(+), 39 deletions(-) create mode 100644 src/Application/Common/Models/Dtos/DashBoard/LargestDriveDto.cs delete mode 100644 src/Application/Dashboards/Queries/GetTheTopDriveUser.cs create mode 100644 src/Application/Dashboards/Queries/GetUserWithLargestDriveData.cs diff --git a/src/Api/Controllers/DashboardController.cs b/src/Api/Controllers/DashboardController.cs index f4e26c12..5d22b627 100644 --- a/src/Api/Controllers/DashboardController.cs +++ b/src/Api/Controllers/DashboardController.cs @@ -50,4 +50,20 @@ public async Task>> GetLoggedInUsers( var result = await Mediator.Send(query); return Ok(Result.Succeed(result)); } + + [RequiresRole(IdentityData.Roles.Admin)] + [HttpPost("largest-drive")] + [ProducesResponseType(StatusCodes.Status200OK)] + [ProducesResponseType(StatusCodes.Status401Unauthorized)] + public async Task>> GetUserWithLargestDrive( + [FromBody] DateTime date) + { + var query = new GetUserWithLargestDriveData.Query() + { + Date = date + }; + + var result = await Mediator.Send(query); + return Ok(Result.Succeed(result)); + } } \ No newline at end of file diff --git a/src/Application/Common/Models/Dtos/DashBoard/LargestDriveDto.cs b/src/Application/Common/Models/Dtos/DashBoard/LargestDriveDto.cs new file mode 100644 index 00000000..b7ce4c77 --- /dev/null +++ b/src/Application/Common/Models/Dtos/DashBoard/LargestDriveDto.cs @@ -0,0 +1,10 @@ +using Application.Users.Queries; + +namespace Application.Common.Models.Dtos.DashBoard; + +public class LargestDriveDto +{ + public string Label { get; set; } + public long Value { get; set; } + public UserDto User { get; set; } +} \ No newline at end of file diff --git a/src/Application/Dashboards/Queries/GetTheTopDriveUser.cs b/src/Application/Dashboards/Queries/GetTheTopDriveUser.cs deleted file mode 100644 index 66c66bac..00000000 --- a/src/Application/Dashboards/Queries/GetTheTopDriveUser.cs +++ /dev/null @@ -1,39 +0,0 @@ -using Application.Common.Interfaces; -using Application.Common.Models.Dtos.DashBoard; -using FluentValidation; -using MediatR; - -namespace Application.Dashboards.Queries; - -public class GetTheTopDriveUser -{ - public class Validator : AbstractValidator - { - public Validator() - { - RuleLevelCascadeMode = CascadeMode.Stop; - - RuleFor(x => x.Date) - .NotEmpty().WithMessage("Date can not be empty."); - } - } - public record Query : IRequest - { - public DateTime Date { get; init; } - } - - public class QueryHandler : IRequestHandler - { - private readonly IApplicationDbContext _context; - - public QueryHandler(IApplicationDbContext context) - { - _context = context; - } - - public Task Handle(Query request, CancellationToken cancellationToken) - { - throw new NotImplementedException(); - } - } -} \ No newline at end of file diff --git a/src/Application/Dashboards/Queries/GetUserWithLargestDriveData.cs b/src/Application/Dashboards/Queries/GetUserWithLargestDriveData.cs new file mode 100644 index 00000000..d5a2be1e --- /dev/null +++ b/src/Application/Dashboards/Queries/GetUserWithLargestDriveData.cs @@ -0,0 +1,60 @@ +using Application.Common.Interfaces; +using Application.Common.Models.Dtos.DashBoard; +using Application.Users.Queries; +using AutoMapper; +using FluentValidation; +using MediatR; +using Microsoft.EntityFrameworkCore; +using NodaTime; + +namespace Application.Dashboards.Queries; + +public class GetUserWithLargestDriveData +{ + public class Validator : AbstractValidator + { + public Validator() + { + RuleLevelCascadeMode = CascadeMode.Stop; + + RuleFor(x => x.Date) + .NotEmpty().WithMessage("Date can not be empty."); + } + } + public record Query : IRequest + { + public DateTime Date { get; init; } + } + + public class QueryHandler : IRequestHandler + { + private readonly IApplicationDbContext _context; + private readonly IMapper _mapper; + + public QueryHandler(IApplicationDbContext context, IMapper mapper) + { + _context = context; + _mapper = mapper; + } + + public async Task Handle(Query request, CancellationToken cancellationToken) + { + var currentDate = request.Date.Date; + var nextDate = currentDate.AddDays(1); + + var usersWithFileData = await _context.Entries + .Where(x => x.FileId != null && x.Created >= LocalDateTime.FromDateTime(currentDate) + && x.Created < LocalDateTime.FromDateTime(nextDate)) + .GroupBy(entry => entry.Uploader) + .Select(group => new LargestDriveDto() + { + User = _mapper.Map(group.Key), + Label = $"{currentDate:dd-MM}", + Value = group.Sum(x => x.File!.FileData.Length) + }) + .OrderByDescending(x => x.Value) + .FirstOrDefaultAsync(cancellationToken); + return usersWithFileData; + } + } +} \ No newline at end of file From a76fcf0e0550f6bda0c20cf971764ca14933bdf2 Mon Sep 17 00:00:00 2001 From: Vzart Date: Sat, 29 Jul 2023 22:49:50 +0700 Subject: [PATCH 8/8] fix: appsettings --- src/Api/appsettings.Development.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Api/appsettings.Development.json b/src/Api/appsettings.Development.json index 6ede2e2e..b547eab4 100644 --- a/src/Api/appsettings.Development.json +++ b/src/Api/appsettings.Development.json @@ -36,7 +36,7 @@ { "Name": "PostgreSQL", "Args": { - "connectionString": "Server=localhost;Port=5432;Database=mydb;User ID=profiletest;Password=supasecured", + "connectionString": "Server=postgres;Port=5432;Database=mydb;User ID=profiletest;Password=supasecured", "tableName": "Logs", "schemaName": null, "needAutoCreateTable": true,