public async Task <QueryResult <IDictionary <string, object> > > QueryQCEventDynamic(
            QCEventQueryProjection projection,
            QCEventQueryOptions options,
            QCEventQueryFilter filter = null,
            QCEventQuerySort sort     = null,
            QCEventQueryPaging paging = null,
            string folderPath         = null)
        {
            var fileService = provider.GetRequiredService <IFileService>();
            var query       = QCEvents;

            #region General
            if (filter != null)
            {
                query = query.Filter(filter);
            }
            query = query.Project(projection);
            int?totalCount = null;
            if (options.count_total)
            {
                totalCount = query.Count();
            }
            #endregion
            if (!options.single_only)
            {
                #region List query
                if (sort != null)
                {
                    query = query.Sort(sort);
                }
                if (paging != null && (!options.load_all || !QCEventQueryOptions.IsLoadAllAllowed))
                {
                    query = query.SelectPage(paging.page, paging.limit);
                }
                #endregion
            }

            if (options.single_only)
            {
                var single = query.SingleOrDefault();
                if (single == null)
                {
                    return(null);
                }
                var singleResult = GetQCEventDynamic(single, projection, options, folderPath, fileService);
                return(new QueryResult <IDictionary <string, object> >()
                {
                    Single = singleResult
                });
            }
            var entities = query.ToList();
            var list     = GetQCEventDynamic(entities, projection, options, folderPath, fileService);
            var result   = new QueryResult <IDictionary <string, object> >();
            result.List = list;
            if (options.count_total)
            {
                result.Count = totalCount;
            }
            return(result);
        }
 public ValidationData ValidateGetQCEvents(
     ClaimsPrincipal principal,
     QCEventQueryFilter filter,
     QCEventQuerySort sort,
     QCEventQueryProjection projection,
     QCEventQueryPaging paging,
     QCEventQueryOptions options)
 {
     return(new ValidationData());
 }
        public async Task <QueryResult <QCEvent> > QueryQCEvent(
            QCEventQueryProjection projection,
            QCEventQueryOptions options,
            QCEventQueryFilter filter = null,
            QCEventQuerySort sort     = null,
            QCEventQueryPaging paging = null)
        {
            var query = QCEvents;

            #region General
            if (filter != null)
            {
                query = query.Filter(filter);
            }
            query = query.Project(projection);
            int?totalCount = null;
            if (options.count_total)
            {
                totalCount = query.Count();
            }
            #endregion
            if (!options.single_only)
            {
                #region List query
                if (sort != null)
                {
                    query = query.Sort(sort);
                }
                if (paging != null && (!options.load_all || !QCEventQueryOptions.IsLoadAllAllowed))
                {
                    query = query.SelectPage(paging.page, paging.limit);
                }
                #endregion
            }

            if (options.single_only)
            {
                var single = query.SingleOrDefault();
                if (single == null)
                {
                    return(null);
                }
                return(new QueryResult <QCEvent>()
                {
                    Single = single
                });
            }
            var result = new QueryResult <QCEvent>();
            result.List = query.ToList();
            if (options.count_total)
            {
                result.Count = totalCount;
            }
            return(result);
        }
        public List <IDictionary <string, object> > GetQCEventDynamic(
            IEnumerable <QCEvent> rows, QCEventQueryProjection projection,
            QCEventQueryOptions options, string qcFolderPath, IFileService fileService)
        {
            var list = new List <IDictionary <string, object> >();

            foreach (var o in rows)
            {
                var obj = GetQCEventDynamic(o, projection, options, qcFolderPath, fileService);
                list.Add(obj);
            }
            return(list);
        }
 public static IQueryable <QCEvent> Project(
     this IQueryable <QCEvent> query, QCEventQueryProjection projection)
 {
     foreach (var f in projection.GetFieldsArr())
     {
         if (QCEventQueryProjection.MAPS.ContainsKey(f))
         {
             foreach (var prop in QCEventQueryProjection.MAPS[f])
             {
                 query = prop.Compile()(query);
             }
         }
     }
     return(query);
 }
        public async Task <IActionResult> Get([FromQuery][QueryObject] QCEventQueryFilter filter,
                                              [FromQuery] QCEventQuerySort sort,
                                              [FromQuery] QCEventQueryProjection projection,
                                              [FromQuery] QCEventQueryPaging paging,
                                              [FromQuery] QCEventQueryOptions options)
        {
            var validationData = _service.ValidateGetQCEvents(
                User, filter, sort, projection, paging, options);

            if (!validationData.IsValid)
            {
                return(BadRequest(AppResult.FailValidation(data: validationData)));
            }
            var result = await _service.QueryQCEventDynamic(
                projection, options, Settings.Instance.QCEventImageFolderPath, filter, sort, paging);

            if (options.single_only && result == null)
            {
                return(NotFound(AppResult.NotFound()));
            }
            return(Ok(AppResult.Success(result)));
        }
        public IDictionary <string, object> GetQCEventDynamic(
            QCEvent row, QCEventQueryProjection projection,
            QCEventQueryOptions options, string qcFolderPath, IFileService fileService)
        {
            var obj = new Dictionary <string, object>();

            foreach (var f in projection.GetFieldsArr())
            {
                switch (f)
                {
                case QCEventQueryProjection.INFO:
                {
                    var entity = row;
                    obj["id"] = entity.Id;
                    var details = entity.Details.Select(o => new
                        {
                            id             = o.Id,
                            defect_type_id = o.DefectTypeId,
                            defect_type    = new
                            {
                                id   = o.DefectType.Id,
                                code = o.DefectType.Code,
                                name = o.DefectType.Name
                            }
                        }).ToList();
                    obj["details"]             = details;
                    obj["description"]         = entity.Description;
                    obj["production_batch_id"] = entity.ProductionBatchId;
                    obj["qc_device_id"]        = entity.QCDeviceId;
                    var time = entity.CreatedTime
                               .ToDefaultTimeZone();
                    var timeStr = time.ToString(options.date_format);
                    obj["created_time"] = new
                    {
                        display = timeStr,
                        iso     = $"{time.ToUniversalTime():s}Z"
                    };
                    time = entity.LastUpdated
                           .ToDefaultTimeZone();
                    timeStr             = time.ToString(options.date_format);
                    obj["last_updated"] = new
                    {
                        display = timeStr,
                        iso     = $"{time.ToUniversalTime():s}Z"
                    };
                    obj["seen"] = entity.Seen;
                }
                break;

                case QCEventQueryProjection.BATCH:
                {
                    var entity = row.Batch;
                    if (entity != null)
                    {
                        obj["batch"] = new
                        {
                            id   = entity.Id,
                            code = entity.Code,
                        }
                    }
                    ;
                }
                break;

                case QCEventQueryProjection.IMAGE:
                {
                    if (!options.single_only)
                    {
                        throw new Exception("Only single option can query image field");
                    }
                    var entity = row;
                    if (entity.LeftImage != null)
                    {
                        var finalPath = Path.Combine(qcFolderPath, entity.LeftImage);
                        if (File.Exists(finalPath))
                        {
                            var img   = File.ReadAllBytes(finalPath);
                            var img64 = Convert.ToBase64String(img);
                            obj["left_image"] = img64;
                        }
                    }
                    if (entity.RightImage != null)
                    {
                        var finalPath = Path.Combine(qcFolderPath, entity.RightImage);
                        if (File.Exists(finalPath))
                        {
                            var img   = File.ReadAllBytes(finalPath);
                            var img64 = Convert.ToBase64String(img);
                            obj["right_image"] = img64;
                        }
                    }
                    if (entity.SideImages != null)
                    {
                        var sideImages    = JsonConvert.DeserializeObject <IEnumerable <string> >(entity.SideImages);
                        var sideImagesB64 = new List <string>();
                        obj["side_images"] = sideImagesB64;
                        foreach (var iPath in sideImages)
                        {
                            var fullPath = fileService.GetFilePath(qcFolderPath, null, iPath).Item2;
                            if (File.Exists(fullPath))
                            {
                                var img   = File.ReadAllBytes(fullPath);
                                var img64 = Convert.ToBase64String(img);
                                sideImagesB64.Add(img64);
                            }
                        }
                    }
                }
                break;
                }
            }
            return(obj);
        }