private Attribute GetAttribute(Attribute request)
        {
            var       id    = request?.Id;
            Attribute ret   = null;
            var       query = DocQuery.ActiveQuery ?? Execute;

            DocPermissionFactory.SetSelect <Attribute>(currentUser, "Attribute", request.Select);

            DocEntityAttribute entity = null;

            if (id.HasValue)
            {
                entity = DocEntityAttribute.Get(id.Value);
            }
            if (null == entity)
            {
                throw new HttpError(HttpStatusCode.NotFound, $"No Attribute found for Id {id.Value}");
            }

            if (!DocPermissionFactory.HasPermission(entity, currentUser, DocConstantPermission.VIEW))
            {
                throw new HttpError(HttpStatusCode.Forbidden, "You do not have VIEW permission for this route.");
            }

            ret = entity?.ToDto();
            return(ret);
        }
        public Attribute Post(Attribute request)
        {
            if (request == null)
            {
                throw new HttpError(HttpStatusCode.NotFound, "Request cannot be null.");
            }

            request.Select = request.Select ?? new List <string>();

            Attribute ret = null;

            using (Execute)
            {
                Execute.Run(ssn =>
                {
                    if (!DocPermissionFactory.HasPermissionTryAdd(currentUser, "Attribute"))
                    {
                        throw new HttpError(HttpStatusCode.Forbidden, "You do not have ADD permission for this route.");
                    }

                    ret = _AssignValues(request, DocConstantPermission.ADD, ssn);
                });
            }
            return(ret);
        }
        public void Delete(Attribute request)
        {
            using (Execute)
            {
                Execute.Run(ssn =>
                {
                    if (!(request?.Id > 0))
                    {
                        throw new HttpError(HttpStatusCode.NotFound, $"No Id provided for delete.");
                    }

                    var en = DocEntityAttribute.Get(request?.Id);
                    if (null == en)
                    {
                        throw new HttpError(HttpStatusCode.NotFound, $"No Attribute could be found for Id {request?.Id}.");
                    }
                    if (en.IsRemoved)
                    {
                        return;
                    }

                    if (!DocPermissionFactory.HasPermission(en, currentUser, DocConstantPermission.DELETE))
                    {
                        throw new HttpError(HttpStatusCode.Forbidden, "You do not have DELETE permission for this route.");
                    }

                    en.Remove();

                    DocCacheClient.RemoveSearch(DocConstantModelName.ATTRIBUTE);
                    DocCacheClient.RemoveById(request.Id);
                });
            }
        }
        public Attribute Post(AttributeCopy request)
        {
            Attribute ret = null;

            using (Execute)
            {
                Execute.Run(ssn =>
                {
                    var entity = DocEntityAttribute.Get(request?.Id);
                    if (null == entity)
                    {
                        throw new HttpError(HttpStatusCode.NoContent, "The COPY request did not succeed.");
                    }
                    if (!DocPermissionFactory.HasPermission(entity, currentUser, DocConstantPermission.ADD))
                    {
                        throw new HttpError(HttpStatusCode.Forbidden, "You do not have ADD permission for this route.");
                    }

                    var pAttributeName    = entity.AttributeName;
                    var pAttributeType    = entity.AttributeType;
                    var pInterval         = entity.Interval;
                    var pIsCharacteristic = entity.IsCharacteristic;
                    var pIsOutcome        = entity.IsOutcome;
                    var pIsPositive       = entity.IsPositive;
                    var pUniqueKey        = entity.UniqueKey;
                    if (!DocTools.IsNullOrEmpty(pUniqueKey))
                    {
                        pUniqueKey += " (Copy)";
                    }
                    var pValueType = entity.ValueType;
                    var copy       = new DocEntityAttribute(ssn)
                    {
                        Hash               = Guid.NewGuid()
                        , AttributeName    = pAttributeName
                        , AttributeType    = pAttributeType
                        , Interval         = pInterval
                        , IsCharacteristic = pIsCharacteristic
                        , IsOutcome        = pIsOutcome
                        , IsPositive       = pIsPositive
                        , UniqueKey        = pUniqueKey
                        , ValueType        = pValueType
                    };

                    copy.SaveChanges(DocConstantPermission.ADD);
                    ret = copy.ToDto();
                });
            }
            return(ret);
        }
        public Attribute Patch(Attribute request)
        {
            if (true != (request?.Id > 0))
            {
                throw new HttpError(HttpStatusCode.NotFound, "Please specify a valid Id of the Attribute to patch.");
            }

            request.Select = request.Select ?? new List <string>();

            Attribute ret = null;

            using (Execute)
            {
                Execute.Run(ssn =>
                {
                    ret = _AssignValues(request, DocConstantPermission.EDIT, ssn);
                });
            }
            return(ret);
        }
 public Attribute Put(Attribute request)
 {
     return(Patch(request));
 }
        private Attribute _AssignValues(Attribute request, DocConstantPermission permission, Session session)
        {
            if (permission != DocConstantPermission.ADD && (request == null || request.Id <= 0))
            {
                throw new HttpError(HttpStatusCode.NotFound, $"No record");
            }

            if (permission == DocConstantPermission.ADD && !DocPermissionFactory.HasPermissionTryAdd(currentUser, "Attribute"))
            {
                throw new HttpError(HttpStatusCode.Forbidden, "You do not have ADD permission for this route.");
            }

            request.Select = request.Select ?? new List <string>();

            Attribute ret = null;

            request = _InitAssignValues <Attribute>(request, permission, session);
            //In case init assign handles create for us, return it
            if (permission == DocConstantPermission.ADD && request.Id > 0)
            {
                return(request);
            }

            var cacheKey = GetApiCacheKey <Attribute>(DocConstantModelName.ATTRIBUTE, nameof(Attribute), request);

            //First, assign all the variables, do database lookups and conversions
            DocEntityLookupTable pAttributeName = GetLookup(DocConstantLookupTable.ATTRIBUTENAME, request.AttributeName?.Name, request.AttributeName?.Id);
            DocEntityLookupTable pAttributeType = GetLookup(DocConstantLookupTable.ATTRIBUTETYPE, request.AttributeType?.Name, request.AttributeType?.Id);
            var pInterval         = DocEntityAttributeInterval.Get(request.Interval?.Id, true, Execute) ?? DocEntityAttributeInterval.Get(request.IntervalId, true, Execute);
            var pIsCharacteristic = request.IsCharacteristic;
            var pIsOutcome        = request.IsOutcome;
            var pIsPositive       = request.IsPositive;
            var pUniqueKey        = request.UniqueKey;
            var pValueType        = DocEntityValueType.Get(request.ValueType);
            var pArchived         = true == request.Archived;
            var pLocked           = request.Locked;

            var entity = InitEntity <DocEntityAttribute, Attribute>(request, permission, session);

            if (AllowPatchValue <Attribute, bool>(request, DocConstantModelName.ATTRIBUTE, pArchived, permission, nameof(request.Archived), pArchived != entity.Archived))
            {
                entity.Archived = pArchived;
            }
            if (AllowPatchValue <Attribute, DocEntityLookupTable>(request, DocConstantModelName.ATTRIBUTE, pAttributeName, permission, nameof(request.AttributeName), pAttributeName != entity.AttributeName))
            {
                entity.AttributeName = pAttributeName;
            }
            if (AllowPatchValue <Attribute, DocEntityLookupTable>(request, DocConstantModelName.ATTRIBUTE, pAttributeType, permission, nameof(request.AttributeType), pAttributeType != entity.AttributeType))
            {
                entity.AttributeType = pAttributeType;
            }
            if (AllowPatchValue <Attribute, DocEntityAttributeInterval>(request, DocConstantModelName.ATTRIBUTE, pInterval, permission, nameof(request.Interval), pInterval != entity.Interval))
            {
                entity.Interval = pInterval;
            }
            if (AllowPatchValue <Attribute, bool>(request, DocConstantModelName.ATTRIBUTE, pIsCharacteristic, permission, nameof(request.IsCharacteristic), pIsCharacteristic != entity.IsCharacteristic))
            {
                entity.IsCharacteristic = pIsCharacteristic;
            }
            if (AllowPatchValue <Attribute, bool>(request, DocConstantModelName.ATTRIBUTE, pIsOutcome, permission, nameof(request.IsOutcome), pIsOutcome != entity.IsOutcome))
            {
                entity.IsOutcome = pIsOutcome;
            }
            if (AllowPatchValue <Attribute, bool?>(request, DocConstantModelName.ATTRIBUTE, pIsPositive, permission, nameof(request.IsPositive), pIsPositive != entity.IsPositive))
            {
                entity.IsPositive = pIsPositive;
            }
            if (AllowPatchValue <Attribute, string>(request, DocConstantModelName.ATTRIBUTE, pUniqueKey, permission, nameof(request.UniqueKey), pUniqueKey != entity.UniqueKey))
            {
                entity.UniqueKey = pUniqueKey;
            }
            if (AllowPatchValue <Attribute, DocEntityValueType>(request, DocConstantModelName.ATTRIBUTE, pValueType, permission, nameof(request.ValueType), pValueType != entity.ValueType))
            {
                entity.ValueType = pValueType;
            }
            if (request.Locked && AllowPatchValue <Attribute, bool>(request, DocConstantModelName.ATTRIBUTE, pArchived, permission, nameof(request.Locked), pLocked != entity.Locked))
            {
                entity.Archived = pArchived;
            }
            entity.SaveChanges(permission);

            var idsToInvalidate = new List <int>();

            if (idsToInvalidate.Any())
            {
                idsToInvalidate.Add(entity.Id);
                DocCacheClient.RemoveByEntityIds(idsToInvalidate);
                DocCacheClient.RemoveSearch(DocConstantModelName.ATTRIBUTE);
            }

            entity.SaveChanges(permission);
            DocPermissionFactory.SetSelect <Attribute>(currentUser, nameof(Attribute), request.Select);
            ret = entity.ToDto();

            var cacheExpires = DocResources.Metadata.GetCacheExpiration(DocConstantModelName.ATTRIBUTE);

            DocCacheClient.Set(key: cacheKey, value: ret, entityId: request.Id, entityType: DocConstantModelName.ATTRIBUTE, cacheExpires);

            return(ret);
        }
 public object Get(Attribute request) => GetEntityWithCache <Attribute>(DocConstantModelName.ATTRIBUTE, request, GetAttribute);