public async Task Handle(object command) { switch (command) { case Commands.V1.Create cmd: if (await _repository.Exists(cmd.Id.ToString())) { throw new InvalidOperationException($"Entity with id {cmd.Id} already exists"); } var classifiedAd = new Domain.ClassifiedAd.ClassifiedAd( new ClassifiedAdId(cmd.Id), new UserId(cmd.OwnerId)); await _repository.Add(classifiedAd); await _unitOfWork.Commit(); break; case Commands.V1.SetTitle cmd: await HandleUpdate(cmd.Id, c => c.SetTitle(ClassifiedAdTitle.FromString(cmd.Title))); break; case Commands.V1.UpdateText cmd: await HandleUpdate(cmd.Id, c => c.UpdateText(ClassifiedAdText.FromString(cmd.Text))); break; case Commands.V1.UpdatePrice cmd: await HandleUpdate(cmd.Id, c => c.UpdatePrice(Price.FromDecimal(cmd.Price, cmd.Currency, _currencyLookup))); break; case Commands.V1.RequestToPublish cmd: await HandleUpdate(cmd.Id, c => c.RequestToPublish()); break; case Commands.V1.Publish cmd: await HandleUpdate(cmd.Id, c => c.Publish(new UserId(cmd.ApprovedBy))); break; default: throw new InvalidOperationException( $"Command type {command.GetType().FullName} is unknown"); } }
private async Task HandleCreate(Contracts.V1.Create cmd) { if (await _repository.Exists(new ClassifiedAdId(cmd.Id))) { throw new InvalidOperationException($"Entity with id {cmd.Id} already exists"); } var classifiedAd = new Domain.ClassifiedAd.ClassifiedAd(new ClassifiedAdId(cmd.Id), new UserId(cmd.OwnerId)); await _repository.Add(classifiedAd); await _unitOfWork.Commit(); }