Beispiel #1
0
        private Junction GetJunction(Junction request)
        {
            var      id    = request?.Id;
            Junction ret   = null;
            var      query = DocQuery.ActiveQuery ?? Execute;

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

            DocEntityJunction entity = null;

            if (id.HasValue)
            {
                entity = DocEntityJunction.Get(id.Value);
            }
            if (null == entity)
            {
                throw new HttpError(HttpStatusCode.NotFound, $"No Junction 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);
        }
Beispiel #2
0
        public void Delete(Junction request)
        {
            using (Execute)
            {
                Execute.Run(ssn =>
                {
                    if (!(request?.Id > 0))
                    {
                        throw new HttpError(HttpStatusCode.NotFound, $"No Id provided for delete.");
                    }

                    var en = DocEntityJunction.Get(request?.Id);
                    if (null == en)
                    {
                        throw new HttpError(HttpStatusCode.NotFound, $"No Junction 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.JUNCTION);
                    DocCacheClient.RemoveById(request.Id);
                });
            }
        }
Beispiel #3
0
        public Junction Post(JunctionCopy request)
        {
            Junction ret = null;

            using (Execute)
            {
                Execute.Run(ssn =>
                {
                    var entity = DocEntityJunction.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 pChildren  = entity.Children.ToList();
                    var pData      = entity.Data;
                    var pOwnerId   = entity.OwnerId;
                    var pOwnerType = entity.OwnerType;
                    if (!DocTools.IsNullOrEmpty(pOwnerType))
                    {
                        pOwnerType += " (Copy)";
                    }
                    var pParent     = entity.Parent;
                    var pTargetId   = entity.TargetId;
                    var pTargetType = entity.TargetType;
                    if (!DocTools.IsNullOrEmpty(pTargetType))
                    {
                        pTargetType += " (Copy)";
                    }
                    var pType = entity.Type;
                    var pUser = entity.User;
                    var copy  = new DocEntityJunction(ssn)
                    {
                        Hash         = Guid.NewGuid()
                        , Data       = pData
                        , OwnerId    = pOwnerId
                        , OwnerType  = pOwnerType
                        , Parent     = pParent
                        , TargetId   = pTargetId
                        , TargetType = pTargetType
                        , Type       = pType
                        , User       = pUser
                    };
                    foreach (var item in pChildren)
                    {
                        entity.Children.Add(item);
                    }

                    copy.SaveChanges(DocConstantPermission.ADD);
                    ret = copy.ToDto();
                });
            }
            return(ret);
        }
Beispiel #4
0
        private Junction _AssignValues(Junction 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, "Junction"))
            {
                throw new HttpError(HttpStatusCode.Forbidden, "You do not have ADD permission for this route.");
            }

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

            Junction ret = null;

            request = _InitAssignValues <Junction>(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 <Junction>(DocConstantModelName.JUNCTION, nameof(Junction), request);

            //First, assign all the variables, do database lookups and conversions
            var pChildren              = GetVariable <Reference>(request, nameof(request.Children), request.Children?.ToList(), request.ChildrenIds?.ToList());
            var pData                  = request.Data;
            var pOwnerId               = request.OwnerId;
            var pOwnerType             = request.OwnerType;
            var pParent                = DocEntityJunction.Get(request.Parent?.Id, true, Execute) ?? DocEntityJunction.Get(request.ParentId, true, Execute);
            var pTargetId              = request.TargetId;
            var pTargetType            = request.TargetType;
            DocEntityLookupTable pType = GetLookup(DocConstantLookupTable.JUNCTIONTYPE, request.Type?.Name, request.Type?.Id);
            var pUser                  = DocEntityUser.Get(request.User?.Id, true, Execute) ?? DocEntityUser.Get(request.UserId, true, Execute);
            var pArchived              = true == request.Archived;
            var pLocked                = request.Locked;

            var entity = InitEntity <DocEntityJunction, Junction>(request, permission, session);

            if (AllowPatchValue <Junction, bool>(request, DocConstantModelName.JUNCTION, pArchived, permission, nameof(request.Archived), pArchived != entity.Archived))
            {
                entity.Archived = pArchived;
            }
            if (AllowPatchValue <Junction, string>(request, DocConstantModelName.JUNCTION, pData, permission, nameof(request.Data), pData != entity.Data))
            {
                entity.Data = pData;
            }
            if (AllowPatchValue <Junction, int?>(request, DocConstantModelName.JUNCTION, pOwnerId, permission, nameof(request.OwnerId), pOwnerId != entity.OwnerId))
            {
                entity.OwnerId = pOwnerId;
            }
            if (AllowPatchValue <Junction, string>(request, DocConstantModelName.JUNCTION, pOwnerType, permission, nameof(request.OwnerType), pOwnerType != entity.OwnerType))
            {
                entity.OwnerType = pOwnerType;
            }
            if (AllowPatchValue <Junction, DocEntityJunction>(request, DocConstantModelName.JUNCTION, pParent, permission, nameof(request.Parent), pParent != entity.Parent))
            {
                entity.Parent = pParent;
            }
            if (AllowPatchValue <Junction, int?>(request, DocConstantModelName.JUNCTION, pTargetId, permission, nameof(request.TargetId), pTargetId != entity.TargetId))
            {
                entity.TargetId = pTargetId;
            }
            if (AllowPatchValue <Junction, string>(request, DocConstantModelName.JUNCTION, pTargetType, permission, nameof(request.TargetType), pTargetType != entity.TargetType))
            {
                entity.TargetType = pTargetType;
            }
            if (AllowPatchValue <Junction, DocEntityLookupTable>(request, DocConstantModelName.JUNCTION, pType, permission, nameof(request.Type), pType != entity.Type))
            {
                entity.Type = pType;
            }
            if (AllowPatchValue <Junction, DocEntityUser>(request, DocConstantModelName.JUNCTION, pUser, permission, nameof(request.User), pUser != entity.User))
            {
                entity.User = pUser;
            }
            if (request.Locked && AllowPatchValue <Junction, bool>(request, DocConstantModelName.JUNCTION, pArchived, permission, nameof(request.Locked), pLocked != entity.Locked))
            {
                entity.Archived = pArchived;
            }
            entity.SaveChanges(permission);

            var idsToInvalidate = new List <int>();

            idsToInvalidate.AddRange(PatchCollection <Junction, DocEntityJunction, Reference, DocEntityJunction>(request, entity, pChildren, permission, nameof(request.Children)));
            if (idsToInvalidate.Any())
            {
                idsToInvalidate.Add(entity.Id);
                DocCacheClient.RemoveByEntityIds(idsToInvalidate);
                DocCacheClient.RemoveSearch(DocConstantModelName.JUNCTION);
            }

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

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

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

            return(ret);
        }