diff --git a/src/LanBackup.WebApp/ClientApp/app/components/backups/backups.component.ts b/src/LanBackup.WebApp/ClientApp/app/components/backups/backups.component.ts index 8664faa..fa1cace 100644 --- a/src/LanBackup.WebApp/ClientApp/app/components/backups/backups.component.ts +++ b/src/LanBackup.WebApp/ClientApp/app/components/backups/backups.component.ts @@ -227,7 +227,7 @@ export class BackupsComponent implements OnInit { this.currentPage = page this.totalPages = data.tp * this.pageSize; this.isloading = false; - }, 300);//TODO - remove - induced delay + }, 200);//TODO - remove - induced delay }, err => { diff --git a/src/LanBackup.WebApp/ClientApp/app/components/logs/logs.component.ts b/src/LanBackup.WebApp/ClientApp/app/components/logs/logs.component.ts index 555bf71..dd9ac52 100644 --- a/src/LanBackup.WebApp/ClientApp/app/components/logs/logs.component.ts +++ b/src/LanBackup.WebApp/ClientApp/app/components/logs/logs.component.ts @@ -57,7 +57,7 @@ export class LogsComponent implements OnInit { this.currentPage = page this.totalPages = data.tp * this.pageSize; this.isloading = false; - }, 400);//induced delay + }, 200);//TODO - remove induced delay }, err => this.log.error(err) ); diff --git a/src/LanBackup.WebApp/Controllers/API/BackupConfigController.cs b/src/LanBackup.WebApp/Controllers/API/BackupConfigController.cs index 00b3419..250af16 100644 --- a/src/LanBackup.WebApp/Controllers/API/BackupConfigController.cs +++ b/src/LanBackup.WebApp/Controllers/API/BackupConfigController.cs @@ -10,6 +10,7 @@ using AutoMapper; using LanBackup.WebApp.Models.DTO; using AutoMapper.QueryableExtensions; using LanBackup.WebApp.Models.Telemetry; +using Microsoft.EntityFrameworkCore; namespace LanBackup.WebApp.Controllers { @@ -44,9 +45,8 @@ namespace LanBackup.WebApp.Controllers [ProducesResponseType(typeof(IEnumerable), 200)] [ProducesResponseType(typeof(PaginatedList), 200)] public async Task Get([FromHeader] string idx, [FromHeader] string siz) - //public IEnumerable Get() { - //return new DatabaseManager(_context).GetBackupConfigs(); + _context.ChangeTracker.QueryTrackingBehavior = QueryTrackingBehavior.NoTracking; if (!string.IsNullOrEmpty(idx) && !string.IsNullOrEmpty(siz)) { return new OkObjectResult( @@ -67,6 +67,7 @@ namespace LanBackup.WebApp.Controllers [ProducesResponseType(typeof(BackupConfigurationDTO), 404)] public IActionResult Get(string id) { + _context.ChangeTracker.QueryTrackingBehavior = QueryTrackingBehavior.NoTracking; var res = new DatabaseManager(_context).GetBackupConfig(id); if (res == null) { @@ -87,6 +88,7 @@ namespace LanBackup.WebApp.Controllers [ProducesResponseType(typeof(IEnumerable), 404)] public IActionResult GetByCient(string clientid) { + _context.ChangeTracker.QueryTrackingBehavior = QueryTrackingBehavior.NoTracking; var results = new DatabaseManager(_context).GetBackupConfigByClient(clientid); if (results == null || results.Count() == 0) return NotFound(); diff --git a/src/LanBackup.WebApp/Controllers/API/LogsController.cs b/src/LanBackup.WebApp/Controllers/API/LogsController.cs index f720e54..ee0da30 100644 --- a/src/LanBackup.WebApp/Controllers/API/LogsController.cs +++ b/src/LanBackup.WebApp/Controllers/API/LogsController.cs @@ -10,6 +10,7 @@ using LanBackup.WebApp.Models.DTO; using AutoMapper; using AutoMapper.QueryableExtensions; using LanBackup.WebApp.Models.Telemetry; +using Microsoft.EntityFrameworkCore; namespace LanBackup.WebApp.Controllers { @@ -35,19 +36,6 @@ namespace LanBackup.WebApp.Controllers } - ///// - ///// retrieves all logs from DB - ///// - ///// - //[AllowAnonymous] - //[HttpGet] - //[ProducesResponseType(typeof(IEnumerable), 200)] - //public IActionResult Get() - //{ - // return new OkObjectResult(_context.Logs.ToList()); - // //return new ObjectResult(_context.Logs.ToList()); - //} - /// /// retrieves paginated logs from DB @@ -57,14 +45,11 @@ namespace LanBackup.WebApp.Controllers /// [AllowAnonymous] [HttpGet] - //[HttpGet("{pageIndex}/{pageSize}")] [ProducesResponseType(typeof(IEnumerable), 200)] [ProducesResponseType(typeof(PaginatedList), 200)] public async Task Get([FromHeader] string idx, [FromHeader] string siz) { - //StringValues val1, val2; - //HttpContext.Request.Headers.TryGetValue("idx", out val1) && - //HttpContext.Request.Headers.TryGetValue("siz", out val2) + _context.ChangeTracker.QueryTrackingBehavior = QueryTrackingBehavior.NoTracking; if (!string.IsNullOrEmpty(idx) && !string.IsNullOrEmpty(siz)) { return new OkObjectResult( @@ -86,6 +71,7 @@ namespace LanBackup.WebApp.Controllers [ProducesResponseType(typeof(BackupLogDTO), 404)] public IActionResult Get(int id) { + _context.ChangeTracker.QueryTrackingBehavior = QueryTrackingBehavior.NoTracking; var result = _context.Logs.SingleOrDefault(p => p.ID == id); if (result == null) { @@ -107,6 +93,7 @@ namespace LanBackup.WebApp.Controllers [ProducesResponseType(typeof(IEnumerable), 404)] public IActionResult GetByCientID(string clientid) { + _context.ChangeTracker.QueryTrackingBehavior = QueryTrackingBehavior.NoTracking; var results = _context.Logs.Where(p => p.ClientIP == clientid).OrderByDescending(o => o.DateTime); if (results == null || results.Count() == 0) return NotFound(); @@ -126,6 +113,7 @@ namespace LanBackup.WebApp.Controllers [ProducesResponseType(typeof(IEnumerable), 404)] public IActionResult GetByConfigurationID(string configurationid) { + _context.ChangeTracker.QueryTrackingBehavior = QueryTrackingBehavior.NoTracking; var results = _context.Logs.Where(p => p.ConfigurationID == configurationid).OrderByDescending(o => o.DateTime); if (results == null || results.Count() == 0) return NotFound(); @@ -141,7 +129,7 @@ namespace LanBackup.WebApp.Controllers /// /// /// - [AllowAnonymous]//[Authorize(Roles = "Admin")] + [AllowAnonymous]//TODO [Authorize(Roles = "Admin")] [HttpPost] [ProducesResponseType(typeof(int), 200)] [ProducesResponseType(typeof(int), 400)] diff --git a/src/LanBackup.WebApp/Controllers/PaginatedList.cs b/src/LanBackup.WebApp/Controllers/PaginatedList.cs index 819bbbb..5f69b8f 100644 --- a/src/LanBackup.WebApp/Controllers/PaginatedList.cs +++ b/src/LanBackup.WebApp/Controllers/PaginatedList.cs @@ -47,8 +47,8 @@ namespace LanBackup.WebApp.Controllers var count = await source.CountAsync(); if (order != null) { - var items = source.OrderByDescending(order).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();// .ToListAsync(); - var mappedItems = mapper.Map, List>(items); + var items = source.OrderByDescending(order).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToAsyncEnumerable(); + var mappedItems = mapper.Map, List>(await items.ToList()); return new PaginatedList(mappedItems, count, pageIndex, pageSize); } else diff --git a/src/LanBackup.WebApp/project.json b/src/LanBackup.WebApp/project.json index 11a5d32..4c4827e 100644 --- a/src/LanBackup.WebApp/project.json +++ b/src/LanBackup.WebApp/project.json @@ -37,7 +37,8 @@ "AutoMapper": "5.2.0", "AutoMapper.Extensions.Microsoft.DependencyInjection": "1.2.0", "Microsoft.ApplicationInsights.AspNetCore": "2.0.0", - "Microsoft.ApplicationInsights.JavaScript": "1.0.7-build00691" + "Microsoft.ApplicationInsights.JavaScript": "1.0.7-build00691", + "Microsoft.EntityFrameworkCore": "1.1.0" }, "tools": { diff --git a/test/LanBackup.WebApp.Tests/IntegrationTests/ApiLogsControllerTests.cs b/test/LanBackup.WebApp.Tests/IntegrationTests/ApiLogsControllerTests.cs index 6e7544f..786fe0f 100644 --- a/test/LanBackup.WebApp.Tests/IntegrationTests/ApiLogsControllerTests.cs +++ b/test/LanBackup.WebApp.Tests/IntegrationTests/ApiLogsControllerTests.cs @@ -63,12 +63,76 @@ namespace LanBackup.WebApp.Tests.IntegrationTests + #region EF InMemory DB + + [Fact] + public async void WithEfInMemoryTestMethod() + { + using (var mctx = new EfInMemoryContext().GetContext()) + { + var service = new LogsController(Mapper.Instance, mctx, _tele); + var res = Assert.IsType(await service.Get(null, null)); + var val1 = Assert.IsType>(res.Value); + + Assert.Equal(5, val1.Count()); + } + } + + private class EfInMemoryContext : IDisposable + { + private BackupsContext _context; + private DbContextOptions options; + + public EfInMemoryContext() + { + options = new DbContextOptionsBuilder() + .UseInMemoryDatabase() + .Options; + + // Create the schema in the database + using (var context = new BackupsContext(options)) + { + context.Database.EnsureCreated(); + } + + // Insert seed data into the database using one instance of the context + using (var context = new BackupsContext(options)) + { + int id = 1; + context.Add(new BackupLog { ClientIP = "192.168.1.100", Description = $"Playing with some test data {Guid.NewGuid()}", Status = "OK", ID = id++ }); + context.Add(new BackupLog { ClientIP = "192.168.1.100", Description = $"Playing with some test data {Guid.NewGuid()}", Status = "OK", ID = id++ }); + context.Add(new BackupLog { ClientIP = "192.168.1.101", Description = $"Playing with some test data {Guid.NewGuid()}", Status = "OK", ID = id++ }); + context.Add(new BackupLog { ClientIP = "192.168.1.102", Description = $"Playing with some test data {Guid.NewGuid()}", Status = "OK", ID = id++ }); + context.Add(new BackupLog { ClientIP = "192.168.1.103", Description = $"Playing with some test data {Guid.NewGuid()}", Status = "OK", ID = id++ }); + context.SaveChanges(); + } + _context = new BackupsContext(options); + } + + public void Dispose() + { + _context.Dispose(); + } + + internal BackupsContext GetContext() + { + return _context; + } + } + + + #endregion EF InMemory DB + + + + + #region SqliteInMemory DB tests [Fact] public async void WithSqliteInmemoryTestMethod() { - using (var mctx = new GetSqliteMemoryContext().GetContext()) + using (var mctx = new SqliteMemoryContext().GetContext()) { var service = new LogsController(Mapper.Instance, mctx, _tele); var res = Assert.IsType(await service.Get(null, null)); @@ -83,7 +147,7 @@ namespace LanBackup.WebApp.Tests.IntegrationTests public async void GetByIdWithSqliteInmemoryTestMethod() { - using (var mctx = new GetSqliteMemoryContext().GetContext()) + using (var mctx = new SqliteMemoryContext().GetContext()) { var recId = 1; var service = new LogsController(Mapper.Instance, mctx, _tele); @@ -103,7 +167,7 @@ namespace LanBackup.WebApp.Tests.IntegrationTests public async void GetByClientIdWithSqliteInmemoryTestMethod() { - using (var mctx = new GetSqliteMemoryContext().GetContext()) + using (var mctx = new SqliteMemoryContext().GetContext()) { var clientId = "192.168.1.100"; var service = new LogsController(Mapper.Instance, mctx, _tele); @@ -119,13 +183,13 @@ namespace LanBackup.WebApp.Tests.IntegrationTests - private class GetSqliteMemoryContext : IDisposable + private class SqliteMemoryContext : IDisposable { private SqliteConnection connection; private BackupsContext _context; private DbContextOptions options; - public GetSqliteMemoryContext() + public SqliteMemoryContext() { connection = new SqliteConnection("DataSource=:memory:"); connection.Open(); @@ -169,5 +233,8 @@ namespace LanBackup.WebApp.Tests.IntegrationTests #endregion + + + } } diff --git a/test/LanBackup.WebApp.Tests/project.json b/test/LanBackup.WebApp.Tests/project.json index 531b81a..b54e4a1 100644 --- a/test/LanBackup.WebApp.Tests/project.json +++ b/test/LanBackup.WebApp.Tests/project.json @@ -21,7 +21,8 @@ "FluentAssertions": "4.18.0", "LanBackup.WebApp": "1.0.0-*", "Moq": "4.6.38-alpha", - "System.Diagnostics.TraceSource": "4.0.0" + "System.Diagnostics.TraceSource": "4.0.0", + "Microsoft.EntityFrameworkCore.InMemory": "1.1.0" }, "testRunner": "xunit", "frameworks": {