예제 #1
0
        private AuditRecord GetAuditRecord(AuditRecord request)
        {
            var         id    = request?.Id;
            AuditRecord ret   = null;
            var         query = DocQuery.ActiveQuery ?? Execute;

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

            DocEntityAuditRecord entity = null;

            if (id.HasValue)
            {
                entity = DocEntityAuditRecord.Get(id.Value);
            }
            if (null == entity)
            {
                throw new HttpError(HttpStatusCode.NotFound, $"No AuditRecord 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);
        }
예제 #2
0
        private AuditDelta _AssignValues(AuditDelta 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, "AuditDelta"))
            {
                throw new HttpError(HttpStatusCode.Forbidden, "You do not have ADD permission for this route.");
            }

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

            AuditDelta ret = null;

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

            //First, assign all the variables, do database lookups and conversions
            var pAudit    = DocEntityAuditRecord.Get(request.Audit?.Id, true, Execute) ?? DocEntityAuditRecord.Get(request.AuditId, true, Execute);
            var pDelta    = request.Delta;
            var pArchived = true == request.Archived;
            var pLocked   = request.Locked;

            var entity = InitEntity <DocEntityAuditDelta, AuditDelta>(request, permission, session);

            if (AllowPatchValue <AuditDelta, bool>(request, DocConstantModelName.AUDITDELTA, pArchived, permission, nameof(request.Archived), pArchived != entity.Archived))
            {
                entity.Archived = pArchived;
            }
            if (AllowPatchValue <AuditDelta, DocEntityAuditRecord>(request, DocConstantModelName.AUDITDELTA, pAudit, permission, nameof(request.Audit), pAudit != entity.Audit))
            {
                entity.Audit = pAudit;
            }
            if (AllowPatchValue <AuditDelta, string>(request, DocConstantModelName.AUDITDELTA, pDelta, permission, nameof(request.Delta), pDelta != entity.Delta))
            {
                entity.Delta = pDelta;
            }
            if (request.Locked && AllowPatchValue <AuditDelta, bool>(request, DocConstantModelName.AUDITDELTA, 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.AUDITDELTA);
            }

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

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

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

            return(ret);
        }