-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Added the option to set a default task list. (#24)
* feat: Added the option to set a default task list. * feat: Minor optimizations.
- Loading branch information
1 parent
86ee9f3
commit ad21adc
Showing
26 changed files
with
1,108 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
using Chrono.Application.Entities.Common; | ||
|
||
namespace Chrono.Application.Entities; | ||
|
||
public class UserSettings : BaseEntity | ||
{ | ||
public int UserId { get; set; } | ||
public User User { get; set; } | ||
|
||
public TaskList DefaultTaskList { get; set; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
using Chrono.Application.Common.Exceptions; | ||
using Chrono.Application.Common.Interfaces; | ||
using MediatR; | ||
using Microsoft.EntityFrameworkCore; | ||
|
||
namespace Chrono.Application.Features.User; | ||
|
||
public record GetUserSettings : IRequest<UserSettingsDto>; | ||
|
||
public class GetUserSettingsHandler : IRequestHandler<GetUserSettings, UserSettingsDto> | ||
{ | ||
private readonly IApplicationDbContext _context; | ||
private readonly ICurrentUserService _currentUserService; | ||
|
||
public GetUserSettingsHandler(IApplicationDbContext context, ICurrentUserService currentUserService) | ||
{ | ||
_context = context; | ||
_currentUserService = currentUserService; | ||
} | ||
|
||
public async Task<UserSettingsDto> Handle(GetUserSettings request, CancellationToken cancellationToken) | ||
{ | ||
var currentUser = await _context.Users | ||
.Include(x => x.UserSettings) | ||
.SingleOrDefaultAsync(x => x.UserId == _currentUserService.UserId, cancellationToken); | ||
|
||
if (currentUser == null) | ||
{ | ||
throw new NotFoundException($"User \"{_currentUserService.UserId}\" not found."); | ||
} | ||
|
||
return new UserSettingsDto { DefaultTaskListId = currentUser.UserSettings?.DefaultTaskList?.Id }; | ||
} | ||
} | ||
|
||
public class UserSettingsDto | ||
{ | ||
public int? DefaultTaskListId { get; init; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
using Chrono.Application.Common.Exceptions; | ||
using Chrono.Application.Common.Extensions; | ||
using Chrono.Application.Common.Interfaces; | ||
using Chrono.Application.Entities; | ||
using MediatR; | ||
using Microsoft.EntityFrameworkCore; | ||
using Task = System.Threading.Tasks.Task; | ||
|
||
namespace Chrono.Application.Features.User; | ||
|
||
public record UpdateUserSettings : IRequest | ||
{ | ||
public int? DefaultTaskListId { get; init; } | ||
} | ||
|
||
public class UpdateUserSettingsHandler : IRequestHandler<UpdateUserSettings> | ||
{ | ||
private readonly IApplicationDbContext _context; | ||
private readonly ICurrentUserService _currentUserService; | ||
|
||
public UpdateUserSettingsHandler(IApplicationDbContext context, ICurrentUserService currentUserService) | ||
{ | ||
_context = context; | ||
_currentUserService = currentUserService; | ||
} | ||
|
||
public async Task Handle(UpdateUserSettings request, CancellationToken cancellationToken) | ||
{ | ||
var currentUser = await _context.Users | ||
.Include(x => x.UserSettings) | ||
.SingleOrDefaultAsync(x => x.UserId == _currentUserService.UserId, cancellationToken); | ||
|
||
if (currentUser == null) | ||
{ | ||
throw new NotFoundException($"User \"{_currentUserService.UserId}\" not found."); | ||
} | ||
|
||
var userSettings = currentUser.UserSettings; | ||
if (userSettings == null) | ||
{ | ||
userSettings = new UserSettings { User = currentUser, UserId = currentUser.Id }; | ||
_context.UserSettings.Add(userSettings); | ||
} | ||
|
||
if (request.DefaultTaskListId.HasValue) | ||
{ | ||
var defaultTaskListId = request.DefaultTaskListId.Value; | ||
var taskList = await _context.TaskLists | ||
.SingleOrDefaultAsync(x => x.Id == defaultTaskListId, cancellationToken); | ||
|
||
if (taskList == null) | ||
{ | ||
throw new NotFoundException($"Task list \"{defaultTaskListId}\" not found."); | ||
} | ||
|
||
if (!taskList.IsPermitted(_currentUserService.UserId)) | ||
{ | ||
throw new ForbiddenAccessException(); | ||
} | ||
|
||
userSettings.DefaultTaskList = taskList; | ||
} | ||
else | ||
{ | ||
userSettings.DefaultTaskList = null; | ||
} | ||
|
||
await _context.SaveChangesAsync(cancellationToken); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
src/Application/Infrastructure/Configurations/UserSettingsConfiguration.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
using Chrono.Application.Entities; | ||
using Microsoft.EntityFrameworkCore; | ||
using Microsoft.EntityFrameworkCore.Metadata.Builders; | ||
|
||
namespace Chrono.Application.Infrastructure.Configurations; | ||
|
||
public class UserSettingsConfiguration : IEntityTypeConfiguration<UserSettings> | ||
{ | ||
public void Configure(EntityTypeBuilder<UserSettings> builder) | ||
{ | ||
builder.Navigation(x => x.DefaultTaskList) | ||
.AutoInclude(); | ||
} | ||
} |
Oops, something went wrong.