示例#1
0
        public static ObjectId FindExisting(DynamicRecord record, SchemaView view, ObjectId tenantId, ObjectId loggedUser, IDynamicService <DynamicRecord> dynamicService, out bool isValidInput)
        {
            isValidInput = true;
            //check if the current record exists... only check for quick create ones
            if ((record._id == null || record._id == ObjectId.Empty) && view != null && view.Schema.UniqueIndex != null)
            {
                var filter = view.Schema.UniqueIndex.ToDictionary(v => v, v =>
                                                                  v.Contains("$") ?
                                                                  //v.Split('$')[0] = View  (View$Name)
                                                                  //v.Split('$')[1] = Name
                                                                  (record[v.Split('$')[0]] as IDictionary <string, object>)[v.Split('$')[1]].ToString() :
                                                                  record[v]?.ToString()
                                                                  );

                // If all filters are empty that's because we're receiving an object without a unique index,
                // which can't be compared
                if (filter.All(x => string.IsNullOrEmpty(x.Value)))
                {
                    // We received something without a valid uniqueIndex
                    isValidInput = false;
                }
                else
                {
                    var existingRecords = dynamicService.GetAll(view, tenantId, loggedUser, RecordLevelSecurityEnum.NoRestriction).ToList();
                    var existingRecord  = existingRecords.Where(e => e.Contains(filter)).ToList();

                    if (existingRecord != null && existingRecord.Count() > 0)
                    {
                        return(existingRecord.First()._id);
                    }
                }
            }
            return(ObjectId.Empty);
        }
示例#2
0
        public virtual async Task <JsonResult> GetRowReferences([Required] Guid entityId, [Required] Guid propertyId)
        {
            var response = new ResultModel();
            var refProp  = _pagesContext.Table
                           .Include(x => x.TableFields)
                           .ThenInclude(x => x.TableFieldConfigValues)
                           .ThenInclude(x => x.TableFieldConfig)
                           .FirstOrDefault(x => x.Id == entityId)?
                           .TableFields?.FirstOrDefault(x => x.Id == propertyId);

            if (refProp == null)
            {
                response.Errors = new List <IErrorModel>
                {
                    new ErrorModel("fail", "Property reference not found")
                };
                return(Json(response));
            }

            var entityRefName = refProp.TableFieldConfigValues
                                .FirstOrDefault(x => x.TableFieldConfig.Code == TableFieldConfigCode.Reference.ForeingTable);

            if (entityRefName == null)
            {
                response.Errors = new List <IErrorModel>
                {
                    new ErrorModel("fail", "Property reference not found")
                };
                return(Json(response));
            }

            var displayFormat = refProp.TableFieldConfigValues
                                .FirstOrDefault(x => x.TableFieldConfig.Code == TableFieldConfigCode.Reference.DisplayFormat);
            var filters = new List <Filter>
            {
                new Filter(nameof(BaseModel.IsDeleted), false)
            };

            if (entityRefName.Value == "Users")
            {
                filters.Add(new Filter
                {
                    Value     = CurrentUserTenantId,
                    Criteria  = Criteria.Equals,
                    Parameter = nameof(BaseModel.TenantId)
                });
            }

            var res = await _service.GetAll(entityRefName.Value, filters : filters);

            if (res.IsSuccess)
            {
                if (displayFormat != null)
                {
                    if (!string.IsNullOrEmpty(displayFormat.Value))
                    {
                        res.Result = res.Result.Select(x =>
                        {
                            var format = displayFormat.Value.Inject(x);
                            if (x.ContainsKey("Name"))
                            {
                                x["Name"] = format;
                            }
                            else
                            {
                                x.Add("Name", format);
                            }
                            return(x);
                        });
                    }
                }

                try
                {
                    res.Result = res.Result.OrderBy(x => x.ContainsKey("Name") ? x["Name"] : x);
                }
                catch
                {
                    //Ignore
                }
            }

            response.IsSuccess = res.IsSuccess;
            response.Result    = new RowReferenceViewModel
            {
                Data       = res.Result?.ToList(),
                EntityName = entityRefName.Value
            };

            return(Json(response, _jsonSerializerSettings));
        }