<commit_before>﻿using System;
using System.Collections.Generic;
using Ledger;
using Ledger.Infrastructure;
using Magistrate.Domain.Events;
using Magistrate.Domain.Events.PermissionEvents;
using Magistrate.Domain.Events.RoleEvents;
using Magistrate.Domain.Events.UserEvents;
using Magistrate.Domain.Services;

namespace Magistrate.Domain.ReadModels
{
	public class ReadModelProjections
	{
		public IEnumerable<UserReadModel> Users => _users.Values;
		public IEnumerable<RoleReadModel> Roles => _roles.Values;
		public IEnumerable<PermissionReadModel> Permissions => _permissions.Values;
		public IEnumerable<HistoryReadModel> History => _history;

		private readonly Dictionary<Guid, UserReadModel> _users;
		private readonly Dictionary<Guid, RoleReadModel> _roles;
		private readonly Dictionary<Guid, PermissionReadModel> _permissions;
		private readonly List<HistoryReadModel> _history;

		private readonly Projector _projector;

		public ReadModelProjections()
		{
			_projector = new Projector();

			_users = new Dictionary<Guid, UserReadModel>();
			_roles = new Dictionary<Guid, RoleReadModel>();
			_permissions = new Dictionary<Guid, PermissionReadModel>();
			_history = new List<HistoryReadModel>();

			RegisterProjections(_projector);
		}

		public void Project(IDomainEvent<Guid> @event)
		{
			_projector.Project(@event);
		}

		private void RegisterProjections(Projector projector)
		{
			projector.Add<UserLoggedEvent>(e => _history.Add(HistoryReadModel.From(e)));

			projector.Add<PermissionCreatedEvent>(e => _permissions[e.AggregateID] = PermissionReadModel.From(e));
			projector.Add<PermissionDescriptionChangedEvent>(e => _permissions[e.AggregateID].Description = e.NewDescription);
			projector.Add<PermissionNameChangedEvent>(e => _permissions[e.AggregateID].Name = e.NewName);

			projector.Add<RoleCreatedEvent>(e => _roles[e.AggregateID] = RoleReadModel.From(e));
			projector.Add<RoleDescriptionChangedEvent>(e => _roles[e.AggregateID].Description = e.NewDescription);
			projector.Add<RoleNameChangedEvent>(e => _roles[e.AggregateID].Name = e.NewName);
			projector.Add<PermissionAddedToRoleEvent>(e => _roles[e.AggregateID].Permissions.Add(_permissions[e.PermissionID]));
			projector.Add<PermissionRemovedFromRoleEvent>(e => _roles[e.AggregateID].Permissions.Remove(_permissions[e.PermissionID]));

			projector.Add<UserCreatedEvent>(e => _users[e.AggregateID] = UserReadModel.From(e));
			projector.Add<UserNameChangedEvent>(e => _users[e.AggregateID].Name = e.NewName);
			projector.Add<IncludeAddedToUserEvent>(e =>
			{
				var permission = _permissions[e.PermissionID];
				var user = _users[e.AggregateID];

				user.Includes.Add(permission);
				user.Revokes.Remove(permission);
			});

			projector.Add<IncludeRemovedFromUserEvent>(e => _users[e.AggregateID].Includes.Remove(_permissions[e.PermissionID]));
			projector.Add<RevokeAddedToUserEvent>(e =>
			{
				var permission = _permissions[e.PermissionID];
				var user = _users[e.AggregateID];

				user.Includes.Remove(permission);
				user.Revokes.Add(permission);
			});

			projector.Add<RevokeRemovedFromUserEvent>(e => _users[e.AggregateID].Revokes.Remove(_permissions[e.PermissionID]));<commit_msg>readModelProjections: simplify projections based on new event usage<commit_after>﻿using System;
using System.Collections.Generic;
using Ledger;
using Ledger.Infrastructure;
using Magistrate.Domain.Events;
using Magistrate.Domain.Events.PermissionEvents;
using Magistrate.Domain.Events.RoleEvents;
using Magistrate.Domain.Events.UserEvents;
using Magistrate.Domain.Services;

namespace Magistrate.Domain.ReadModels
{
	public class ReadModelProjections
	{
		public IEnumerable<UserReadModel> Users => _users.Values;
		public IEnumerable<RoleReadModel> Roles => _roles.Values;
		public IEnumerable<PermissionReadModel> Permissions => _permissions.Values;
		public IEnumerable<HistoryReadModel> History => _history;

		private readonly Dictionary<Guid, UserReadModel> _users;
		private readonly Dictionary<Guid, RoleReadModel> _roles;
		private readonly Dictionary<Guid, PermissionReadModel> _permissions;
		private readonly List<HistoryReadModel> _history;

		private readonly Projector _projector;

		public ReadModelProjections()
		{
			_projector = new Projector();

			_users = new Dictionary<Guid, UserReadModel>();
			_roles = new Dictionary<Guid, RoleReadModel>();
			_permissions = new Dictionary<Guid, PermissionReadModel>();
			_history = new List<HistoryReadModel>();

			RegisterProjections(_projector);
		}

		public void Project(IDomainEvent<Guid> @event)
		{
			_projector.Project(@event);
		}

		private void RegisterProjections(Projector projector)
		{
			projector.Add<UserLoggedEvent>(e => _history.Add(HistoryReadModel.From(e)));

			projector.Add<PermissionCreatedEvent>(e => _permissions[e.AggregateID] = PermissionReadModel.From(e));
			projector.Add<PermissionDescriptionChangedEvent>(e => _permissions[e.AggregateID].Description = e.NewDescription);
			projector.Add<PermissionNameChangedEvent>(e => _permissions[e.AggregateID].Name = e.NewName);

			projector.Add<RoleCreatedEvent>(e => _roles[e.AggregateID] = RoleReadModel.From(e));
			projector.Add<RoleDescriptionChangedEvent>(e => _roles[e.AggregateID].Description = e.NewDescription);
			projector.Add<RoleNameChangedEvent>(e => _roles[e.AggregateID].Name = e.NewName);
			projector.Add<PermissionAddedToRoleEvent>(e => _roles[e.AggregateID].Permissions.Add(_permissions[e.PermissionID]));
			projector.Add<PermissionRemovedFromRoleEvent>(e => _roles[e.AggregateID].Permissions.Remove(_permissions[e.PermissionID]));

			projector.Add<UserCreatedEvent>(e => _users[e.AggregateID] = UserReadModel.From(e));
			projector.Add<UserNameChangedEvent>(e => _users[e.AggregateID].Name = e.NewName);
			projector.Add<IncludeAddedToUserEvent>(e => _users[e.AggregateID].Includes.Add(_permissions[e.PermissionID]));
			projector.Add<IncludeRemovedFromUserEvent>(e => _users[e.AggregateID].Includes.Remove(_permissions[e.PermissionID]));

			projector.Add<RevokeAddedToUserEvent>(e => _users[e.AggregateID].Revokes.Add(_permissions[e.PermissionID]));
			projector.Add<RevokeRemovedFromUserEvent>(e => _users[e.AggregateID].Revokes.Remove(_permissions[e.PermissionID]));
{'fraction_non_alphanumeric': 0.12014757779916586, 'fraction_numerical': 0.0, 'mean_word_length': 6.6691266912669125, 'pattern_counts': {'":': 0, '<': 59, '<?xml version=': 0, '>': 97, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7584454', 'n_tokens_mistral': 2064, 'n_tokens_neox': 1877, 'n_words': 280}