/// <inheritdoc /> public async Task <IList <IMeeting> > GetRecentMeetingsMostRecentFirstAsync( IWho who, TimeSpan timeSpan, int maxNumberOfMeetings) { this.logger.ReportEntry( who, new { TimeSpan = timeSpan, MaxNumberOfMeetings = maxNumberOfMeetings }); DateTime earliestDate = DateTime.Now.Subtract(timeSpan); IList <MeetingDto> meetingDtos = await this.context.Meetings .AsNoTracking() .TagWith(this.Tag(who)) .Take(maxNumberOfMeetings) .Include(m => m.Committee) .ThenInclude(c => c !.Organisation) .Where(m => m.MeetingDateTime >= earliestDate) .ToListAsync() .ConfigureAwait(false); IList <IMeeting> meetings = meetingDtos.Select(m => m.ToDomain()).ToList(); this.logger.ReportExit( who, new { Meetings = meetings }); return(meetings); }
/// <summary> /// Reports the entry into a method. /// </summary> /// <param name="logger">Logger.</param> /// <param name="who">Who details.</param> /// <param name="viewName">Name of the View.</param> /// <param name="model">View Model.</param> /// <param name="statusCode">Status Code.</param> /// <param name="className">Name of the class.</param> /// <param name="methodName">Name of the method.</param> /// <param name="lineNumber">The line number.</param> public static void ReportExitView( this ILogger logger, IWho who, string?viewName, object?model, int?statusCode, [CallerFilePath] string className = "Unknown", [CallerMemberName] string methodName = "Unknown", [CallerLineNumber] int lineNumber = 0) { if (!logger.IsEnabled(LogLevel.Trace)) { return; } ClassMethod classMethod = new ClassMethod(className, methodName, lineNumber); logger.LogTrace( ResourceMessageTemplates.ReportExitView, "Exit", viewName ?? classMethod.MethodName, model, statusCode, who, classMethod); }
/// <summary> /// Update Committee. /// </summary> /// <param name="who">Who Details.</param> /// <param name="model">Edit view model.</param> /// <returns>Nothing.</returns> private async Task UpdateRecordAsync( IWho who, EditViewModel model) { this.logger.ReportEntry( who, new { Model = model }); ICommittee originalCommittee = await this.service .GetCommitteeByIdAsync( who : who, committeeId : model.CommitteeId) .ConfigureAwait(false); ICommittee committee = model.ToDomain(originalCommittee.Organisation); await this.service .UpdateCommitteeAsync( who : who, auditEvent : AuditEvent.CommitteeMaintenance, committee : committee) .ConfigureAwait(false); this.logger.ReportExit(who); }
/// <summary> /// Display committees for the specified organisation. /// </summary> /// <param name="organisationId">The organisation identifier.</param> /// <returns>View.</returns> public async Task <IActionResult> Index(Guid organisationId) { IWho who = this.Who(); this.logger.ReportEntry( who, new { OrganisationId = organisationId }); IOrganisationWithCommittees organisation = await this.service .GetOrganisationByIdWithCommitteesAsync( who : who, organisationId : organisationId) .ConfigureAwait(false); IndexViewModel model = IndexViewModel.Create(organisation); ViewResult view = this.View(model); this.logger.ReportExitView( who, view.ViewName, view.Model, view.StatusCode); return(view); }
public async Task Test_Passing_Null_Organisation_Throws_Exception() { // ARRANGE IWho who = Create.Who(); Mock <ILogger <IOrganisationService> > loggerMock = MockFactory.CreateLoggerMock <IOrganisationService>(); DbContextOptions <DataContext> dbOptions = TestUtils.DbContextOptionsInMemory <CreateAsyncTests>( nameof(this.Test_Passing_Valid_Values)); await using DataContext dataContext = new DataContext(dbOptions); IVirtualBridgeData data = MockFactory.CreateVirtualBridgeData( dataContext); IOrganisationService service = new OrganisationService( loggerMock.Object, data); // ACT await service.CreateAsync( who : who, auditEvent : EAuditEvent.OrganisationMaintenance, organisation : null !) .ConfigureAwait(false); }
/// <summary> /// Display the specified Meeting. /// </summary> /// <param name="meetingId">Meeting Id.</param> /// <returns>View.</returns> public async Task <IActionResult> Index(Guid meetingId) { IWho who = this.Who(); this.logger.ReportEntry( who, new { MeetingId = meetingId }); IMeeting meeting = await this.service .GetMeetingByIdAsync( who : who, meetingId : meetingId) .ConfigureAwait(false); IndexViewModel model = IndexViewModel.Create(meeting); ViewResult view = this.View(model); this.logger.ReportExitView( who, view.ViewName, view.Model, view.StatusCode); return(view); }
/// <inheritdoc/> public async Task <IList <IMeeting> > GetRecentMeetingsMostRecentFirstAsync( IWho who, TimeSpan?timeSpan = null, int?maxNumberOfMeetings = null) { this.logger.ReportEntry( who, new { TimeSpan = timeSpan, MaxNumberOfMeeeting = maxNumberOfMeetings }); IList <IMeeting> meetings = await this.data .GetRecentMeetingsMostRecentFirstAsync( who : who, timeSpan : timeSpan ?? TimeSpan.FromDays(365), maxNumberOfMeetings : maxNumberOfMeetings ?? 20) .ConfigureAwait(false); this.logger.ReportExit( who, new { Meetings = meetings }); return(meetings); }
/// <summary> /// Reports the entry into a method. /// </summary> /// <param name="logger">Logger.</param> /// <param name="who">Who details.</param> /// <param name="controllerName">Name of the Controller.</param> /// <param name="actionName">Name of the Action.</param> /// <param name="model">View Model.</param> /// <param name="className">Name of the class.</param> /// <param name="methodName">Name of the method.</param> /// <param name="lineNumber">The line number.</param> public static void ReportExitRedirectToAction( this ILogger logger, IWho who, string?controllerName, string?actionName, object?model, [CallerFilePath] string className = "Unknown", [CallerMemberName] string methodName = "Unknown", [CallerLineNumber] int lineNumber = 0) { if (!logger.IsEnabled(LogLevel.Trace)) { return; } ClassMethod classMethod = new ClassMethod(className, methodName, lineNumber); logger.LogTrace( ResourceMessageTemplates.ReportExitRedirectToAction, "Exit", controllerName ?? classMethod.ClassName, actionName ?? classMethod.MethodName, model, who, classMethod); }
/// <inheritdoc/> public async Task <IOrganisationWithCommittees> GetOrganisationByIdWithCommitteesAsync( IWho who, Guid organisationId) { this.logger.ReportEntry( who, new { OrganisationId = organisationId }); IOrganisationWithCommittees organisationWithCommittees = (await this.context.Organisations .AsNoTracking() .TagWith(this.Tag(who)) .Include(o => o.Committees) .FirstOrDefaultAsync(o => o.Id == organisationId) .ConfigureAwait(false)) .ToDomainWithCommittees(); this.logger.ReportExit( who, new { OrganisationWithCommittees = organisationWithCommittees }); return(organisationWithCommittees); }
/// <inheritdoc/> public async Task UpdateMeetingAsync( IWho who, AuditEvent auditEvent, IMeeting meeting) { this.logger.ReportEntry( who, new { Meeting = meeting }); try { IAuditHeaderWithAuditDetails auditHeader = this.data.BeginTransaction(auditEvent, who); await this.data .UpdateMeetingAsync( who : who, auditHeader : auditHeader, meeting : meeting) .ConfigureAwait(false); await this.data.CommitTransactionAsync(auditHeader) .ConfigureAwait(false); } catch (Exception) { this.data.RollbackTransaction(); throw; } this.logger.ReportExit(who); }
/// <summary> /// Log an error message. /// </summary> /// <param name="logger">Logger.</param> /// <param name="who">Who details.</param> /// <param name="message">Error message.</param> /// <param name="data">Relevant data.</param> /// <param name="className">Name of the class.</param> /// <param name="methodName">Name of the method.</param> /// <param name="lineNumber">The line number.</param> public static void Error( this ILogger logger, IWho who, string message, object?data = null, [CallerFilePath] string className = "Unknown", [CallerMemberName] string methodName = "Unknown", [CallerLineNumber] int lineNumber = 0) { if (!logger.IsEnabled(LogLevel.Error)) { return; } if (message == null) { throw new ArgumentNullException(nameof(message)); } ClassMethod classMethod = new ClassMethod(className, methodName, lineNumber); logger.LogDebug( ResourceMessageTemplates.Error, message, data, who, classMethod); }
/// <summary> /// SOMETHING. /// </summary> /// <param name="meetingId">Meeting Id.</param> /// <returns>Action Result.</returns> public async Task <IActionResult> Index(Guid meetingId) { IWho who = this.Who(); this.logger.ReportEntry( who, new { MeetingId = meetingId }); IMeeting meeting = await this.service.GetMeetingByIdAsync( who, meetingId).ConfigureAwait(false); IAgendaItem agendaItem = await this.service.GetAgendaItemsByMeetingIdAsTreeAsync( who, meetingId).ConfigureAwait(false); if (agendaItem == null) { agendaItem = await this.service.CreateSkeletonAgendaAsync( who, AuditEvent.ViewAgenda, meetingId, SkeletonAgendaType.BasicContinuation).ConfigureAwait(false); } _ = meeting; _ = agendaItem; throw new NotImplementedException(); ////return this.View(); }
/// <summary> /// Display meetings for the specified committee. /// </summary> /// <param name="committeeId">CommitteeId.</param> /// <returns>View.</returns> public async Task <IActionResult> Index(Guid committeeId) { IWho who = this.Who(); this.logger.ReportEntry( who, new { CommitteeId = committeeId }); ICommitteeWithMeetings committee = await this.service .GetCommitteeByIdWithMeetingsAsync( who : who, committeeId : committeeId) .ConfigureAwait(false); IndexViewModel model = IndexViewModel.Create(committee); ViewResult view = this.View(model); this.logger.ReportExitView( who, view.ViewName, view.Model, view.StatusCode); return(view); }
/// <inheritdoc /> public Task <IList <IOrganisation> > GetAllAsync(IWho who) { if (who == null) { throw new ArgumentNullException(nameof(who)); } return(GetAllAsyncInternal()); async Task <IList <IOrganisation> > GetAllAsyncInternal() { this.logger.LogTrace( "ENTRY {Method}(who, organisation) {@Who}", nameof(this.GetAllAsync), who); IList <IOrganisation> organisations = await this.data.Organisation.GetAllAsync( who : who) .ConfigureAwait(false); this.logger.LogTrace( "EXIT {Method}(who, organisations) {@Who} {@Organisations}", nameof(this.GetAllAsync), who); return(organisations); } }
/// <inheritdoc/> public async Task UpdateOrganisationAsync( IWho who, AuditEvent auditEvent, IOrganisation organisation) { this.logger.ReportEntry( who, new { Organisation = organisation }); try { IAuditHeaderWithAuditDetails auditHeader = this.data.BeginTransaction(auditEvent, who); await this.data .UpdateOrganisationAsync( who : who, auditHeader : auditHeader, organisation : organisation) .ConfigureAwait(false); await this.data.CommitTransactionAsync(auditHeader) .ConfigureAwait(false); } catch (Exception) { this.data.RollbackTransaction(); throw; } this.logger.ReportExit(who); }
public async Task <IActionResult> Index() { IWho who = this.Who(); this.logger.ReportEntry(who); IList <IMeeting> meetings = await this.service .GetRecentMeetingsMostRecentFirstAsync(who) .ConfigureAwait(false); ISetupStatus setupStatus = meetings.Any() ? new SetupStatus(haveOrganisations: true, haveCommittees: true) : await this.service .GetSetupStatusAsync(who) .ConfigureAwait(false); bool featureEnabled = await this.featureManager .IsEnabledAsync(nameof(FeatureFlag.NewReferenceSearch)) .ConfigureAwait(false); IndexViewModel model = IndexViewModel.Create(meetings, setupStatus, featureEnabled); ViewResult view = this.View(model); this.logger.ReportExitView( who, view.ViewName, view.Model, view.StatusCode); return(view); }
/// <inheritdoc/> public async Task UpdateAsync( IWho who, IAuditHeaderWithAuditDetails auditHeader, ICompetition competition) { this.logger.LogTrace( "ENTRY {Method}(who, host) {@Who} {@Competition}", nameof(this.UpdateAsync), who, competition); CompetitionDto dto = CompetitionDto.ToDto(competition); CompetitionDto original = await this.context.FindAsync <CompetitionDto>(competition.Id) .ConfigureAwait(false); Audit.AuditUpdate(auditHeader, dto.Id, original, dto); this.context.Entry(original).CurrentValues.SetValues(dto); await this.context.SaveChangesAsync().ConfigureAwait(false); this.logger.LogTrace( "EXIT {Method}(who) {@Who}", nameof(this.UpdateAsync), who); }
/// <inheritdoc/> public async Task CreateCommitteeAsync( IWho who, AuditEvent auditEvent, ICommittee committee) { this.logger.ReportEntry( who, new { Committee = committee }); try { IAuditHeaderWithAuditDetails auditHeader = this.data.BeginTransaction(auditEvent, who); await this.data.CreateCommitteeAsync( who : who, auditHeader : auditHeader, committee : committee) .ConfigureAwait(false); await this.data.CommitTransactionAsync(auditHeader) .ConfigureAwait(false); } catch (Exception) { this.data.RollbackTransaction(); throw; } this.logger.ReportExit(who); }
/// <inheritdoc/> public async Task <ISetupStatus> GetSetupStatusAsync(IWho who) { this.logger.ReportEntry(who); bool haveOrganisations = await this.data .HaveOrganisationsAsync(who) .ConfigureAwait(false); SetupStatus setupStatus; if (!haveOrganisations) { setupStatus = new SetupStatus( haveOrganisations: false, haveCommittees: false); } else { bool haveCommittees = await this.data .HaveCommitteesAsync(who) .ConfigureAwait(false); setupStatus = new SetupStatus( haveOrganisations: true, haveCommittees: haveCommittees); } this.logger.ReportExit( who, new { SetupStatus = setupStatus }); return(setupStatus); }
/// <inheritdoc/> public async Task CreateAgendaItemRecursivelyAsync( IWho who, IAuditHeaderWithAuditDetails auditHeader, IAgendaItem agendaItem) { AgendaItemDto dto = AgendaItemDto.ToDto(agendaItem); this.context.AgendaItems.Add(dto); Audit.AuditCreate(auditHeader, dto, dto.Id); foreach (IAgendaItem child in agendaItem.ChildAgendaItems) { await this.CreateAgendaItemRecursivelyAsync( who, auditHeader, child); } if (agendaItem.ParentId == null) { this.logger.Debug( who, "Saving changes", new { AgendaItemId = agendaItem }); await this.context.SaveChangesAsync(); } this.logger.ReportExit(who); }
/// <summary> /// Update Meeting. /// </summary> /// <param name="who">Who Details.</param> /// <param name="model">Edit view model.</param> /// <returns>Nothing.</returns> private async Task UpdateRecordAsync( IWho who, EditViewModel model) { this.logger.ReportEntry( who, new { Model = model }); IMeeting originalMeeting = await this.service .GetMeetingByIdAsync( who : who, meetingId : model.MeetingId) .ConfigureAwait(false); IMeeting meeting = model.ToDomain( committee: originalMeeting.Committee); await this.service .UpdateMeetingAsync( who : who, auditEvent : AuditEvent.MeetingMaintenance, meeting : meeting) .ConfigureAwait(false); this.logger.ReportExit(who); }
public Context(IWho who, IWhat what, IWhere where, IWhen when, IWhy why) { Who = who; What = what; Where = where; When = when; Why = why; }
/// <summary> /// Adds this instance. /// </summary> /// <param name="model">Add view model.</param> /// <returns>View.</returns> public Task <IActionResult> Add(AddViewModel model) { if (model == null) { throw new ArgumentNullException(nameof(model)); } return(Internal()); async Task <IActionResult> Internal() { IWho who = this.Who(); this.logger.ReportEntry( who, new { Model = model }); if (model.FormState == FormState.Initial) { this.ModelState.Clear(); } else { if (this.ModelState.IsValid) { this.logger.Debug( who, "Inserting model", new { Model = model }); await this.InsertRecordAsync(who, model).ConfigureAwait(false); RedirectToActionResult redirect = this.RedirectToAction( "Index", "MeetingOverview", new { committeeId = model.CommitteeId }); this.logger.ReportExitRedirectToAction( who, redirect.ControllerName, redirect.ActionName, redirect.RouteValues); return(redirect); } } ViewResult view = this.View(model); this.logger.ReportExitView( who, view.ViewName, view.Model, view.StatusCode); return(view); } }
public Task <IActionResult> Add(AddViewModel model) { if (model == null) { throw new ArgumentNullException(nameof(model)); } return(Internal()); async Task <IActionResult> Internal() { IWho who = this.Who(); this.logger.ReportEntry( who, new { Model = model }); if (model.FormState == FormState.Initial) { model = new AddViewModel( formState: FormState.Initial, code: string.Empty, name: string.Empty, bgColour: string.Empty); this.ModelState.Clear(); } else { if (this.ModelState.IsValid) { await this.InsertRecordAsync(who, model).ConfigureAwait(false); RedirectToActionResult redirect = this.RedirectToAction("Index", "OrganisationOverview"); this.logger.ReportExitRedirectToAction( who, redirect.ControllerName, redirect.ActionName, redirect.RouteValues); return(redirect); } } ViewResult view = this.View(model); this.logger.ReportExitView( who, view.ViewName, view.Model, view.StatusCode); return(view); } }
/// <summary> /// Returns tag for use with .TagWith(). /// </summary> /// <param name="who">Who details.</param> /// <param name="methodName">Method Name.</param> /// <returns>Tag.</returns> protected string Tag(IWho who, string methodName) { if (who == null) { throw new ArgumentNullException(nameof(who)); } string username = "******"; return($"{this.repositoryName}.{methodName}.{who.CorrelationId}.{username}"); }
public async Task Test_Commit_Writes_Record() { // ARRANGE Mock <ILogger <VirtualBridgeData> > loggerMock = MockFactory.CreateLoggerMock <VirtualBridgeData>(); Mock <ILogger <AuditHeaderRepository> > auditHeaderRepositoryLoggerMock = MockFactory.CreateLoggerMock <AuditHeaderRepository>(); DbContextOptions <DataContext> dbOptions = TestUtils.DbContextOptionsInMemory <TestBeginCommit>( nameof(this.Test_Commit_Writes_Record)); await using DataContext dataContext = new DataContext(dbOptions); AuditHeaderRepository auditHeaderRepository = new AuditHeaderRepository( logger: auditHeaderRepositoryLoggerMock.Object, dataContext: dataContext); Mock <IOrganisationRepository> organisationRepositoryMock = MockFactory.CreateRepositoryMock <IOrganisationRepository>(); VirtualBridgeData data = new VirtualBridgeData( logger: loggerMock.Object, dataContext: dataContext, auditHeaderRepository: auditHeaderRepository, organisationRepository: organisationRepositoryMock.Object); IWho who = Create.Who(); // ACT IAuditHeaderWithAuditDetails auditHeader = await data.BeginTransactionAsync( who : who, auditEvent : EAuditEvent.OrganisationMaintenance) .ConfigureAwait(false); auditHeader.AuditDetails.Add( AuditDetail.CreateForCreate( auditHeader: auditHeader, tableName: "Organisation", columnName: "Name", recordId: Guid.NewGuid(), newValue: "NewValue")); await data.CommitTransactionAsync(who, auditHeader) .ConfigureAwait(false); // ASSERT int auditHeadersCount = await dataContext.AuditHeaders.CountAsync() .ConfigureAwait(false); int auditDetailsCount = await dataContext.AuditDetails.CountAsync() .ConfigureAwait(false); Assert.AreEqual(1, auditHeadersCount); Assert.AreEqual(1, auditDetailsCount); }
/// <inheritdoc /> public Task CreateOrganisationAsync( IWho who, EAuditEvent auditEvent, IOrganisation organisation) { return(OrganisationHelper.CreateOrganisationAsync( logger: this.logger, data: this.data, who: who, auditEvent: auditEvent, organisation: organisation)); }
/// <inheritdoc /> public Task CreateAsync( IWho who, EAuditEvent auditEvent, IOrganisation organisation) { if (who == null) { throw new ArgumentNullException(nameof(who)); } if (organisation == null) { throw new ArgumentNullException(nameof(organisation)); } return(CreateOrganisationInternalAsync()); async Task CreateOrganisationInternalAsync() { this.logger.LogTrace( "ENTRY {Method}(who, organisation) {@Who} {@Organisation}", nameof(this.CreateAsync), who, organisation); try { IAuditHeaderWithAuditDetails auditHeader = await this.data.BeginTransactionAsync( who, auditEvent) .ConfigureAwait(false); await this.data.Organisation.CreateAsync( who : who, auditHeader : auditHeader, organisation : organisation) .ConfigureAwait(false); await this.data.CommitTransactionAsync(who, auditHeader) .ConfigureAwait(false); } catch (Exception) { this.data.RollbackTransaction(who); throw; } this.logger.LogTrace( "EXIT {Method}(who) {@Who}", nameof(this.CreateAsync), who); } }
private string Tag( IWho who, [CallerMemberName] string methodName = "Unknown") { if (who == null) { throw new ArgumentNullException(nameof(who)); } string username = who.Username ?? who.ClientIpAddress; return($"{this.GetType().Name}.{methodName}.{who.CorrelationId}.{username}"); }
public IAuditHeaderWithAuditDetails BeginTransaction(AuditEvent auditEvent, IWho who) { if (who == null) { throw new ArgumentNullException(nameof(who)); } this.context.Database.BeginTransaction(); return(new AuditHeaderWithAuditDetails( auditEvent: auditEvent, username: who.Username ?? who.ClientIpAddress, correlationId: who.CorrelationId)); }