public async Task Handle(ChangeDescription message) { var uploadData = await repository.Get(message.Id); uploadData.ChangeDescription(message.Description); await repository.Save(uploadData); }
/// <summary> /// Handles (reacts to) command. /// </summary> /// <param name="command">Command instance.</param> public void Handle(AddDrawing command) { var history = repository.GetById <History>(historyId); history.AddDrawing(command.Drawing); repository.Save(history); }
public AdminModule(IAggregateRootRepository repository, IIdGenerator idGenerator) : base("/write/admin") { this.RequiresAuthentication(); this.RequiresClaims(new[] { "Admin" }); Post["/account/create"] = parameters => { var model = this.Bind<EditableAccount>(); var account = new Domain.Account(idGenerator.NextGuid(), model.Name, model.FirstName, model.LastName, model.Email); var password = account.GeneratePassword(); repository.Save(account); return Json(new { Account = new Account(account), Password = password }); }; Post["/account/update/{id:guid}"] = parameters => { var model = this.Bind<EditableAccount>(); var account = repository.GetById<Domain.Account>((Guid)parameters.id); if (account != null) { account.ChangeDetails(model.Name, model.FirstName, model.LastName, model.Email); repository.Save(account); return Json(new { Account = new Account(account) }); } return null; }; Post["/account/{id:guid}/newpassword"] = parameters => { var model = this.Bind<AccountNewPassword>(); var account = repository.GetById<Domain.Account>((Guid)parameters.id); if (account != null) { account.ChangePassword(model.Password); repository.Save(account); } return null; }; }
public async Task Handle(MembersAssign message) { var group = await repository.Get(message.Id); UnnasignMembers(message, group); AssignMembers(message, group); await repository.Save(group); }
private async Task <PendingBasket> CreateBasketHandler(Command <CreateBasket> cmd) { var basket = new PendingBasket(cmd.Payload.AggregateRootId); await _repository.Save(basket, cmd.Metadata.CorrelationId, cmd.Id); await _bus.Dispatch(basket.UncommittedEvents); return(basket); }
private void ConsumeInternal(Guid aggregateId, Func <RoomTypes, IEnumerable <object> > action) { using (new Mutex(true, aggregateId.ToString())) { var rooms = _aggregateRootRepository.Get <RoomTypes>(aggregateId); var events = action(rooms).ToList(); _aggregateRootRepository.Save(rooms, events); _messageBus.Publish(events); } }
public async Task HandleAsync(AccountTransferedEvent @event) { var client = await _aggregateRootRepository.Get <Account>(@event.Id); client.Deposit(@event.Amount, @event.Description); await _aggregateRootRepository.Save(client); }
public SetupModule(IAggregateRootRepository repository, IAccountRepository accountRepository, IIdGenerator idGenerator) :base("/setup") { Get["/"] = _ => { if (accountRepository.Count() > 0) return HttpStatusCode.NotFound; return View["Index"]; }; Post["/"] = _ => { var model = this.Bind<CreateModel>(); if (accountRepository.Count() > 0) return HttpStatusCode.NotFound; var account = new Domain.Account(idGenerator.NextGuid(), model.Name, model.FirstName, model.LastName, model.Email); account.ChangePassword(model.Password); account.MakeAdmin(); repository.Save(account); return Response.AsRedirect("/"); }; }
public async Task Handle(CreateCategory message) { try { var item = await repository.Get(message.Id.ToString()); if (item != null) { throw new Exception($"Category with id: {message.Id} already exist"); } } catch (AggregateNotFoundException) { // That is fine that id not used } var category = CategorySource.Create(message.Id, message.ParentId, message.Name, message.Color, message.Visible); await repository.Save(category); }
public async Task HandleAsync(ProductSubscribedEvent @event) { var client = await _aggregateRootRepository.Get <AggregatesModel.ClientAggregate.Client>(@event.ClientId); #region product var product = await _aggregateRootRepository.Get <AggregatesModel.ProductAggregate.Product>(@event.ProductId); product.SubscribeClient(@event.ClientId, $"{client.FirstName} {client.LastName}", client.Email); await _aggregateRootRepository.Save(product); #endregion #region account var aggregate = new Account(IdentityGenerator.NewSequentialGuid(IdentityGeneratorType.SequentialAsString), @event.Number, @event.ClientId, @event.ProductId); aggregate.Activate(); await _aggregateRootRepository.Save(aggregate); #endregion }
protected async Task <CommandResult> SaveAndCommit(TAggregateRoot aggregate) { if (aggregate.Notification.HasErrors) { return(new CommandResult(aggregate.Id, aggregate.Notification.Errors)); } await _aggregateRootRepository.Save(aggregate); _unitOfWork.Commit(); return(new CommandResult(aggregate.Id)); }
public async Task Handle(CreateJobCommand message) { try { var item = await repository.Get(message.Id); if (item != null) { throw new Exception($"Operational task with id: {message.Id} already exist"); } } catch (AggregateNotFoundException) { // That is fine that id not used } var operationalTask = JobDomain.Create( message.Id, message.CategoryId, message.Title, message.JobTypeId, message.CreatorId, message.CreatedByRole, message.AddressList, message.RelationGroupList, message.ParentId); await repository.Save(operationalTask); }
public ClientModule(IAggregateRootRepository repository, IIdGenerator idGenerator) :base("/write/client") { this.RequiresAuthentication(); Post["/create"] = parameters => { var model = this.Bind<EditableClient>(); var client = new Domain.Client(idGenerator.NextGuid(), model.Name); repository.Save(client); return Json(new { Client = new Client(client) }); }; Post["/update/{id:guid}"] = parameters => { var model = this.Bind<EditableClient>(); var client = repository.GetById<Domain.Client>((Guid)parameters.id); if (client != null) { client.ChangeDetails(model.Name); repository.Save(client); return Json(new { Client = new Client(client) }); } return null; }; }
public async Task Commit(ISession session, ICommand command) { using (var connection = await _dataStoreConnectionFactory.Connect()) { using (var transaction = connection.BeginTransaction(TransactionIsolationLevel)) { try { using (await _databaseScopeMutex.LockAsync()) { _activeDatabaseScopes[session.Key] = new DatabaseScope(connection, transaction); } foreach (var descriptor in session.TrackedAggregates) { if (descriptor.IsMarkedForDeletion) { await _repository.Delete(descriptor.Aggregate.Id); await _eventPublisher.Publish(new AggregateDeletedEvent(descriptor.Aggregate.Id, command.Id, command.UserId)); } else { await _repository.Save(descriptor.Aggregate, command.UserId, session.Key); } } await _sessionTracker.CommitSession(command); transaction.Commit(); } catch (Exception e) // Todo: For testing - remove later { transaction.Rollback(); await _sessionTracker.RollbackSession(session.Key); await _eventPublisher.Publish(new SessionRolledBackEvent(command.Id, command.UserId)); throw; } finally { await Cleanup(session); } } } await _eventPublisher.Publish(new SessionCommittedEvent(command.Id, command.UserId)); }
private async Task <Product> CreateProductHandler(Command <CreateProduct> cmd) { var product = new Product( cmd.Payload.AggregateRootId, cmd.Payload.Name, new Price(cmd.Payload.Price) ); await _repository.Save(product, cmd.Metadata.CorrelationId, cmd.Id); await _bus.Dispatch(product.UncommittedEvents); return(product); }
public async Task Handle(CreateOperationalTaskCommand message) { try { var item = await repository.Get(message.Id.ToString()); if (item != null) { throw new Exception($"Operational task with id: {message.Id} already exist"); } } catch (AggregateNotFoundException) { // That is fine that id not used } var operationalTask = OperationalTask.Create(message.Id, message.Year, message.Week, message.CategoryId, message.DepartmentId, message.Title); operationalTask.SaveDaysPerWeek(message.DaysPerWeek); operationalTask.ChangeAssignedEmployees(message.GroupId, message.AssignedEmployees); await repository.Save(operationalTask); }
public async Task Handle(CreateDayAssignCommand message) { try { var item = await repository.Get(message.Id); if (item != null) { throw new Exception($"DayAssign with id: {message.Id} already exist"); } } catch (AggregateNotFoundException) { // That is fine that id not used } var time = GetTime(message.Date); var date = message.Date ?? GetDate(message.WeekDay, message.WeekNumber); var year = message.Date?.Year ?? DateTime.UtcNow.Year; var dayAssign = DayAssignDomain.Create( message.Id, message.JobId, message.JobAssignId, message.DepartmentId, message.GroupId, message.UserIdList, date, time, message.EstimatedMinutes, message.WeekNumber, message.Address, message.TeamLeadId, message.WeekDay, message.DayPerWeekId, message.IsAssignedToAllUsers, message.Comment, message.ResidentName, message.ResidentPhone, message.Type, message.ExpiredDayAssignId, message.ExpiredWeekNumber, year, message.IsUrgent); await repository.Save(dayAssign, message.CreatorId); }
public ProjectModule(IAggregateRootRepository repository, IIdGenerator idGenerator) :base("/write/project") { this.RequiresAuthentication(); Post["/create"] = parameters => { var model = this.Bind<EditableProject>(); var client = repository.GetById<Domain.Client>(model.ClientId); var project = new Domain.Project(idGenerator.NextGuid(), model.Name, model.Description, client); repository.Save(project); return Json(new { Project = new Project(client, project) }); }; Post["/update/{id:guid}"] = parameters => { var model = this.Bind<EditableProject>(); var project = repository.GetById<Domain.Project>((Guid)parameters.id); if (project != null) { project.ChangeDetails(model.Name, model.Description); if (model.Hidden) project.Hide(); else project.Unhide(); repository.Save(project); var client = repository.GetById<Domain.Client>(project.ClientId); return Json(new { Project = new Project(client, project) }); } return null; }; Post["/updatetasks/{id:guid}"] = parameters => { var model = this.Bind<EditableProjectTasks>(); var project = repository.GetById<Domain.Project>((Guid)parameters.id); if (project != null) { project.ChangeTasks(model.Tasks.Select(t => new Domain.ValueObjects.Task { Name = t.Name, Rate = t.Rate }).ToArray()); repository.Save(project); var client = repository.GetById<Domain.Client>(project.ClientId); return Json(new { Project = new Project(client, project) }); } return null; }; }
public async Task Handle(CreateJobAssignCommand message) { try { var item = await repository.Get(message.Id.ToString()); if (item != null) { throw new Exception($"JobAssign with id: {message.Id} already exist"); } } catch (AggregateNotFoundException) { // That is fine that id not used } var jobAssign = JobAssignDomain.Create(message.Id, new List <string> { message.JobId }, message.CreatedByRole, message.TillYear); await repository.Save(jobAssign); }
public TimeRegistrationModule(IAggregateRootRepository repository, IIdGenerator idGenerator) : base("/write/timeregistration") { this.RequiresAuthentication(); Post["/create"] = parameters => { var model = this.Bind<EditableTimeRegistration>(); var client = repository.GetById<Domain.Client>(model.ClientId); var project = repository.GetById<Domain.Project>(model.ProjectId); var task = project.FindTask(model.Task); var date = Date.Parse(model.Date); var from = Time.Parse(model.From); var to = Time.Parse(model.To); var timeRegistration = new Domain.TimeRegistration(idGenerator.NextGuid(), client, project, task, model.Description, date, from, to); if (model.CorrectedIncome.HasValue) timeRegistration.CorrectIncome(model.CorrectedIncome.Value, model.CorrectedIncomeMessage); repository.Save(timeRegistration); return Json(new { TimeRegistration = new TimeRegistration(timeRegistration, client, project) }); }; Post["/update/{id:guid}"] = parameters => { var model = this.Bind<EditableTimeRegistration>(); var timeRegistration = repository.GetById<Domain.TimeRegistration>((Guid)parameters.id); if (timeRegistration != null) { var client = repository.GetById<Domain.Client>(model.ClientId); var project = repository.GetById<Domain.Project>(model.ProjectId); var task = project.FindTask(model.Task); var date = Date.Parse(model.Date); var from = Time.Parse(model.From); var to = Time.Parse(model.To); timeRegistration.ChangeDetails(client, project, task, model.Description, date, from, to); if (model.CorrectedIncome.HasValue) timeRegistration.CorrectIncome(model.CorrectedIncome.Value, model.CorrectedIncomeMessage); else timeRegistration.ClearCorrectedIncome(); if (model.RefreshRate) timeRegistration.RefreshRate(task); repository.Save(timeRegistration); return Json(new { TimeRegistration = new TimeRegistration(timeRegistration, client, project) }); } return null; }; Post["/delete/{id:guid}"] = parameters => { var timeRegistration = repository.GetById<Domain.TimeRegistration>((Guid)parameters.id); if (timeRegistration != null) { timeRegistration.Delete(); repository.Save(timeRegistration); } return null; }; }