// TODO: Figure out how to map filters from domain to EF
        //public DataResult<List<Organization>> GetAllWhereAsync(
        //        Expression<Func<EfOrganization, bool>> filterFunc)
        //{
        //    try
        //    {
        //        using (var dbContext = new ClassTrackerDbContext())
        //        {
        //            var entities = dbContext
        //                    .Organizations
        //                    .Where(filterFunc);
        //            var domains = entities.Select(x => MapEntityToDomain(x));
        //            return DataResult<List<Organization>>.CreateSuccessResult(domains.ToList());
        //        }
        //    }
        //    catch (Exception ex)
        //    {
        //        return DataResult<List<Organization>>.CreateErrorResult(
        //            ErrorCode.ExceptionThrown, ex, null);
        //    }
        //}

        public DataResult <int?> CreateAsync(Organization domain)
        {
            try
            {
                var entity = _mapDomainToEntity(domain);
                using (var dbContext = new ClassTrackerDbContext())
                {
                    dbContext.Organizations.Add(entity);
                    dbContext.SaveChanges();
                    return(DataResult <int?> .CreateSuccessResult(entity.Id));
                }
            }
            catch (Exception ex)
            {
                return(DataResult <int?> .CreateErrorResult(
                           ErrorCode.ExceptionThrown, ex, null));
            }
        }
 public DataResult <List <Term> > GetAllAsync()
 {
     try
     {
         using (var dbContext = new ClassTrackerDbContext())
         {
             var entities = dbContext
                            .Terms;
             var domains = entities.Select(x => _mapEntityToDomain(x));
             return(DataResult <List <Term> > .CreateSuccessResult(domains.ToList()));
         }
     }
     catch (Exception ex)
     {
         return(DataResult <List <Term> > .CreateErrorResult(
                    ErrorCode.ExceptionThrown, ex, null));
     }
 }
 public DataResult <Organization> GetAsync(int id)
 {
     try
     {
         using (var dbContext = new ClassTrackerDbContext())
         {
             var entity = dbContext
                          .Organizations
                          .Where(x => x.Id == id)
                          .SingleOrDefault();
             var domain = _mapEntityToDomain(entity);
             return(DataResult <Organization> .CreateSuccessResult(domain));
         }
     }
     catch (Exception ex)
     {
         return(DataResult <Organization> .CreateErrorResult(
                    ErrorCode.ExceptionThrown, ex, null));
     }
 }
        public async Task <DataResult <T2> > GetLatest(string partitionKey)
        {
            try
            {
                var table = GetTableReference(TableName);

                var query = new TableQuery <T1>()
                            .Where(TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.Equal, partitionKey))
                            .Take(1);

                var result = (await table.ExecuteQuerySegmentedAsync(query, null)).FirstOrDefault();
                return(result != null
                    ? DataResult <T2> .CreateSuccessResult(result.MapToBusinessModel())
                    : DataResult <T2> .CreateNotFoundResult());
            }
            catch (Exception e)
            {
                return(DataResult <T2> .CreateErrorResult(new Error(ErrorCode.ExceptionThrown, e)));
            }
        }
Esempio n. 5
0
        public async Task <DataResult <IEnumerable <AggregatedTemperatureHumidityReadings> > > GetTemperatureHumidityReadings(string sensorId, DateTimeOffset from, DateTimeOffset to)
        {
            if (!(to > from))
            {
                return(DataResult <IEnumerable <AggregatedTemperatureHumidityReadings> > .CreateErrorResult(new Error(ErrorCode.FailedWithoutException, null, "To must be after from")));
            }

            var readingsResult = await _temperatureHumidityRepository.Get(sensorId, from, to);

            if (!readingsResult.IsSuccessful)
            {
                return(DataResult <IEnumerable <AggregatedTemperatureHumidityReadings> > .CreateErrorResult(readingsResult.Error));
            }

            var intervalDataGroups = SortDataIntoGroups(
                sensorId,
                readingsResult.Data,
                CalculateIntervalLength(to - from)
                );

            return(DataResult <IEnumerable <AggregatedTemperatureHumidityReadings> >
                   .CreateSuccessResult(intervalDataGroups.Select(g => g.MapToBusinessModel())));
        }
        public async Task <DataResult <IEnumerable <T2> > > Get(string partitionKey, DateTimeOffset from, DateTimeOffset to)
        {
            try
            {
                var table = GetTableReference(TableName);

                var fromInvertedTicks = ConvertDateTimeOffsetToInvertedTicksString(from);
                var toInvertedTicks   = ConvertDateTimeOffsetToInvertedTicksString(to);

                var sensorQuery = TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.Equal, partitionKey);

                var fromToQuery = TableQuery.CombineFilters(
                    TableQuery.GenerateFilterCondition("RowKey", QueryComparisons.LessThanOrEqual, fromInvertedTicks),
                    TableOperators.And,
                    TableQuery.GenerateFilterCondition("RowKey", QueryComparisons.GreaterThanOrEqual, toInvertedTicks)
                    );

                var query = new TableQuery <T1>()
                            .Where(TableQuery.CombineFilters(sensorQuery, TableOperators.And, fromToQuery));

                var result = new List <T2>();
                TableContinuationToken continuationToken = null;
                do
                {
                    var queryResult = await table.ExecuteQuerySegmentedAsync(query, continuationToken);

                    result.AddRange(queryResult.Results.Select(r => r.MapToBusinessModel()));
                    continuationToken = queryResult.ContinuationToken;
                } while (continuationToken != null);

                return(DataResult <IEnumerable <T2> > .CreateSuccessResult(result));
            }
            catch (Exception e)
            {
                return(DataResult <IEnumerable <T2> > .CreateErrorResult(new Error(ErrorCode.ExceptionThrown, e)));
            }
        }