Skip to content

Commit

Permalink
Merge pull request #63 from TunNetCom/FixAndUpgrade
Browse files Browse the repository at this point in the history
Add Organization process
  • Loading branch information
MarwenSaad authored Sep 17, 2024
2 parents cf9aff9 + 1e2c116 commit e64f317
Show file tree
Hide file tree
Showing 12 changed files with 101 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -1,15 +1,55 @@
using TimeLogService.Application.Feature.UserAction.Commands.AddUser;
using System.Collections.Generic;
using TimeLogService.Application.Feature.OrganizationAction.Commands.AddOrganization;
using TimeLogService.Application.Feature.OrganizationAction.Commands.AddOrganizationList;
using TimeLogService.Application.Feature.UserAction.Commands.AddUser;

namespace TimeLogService.Application.Feature.MessageBroker.Consumer.ProfileUser;

public class ProfileUserConsumer(ILogger<ProfileUserConsumer> logger, IMediator mediator) : IConsumer<UserProfile>, IConsumer<CustomProblemDetailsResponce>
public class ProfileUserConsumer(ILogger<ProfileUserConsumer> logger, IMediator mediator, IRepository<User> repository, IRepository<Organization> repositoryOrganization) : IConsumer<UserProfile>, IConsumer<CustomProblemDetailsResponce>
{
private readonly ILogger<ProfileUserConsumer> _logger = logger;
private readonly IMediator _mediator = mediator;
private readonly IRepository<User> _repository = repository;
private readonly IRepository<Organization> _repositoryOrganization = repositoryOrganization;

public async Task Consume(ConsumeContext<UserProfile> context)
{
await _mediator.Send(new AddUserCommand(context.Message));
User user = await _repository.GetSingleAsync(x => x.UserId == context.Message.Id);

if (user is null)
{
await _mediator.Send(new AddUserCommand(context.Message));

if (context.Message.UserAccount.Count > 0)
{
IReadOnlyList<Organization> organizationList = await _repositoryOrganization.GetManyAsync(x => x.UserId == context.Message!.Id);

var existingAccountIds = new HashSet<string>(organizationList.Select(x => x.AccountId));

List<Organization> organizations = new List<Organization>();

foreach (UserOrganization org in context.Message.UserAccount!.Value)
{
if (!existingAccountIds.Contains(org.AccountId))
{
organizations.Add(new Organization
{
AccountId = org.AccountId,
AccountUri = org.AccountUri.ToString(),
Name = org.AccountName,
UserId = context.Message.Id,
IsAionTimeApproved = false,
});
}
}

if (organizations.Count > 0)
{
await _mediator.Send(new AddOrganizationListCommand(organizations));
}
}
}

_logger.LogInformation(JsonConvert.SerializeObject(context.Message));
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TimeLogService.Application.Feature.OrganizationAction.Commands.AddOrganizationList;

public record class AddOrganizationListCommand(List<Organization> Organizations) : IRequest;
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace TimeLogService.Application.Feature.OrganizationAction.Commands.AddOrganizationList
{
public class AddOrganizationListCommandHandler(IRepository<Organization> repository) : IRequestHandler<AddOrganizationListCommand>
{
private readonly IRepository<Organization> _repository = repository;

public async Task Handle(AddOrganizationListCommand request, CancellationToken cancellationToken)
{
await _repository.AddRangeAsync(request.Organizations);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@

namespace TimeLogService.Application.Feature.UserAction.Commands.AddUser;

public record class AddUserCommand(UserProfile UserProfile) : IRequest<OneOf<True, False>>;
public record class AddUserCommand(UserProfile UserProfile) : IRequest;
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public class AddUserCommandHandler(IRepository<User> repository, IMapper mapper)
private readonly IRepository<User> _repository = repository;
private readonly IMapper _mapper = mapper;

public async Task<OneOf<True,False>> Handle(AddUserCommand request, CancellationToken cancellationToken)
public async Task Handle(AddUserCommand request, CancellationToken cancellationToken)
{

User user = new()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace TimeLogService.Domain.Interfaces.Repositories;
using System.Linq.Expressions;

namespace TimeLogService.Domain.Interfaces.Repositories;

public interface IRepository<T>
where T : BaseEntity
Expand All @@ -7,6 +9,10 @@ public interface IRepository<T>

Task<T?> GetByIdAsync(int id);

Task<IReadOnlyList<T>> GetManyAsync(Expression<Func<T, bool>> predicate);

Task<T?> GetSingleAsync(Expression<Func<T, bool>> predicate);

Task AddAsync(T entity);

Task AddRangeAsync(IEnumerable<T> entities);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public partial class AionTimeSubscription : BaseEntity

public int OrganizationId { get; set; }

public virtual ICollection<AionTimeSubscriptionHistory>? AionTimeSubscriptionHistories { get; set; } //= new List<AionTimeSubscriptionHistory>();
public virtual ICollection<AionTimeSubscriptionHistory>? AionTimeSubscriptionHistories { get; set; } // = new List<AionTimeSubscriptionHistory>();

public virtual Organization? Organization { get; set; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ public partial class Organization : BaseEntity

public bool IsAionTimeApproved { get; set; }

public virtual ICollection<AionTimeSubscription>? AionTimeSubscriptions { get; set; } //= new List<AionTimeSubscription>();
public virtual ICollection<AionTimeSubscription>? AionTimeSubscriptions { get; set; }

public virtual ICollection<Project>? Projects { get; set; } //= new List<Project>();
public virtual ICollection<Project>? Projects { get; set; }

public virtual User? User { get; set; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,5 @@ public partial class Project : BaseEntity

public virtual Organization? Organization { get; set; }

public virtual ICollection<WorkItem>? WorkItems { get; set; } //= new List<WorkItem>();
public virtual ICollection<WorkItem>? WorkItems { get; set; } // = new List<WorkItem>();
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@ public partial class User : BaseEntity

public int Revision { get; set; }

public virtual ICollection<Organization>? Organizations { get; set; } //= new List<Organization>();
public virtual ICollection<Organization>? Organizations { get; set; } // = new List<Organization>();
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public partial class WorkItem : BaseEntity

public virtual Project? Project { get; set; }

public virtual ICollection<WorkItemHistory>? WorkItemHistories { get; set; } //= new List<WorkItemHistory>();
public virtual ICollection<WorkItemHistory>? WorkItemHistories { get; set; } // = new List<WorkItemHistory>();

public virtual ICollection<WorkItemTimeLog>? WorkItemTimeLogs { get; set; } //= new List<WorkItemTimeLog>();
public virtual ICollection<WorkItemTimeLog>? WorkItemTimeLogs { get; set; } // = new List<WorkItemTimeLog>();
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
namespace TimeLogService.Infrastructure.Repositories;
using System;
using System.Linq.Expressions;

namespace TimeLogService.Infrastructure.Repositories;

public class Repository<T>(TimeLogServiceDataBaseContext context)
: IRepository<T>
Expand Down Expand Up @@ -54,4 +57,14 @@ public async Task DeleteRangeAsync(IEnumerable<T> entities)
_context.RemoveRange(entities);
_ = await _context.SaveChangesAsync();
}

public async Task<IReadOnlyList<T>> GetManyAsync(Expression<Func<T, bool>> predicate)
{
return await _context.Set<T>().AsNoTracking().Where(predicate).ToListAsync();
}

public async Task<T?> GetSingleAsync(Expression<Func<T, bool>> predicate)
{
return await _context.Set<T>().AsNoTracking().Where(predicate).FirstOrDefaultAsync();
}
}

0 comments on commit e64f317

Please sign in to comment.