コード例 #1
0
        private TimePoint GetTimePoint(TimePoint request)
        {
            var       id    = request?.Id;
            TimePoint ret   = null;
            var       query = DocQuery.ActiveQuery ?? Execute;

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

            DocEntityTimePoint entity = null;

            if (id.HasValue)
            {
                entity = DocEntityTimePoint.Get(id.Value);
            }
            if (null == entity)
            {
                throw new HttpError(HttpStatusCode.NotFound, $"No TimePoint 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 Interval _AssignValues(Interval 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, "Interval"))
            {
                throw new HttpError(HttpStatusCode.Forbidden, "You do not have ADD permission for this route.");
            }

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

            Interval ret = null;

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

            //First, assign all the variables, do database lookups and conversions
            var pCalendarDateEnd   = DocEntityDateTime.Get(request.CalendarDateEnd?.Id, true, Execute) ?? DocEntityDateTime.Get(request.CalendarDateEndId, true, Execute);
            var pCalendarDateStart = DocEntityDateTime.Get(request.CalendarDateStart?.Id, true, Execute) ?? DocEntityDateTime.Get(request.CalendarDateStartId, true, Execute);
            var pCalendarType      = request.CalendarType;
            var pFollowUp          = DocEntityTimePoint.Get(request.FollowUp?.Id, true, Execute) ?? DocEntityTimePoint.Get(request.FollowUpId, true, Execute);
            var pTimeOfDay         = DocEntityTimePoint.Get(request.TimeOfDay?.Id, true, Execute) ?? DocEntityTimePoint.Get(request.TimeOfDayId, true, Execute);
            var pArchived          = true == request.Archived;
            var pLocked            = request.Locked;

            var entity = InitEntity <DocEntityInterval, Interval>(request, permission, session);

            if (AllowPatchValue <Interval, bool>(request, DocConstantModelName.INTERVAL, pArchived, permission, nameof(request.Archived), pArchived != entity.Archived))
            {
                entity.Archived = pArchived;
            }
            if (AllowPatchValue <Interval, DocEntityDateTime>(request, DocConstantModelName.INTERVAL, pCalendarDateEnd, permission, nameof(request.CalendarDateEnd), pCalendarDateEnd != entity.CalendarDateEnd))
            {
                entity.CalendarDateEnd = pCalendarDateEnd;
            }
            if (AllowPatchValue <Interval, DocEntityDateTime>(request, DocConstantModelName.INTERVAL, pCalendarDateStart, permission, nameof(request.CalendarDateStart), pCalendarDateStart != entity.CalendarDateStart))
            {
                entity.CalendarDateStart = pCalendarDateStart;
            }
            if (AllowPatchValue <Interval, string>(request, DocConstantModelName.INTERVAL, pCalendarType, permission, nameof(request.CalendarType), pCalendarType != entity.CalendarType))
            {
                entity.CalendarType = pCalendarType;
            }
            if (AllowPatchValue <Interval, DocEntityTimePoint>(request, DocConstantModelName.INTERVAL, pFollowUp, permission, nameof(request.FollowUp), pFollowUp != entity.FollowUp))
            {
                entity.FollowUp = pFollowUp;
            }
            if (AllowPatchValue <Interval, DocEntityTimePoint>(request, DocConstantModelName.INTERVAL, pTimeOfDay, permission, nameof(request.TimeOfDay), pTimeOfDay != entity.TimeOfDay))
            {
                entity.TimeOfDay = pTimeOfDay;
            }
            if (request.Locked && AllowPatchValue <Interval, bool>(request, DocConstantModelName.INTERVAL, 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.INTERVAL);
            }

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

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

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

            return(ret);
        }