Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implemented task list options (see #9) #10

Merged
merged 3 commits into from
Jul 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/Application/Application.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\Domain\Domain.csproj"/>
<ProjectReference Include="..\Domain\Domain.csproj" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="FluentValidation.DependencyInjectionExtensions" Version="11.6.0"/>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="7.0.9"/>
<PackageReference Include="FluentValidation.DependencyInjectionExtensions" Version="11.6.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="7.0.9" />
</ItemGroup>
</Project>
1 change: 1 addition & 0 deletions src/Application/Common/Interfaces/IApplicationDbContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ public interface IApplicationDbContext
{
DbSet<Task> Tasks { get; }
DbSet<TaskList> TaskLists { get; }
DbSet<TaskListOptions> TaskListOptions { get; }
DbSet<Category> Categories { get; }
DbSet<TaskCategory> TaskCategories { get; }
DbSet<User> Users { get; }
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
using Chrono.Application.Common.Exceptions;
using Chrono.Application.Common.Interfaces;
using Chrono.Application.Common.Security;
using Chrono.Domain.Entities;
using MediatR;
using Microsoft.EntityFrameworkCore;
using Task = System.Threading.Tasks.Task;

namespace Chrono.Application.Tasks.Commands.UpdateTaskList;

public record UpdateTaskListCommand : IRequest
{
public int TaskListId { get; init; }
public string Title { get; init; }
public bool? RequireBusinessValue { get; init; }
public bool? RequireDescription { get; init; }
}

public class UpdateTaskListCommandHandler : IRequestHandler<UpdateTaskListCommand>
{
private readonly IApplicationDbContext _context;
private readonly ICurrentUserService _currentUserService;

public UpdateTaskListCommandHandler(IApplicationDbContext context, ICurrentUserService currentUserService)
{
_context = context;
_currentUserService = currentUserService;
}

public async Task Handle(UpdateTaskListCommand request, CancellationToken cancellationToken)
{
var taskList = await _context.TaskLists
.SingleOrDefaultAsync(x => x.Id == request.TaskListId, cancellationToken);

if (taskList == null)
{
throw new NotFoundException($"Task list \"{request.TaskListId}\" not found.");
}

if (!taskList.IsPermitted(_currentUserService.UserId))
{
throw new ForbiddenAccessException();
}

if (taskList.Title != request.Title)
{
taskList.Title = request.Title;
}

var options = taskList.Options;
if (options == null)
{
options = new TaskListOptions { TaskList = taskList, TaskListId = taskList.Id };
_context.TaskListOptions.Add(options);
}

options.RequireBusinessValue = request.RequireBusinessValue.GetValueOrDefault();
options.RequireDescription = request.RequireDescription.GetValueOrDefault();

await _context.SaveChangesAsync(cancellationToken);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using Chrono.Application.Tasks.Commands.UpdateTaskList;
using FluentValidation;

namespace Chrono.Application.TaskLists.Commands.UpdateTaskList;

public class UpdateTaskListCommandValidator : AbstractValidator<UpdateTaskListCommand>
{
public UpdateTaskListCommandValidator()
{
RuleFor(v => v.TaskListId)
.NotEmpty();

RuleFor(v => v.Title)
.NotEmpty();

RuleFor(v => v.RequireBusinessValue)
.NotNull();

RuleFor(v => v.RequireDescription)
.NotNull();
}
}
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 Chrono.Application.Common.Security;
using MediatR;
using Microsoft.EntityFrameworkCore;

namespace Chrono.Application.TaskLists.Queries.GetTaskListOptions;

public record GetTaskListOptionsQuery(int ListId) : IRequest<TaskListOptionsDto>;

public class GetTaskListOptionsQueryHandler : IRequestHandler<GetTaskListOptionsQuery, TaskListOptionsDto>
{
private readonly IApplicationDbContext _context;
private readonly ICurrentUserService _currentUserService;

public GetTaskListOptionsQueryHandler(IApplicationDbContext context, ICurrentUserService currentUserService)
{
_context = context;
_currentUserService = currentUserService;
}

public async Task<TaskListOptionsDto> Handle(GetTaskListOptionsQuery request, CancellationToken cancellationToken)
{
var taskList = await _context.TaskLists
.SingleOrDefaultAsync(x => x.Id == request.ListId, cancellationToken);

if (taskList == null)
{
throw new NotFoundException($"Task list \"{request.ListId}\" not found.");
}

if (!taskList.IsPermitted(_currentUserService.UserId))
{
throw new ForbiddenAccessException();
}

return TaskListOptionsDto.FromEntity(taskList.Options);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using Chrono.Domain.Entities;

namespace Chrono.Application.TaskLists.Queries.GetTaskListOptions;

public class TaskListOptionsDto
{
public bool RequireBusinessValue { get; set; }
public bool RequireDescription { get; set; }

public static TaskListOptionsDto FromEntity(TaskListOptions taskListOptions)
{
return new TaskListOptionsDto
{
RequireBusinessValue = taskListOptions?.RequireBusinessValue ?? true,
RequireDescription = taskListOptions?.RequireDescription ?? true
};
}
}
28 changes: 18 additions & 10 deletions src/Application/Tasks/Commands/CreateTask/CreateTaskCommand.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
using MediatR;
using Chrono.Domain.Services;
using Microsoft.EntityFrameworkCore;
using Chrono.Application.Common.Dtos;
using Chrono.Application.Common.Security;
using Chrono.Application.Common.Exceptions;
using Chrono.Application.Common.Interfaces;
using Chrono.Application.Common.Security;
using Chrono.Domain.Services;
using MediatR;
using Microsoft.EntityFrameworkCore;
using Task = Chrono.Domain.Entities.Task;

namespace Chrono.Application.Tasks.Commands.CreateTask;

Expand Down Expand Up @@ -36,9 +37,16 @@ public async Task<int> Handle(CreateTaskCommand request, CancellationToken cance
.SingleOrDefaultAsync(x => x.Id == request.ListId, cancellationToken);

if (taskList == null)
{
throw new NotFoundException($"Task list \"{request.ListId}\" not found.");
}

if (!taskList.IsPermitted(_currentUserService.UserId))
{
throw new ForbiddenAccessException();
}

var entity = new Domain.Entities.Task
var entity = new Task
{
Name = request.Name,
Position = request.Position,
Expand All @@ -48,13 +56,13 @@ public async Task<int> Handle(CreateTaskCommand request, CancellationToken cance
var newCategoryNames = request.Categories.Select(x => x.Name).ToArray();
TaskService.SetCategories(entity,
_context.Categories
.Where(x => newCategoryNames.Contains(x.Name))
.AsEnumerable()
.Where(x => x.IsPermitted(_currentUserService.UserId))
.ToArray()
.Where(x => newCategoryNames.Contains(x.Name))
.AsEnumerable()
.Where(x => x.IsPermitted(_currentUserService.UserId))
.ToArray()
);

TaskListService.InsertAt(request.Position, entity, targetTaskList: taskList);
TaskListService.InsertAt(request.Position, entity, taskList);

_context.Tasks.Add(entity);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
using Chrono.Application.Common.Interfaces;
using Chrono.Domain.Entities;
using FluentValidation;

namespace Chrono.Application.Tasks.Commands.CreateTask;

public class CreateTaskCommandValidator : AbstractValidator<CreateTaskCommand>
{
public CreateTaskCommandValidator()
public CreateTaskCommandValidator(IApplicationDbContext dbContext)
{
RuleFor(v => v.ListId)
.NotEmpty();
Expand All @@ -15,16 +17,23 @@ public CreateTaskCommandValidator()
RuleFor(v => v.Name)
.NotEmpty();

RuleFor(v => v.Categories)
.NotNull();

RuleFor(v => v.BusinessValue)
.NotEmpty();
.NotEmpty()
.When(x => GetTaskListOptions(dbContext, x.ListId)?.RequireBusinessValue ?? true);

RuleFor(v => v.Description)
.NotEmpty();

RuleFor(v => v.Categories)
.NotNull();
.NotEmpty()
.When(x => GetTaskListOptions(dbContext, x.ListId)?.RequireDescription ?? true);

RuleForEach(v => v.Categories)
.ChildRules(child => child.RuleFor(x => x.Name).NotEmpty());
}

private TaskListOptions GetTaskListOptions(IApplicationDbContext dbContext, int taskListId)
{
return dbContext.TaskLists.FirstOrDefault(x => x.Id == taskListId)?.Options;
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
using Chrono.Application.Common.Interfaces;
using Chrono.Domain.Entities;
using FluentValidation;

namespace Chrono.Application.Tasks.Commands.UpdateTask;

public class UpdateTaskCommandValidator : AbstractValidator<UpdateTaskCommand>
{
public UpdateTaskCommandValidator()
public UpdateTaskCommandValidator(IApplicationDbContext dbContext)
{
RuleFor(v => v.Id)
.NotEmpty();
Expand All @@ -15,16 +17,24 @@ public UpdateTaskCommandValidator()
RuleFor(v => v.Name)
.NotEmpty();

RuleFor(v => v.BusinessValue)
.NotEmpty();

RuleFor(v => v.Description)
.NotEmpty();

RuleFor(v => v.Categories)
.NotNull();

RuleForEach(v => v.Categories)
.ChildRules(child => child.RuleFor(x => x.Name).NotEmpty());

RuleFor(v => v.BusinessValue)
.NotEmpty()
.When(x => GetTaskListOptions(dbContext, x.Id)?.RequireBusinessValue ?? false);

RuleFor(v => v.Description)
.NotEmpty()
.When(x => GetTaskListOptions(dbContext, x.Id)?.RequireDescription ?? false);
}

private TaskListOptions GetTaskListOptions(IApplicationDbContext dbContext, int taskId)
{
var task = dbContext.Tasks.FirstOrDefault(x => x.Id == taskId);
return task?.List?.Options;
}
}
2 changes: 1 addition & 1 deletion src/Domain/Domain.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="MediatR" Version="12.1.1"/>
<PackageReference Include="MediatR" Version="12.1.1" />
</ItemGroup>

</Project>
1 change: 1 addition & 0 deletions src/Domain/Entities/TaskList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ public class TaskList : BaseAuditableEntity
{
public string Title { get; set; }
public IList<Task> Tasks { get; set; } = new List<Task>();
public TaskListOptions Options { get; set; }
}
11 changes: 11 additions & 0 deletions src/Domain/Entities/TaskListOptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
namespace Chrono.Domain.Entities;

public class TaskListOptions
{
public int Id { get; set; }
public bool RequireBusinessValue { get; set; } = true;
public bool RequireDescription { get; set; } = true;

public int TaskListId { get; set; }
public TaskList TaskList { get; set; }
}
5 changes: 1 addition & 4 deletions src/Infrastructure/Configurations/TaskConfiguration.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using Microsoft.EntityFrameworkCore;
using Task = Chrono.Domain.Entities.Task;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using Task = Chrono.Domain.Entities.Task;

namespace Chrono.Infrastructure.Configurations;

Expand All @@ -12,9 +12,6 @@ public void Configure(EntityTypeBuilder<Task> builder)
.HasMaxLength(200)
.IsRequired();

builder.Property(x => x.BusinessValue)
.IsRequired();

builder.Property(x => x.Position)
.IsRequired();

Expand Down
12 changes: 11 additions & 1 deletion src/Infrastructure/Configurations/TaskListConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,17 @@ public void Configure(EntityTypeBuilder<TaskList> builder)
.WithOne(x => x.List)
.HasForeignKey(x => x.ListId)
.HasPrincipalKey(x => x.Id);


builder.HasOne(x => x.Options)
.WithOne(x => x.TaskList)
.HasPrincipalKey<TaskList>(x => x.Id)
.HasForeignKey<TaskListOptions>(x => x.TaskListId)
.OnDelete(DeleteBehavior.Cascade)
.IsRequired(false);

builder.Navigation(x => x.Options)
.AutoInclude();

builder.Navigation(x => x.CreatedBy)
.AutoInclude();
}
Expand Down
Empty file.
Empty file.
Loading