Exemple #1
0
        public async Task <IEnumerable <T> > FindLastDocumentsAsync <T>(Expression <Func <T, bool> > filter = null,
                                                                        string orderBy  = null,
                                                                        bool @ascending = true, int limit = 0) where T : DtoBase
        {
            try
            {
                var collection = this._mongoDatabase.GetCollection <T>(GetCollectionName <T>()).AsQueryable();

                collection = await Task.Run(() => filter != null
                                            ?collection.Where(filter)
                                            : collection);

                collection = await Task.Run(() => !string.IsNullOrWhiteSpace(orderBy)
                                            ?collection.OrderByField(orderBy, @ascending)
                                            : collection);

                collection = limit > 0
                    ? collection.Take(limit)
                    : collection;

                return(collection);
            }
            catch (Exception ex)
            {
                this._logger.LogError(CommonServices.GetDefaultErrorTrace(ex));
                throw;
            }
        }
Exemple #2
0
 public async Task UpdateAsync <T>(T dtoToUpdate) where T : DtoBase
 {
     try
     {
         var collection = this._mongoDatabase.GetCollection <T>(GetCollectionName <T>());
         await collection.ReplaceOneAsync(x => x.Id == dtoToUpdate.Id, dtoToUpdate);
     }
     catch (Exception ex)
     {
         this._logger.LogError(CommonServices.GetDefaultErrorTrace(ex));
         throw;
     }
 }
Exemple #3
0
 public async Task InsertAsync <T>(T dtoToInsert) where T : DtoBase
 {
     try
     {
         var collection = this._mongoDatabase.GetCollection <T>(GetCollectionName <T>());
         await collection.InsertOneAsync(dtoToInsert);
     }
     catch (Exception ex)
     {
         this._logger.LogError(CommonServices.GetDefaultErrorTrace(ex));
         throw;
     }
 }
Exemple #4
0
        public async Task DeleteAsync <T>(string id) where T : DtoBase
        {
            try
            {
                var collection = this._mongoDatabase.GetCollection <T>(GetCollectionName <T>());
                var filter     = Builders <T> .Filter.Eq("_id", id);

                await collection.FindOneAndDeleteAsync(filter);
            }
            catch (Exception ex)
            {
                this._logger.LogError(CommonServices.GetDefaultErrorTrace(ex));
                throw;
            }
        }
Exemple #5
0
        public async Task <IEnumerable <T> > FindAsync <T>(Expression <Func <T, bool> > filter = null) where T : DtoBase
        {
            try
            {
                var collection = this._mongoDatabase.GetCollection <T>(GetCollectionName <T>()).AsQueryable();

                return(await Task.Run(() => filter != null
                                      ?collection.Where(filter)
                                      : collection));
            }
            catch (Exception ex)
            {
                this._logger.LogError(CommonServices.GetDefaultErrorTrace(ex));
                throw;
            }
        }
Exemple #6
0
        public async Task <T> GetByIdAsync <T>(string id) where T : DtoBase
        {
            try
            {
                var collection = this._mongoDatabase.GetCollection <T>(GetCollectionName <T>()).AsQueryable();

                var results = collection.Where(t => t.Id.Equals(id));
                return(await results.AnyAsync()
                    ? results.First()
                    : null);
            }
            catch (Exception ex)
            {
                this._logger.LogError(CommonServices.GetDefaultErrorTrace(ex));
                throw;
            }
        }
        public async Task <IEnumerable <DeviceJson> > GetDevicesAsync()
        {
            try
            {
                var deviceDto = await this.Persister.FindAsync <Device>();

                var deviceArray = deviceDto as Device[] ?? deviceDto.ToArray();

                return(deviceArray.Any()
                    ? deviceArray.Select(dto => dto.ToJson())
                    : Enumerable.Empty <DeviceJson>());
            }
            catch (Exception ex)
            {
                this.Logger.LogError(CommonServices.GetDefaultErrorTrace(ex));
                throw;
            }
        }
        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));
            }
        }
        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 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));
            }
        }