Skip to content

Commit

Permalink
Use primary constructors
Browse files Browse the repository at this point in the history
  • Loading branch information
sboulema committed Nov 20, 2023
1 parent 5d2d3c3 commit dba54e3
Show file tree
Hide file tree
Showing 12 changed files with 100 additions and 202 deletions.
2 changes: 1 addition & 1 deletion src/BabyTracker.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
<PackageReference Include="TimeZoneConverter" Version="6.1.0" />
<PackageReference Include="tusdotnet" Version="2.7.2" />
<PackageReference Include="ClosedXML" Version="0.102.1" />
<PackageReference Include="Quartz.AspNetCore" Version="3.7.0" />
<PackageReference Include="Quartz.AspNetCore" Version="3.8.0" />
</ItemGroup>

<ItemGroup>
Expand Down
19 changes: 6 additions & 13 deletions src/Controllers/AccountController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "")
{
Expand All @@ -34,7 +27,7 @@ public IActionResult Login(string returnUrl = "")
[HttpPost("[action]")]
public async Task<IActionResult> Login(LoginViewModel model, string returnUrl = "")
{
var claimsPrincipal = await _accountService.Login(model);
var claimsPrincipal = await accountService.Login(model);

if (claimsPrincipal == null)
{
Expand All @@ -60,7 +53,7 @@ public IActionResult Register()
[HttpPost("[action]")]
public async Task<IActionResult> Register(LoginViewModel model, string returnUrl = "")
{
await _accountService.Register(model);
await accountService.Register(model);

return await Login(model, returnUrl);
}
Expand All @@ -76,7 +69,7 @@ public IActionResult ResetPassword()
[HttpPost("[action]")]
public async Task<IActionResult> ResetPassword(LoginViewModel model)
{
var result = await _accountService.ResetPassword(model);
var result = await accountService.ResetPassword(model);

TempData["notificationMessage"] = result;
TempData["notificationType"] = "success";
Expand All @@ -99,7 +92,7 @@ public async Task Logout()
[HttpGet("[action]")]
public async Task<IActionResult> Profile()
{
var userMetaData = await _accountService.GetUserMetaData(User);
var userMetaData = await accountService.GetUserMetaData(User);

var model = new ProfileViewModel
{
Expand Down Expand Up @@ -131,7 +124,7 @@ public async Task<IActionResult> 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";
Expand Down
88 changes: 34 additions & 54 deletions src/Controllers/HomeController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,59 +7,39 @@
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;

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<IActionResult> 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);
Expand All @@ -75,7 +55,7 @@ public async Task<IActionResult> 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);
Expand All @@ -92,9 +72,9 @@ public async Task<IActionResult> Index()
[HttpGet("{babyName}/{date?}")]
public async Task<IActionResult> 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)
{
Expand All @@ -112,11 +92,11 @@ public async Task<IActionResult> 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);
Expand All @@ -134,14 +114,14 @@ public async Task<IActionResult> 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;

Expand All @@ -157,10 +137,10 @@ public async Task<IActionResult> 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);
}
Expand All @@ -169,11 +149,11 @@ public async Task<IActionResult> Diary(string babyName, DateOnly? date, string?
[HttpGet("{babyName}/memories")]
public async Task<IActionResult> 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);

Expand All @@ -185,7 +165,7 @@ public async Task<IActionResult> 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);
Expand All @@ -195,13 +175,13 @@ public async Task<IActionResult> Memories(string babyName)
[HttpGet("{babyName}/charts/{months?}")]
public async Task<IActionResult> 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;
Expand All @@ -211,7 +191,7 @@ public async Task<IActionResult> 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);
Expand All @@ -221,12 +201,12 @@ public async Task<IActionResult> Charts(string babyName, int? months = null)
[HttpGet("{babyName}/gallery")]
public async Task<IActionResult> 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
{
Expand All @@ -239,7 +219,7 @@ public async Task<IActionResult> 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);
Expand Down
11 changes: 2 additions & 9 deletions src/Controllers/ImportController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<IActionResult> Import()
Expand All @@ -29,7 +22,7 @@ public async Task<IActionResult> 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);
Expand Down
11 changes: 2 additions & 9 deletions src/Controllers/MemoriesController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/// <summary>
/// Used for testing the memories job
/// </summary>
/// <returns></returns>
[Authorize]
[HttpGet("[action]")]
public async Task Send() => await _memoriesService.SendMemoriesEmail();
public async Task Send() => await memoriesService.SendMemoriesEmail();
}
21 changes: 6 additions & 15 deletions src/Controllers/PictureController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<IActionResult> GetPicture(string fileName)
{
var picture = await _pictureService.GetPicture(_hostEnvironment, User, fileName);
var picture = await pictureService.GetPicture(hostEnvironment, User, fileName);

if (picture == null)
{
Expand All @@ -40,7 +31,7 @@ public async Task<IActionResult> GetPicture(string fileName)
[HttpGet("{filename}/thumbnail")]
public async Task<IActionResult> GetThumbnail(string fileName)
{
var thumbnail = await _pictureService.GetPicture(_hostEnvironment, User, $"{fileName}__thumbnail");
var thumbnail = await pictureService.GetPicture(hostEnvironment, User, $"{fileName}__thumbnail");

if (thumbnail == null)
{
Expand All @@ -53,7 +44,7 @@ public async Task<IActionResult> GetThumbnail(string fileName)
[HttpGet("{userId}/{fileName}")]
public async Task<IActionResult> GetPicture(string userId, string fileName)
{
var picture = await _pictureService.GetPicture(_hostEnvironment, userId, fileName);
var picture = await pictureService.GetPicture(hostEnvironment, userId, fileName);

if (picture == null)
{
Expand All @@ -66,7 +57,7 @@ public async Task<IActionResult> GetPicture(string userId, string fileName)
[HttpGet("{userId}/{filename}/thumbnail")]
public async Task<IActionResult> 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)
{
Expand Down
Loading

0 comments on commit dba54e3

Please sign in to comment.