Skip to content

Commit

Permalink
✨ add partial class for PermissionSet
Browse files Browse the repository at this point in the history
  • Loading branch information
neozhu committed Feb 13, 2024
1 parent cb77fe7 commit 57cca57
Show file tree
Hide file tree
Showing 33 changed files with 806 additions and 799 deletions.
42 changes: 16 additions & 26 deletions src/Application/Features/Customers/Caching/CustomerCacheKey.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,41 +5,31 @@ namespace CleanArchitecture.Blazor.Application.Features.Customers.Caching;

public static class CustomerCacheKey
{
public const string GetAllCacheKey = "all-Customers";
private static readonly TimeSpan refreshInterval = TimeSpan.FromHours(3);
private static CancellationTokenSource _tokensource;

static CustomerCacheKey()
{
_tokensource = new CancellationTokenSource(refreshInterval);
}

public static MemoryCacheEntryOptions MemoryCacheEntryOptions =>
new MemoryCacheEntryOptions().AddExpirationToken(new CancellationChangeToken(SharedExpiryTokenSource().Token));

public static string GetPaginationCacheKey(string parameters)
{
public const string GetAllCacheKey = "all-Customers";
public static string GetPaginationCacheKey(string parameters) {
return $"CustomerCacheKey:CustomersWithPaginationQuery,{parameters}";
}

public static string GetByNameCacheKey(string parameters)
{
public static string GetByNameCacheKey(string parameters) {
return $"CustomerCacheKey:GetByNameCacheKey,{parameters}";
}

public static string GetByIdCacheKey(string parameters)
{
public static string GetByIdCacheKey(string parameters) {
return $"CustomerCacheKey:GetByIdCacheKey,{parameters}";
}

static CustomerCacheKey()
{
_tokensource = new CancellationTokenSource(refreshInterval);
}
private static CancellationTokenSource _tokensource;
public static CancellationTokenSource SharedExpiryTokenSource()
{
if (_tokensource.IsCancellationRequested) _tokensource = new CancellationTokenSource(refreshInterval);
if (_tokensource.IsCancellationRequested)
{
_tokensource = new CancellationTokenSource(refreshInterval);
}
return _tokensource;
}
public static void Refresh() => SharedExpiryTokenSource().Cancel();
public static MemoryCacheEntryOptions MemoryCacheEntryOptions => new MemoryCacheEntryOptions().AddExpirationToken(new CancellationChangeToken(SharedExpiryTokenSource().Token));
}

public static void Refresh()
{
SharedExpiryTokenSource().Cancel();
}
}
Original file line number Diff line number Diff line change
@@ -1,70 +1,70 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using CleanArchitecture.Blazor.Application.Features.Customers.Caching;
using CleanArchitecture.Blazor.Application.Features.Customers.DTOs;

using CleanArchitecture.Blazor.Application.Features.Customers.Caching;
namespace CleanArchitecture.Blazor.Application.Features.Customers.Commands.AddEdit;

public class AddEditCustomerCommand : ICacheInvalidatorRequest<Result<int>>
public class AddEditCustomerCommand: ICacheInvalidatorRequest<Result<int>>
{
[Description("Id")] public int Id { get; set; }
[Description("Id")]
public int Id { get; set; }
[Description("Name")]
public string Name {get;set;} = String.Empty;
[Description("Description")]
public string? Description {get;set;}

[Description("Name")] public string Name { get; set; } = string.Empty;

[Description("Description")] public string? Description { get; set; }


public string CacheKey => CustomerCacheKey.GetAllCacheKey;
public CancellationTokenSource? SharedExpiryTokenSource => CustomerCacheKey.SharedExpiryTokenSource();
public string CacheKey => CustomerCacheKey.GetAllCacheKey;
public CancellationTokenSource? SharedExpiryTokenSource => CustomerCacheKey.SharedExpiryTokenSource();

private class Mapping : Profile
{
public Mapping()
{
CreateMap<CustomerDto, AddEditCustomerCommand>(MemberList.None);
CreateMap<AddEditCustomerCommand, Customer>(MemberList.None);
CreateMap<CustomerDto,AddEditCustomerCommand>(MemberList.None);
CreateMap<AddEditCustomerCommand,Customer>(MemberList.None);

}
}
}

public class AddEditCustomerCommandHandler : IRequestHandler<AddEditCustomerCommand, Result<int>>
{
private readonly IApplicationDbContext _context;
private readonly IStringLocalizer<AddEditCustomerCommandHandler> _localizer;
private readonly IMapper _mapper;

public AddEditCustomerCommandHandler(
IApplicationDbContext context,
IStringLocalizer<AddEditCustomerCommandHandler> localizer,
IMapper mapper
)
public class AddEditCustomerCommandHandler : IRequestHandler<AddEditCustomerCommand, Result<int>>
{
_context = context;
_localizer = localizer;
_mapper = mapper;
}

public async Task<Result<int>> Handle(AddEditCustomerCommand request, CancellationToken cancellationToken)
{
if (request.Id > 0)
private readonly IApplicationDbContext _context;
private readonly IMapper _mapper;
private readonly IStringLocalizer<AddEditCustomerCommandHandler> _localizer;
public AddEditCustomerCommandHandler(
IApplicationDbContext context,
IStringLocalizer<AddEditCustomerCommandHandler> localizer,
IMapper mapper
)
{
var item = await _context.Customers.FindAsync(new object[] { request.Id }, cancellationToken) ??
throw new NotFoundException($"Customer with id: [{request.Id}] not found.");
item = _mapper.Map(request, item);
// raise a update domain event
item.AddDomainEvent(new CustomerUpdatedEvent(item));
await _context.SaveChangesAsync(cancellationToken);
return await Result<int>.SuccessAsync(item.Id);
_context = context;
_localizer = localizer;
_mapper = mapper;
}
else
public async Task<Result<int>> Handle(AddEditCustomerCommand request, CancellationToken cancellationToken)
{
var item = _mapper.Map<Customer>(request);
// raise a create domain event
item.AddDomainEvent(new CustomerCreatedEvent(item));
_context.Customers.Add(item);
await _context.SaveChangesAsync(cancellationToken);
return await Result<int>.SuccessAsync(item.Id);
if (request.Id > 0)
{
var item = await _context.Customers.FindAsync(new object[] { request.Id }, cancellationToken) ?? throw new NotFoundException($"Customer with id: [{request.Id}] not found.");
item = _mapper.Map(request, item);
// raise a update domain event
item.AddDomainEvent(new CustomerUpdatedEvent(item));
await _context.SaveChangesAsync(cancellationToken);
return await Result<int>.SuccessAsync(item.Id);
}
else
{
var item = _mapper.Map<Customer>(request);
// raise a create domain event
item.AddDomainEvent(new CustomerCreatedEvent(item));
_context.Customers.Add(item);
await _context.SaveChangesAsync(cancellationToken);
return await Result<int>.SuccessAsync(item.Id);
}

}
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@ public class AddEditCustomerCommandValidator : AbstractValidator<AddEditCustomer
{
public AddEditCustomerCommandValidator()
{
RuleFor(v => v.Name)
.MaximumLength(256)
.NotEmpty();
}
}
RuleFor(v => v.Name)
.MaximumLength(256)
.NotEmpty();

}

}

Original file line number Diff line number Diff line change
@@ -1,56 +1,55 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using CleanArchitecture.Blazor.Application.Features.Customers.Caching;
using System.ComponentModel;
using CleanArchitecture.Blazor.Application.Features.Customers.DTOs;
using CleanArchitecture.Blazor.Application.Features.Customers.Caching;

namespace CleanArchitecture.Blazor.Application.Features.Customers.Commands.Create;

public class CreateCustomerCommand : ICacheInvalidatorRequest<Result<int>>
public class CreateCustomerCommand: ICacheInvalidatorRequest<Result<int>>
{
[Description("Id")] public int Id { get; set; }

[Description("Name")] public string Name { get; set; } = string.Empty;

[Description("Description")] public string? Description { get; set; }

public string CacheKey => CustomerCacheKey.GetAllCacheKey;
public CancellationTokenSource? SharedExpiryTokenSource => CustomerCacheKey.SharedExpiryTokenSource();

[Description("Id")]
public int Id { get; set; }
[Description("Name")]
public string Name {get;set;} = String.Empty;
[Description("Description")]
public string? Description {get;set;}

public string CacheKey => CustomerCacheKey.GetAllCacheKey;
public CancellationTokenSource? SharedExpiryTokenSource => CustomerCacheKey.SharedExpiryTokenSource();
private class Mapping : Profile
{
public Mapping()
{
CreateMap<CustomerDto, CreateCustomerCommand>(MemberList.None);
CreateMap<CreateCustomerCommand, Customer>(MemberList.None);
CreateMap<CustomerDto,CreateCustomerCommand>(MemberList.None);
CreateMap<CreateCustomerCommand,Customer>(MemberList.None);
}
}
}

public class CreateCustomerCommandHandler : IRequestHandler<CreateCustomerCommand, Result<int>>
{
private readonly IApplicationDbContext _context;
private readonly IStringLocalizer<CreateCustomerCommand> _localizer;
private readonly IMapper _mapper;

public CreateCustomerCommandHandler(
IApplicationDbContext context,
IStringLocalizer<CreateCustomerCommand> localizer,
IMapper mapper
)

public class CreateCustomerCommandHandler : IRequestHandler<CreateCustomerCommand, Result<int>>
{
_context = context;
_localizer = localizer;
_mapper = mapper;
private readonly IApplicationDbContext _context;
private readonly IMapper _mapper;
private readonly IStringLocalizer<CreateCustomerCommand> _localizer;
public CreateCustomerCommandHandler(
IApplicationDbContext context,
IStringLocalizer<CreateCustomerCommand> localizer,
IMapper mapper
)
{
_context = context;
_localizer = localizer;
_mapper = mapper;
}
public async Task<Result<int>> Handle(CreateCustomerCommand request, CancellationToken cancellationToken)
{
var item = _mapper.Map<Customer>(request);
// raise a create domain event
item.AddDomainEvent(new CustomerCreatedEvent(item));
_context.Customers.Add(item);
await _context.SaveChangesAsync(cancellationToken);
return await Result<int>.SuccessAsync(item.Id);
}
}

public async Task<Result<int>> Handle(CreateCustomerCommand request, CancellationToken cancellationToken)
{
var item = _mapper.Map<Customer>(request);
// raise a create domain event
item.AddDomainEvent(new CustomerCreatedEvent(item));
_context.Customers.Add(item);
await _context.SaveChangesAsync(cancellationToken);
return await Result<int>.SuccessAsync(item.Id);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,14 @@ namespace CleanArchitecture.Blazor.Application.Features.Customers.Commands.Creat

public class CreateCustomerCommandValidator : AbstractValidator<CreateCustomerCommand>
{
public CreateCustomerCommandValidator()
{
RuleFor(v => v.Name)
.MaximumLength(256)
.NotEmpty();
}
}
public CreateCustomerCommandValidator()
{

RuleFor(v => v.Name)
.MaximumLength(256)
.NotEmpty();

}

}

Original file line number Diff line number Diff line change
Expand Up @@ -3,50 +3,49 @@

using CleanArchitecture.Blazor.Application.Features.Customers.Caching;


namespace CleanArchitecture.Blazor.Application.Features.Customers.Commands.Delete;

public class DeleteCustomerCommand : ICacheInvalidatorRequest<Result<int>>
{
public DeleteCustomerCommand(int[] id)
public class DeleteCustomerCommand: ICacheInvalidatorRequest<Result<int>>
{
public int[] Id { get; }
public string CacheKey => CustomerCacheKey.GetAllCacheKey;
public CancellationTokenSource? SharedExpiryTokenSource => CustomerCacheKey.SharedExpiryTokenSource();
public DeleteCustomerCommand(int[] id)
{
Id = id;
}
}

public int[] Id { get; }
public string CacheKey => CustomerCacheKey.GetAllCacheKey;
public CancellationTokenSource? SharedExpiryTokenSource => CustomerCacheKey.SharedExpiryTokenSource();
}

public class DeleteCustomerCommandHandler :
IRequestHandler<DeleteCustomerCommand, Result<int>>

{
private readonly IApplicationDbContext _context;
private readonly IStringLocalizer<DeleteCustomerCommandHandler> _localizer;
private readonly IMapper _mapper;
public class DeleteCustomerCommandHandler :
IRequestHandler<DeleteCustomerCommand, Result<int>>

public DeleteCustomerCommandHandler(
IApplicationDbContext context,
IStringLocalizer<DeleteCustomerCommandHandler> localizer,
IMapper mapper
)
{
_context = context;
_localizer = localizer;
_mapper = mapper;
}

public async Task<Result<int>> Handle(DeleteCustomerCommand request, CancellationToken cancellationToken)
{
var items = await _context.Customers.Where(x => request.Id.Contains(x.Id)).ToListAsync(cancellationToken);
foreach (var item in items)
private readonly IApplicationDbContext _context;
private readonly IMapper _mapper;
private readonly IStringLocalizer<DeleteCustomerCommandHandler> _localizer;
public DeleteCustomerCommandHandler(
IApplicationDbContext context,
IStringLocalizer<DeleteCustomerCommandHandler> localizer,
IMapper mapper
)
{
_context = context;
_localizer = localizer;
_mapper = mapper;
}
public async Task<Result<int>> Handle(DeleteCustomerCommand request, CancellationToken cancellationToken)
{
// raise a delete domain event
item.AddDomainEvent(new CustomerDeletedEvent(item));
_context.Customers.Remove(item);
var items = await _context.Customers.Where(x=>request.Id.Contains(x.Id)).ToListAsync(cancellationToken);
foreach (var item in items)
{
// raise a delete domain event
item.AddDomainEvent(new CustomerDeletedEvent(item));
_context.Customers.Remove(item);
}
var result = await _context.SaveChangesAsync(cancellationToken);
return await Result<int>.SuccessAsync(result);
}

var result = await _context.SaveChangesAsync(cancellationToken);
return await Result<int>.SuccessAsync(result);
}
}

Loading

0 comments on commit 57cca57

Please sign in to comment.