Пример #1
0
        public void Delete(AttributeInterval request)
        {
            using (Execute)
            {
                Execute.Run(ssn =>
                {
                    if (!(request?.Id > 0))
                    {
                        throw new HttpError(HttpStatusCode.NotFound, $"No Id provided for delete.");
                    }

                    var en = DocEntityAttributeInterval.Get(request?.Id);
                    if (null == en)
                    {
                        throw new HttpError(HttpStatusCode.NotFound, $"No AttributeInterval 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.ATTRIBUTEINTERVAL);
                    DocCacheClient.RemoveById(request.Id);
                });
            }
        }
Пример #2
0
        private AttributeInterval GetAttributeInterval(AttributeInterval request)
        {
            var id = request?.Id;
            AttributeInterval ret = null;
            var query             = DocQuery.ActiveQuery ?? Execute;

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

            DocEntityAttributeInterval entity = null;

            if (id.HasValue)
            {
                entity = DocEntityAttributeInterval.Get(id.Value);
            }
            if (null == entity)
            {
                throw new HttpError(HttpStatusCode.NotFound, $"No AttributeInterval 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);
        }
Пример #3
0
        public AttributeInterval Post(AttributeIntervalCopy request)
        {
            AttributeInterval ret = null;

            using (Execute)
            {
                Execute.Run(ssn =>
                {
                    var entity = DocEntityAttributeInterval.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 pInterval = entity.Interval;
                    var copy      = new DocEntityAttributeInterval(ssn)
                    {
                        Hash       = Guid.NewGuid()
                        , Interval = pInterval
                    };

                    copy.SaveChanges(DocConstantPermission.ADD);
                    ret = copy.ToDto();
                });
            }
            return(ret);
        }
Пример #4
0
        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);
        }