Example #1
0
        private Trial GetTrial(Trial request)
        {
            var   id    = request?.Id;
            Trial ret   = null;
            var   query = DocQuery.ActiveQuery ?? Execute;

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

            DocEntityTrial entity = null;

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

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

            using (Execute)
            {
                Execute.Run(ssn =>
                {
                    var entity = DocEntityTrial.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 pDocuments = entity.Documents.ToList();
                    var pName      = entity.Name;
                    if (!DocTools.IsNullOrEmpty(pName))
                    {
                        pName += " (Copy)";
                    }
                    var pParent = entity.Parent;
                    var copy    = new DocEntityTrial(ssn)
                    {
                        Hash     = Guid.NewGuid()
                        , Name   = pName
                        , Parent = pParent
                    };
                    foreach (var item in pDocuments)
                    {
                        entity.Documents.Add(item);
                    }

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

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

            Trial ret = null;

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

            //First, assign all the variables, do database lookups and conversions
            var pDocuments = GetVariable <Reference>(request, nameof(request.Documents), request.Documents?.ToList(), request.DocumentsIds?.ToList());
            var pName      = request.Name;
            var pParent    = DocEntityTrial.Get(request.Parent?.Id, true, Execute) ?? DocEntityTrial.Get(request.ParentId, true, Execute);
            var pArchived  = true == request.Archived;
            var pLocked    = request.Locked;

            var entity = InitEntity <DocEntityTrial, Trial>(request, permission, session);

            if (AllowPatchValue <Trial, bool>(request, DocConstantModelName.TRIAL, pArchived, permission, nameof(request.Archived), pArchived != entity.Archived))
            {
                entity.Archived = pArchived;
            }
            if (AllowPatchValue <Trial, string>(request, DocConstantModelName.TRIAL, pName, permission, nameof(request.Name), pName != entity.Name))
            {
                entity.Name = pName;
            }
            if (AllowPatchValue <Trial, DocEntityTrial>(request, DocConstantModelName.TRIAL, pParent, permission, nameof(request.Parent), pParent != entity.Parent))
            {
                entity.Parent = pParent;
            }
            if (request.Locked && AllowPatchValue <Trial, bool>(request, DocConstantModelName.TRIAL, pArchived, permission, nameof(request.Locked), pLocked != entity.Locked))
            {
                entity.Archived = pArchived;
            }
            entity.SaveChanges(permission);

            var idsToInvalidate = new List <int>();

            idsToInvalidate.AddRange(PatchCollection <Trial, DocEntityTrial, Reference, DocEntityDocument>(request, entity, pDocuments, permission, nameof(request.Documents)));
            if (idsToInvalidate.Any())
            {
                idsToInvalidate.Add(entity.Id);
                DocCacheClient.RemoveByEntityIds(idsToInvalidate);
                DocCacheClient.RemoveSearch(DocConstantModelName.TRIAL);
            }

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

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

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

            return(ret);
        }