public async Task SavePositionAsync(EventPosition commitPosition)
        {
            try
            {
                var filter          = Builders <NoSqlPosition> .Filter.Empty;
                var documentsResult = await this._documentUnitOfWork.NoSqlPositionRepository.FindAsync(filter);

                if (!documentsResult.Any())
                {
                    var noSqlDocument = NoSqlPosition.CreateNoSqlPosition(commitPosition.CommitPosition);
                    await this._documentUnitOfWork.NoSqlPositionRepository.InsertOneAsync(noSqlDocument);
                }
                else
                {
                    var noSqlDocument = documentsResult.First();
                    var updateCommand = noSqlDocument.UpdatePosition(commitPosition.CommitPosition);
                    var filterUpdate  = Builders <NoSqlPosition> .Filter.Eq("_id", noSqlDocument.Id);

                    await this._documentUnitOfWork.NoSqlPositionRepository.UpdateOneAsync(filterUpdate, updateCommand);
                }
            }
            catch (Exception ex)
            {
                throw new Exception(CommonServices.GetErrorMessage(ex));
            }
        }
示例#2
0
 public async Task <ClienteJson> GetClienteDetailsById(string clienteId)
 {
     try
     {
         return(await this._clienteOrchestrator.GetClienteDetailsByIdAsync(clienteId));
     }
     catch (Exception ex)
     {
         this._logger.LogError($"[ClientiController.CreateCliente] - {CommonServices.GetErrorMessage(ex)}");
         throw new Exception($"[ClientiController.CreateCliente] - {CommonServices.GetErrorMessage(ex)}");
     }
 }
示例#3
0
 public async Task <IEnumerable <ArticoloJson> > GetArticoli()
 {
     try
     {
         return(await this._articoloOrchestrator.GetArticoliAsync());
     }
     catch (Exception ex)
     {
         this._logger.LogError($"[ArticoliController.GetArticoli] - {CommonServices.GetErrorMessage(ex)}");
         throw new Exception($"[ArticoliController.GetArticoli] - {CommonServices.GetErrorMessage(ex)}");
     }
 }
示例#4
0
 public async Task <ArticoloJson> GetArticoloDetailsById(string articoloId)
 {
     try
     {
         return(await this._articoloOrchestrator.GetArticoloByIdAsync(articoloId));
     }
     catch (Exception ex)
     {
         this._logger.LogError($"[ArticoliController.GetArticoloDetailsById] - {CommonServices.GetErrorMessage(ex)}");
         throw new Exception($"[ArticoliController.GetArticoloDetailsById] - {CommonServices.GetErrorMessage(ex)}");
     }
 }
示例#5
0
 public async Task <IEnumerable <ClienteJson> > GetClienti()
 {
     try
     {
         return(await this._clienteOrchestrator.GetClientiAsync());
     }
     catch (Exception ex)
     {
         this._logger.LogError($"[ClientiController.CreateCliente] - {CommonServices.GetErrorMessage(ex)}");
         throw new Exception($"[ClientiController.CreateCliente] - {CommonServices.GetErrorMessage(ex)}");
     }
 }
        public async Task AppendToStreamAsync(IAggregate aggregate, long expectedVersion, IEnumerable <EventData> events)
        {
            try
            {
                var filter = Builders <NoSqlEventData> .Filter.Eq("_id", aggregate.Id.ToString());

                var documentsResult = await this._documentUnitOfWork.NoSqlEventDataRepository.FindAsync(filter);

                if (!documentsResult.Any())
                {
                    var noSqlDocument = NoSqlEventData.CreateNoSqlEventData(aggregate);
                    await this._documentUnitOfWork.NoSqlEventDataRepository.InsertOneAsync(noSqlDocument);

                    for (var i = 0; i <= 5; i++)
                    {
                        documentsResult = await this._documentUnitOfWork.NoSqlEventDataRepository.FindAsync(filter);

                        if (!documentsResult.Any())
                        {
                            break;
                        }

                        Thread.Sleep(100);
                    }
                }

                if (!documentsResult.Any())
                {
                    return;
                }

                var noSqlAggregate = documentsResult.First();
                var eventDatas     = events as EventData[] ?? events.ToArray();
                foreach (var eventData in eventDatas)
                {
                    var commitPosition = await this.GetLastPositionAsync();

                    commitPosition.IncrementCommitPosition();
                    await this.SavePositionAsync(commitPosition);

                    noSqlAggregate.AppendEvent(eventData, commitPosition);
                }

                await this._documentUnitOfWork.NoSqlEventDataRepository.ReplaceOneAsync(filter, noSqlAggregate);
            }
            catch (Exception ex)
            {
                throw new Exception(CommonServices.GetErrorMessage(ex));
            }
        }
示例#7
0
        public async Task <IActionResult> CreateArticolo([FromBody] ArticoloJson articoloToCreate)
        {
            try
            {
                await this._articoloOrchestrator.CreateArticoloAsync(articoloToCreate, this.CommandInfo.Who, this.CommandInfo.When);

                return(this.Created("articoli", new PostResult("/", "")));
            }
            catch (Exception ex)
            {
                this._logger.LogError($"[ArticoliController.CreateArticolo] - {CommonServices.GetErrorMessage(ex)}");
                return(this.BadRequest($"[ArticoliController.CreateArticolo] - {CommonServices.GetErrorMessage(ex)}"));
            }
        }
示例#8
0
        public async Task <IActionResult> CreateCliente([FromBody] ClienteJson cliente)
        {
            try
            {
                await this._clienteOrchestrator.CreateClienteAsync(cliente, this.CommandInfo.Who, this.CommandInfo.When);

                var clienteUri = $"{GetUri(this.Request)}";
                return(this.Created("clienti", new PostResult(clienteUri, "")));
            }
            catch (Exception ex)
            {
                this._logger.LogError($"[ClientiController.CreateCliente] - {CommonServices.GetErrorMessage(ex)}");
                return(this.BadRequest($"[ClientiController.CreateCliente] - {CommonServices.GetErrorMessage(ex)}"));
            }
        }
示例#9
0
        public async Task <IActionResult> ModificaDescrizione([FromBody] ArticoloJson articolo)
        {
            try
            {
                await this._articoloOrchestrator.ModificaDescrizioneArticoloAsync(articolo, this.CommandInfo.Who,
                                                                                  this.CommandInfo.When);

                return(this.NoContent());
            }
            catch (Exception ex)
            {
                this._logger.LogError($"[ArticoliController.GetArticoloDetailsById] - {CommonServices.GetErrorMessage(ex)}");
                throw new Exception($"[ArticoliController.GetArticoloDetailsById] - {CommonServices.GetErrorMessage(ex)}");
            }
        }
        public async Task Cannot_CreateOrdineCliente_Without_ClienteId()
        {
            var ordineClienteOrchestrator = this._container.Resolve <IOrdineClienteOrchestrator>();
            var ordineCliente             = new OrdineClienteJson
            {
                ClienteId            = string.Empty,
                DataInserimento      = DateTime.UtcNow,
                DataPrevistaConsegna = DateTime.UtcNow.AddMonths(3)
            };

            Exception ex = await Assert.ThrowsAnyAsync <Exception>(() =>
                                                                   ordineClienteOrchestrator.CreateOrdineClienteAsync(ordineCliente, this._who, this._when));

            Assert.Equal(DomainExceptions.ClienteIdNullException, CommonServices.GetErrorMessage(ex));
        }
示例#11
0
        public async Task Cannot_CreateArticolo_Without_ArticoloDescrizione()
        {
            var articoloOrchestrator = this._container.Resolve <IArticoloOrchestrator>();
            var articoloJson         = new ArticoloJson
            {
                ArticoloId          = string.Empty,
                ArticoloDescrizione = string.Empty,
                UnitaMisura         = "NR",
                ScortaMinima        = 1
            };

            Exception ex = await Assert.ThrowsAnyAsync <Exception>(() =>
                                                                   articoloOrchestrator.CreateArticoloAsync(articoloJson, this._who, this._when));

            Assert.Equal(DomainExceptions.ArticoloDescrizioneNullException, CommonServices.GetErrorMessage(ex));
        }
        public void ModificaArticoloHandler()
        {
            var descrizioneModificataEventHandler =
                this._container.Resolve <IHandleRequests <DescrizioneArticoloModificata> >();

            var articoloId  = new ArticoloId(Guid.NewGuid().ToString());
            var descrizione = new ArticoloDescrizione("Nuova Descrizione");

            var descrizioneArticoloModificata =
                new DescrizioneArticoloModificata(articoloId, descrizione, this._who, this._when);

            Exception ex = Assert.Throws <Exception>(() =>
                                                     descrizioneModificataEventHandler.Handle(descrizioneArticoloModificata));

            Assert.Equal($"Articolo {articoloId.GetValue()} Non Trovato!", CommonServices.GetErrorMessage(ex));
        }
示例#13
0
        public async Task Cannot_CreateCliente_Without_ClienteRagioneSociale()
        {
            var clienteOrchestrator = this._container.Resolve <IClienteOrchestrator>();
            var clienteJson         = new ClienteJson
            {
                ClienteId      = string.Empty,
                RagioneSociale = string.Empty,
                PartitaIva     = "03466990177",
                CodiceFiscale  = "03466990177"
            };

            Exception ex = await Assert.ThrowsAnyAsync <Exception>(() =>
                                                                   clienteOrchestrator.CreateClienteAsync(clienteJson, this._who, this._when));

            Assert.Equal(DomainExceptions.RagioneSocialeNullException, CommonServices.GetErrorMessage(ex));
        }
        public async Task <EventPosition> GetLastPositionAsync()
        {
            try
            {
                var filter          = Builders <NoSqlPosition> .Filter.Empty;
                var documentsResult = await this._documentUnitOfWork.NoSqlPositionRepository.FindAsync(filter);

                return(documentsResult.Any()
                    ? new EventPosition(documentsResult.First().CommitPosition)
                    : new EventPosition(0));
            }
            catch (Exception ex)
            {
                throw new Exception(CommonServices.GetErrorMessage(ex));
            }
        }
        public async Task <IEnumerable <ArticoloJson> > GetArticoliAsync()
        {
            try
            {
                var filter          = Builders <NoSqlArticolo> .Filter.Empty;
                var documentsResult = await this._documentUnitOfWork.NoSqlArticoloRepository.FindAsync(filter);

                return(documentsResult.Any()
                    ? documentsResult.Select(noSqlDocument => noSqlDocument.ToJson())
                    : Enumerable.Empty <ArticoloJson>());
            }
            catch (Exception ex)
            {
                this._logger.LogError($"[ArticoloFactory.GetArticoliAsync] - {CommonServices.GetErrorMessage(ex)}");
                throw new Exception($"[ArticoloFactory.GetArticoliAsync] - {CommonServices.GetErrorMessage(ex)}");
            }
        }
示例#16
0
        public async Task <ClienteJson> GetClienteDetailsByIdAsync(ClienteId clienteId)
        {
            try
            {
                var filter = Builders <NoSqlCliente> .Filter.Eq("_id", clienteId.GetValue());

                var documentResults = await this._documentUnitOfWork.NoSqlClienteRepository.FindAsync(filter);

                return(documentResults.Any()
                    ? documentResults.First().ToJson()
                    : new ClienteJson());
            }
            catch (Exception ex)
            {
                this._logger.LogError($"[ClienteFactory.GetClienteDetailsByIdAsync] - {CommonServices.GetErrorMessage(ex)}");
                throw new Exception($"[ClienteFactory.GetClienteDetailsByIdAsync] - {CommonServices.GetErrorMessage(ex)}");
            }
        }
        public async Task <ArticoloJson> GetArticoloByIdAsync(ArticoloId articoloId)
        {
            try
            {
                var filter = Builders <NoSqlArticolo> .Filter.Eq("_id", articoloId.GetValue());

                var documentsResult = await this._documentUnitOfWork.NoSqlArticoloRepository.FindAsync(filter);

                return(documentsResult.Any()
                    ? documentsResult.First().ToJson()
                    : new ArticoloJson());
            }
            catch (Exception ex)
            {
                this._logger.LogError($"[ArticoloFactory.GetArticoloByIdAsync] - {CommonServices.GetErrorMessage(ex)}");
                throw new Exception($"[ArticoloFactory.GetArticoloByIdAsync] - {CommonServices.GetErrorMessage(ex)}");
            }
        }
        public async Task <IEnumerable <ThermometerEventStoreJson> > GetEventsNotDispatchedAsync <T>() where T : EventStoreBase
        {
            try
            {
                var streamEvents =
                    await this.Persister.FindAsync <T>(s => !s.IsDispatched);

                var eventsArray = streamEvents as T[] ?? streamEvents.ToArray();
                return(eventsArray.Any()
                    ? eventsArray.Select(dto => dto.ToJson())
                    : Enumerable.Empty <ThermometerEventStoreJson>());
            }
            catch (Exception ex)
            {
                this.Logger.LogError(CommonServices.GetDefaultErrorTrace(ex));
                throw new Exception(CommonServices.GetErrorMessage(ex));
            }
        }
示例#19
0
        /// <summary>
        /// Deserializes the event from the raw GetEventStore event to my event.
        /// Took this from a gist that James Nugent posted on the GetEventStore forums.
        /// </summary>
        /// <param name="metadata"></param>
        /// <param name="data"></param>
        /// <returns></returns>
        private static dynamic DeserializeEvent(byte[] metadata, byte[] data)
        {
            if (JObject.Parse(Encoding.UTF8.GetString(metadata)).Property(EventClrTypeHeader) == null)
            {
                return(null);
            }

            var eventClrTypeName = JObject.Parse(Encoding.UTF8.GetString(metadata)).Property(EventClrTypeHeader).Value;

            try
            {
                return(JsonConvert.DeserializeObject(Encoding.UTF8.GetString(data), Type.GetType((string)eventClrTypeName)));
            }
            catch (Exception ex)
            {
                Console.WriteLine($"[DeserializeEvent] - {CommonServices.GetErrorMessage(ex)}");
                return(null);
            }
        }
        public async Task SetEventToDispatched <T>(EventId eventId) where T : EventStoreBase
        {
            try
            {
                var streamEvent = await this.Persister.GetByIdAsync <T>(eventId.GetValue());

                if (streamEvent == null || string.IsNullOrWhiteSpace(streamEvent.Id))
                {
                    return;
                }

                streamEvent.SetEventDispatched();
                await this.Persister.UpdateAsync(streamEvent);
            }
            catch (Exception ex)
            {
                this.Logger.LogError(CommonServices.GetDefaultErrorTrace(ex));
                throw new Exception(CommonServices.GetErrorMessage(ex));
            }
        }
        public async Task CreateArticoloAsync(ArticoloId articoloId, ArticoloDescrizione articoloDescrizione)
        {
            try
            {
                var filter = Builders <NoSqlArticolo> .Filter.Eq("_id", articoloId.GetValue());

                var documentsResult = await this._documentUnitOfWork.NoSqlArticoloRepository.FindAsync(filter);

                if (documentsResult.Any())
                {
                    return;
                }

                var noSqlDocument = NoSqlArticolo.CreateNoSqlArticolo(articoloId, articoloDescrizione);
                await this._documentUnitOfWork.NoSqlArticoloRepository.InsertOneAsync(noSqlDocument);
            }
            catch (Exception ex)
            {
                this._logger.LogError($"[ArticoloFactory.CreateArticoloAsync] - {CommonServices.GetErrorMessage(ex)}");
                throw new Exception($"[ArticoloFactory.CreateArticoloAsync] - {CommonServices.GetErrorMessage(ex)}");
            }
        }
示例#22
0
        public async Task CreateClienteAsync(ClienteId clienteId, RagioneSociale ragioneSociale)
        {
            try
            {
                var filter = Builders <NoSqlCliente> .Filter.Eq("_id", clienteId.GetValue());

                var documentsResult = await this._documentUnitOfWork.NoSqlClienteRepository.FindAsync(filter);

                if (documentsResult.Any())
                {
                    return;
                }

                var noSqlDocument = NoSqlCliente.CreateNoSqlCliente(clienteId, ragioneSociale);
                await this._documentUnitOfWork.NoSqlClienteRepository.InsertOneAsync(noSqlDocument);
            }
            catch (Exception ex)
            {
                this._logger.LogError($"[ClienteFactory.CreateClienteAsync] - {CommonServices.GetErrorMessage(ex)}");
                throw new Exception($"[ClienteFactory.CreateClienteAsync] - {CommonServices.GetErrorMessage(ex)}");
            }
        }
        public async Task AppendEventAsync <T>(EventId eventId, StreamType streamType, StreamData streamData,
                                               DeviceId aggregateId, DeviceName aggregateName, StreamWhen streamWhen) where T : EventStoreBase
        {
            try
            {
                var streamEvent =
                    await this.Persister.GetByIdAsync <T>(eventId.GetValue());

                if (streamEvent != null && !string.IsNullOrEmpty(streamEvent.Id))
                {
                    return;
                }

                streamEvent = ConstructAggregate <T>(eventId, streamType, streamData, aggregateId, aggregateName, streamWhen);
                await this.Persister.InsertAsync(streamEvent);
            }
            catch (Exception ex)
            {
                this.Logger.LogError(CommonServices.GetDefaultErrorTrace(ex));
                throw new Exception(CommonServices.GetErrorMessage(ex));
            }
        }
        public async Task <IEnumerable <EventData> > ReadStreamEventsForwardAsync(Guid aggregateId, long startPosition, int eventsCount)
        {
            try
            {
                var filter = Builders <NoSqlEventData> .Filter.Eq("_id", aggregateId.ToString());

                var documentsResult = await this._documentUnitOfWork.NoSqlEventDataRepository.FindAsync(filter);

                if (!documentsResult.Any())
                {
                    return(Enumerable.Empty <EventData>());
                }

                var noSqlEventData = documentsResult.First();

                return(noSqlEventData.EventStream.Any()
                    ? noSqlEventData.EventStream.Select(noSqlEvent => noSqlEvent.ToEventData())
                    : Enumerable.Empty <EventData>());
            }
            catch (Exception ex)
            {
                throw new Exception(CommonServices.GetErrorMessage(ex));
            }
        }
示例#25
0
        public async Task CreateOrdineClienteAsync(OrdineClienteId ordineClienteId, ClienteId clienteId, RagioneSociale ragioneSociale,
                                                   DataInserimento dataInserimento, DataPrevistaConsegna dataPrevistaConsegna)
        {
            try
            {
                var filter = Builders <NoSqlOrdineCliente> .Filter.Eq("_id", ordineClienteId.GetValue());

                var documentsResult = await this._documentUnitOfWork.NoSqlOrdineClienteRepository.FindAsync(filter);

                if (!documentsResult.Any())
                {
                    return;
                }

                var noSqlDocument = NoSqlOrdineCliente.CreateNoSqlOrdineCliente(ordineClienteId, clienteId,
                                                                                ragioneSociale, dataInserimento, dataPrevistaConsegna);
                await this._documentUnitOfWork.NoSqlOrdineClienteRepository.InsertOneAsync(noSqlDocument);
            }
            catch (Exception ex)
            {
                this._logger.LogError($"[OrdineClienteFactory.CreateOrdineClienteAsync] - {CommonServices.GetErrorMessage(ex)}");
                throw new Exception($"[OrdineClienteFactory.CreateOrdineClienteAsync] - {CommonServices.GetErrorMessage(ex)}");
            }
        }