diff --git a/src/BabyTracker.csproj b/src/BabyTracker.csproj index 653bae8..53af074 100644 --- a/src/BabyTracker.csproj +++ b/src/BabyTracker.csproj @@ -21,7 +21,7 @@ - + diff --git a/src/Controllers/AccountController.cs b/src/Controllers/AccountController.cs index beb257a..bc127fc 100644 --- a/src/Controllers/AccountController.cs +++ b/src/Controllers/AccountController.cs @@ -13,15 +13,8 @@ namespace BabyTracker.Controllers; [Route("[controller]")] -public class AccountController : Controller +public class AccountController(IAccountService accountService) : Controller { - private readonly IAccountService _accountService; - - public AccountController(IAccountService accountService) - { - _accountService = accountService; - } - [HttpGet("[action]")] public IActionResult Login(string returnUrl = "") { @@ -34,7 +27,7 @@ public IActionResult Login(string returnUrl = "") [HttpPost("[action]")] public async Task Login(LoginViewModel model, string returnUrl = "") { - var claimsPrincipal = await _accountService.Login(model); + var claimsPrincipal = await accountService.Login(model); if (claimsPrincipal == null) { @@ -60,7 +53,7 @@ public IActionResult Register() [HttpPost("[action]")] public async Task Register(LoginViewModel model, string returnUrl = "") { - await _accountService.Register(model); + await accountService.Register(model); return await Login(model, returnUrl); } @@ -76,7 +69,7 @@ public IActionResult ResetPassword() [HttpPost("[action]")] public async Task ResetPassword(LoginViewModel model) { - var result = await _accountService.ResetPassword(model); + var result = await accountService.ResetPassword(model); TempData["notificationMessage"] = result; TempData["notificationType"] = "success"; @@ -99,7 +92,7 @@ public async Task Logout() [HttpGet("[action]")] public async Task Profile() { - var userMetaData = await _accountService.GetUserMetaData(User); + var userMetaData = await accountService.GetUserMetaData(User); var model = new ProfileViewModel { @@ -131,7 +124,7 @@ public async Task ProfileSave(ProfileViewModel viewModel) Theme = viewModel.Theme }; - var success = await _accountService.SaveUserMetaData(User, userMetaDate); + var success = await accountService.SaveUserMetaData(User, userMetaDate); TempData["notificationMessage"] = "Profile settings saved."; TempData["notificationType"] = success ? "success" : "danger"; diff --git a/src/Controllers/HomeController.cs b/src/Controllers/HomeController.cs index 4895cfd..44ce0b9 100644 --- a/src/Controllers/HomeController.cs +++ b/src/Controllers/HomeController.cs @@ -7,7 +7,6 @@ using System.Security.Claims; using System.Linq; using BabyTracker.Constants; -using Microsoft.Extensions.Hosting; using BabyTracker.Models.Database; using System.Collections.Generic; using Microsoft.AspNetCore.OutputCaching; @@ -15,51 +14,32 @@ namespace BabyTracker.Controllers; [Route("")] -public class HomeController : Controller +public class HomeController( + IImportService importService, + ISqLiteService sqLiteService, + IChartService chartService, + IAccountService accountService) : Controller { - private readonly IImportService _importService; - private readonly ISqLiteService _sqLiteService; - private readonly IChartService _chartService; - private readonly IAccountService _accountService; - private readonly IPictureService _pictureService; - private readonly IHostEnvironment _hostEnvironment; - - public HomeController( - IImportService importService, - ISqLiteService sqLiteService, - IChartService chartService, - IAccountService accountService, - IPictureService pictureService, - IHostEnvironment hostEnvironment) - { - _importService = importService; - _sqLiteService = sqLiteService; - _chartService = chartService; - _accountService = accountService; - _pictureService = pictureService; - _hostEnvironment = hostEnvironment; - } - [HttpGet] public async Task Index() { // User logged in and has a data clone available if (User.Identity?.IsAuthenticated == true && - _importService.HasDataClone(User)) + importService.HasDataClone(User)) { - _sqLiteService.OpenDataConnection(User); + sqLiteService.OpenDataConnection(User); var babiesViewModel = new BabiesViewModel { - Babies = await _sqLiteService.GetBabiesFromDb(), + Babies = await sqLiteService.GetBabiesFromDb(), NickName = User.FindFirstValue("nickname") ?? string.Empty, ProfileImageUrl = User.FindFirstValue("picture") ?? string.Empty, UserId = User.FindFirstValue("activeUserId") ?? string.Empty, }; - await _sqLiteService.CloseDataConnection(); + await sqLiteService.CloseDataConnection(); - var userMetaData = await _accountService.GetUserMetaData(User); + var userMetaData = await accountService.GetUserMetaData(User); ViewBag.Theme = userMetaData?.Theme; return View("Babies", babiesViewModel); @@ -75,7 +55,7 @@ public async Task Index() UserId = User.FindFirstValue("activeUserId") ?? string.Empty }; - var userMetaData = await _accountService.GetUserMetaData(User); + var userMetaData = await accountService.GetUserMetaData(User); ViewBag.Theme = userMetaData?.Theme; return View("LoggedIn", model); @@ -92,9 +72,9 @@ public async Task Index() [HttpGet("{babyName}/{date?}")] public async Task Diary(string babyName, DateOnly? date, string? q) { - _sqLiteService.OpenDataConnection(User); + sqLiteService.OpenDataConnection(User); - var availableDates = await _sqLiteService.GetAllEntryDates(babyName); + var availableDates = await sqLiteService.GetAllEntryDates(babyName); if (date == null) { @@ -112,11 +92,11 @@ public async Task Diary(string babyName, DateOnly? date, string? // or do we have a search query? if (!string.IsNullOrEmpty(q)) { - entries = await _sqLiteService.Search(q, babyName); + entries = await sqLiteService.Search(q, babyName); } else { - entries = await _sqLiteService.GetEntriesFromDb(date.Value, babyName); + entries = await sqLiteService.GetEntriesFromDb(date.Value, babyName); } var model = DiaryService.GetDays(entries); @@ -134,14 +114,14 @@ public async Task Diary(string babyName, DateOnly? date, string? model.ProfileImageUrl = User.FindFirstValue("picture") ?? string.Empty; model.UserId = User.FindFirstValue("activeUserId") ?? string.Empty; - var memories = await _sqLiteService.GetMemoriesFromDb(DateTime.Now, babyName); + var memories = await sqLiteService.GetMemoriesFromDb(DateTime.Now, babyName); model.MemoriesBadgeCount = memories.Count; model.ShowMemoriesLink = true; - var lastEntryDateTime = await _sqLiteService.GetLastEntryDateTime(babyName); + var lastEntryDateTime = await sqLiteService.GetLastEntryDateTime(babyName); ViewData["LastEntry"] = lastEntryDateTime; - var userMetaData = await _accountService.GetUserMetaData(User); + var userMetaData = await accountService.GetUserMetaData(User); model.FontSize = userMetaData?.FontSize ?? 6; ViewBag.Theme = userMetaData?.Theme; @@ -157,10 +137,10 @@ public async Task Diary(string babyName, DateOnly? date, string? if (date != null && date.Value != DateOnly.FromDateTime(lastEntryDateTime)) { userMetaData!.LastViewedDate = date; - await _accountService.SaveUserMetaData(User, userMetaData); + await accountService.SaveUserMetaData(User, userMetaData); } - await _sqLiteService.CloseDataConnection(); + await sqLiteService.CloseDataConnection(); return View("Diary", model); } @@ -169,11 +149,11 @@ public async Task Diary(string babyName, DateOnly? date, string? [HttpGet("{babyName}/memories")] public async Task Memories(string babyName) { - _sqLiteService.OpenDataConnection(User); + sqLiteService.OpenDataConnection(User); - var memories = await _sqLiteService.GetMemoriesFromDb(DateTime.Now, babyName); + var memories = await sqLiteService.GetMemoriesFromDb(DateTime.Now, babyName); - await _sqLiteService.CloseDataConnection(); + await sqLiteService.CloseDataConnection(); var model = DiaryService.GetDays(memories); @@ -185,7 +165,7 @@ public async Task Memories(string babyName) model.ProfileImageUrl = User.FindFirstValue("picture") ?? string.Empty; model.UserId = User.FindFirstValue("activeUserId") ?? string.Empty; - var userMetaData = await _accountService.GetUserMetaData(User); + var userMetaData = await accountService.GetUserMetaData(User); ViewBag.Theme = userMetaData?.Theme; return View("Memories", model); @@ -195,13 +175,13 @@ public async Task Memories(string babyName) [HttpGet("{babyName}/charts/{months?}")] public async Task Charts(string babyName, int? months = null) { - _sqLiteService.OpenDataConnection(User); + sqLiteService.OpenDataConnection(User); - var memories = await _sqLiteService.GetMemoriesFromDb(DateTime.Now, babyName); + var memories = await sqLiteService.GetMemoriesFromDb(DateTime.Now, babyName); - await _sqLiteService.CloseDataConnection(); + await sqLiteService.CloseDataConnection(); - var model = await _chartService.GetViewModel(User, babyName, months + 1); + var model = await chartService.GetViewModel(User, babyName, months + 1); model.BabyName = babyName; model.MemoriesBadgeCount = memories.Count; @@ -211,7 +191,7 @@ public async Task Charts(string babyName, int? months = null) model.ProfileImageUrl = User.FindFirstValue("picture") ?? string.Empty; model.UserId = User.FindFirstValue("activeUserId") ?? string.Empty; - var userMetaData = await _accountService.GetUserMetaData(User); + var userMetaData = await accountService.GetUserMetaData(User); ViewBag.Theme = userMetaData?.Theme; return View("Charts", model); @@ -221,12 +201,12 @@ public async Task Charts(string babyName, int? months = null) [HttpGet("{babyName}/gallery")] public async Task Gallery(string babyName) { - _sqLiteService.OpenDataConnection(User); + sqLiteService.OpenDataConnection(User); - var pictures = await _sqLiteService.GetPictures(babyName); - var memories = await _sqLiteService.GetMemoriesFromDb(DateTime.Now, babyName); + var pictures = await sqLiteService.GetPictures(babyName); + var memories = await sqLiteService.GetMemoriesFromDb(DateTime.Now, babyName); - await _sqLiteService.CloseDataConnection(); + await sqLiteService.CloseDataConnection(); var model = new GalleryViewModel { @@ -239,7 +219,7 @@ public async Task Gallery(string babyName) UserId = User.FindFirstValue("activeUserId") ?? string.Empty }; - var userMetaData = await _accountService.GetUserMetaData(User); + var userMetaData = await accountService.GetUserMetaData(User); ViewBag.Theme = userMetaData?.Theme; return View("Gallery", model); diff --git a/src/Controllers/ImportController.cs b/src/Controllers/ImportController.cs index 36ad2e2..0f2693d 100644 --- a/src/Controllers/ImportController.cs +++ b/src/Controllers/ImportController.cs @@ -8,15 +8,8 @@ namespace BabyTracker.Controllers; [Route("[controller]")] -public class ImportController : Controller +public class ImportController(IAccountService accountService) : Controller { - private readonly IAccountService _accountService; - - public ImportController(IAccountService accountService) - { - _accountService = accountService; - } - [Authorize] [HttpGet] public async Task Import() @@ -29,7 +22,7 @@ public async Task Import() UserId = User.FindFirstValue("userId") ?? string.Empty, }; - var userMetaData = await _accountService.GetUserMetaData(User); + var userMetaData = await accountService.GetUserMetaData(User); ViewBag.Theme = userMetaData?.Theme; return View(model); diff --git a/src/Controllers/MemoriesController.cs b/src/Controllers/MemoriesController.cs index 7af52b6..0d3a282 100644 --- a/src/Controllers/MemoriesController.cs +++ b/src/Controllers/MemoriesController.cs @@ -6,20 +6,13 @@ namespace BabyTracker.Controllers; [Route("[controller]")] -public class MemoriesController : Controller +public class MemoriesController(IMemoriesService memoriesService) : Controller { - private readonly IMemoriesService _memoriesService; - - public MemoriesController(IMemoriesService memoriesService) - { - _memoriesService = memoriesService; - } - /// /// Used for testing the memories job /// /// [Authorize] [HttpGet("[action]")] - public async Task Send() => await _memoriesService.SendMemoriesEmail(); + public async Task Send() => await memoriesService.SendMemoriesEmail(); } diff --git a/src/Controllers/PictureController.cs b/src/Controllers/PictureController.cs index e29c3d9..26131f2 100644 --- a/src/Controllers/PictureController.cs +++ b/src/Controllers/PictureController.cs @@ -8,24 +8,15 @@ namespace BabyTracker.Controllers; [Route("[controller]")] -public class PictureController : Controller +public class PictureController(IPictureService pictureService, + IHostEnvironment hostEnvironment) : Controller { - private readonly IPictureService _pictureService; - private readonly IHostEnvironment _hostEnvironment; - - public PictureController(IPictureService pictureService, - IHostEnvironment hostEnvironment) - { - _pictureService = pictureService; - _hostEnvironment = hostEnvironment; - } - [OutputCache(PolicyName = "AuthenticatedOutputCache")] [Authorize] [HttpGet("{fileName}")] public async Task GetPicture(string fileName) { - var picture = await _pictureService.GetPicture(_hostEnvironment, User, fileName); + var picture = await pictureService.GetPicture(hostEnvironment, User, fileName); if (picture == null) { @@ -40,7 +31,7 @@ public async Task GetPicture(string fileName) [HttpGet("{filename}/thumbnail")] public async Task GetThumbnail(string fileName) { - var thumbnail = await _pictureService.GetPicture(_hostEnvironment, User, $"{fileName}__thumbnail"); + var thumbnail = await pictureService.GetPicture(hostEnvironment, User, $"{fileName}__thumbnail"); if (thumbnail == null) { @@ -53,7 +44,7 @@ public async Task GetThumbnail(string fileName) [HttpGet("{userId}/{fileName}")] public async Task GetPicture(string userId, string fileName) { - var picture = await _pictureService.GetPicture(_hostEnvironment, userId, fileName); + var picture = await pictureService.GetPicture(hostEnvironment, userId, fileName); if (picture == null) { @@ -66,7 +57,7 @@ public async Task GetPicture(string userId, string fileName) [HttpGet("{userId}/{filename}/thumbnail")] public async Task GetThumbnail(string userId, string fileName) { - var thumbnail = await _pictureService.GetPicture(_hostEnvironment, userId, $"{fileName}__thumbnail"); + var thumbnail = await pictureService.GetPicture(hostEnvironment, userId, $"{fileName}__thumbnail"); if (thumbnail == null) { diff --git a/src/Services/AccountService.cs b/src/Services/AccountService.cs index 3c895d1..a1e7a28 100644 --- a/src/Services/AccountService.cs +++ b/src/Services/AccountService.cs @@ -31,31 +31,20 @@ public interface IAccountService Task ResetPassword(LoginViewModel model); } -public class AccountService : IAccountService +public class AccountService(IConfiguration configuration, + IAuthenticationApiClient authenticationApiClient, + IManagementApiClient managementApiClient) : IAccountService { - private readonly IConfiguration _configuration; - private readonly IAuthenticationApiClient _authenticationApiClient; - private readonly IManagementApiClient _managementApiClient; - - public AccountService(IConfiguration configuration, - IAuthenticationApiClient authenticationApiClient, - IManagementApiClient managementApiClient) - { - _configuration = configuration; - _authenticationApiClient = authenticationApiClient; - _managementApiClient = managementApiClient; - } - public async Task Login(LoginViewModel model) { AccessTokenResponse? result; try { - result = await _authenticationApiClient.GetTokenAsync(new ResourceOwnerTokenRequest + result = await authenticationApiClient.GetTokenAsync(new ResourceOwnerTokenRequest { - ClientId = _configuration["AUTH0_CLIENTID"], - ClientSecret = _configuration["AUTH0_CLIENTSECRET"], + ClientId = configuration["AUTH0_CLIENTID"], + ClientSecret = configuration["AUTH0_CLIENTSECRET"], Scope = "openid profile", Realm = "Username-Password-Authentication", Username = model.EmailAddress, @@ -67,7 +56,7 @@ public AccountService(IConfiguration configuration, return null; } - var user = await _authenticationApiClient.GetUserInfoAsync(result.AccessToken); + var user = await authenticationApiClient.GetUserInfoAsync(result.AccessToken); var userId = user.UserId.Replace("auth0|", string.Empty); var shareUser = await SearchUsersWithShareList(user.FullName); @@ -91,9 +80,9 @@ public AccountService(IConfiguration configuration, public async Task Register(LoginViewModel model) { - var result = await _authenticationApiClient.SignupUserAsync(new SignupUserRequest + var result = await authenticationApiClient.SignupUserAsync(new SignupUserRequest { - ClientId = _configuration["AUTH0_CLIENTID"], + ClientId = configuration["AUTH0_CLIENTID"], Email = model.EmailAddress, Password = model.Password, Connection = "Username-Password-Authentication", @@ -104,9 +93,9 @@ public AccountService(IConfiguration configuration, public async Task ResetPassword(LoginViewModel model) { - var result = await _authenticationApiClient.ChangePasswordAsync(new() + var result = await authenticationApiClient.ChangePasswordAsync(new() { - ClientId = _configuration["AUTH0_CLIENTID"], + ClientId = configuration["AUTH0_CLIENTID"], Connection = "Username-Password-Authentication", Email = model.EmailAddress }); @@ -117,7 +106,7 @@ public async Task ResetPassword(LoginViewModel model) { var userId = user.FindFirst(ClaimTypes.NameIdentifier)?.Value; - var clientUser = await _managementApiClient.Users.GetAsync(userId); + var clientUser = await managementApiClient.Users.GetAsync(userId); return GetUserMetaData(clientUser); } @@ -136,7 +125,7 @@ public async Task SaveUserMetaData(ClaimsPrincipal user, UserMetaData user { var userId = user.FindFirst(ClaimTypes.NameIdentifier)?.Value; - await _managementApiClient.Users.UpdateAsync(userId, new() + await managementApiClient.Users.UpdateAsync(userId, new() { UserMetadata = userMetaData }); @@ -154,7 +143,7 @@ public async Task SaveUserMetaData(ClaimsPrincipal user, UserMetaData user do { - page = await _managementApiClient.Users.GetAllAsync( + page = await managementApiClient.Users.GetAllAsync( new() { Query = "user_metadata.EnableMemoriesEmail:true" }, new(pageNo) ); @@ -177,7 +166,7 @@ public async Task SaveUserMetaData(ClaimsPrincipal user, UserMetaData user do { - page = await _managementApiClient.Users.GetAllAsync( + page = await managementApiClient.Users.GetAllAsync( new() { Query = $"user_metadata.ShareList:{emailAddress}" }, new(pageNo) ); diff --git a/src/Services/ChartService.cs b/src/Services/ChartService.cs index d49d432..8cea596 100644 --- a/src/Services/ChartService.cs +++ b/src/Services/ChartService.cs @@ -18,27 +18,20 @@ public interface IChartService Task GetViewModel(ClaimsPrincipal user, string babyName, int? maxAge = null); } -public class ChartService : IChartService +public class ChartService(ISqLiteService sqLiteService) : IChartService { - private readonly ISqLiteService _sqLiteService; - - public ChartService(ISqLiteService sqLiteService) - { - _sqLiteService = sqLiteService; - } - public async Task GetViewModel(ClaimsPrincipal user, string babyName, int? maxAge = null) { var viewModel = new ChartsViewModel(); - _sqLiteService.OpenDataConnection(user); + sqLiteService.OpenDataConnection(user); - var entries = await _sqLiteService.GetGrowth(long.MinValue, long.MaxValue, babyName); + var entries = await sqLiteService.GetGrowth(long.MinValue, long.MaxValue, babyName); - var babies = await _sqLiteService.GetBabiesFromDb(); + var babies = await sqLiteService.GetBabiesFromDb(); var baby = babies.FirstOrDefault(baby => baby.Name == babyName); - await _sqLiteService.CloseDataConnection(); + await sqLiteService.CloseDataConnection(); if (baby == null) { diff --git a/src/Services/ImportService.cs b/src/Services/ImportService.cs index 3a19a12..4052811 100644 --- a/src/Services/ImportService.cs +++ b/src/Services/ImportService.cs @@ -17,21 +17,10 @@ public interface IImportService bool HasDataClone(ClaimsPrincipal user); } -public class ImportService : IImportService +public class ImportService(IWebHostEnvironment webHostEnvironment, + IOutputCacheStore outputCacheStore, + ILogger logger) : IImportService { - private readonly IWebHostEnvironment _webHostEnvironment; - private readonly IOutputCacheStore _outputCacheStore; - private readonly ILogger _logger; - - public ImportService(IWebHostEnvironment webHostEnvironment, - IOutputCacheStore outputCacheStore, - ILogger logger) - { - _webHostEnvironment = webHostEnvironment; - _outputCacheStore = outputCacheStore; - _logger = logger; - } - public async Task Unzip(ClaimsPrincipal user, Stream stream) { var extractPath = GetDataClonePath(user); @@ -45,7 +34,7 @@ public async Task Unzip(ClaimsPrincipal user, Stream stream) } catch (Exception e) { - _logger.LogError($"Error removing data clone path '{extractPath}'. {e.Message}"); + logger.LogError($"Error removing data clone path '{extractPath}'. {e.Message}"); } try @@ -55,10 +44,10 @@ public async Task Unzip(ClaimsPrincipal user, Stream stream) } catch (Exception e) { - _logger.LogError($"Error extracting data into clone path '{extractPath}'. {e.Message}"); + logger.LogError($"Error extracting data into clone path '{extractPath}'. {e.Message}"); } - await _outputCacheStore.EvictByTagAsync(user.FindFirstValue(ClaimTypes.NameIdentifier), default); + await outputCacheStore.EvictByTagAsync(user.FindFirstValue(ClaimTypes.NameIdentifier), default); return extractPath; } @@ -72,9 +61,9 @@ private string GetDataClonePath(ClaimsPrincipal user) var path = $"/data/Data/{activeUserId}"; - if (!_webHostEnvironment.IsProduction()) + if (!webHostEnvironment.IsProduction()) { - path = $"{_webHostEnvironment.ContentRootPath}/Data/{activeUserId}"; + path = $"{webHostEnvironment.ContentRootPath}/Data/{activeUserId}"; } return path; diff --git a/src/Services/MemoriesService.cs b/src/Services/MemoriesService.cs index 93c4b62..dace15a 100644 --- a/src/Services/MemoriesService.cs +++ b/src/Services/MemoriesService.cs @@ -20,32 +20,17 @@ public interface IMemoriesService Task SendMemoriesEmail(); } -public class MemoriesService : IMemoriesService +public class MemoriesService(IConfiguration configuration, + IAccountService accountService, + ISqLiteService sqLiteService, + ISendGridClient sendGridClient, + ILogger logger) : IMemoriesService { - private readonly IConfiguration _configuration; - private readonly IAccountService _accountService; - private readonly ISqLiteService _sqLiteService; - private readonly ISendGridClient _sendGridClient; - private readonly ILogger _logger; - - public MemoriesService(IConfiguration configuration, - IAccountService accountService, - ISqLiteService sqLiteService, - ISendGridClient sendGridClient, - ILogger logger) - { - _configuration = configuration; - _accountService = accountService; - _sqLiteService = sqLiteService; - _sendGridClient = sendGridClient; - _logger = logger; - } - public async Task SendMemoriesEmail() { - var users = await _accountService.SearchUsersWithEnableMemoriesEmail(); + var users = await accountService.SearchUsersWithEnableMemoriesEmail(); - if (users?.Any() != true) + if (users?.Count == 0) { return; } @@ -54,15 +39,15 @@ public async Task SendMemoriesEmail() { var userId = user.UserId.Replace("auth0|", string.Empty); - _sqLiteService.OpenDataConnection(userId); + sqLiteService.OpenDataConnection(userId); - var babies = await _sqLiteService.GetBabiesFromDb(); + var babies = await sqLiteService.GetBabiesFromDb(); foreach (var baby in babies) { - var memories = await _sqLiteService.GetMemoriesFromDb(DateTime.Now, baby.Name); + var memories = await sqLiteService.GetMemoriesFromDb(DateTime.Now, baby.Name); - _logger.LogInformation($"Found {memories.Count} memories for {baby.Name}"); + logger.LogInformation($"Found {memories.Count} memories for {baby.Name}"); if (memories.Count > 0) { @@ -70,7 +55,7 @@ public async Task SendMemoriesEmail() } } - await _sqLiteService.CloseDataConnection(); + await sqLiteService.CloseDataConnection(); } } @@ -79,7 +64,7 @@ private async Task GetMJML(List memories, string userId, s var model = new MemoriesEmailViewModel { BabyName = babyName, - BaseUrl = _configuration["BASE_URL"] ?? string.Empty, + BaseUrl = configuration["BASE_URL"] ?? string.Empty, UserId = userId, Entries = memories .OrderByDescending(entry => entry.Time.ToDateTimeUTC().Year) @@ -106,7 +91,7 @@ private static string GetHTML(string mjml) { var msg = new SendGridMessage() { - From = new EmailAddress(_configuration["MEMORIES_FROM_EMAIL"], _configuration["MEMORIES_FROM_NAME"]), + From = new EmailAddress(configuration["MEMORIES_FROM_EMAIL"], configuration["MEMORIES_FROM_NAME"]), Subject = $"BabyTracker - Memories {DateTime.Now.ToShortDateString()}" }; @@ -129,7 +114,7 @@ private static string GetHTML(string mjml) msg.AddTo(new EmailAddress(recipient)); } - var response = await _sendGridClient.SendEmailAsync(msg); + var response = await sendGridClient.SendEmailAsync(msg); return response; } diff --git a/src/Services/PictureService.cs b/src/Services/PictureService.cs index f527d2f..ed37612 100644 --- a/src/Services/PictureService.cs +++ b/src/Services/PictureService.cs @@ -1,8 +1,6 @@ -using System.Collections.Generic; -using System.IO; +using System.IO; using System.Security.Claims; using System.Threading.Tasks; -using BabyTracker.Models.Database; using Microsoft.Extensions.Hosting; namespace BabyTracker.Services; diff --git a/src/Services/SqLiteService.cs b/src/Services/SqLiteService.cs index b60b769..16af7b0 100644 --- a/src/Services/SqLiteService.cs +++ b/src/Services/SqLiteService.cs @@ -40,16 +40,10 @@ public interface ISqLiteService Task> Search(string match, string babyName); } -public class SqLiteService : ISqLiteService +public class SqLiteService(IWebHostEnvironment webHostEnvironment) : ISqLiteService { - private readonly IWebHostEnvironment _webHostEnvironment; private DataConnection? _db; - public SqLiteService(IWebHostEnvironment webHostEnvironment) - { - _webHostEnvironment = webHostEnvironment; - } - public void OpenDataConnection(ClaimsPrincipal user) { var activeUserId = user.FindFirstValue("activeUserId"); @@ -63,7 +57,7 @@ public void OpenDataConnection(ClaimsPrincipal user) } /// - /// Open a connection with the sqlite db file + /// Open a connection with the SQLite db file /// /// userId without prefix /// @@ -71,9 +65,9 @@ public void OpenDataConnection(string userId) { var path = $"/data/Data/{userId}/EasyLog.db"; - if (!_webHostEnvironment.IsProduction()) + if (!webHostEnvironment.IsProduction()) { - path = Path.Combine(_webHostEnvironment.ContentRootPath, "Data", userId, "EasyLog.db"); + path = Path.Combine(webHostEnvironment.ContentRootPath, "Data", userId, "EasyLog.db"); } if (!Path.Exists(path)) @@ -136,7 +130,7 @@ public async Task> GetBabiesFromDb() { if (_db == null) { - return new(); + return []; } var babies = await _db.GetTable()