示例#1
0
        public async Task <ActionResult <Core2Group> > Get(string id)
        {
            StringValues requested = this.Request.Query[QueryKeys.Attributes];
            StringValues excluded  = this.Request.Query[QueryKeys.ExcludedAttributes];

            try
            {
                Core2Group group = (Core2Group)await this._provider.GetById(id).ConfigureAwait(false);

                if (group == null)
                {
                    ErrorResponse notFoundError = new ErrorResponse(string.Format(CultureInfo.InvariantCulture, ErrorDetail.NotFound, id), ErrorDetail.Status404);
                    return(this.NotFound(notFoundError));
                }

                group = ColumnsUtility.FilterAttributes(requested, excluded, group, this.alwaysRetuned);

                this.Response.ContentType = ControllerConstants.DefaultContentType;
                return(group);
            }
            catch (Exception e)
            {
                this._logger.LogError(e.ToString());
                ErrorResponse databaseException = new ErrorResponse(ErrorDetail.DatabaseError, ErrorDetail.Status500);
                return(this.StatusCode(500, databaseException));

                throw;
            }
        }
示例#2
0
        public async Task <ActionResult <Core2Group> > Put(string id, Core2Group item)
        {
            if (id != item.Identifier)
            {
                ErrorResponse badRequestError = new ErrorResponse(ErrorDetail.Mutability, ErrorDetail.Status400);
                return(this.NotFound(badRequestError));
            }

            try
            {
                Core2Group group = (Core2Group)await this._provider.GetById(id).ConfigureAwait(false);

                await this._provider.Replace(item, group).ConfigureAwait(false);

                this.Response.ContentType = ControllerConstants.DefaultContentType;
                return(this.Ok(group));
            }
            catch (Exception e)
            {
                this._logger.LogError(e.ToString());
                ErrorResponse databaseException = new ErrorResponse(ErrorDetail.DatabaseError, ErrorDetail.Status500);
                return(this.StatusCode(500, databaseException));

                throw;
            }
        }
        public override Task <Resource> CreateAsync(Resource resource, string correlationIdentifier)
        {
            if (resource.Identifier != null)
            {
                throw new HttpResponseException(HttpStatusCode.BadRequest);
            }

            Core2Group group = resource as Core2Group;

            if (string.IsNullOrWhiteSpace(group.DisplayName))
            {
                throw new HttpResponseException(HttpStatusCode.BadRequest);
            }

            IEnumerable <Core2Group> exisitingGroups = this.storage.Groups.Values;

            if
            (
                exisitingGroups.Any(
                    (Core2Group exisitingGroup) =>
                    string.Equals(exisitingGroup.DisplayName, group.DisplayName, StringComparison.Ordinal))
            )
            {
                throw new HttpResponseException(HttpStatusCode.Conflict);
            }

            string resourceIdentifier = Guid.NewGuid().ToString();

            resource.Identifier = resourceIdentifier;
            this.storage.Groups.Add(resourceIdentifier, group);

            return(Task.FromResult(resource));
        }
        public override Task <Resource> CreateAsync(Resource resource, string correlationIdentifier)
        {
            if (resource.Identifier != null)
            {
                throw new HttpResponseException(HttpStatusCode.BadRequest);
            }

            Core2Group group = resource as Core2Group;

            if (string.IsNullOrWhiteSpace(group.DisplayName))
            {
                throw new HttpResponseException(HttpStatusCode.BadRequest);
            }

            // call service
            TargetGroup target;

            try
            {
                target = _storageService.CreateGroup((TargetGroup)group);
            }
            catch (Exception err)
            {
                switch (err.Message)
                {
                case "Conflict":
                    throw new HttpResponseException(HttpStatusCode.Conflict);

                case "InvalidMemberType":
                    throw new HttpResponseException(HttpStatusCode.NotAcceptable);

                default:
                    throw new HttpResponseException(HttpStatusCode.InternalServerError);
                }
            }

            return(Task.FromResult((Core2Group)target as Resource));
        }
        public override Task <Resource> ReplaceAsync(Resource resource, string correlationIdentifier)
        {
            if (resource.Identifier == null)
            {
                throw new HttpResponseException(HttpStatusCode.BadRequest);
            }

            Core2Group group = resource as Core2Group;

            if (string.IsNullOrWhiteSpace(group.DisplayName))
            {
                throw new HttpResponseException(HttpStatusCode.BadRequest);
            }

            IEnumerable <Core2Group> exisitingGroups = this.storage.Groups.Values;

            if
            (
                exisitingGroups.Any(
                    (Core2Group exisitingUser) =>
                    string.Equals(exisitingUser.DisplayName, group.DisplayName, StringComparison.Ordinal) &&
                    !string.Equals(exisitingUser.Identifier, group.Identifier, StringComparison.OrdinalIgnoreCase))
            )
            {
                throw new HttpResponseException(HttpStatusCode.Conflict);
            }

            if (!this.storage.Groups.TryGetValue(group.Identifier, out Core2Group _))
            {
                throw new HttpResponseException(HttpStatusCode.NotFound);
            }

            this.storage.Groups[group.Identifier] = group;
            Resource result = group as Resource;

            return(Task.FromResult(result));
        }
        public override Task <Resource[]> QueryAsync(IQueryParameters parameters, string correlationIdentifier)
        {
            if (parameters == null)
            {
                throw new ArgumentNullException(nameof(parameters));
            }

            if (string.IsNullOrWhiteSpace(correlationIdentifier))
            {
                throw new ArgumentNullException(nameof(correlationIdentifier));
            }

            if (null == parameters.AlternateFilters)
            {
                throw new ArgumentException(SampleServiceResources.ExceptionInvalidParameters);
            }

            if (string.IsNullOrWhiteSpace(parameters.SchemaIdentifier))
            {
                throw new ArgumentException(SampleServiceResources.ExceptionInvalidParameters);
            }

            Resource[] results;
            IFilter    queryFilter           = parameters.AlternateFilters.SingleOrDefault();
            IEnumerable <TargetGroup> buffer = Enumerable.Empty <TargetGroup>();

            // get all users if no filter
            if (queryFilter == null)
            {
                buffer = this._storageService.QueryGroups();
            }
            else
            {
                if (string.IsNullOrWhiteSpace(queryFilter.AttributePath))
                {
                    throw new ArgumentException(SampleServiceResources.ExceptionInvalidParameters);
                }

                if (string.IsNullOrWhiteSpace(queryFilter.ComparisonValue))
                {
                    throw new ArgumentException(SampleServiceResources.ExceptionInvalidParameters);
                }

                if (queryFilter.FilterOperator != ComparisonOperator.Equals)
                {
                    throw new NotSupportedException(SampleServiceResources.UnsupportedComparisonOperator);
                }

                if (queryFilter.AttributePath.Equals(AttributeNames.DisplayName))
                {
                    buffer = this._storageService.QueryGroups(displayName: parameters.AlternateFilters.Single().ComparisonValue);
                }
                else
                {
                    throw new NotSupportedException(SampleServiceResources.UnsupportedFilterAttributeGroup);
                }
            }

            results =
                buffer
                .Select((TargetGroup item) =>
            {
                Core2Group bufferItem = (Core2Group)item;

                if (parameters?.ExcludedAttributePaths?.Any(
                        (string excludedAttributes) =>
                        excludedAttributes.Equals(AttributeNames.Members, StringComparison.OrdinalIgnoreCase))
                    == true)
                {
                    bufferItem.Members = null;
                }

                return(bufferItem);
            })
                .Select((Core2Group item) => item as Resource).ToArray();

            return(Task.FromResult(results));
        }
        public override Task UpdateAsync(IPatch patch, string correlationIdentifier)
        {
            if (null == patch)
            {
                throw new ArgumentNullException(nameof(patch));
            }

            if (null == patch.ResourceIdentifier)
            {
                throw new ArgumentException(SampleServiceResources.ExceptionInvalidPatch);
            }

            if (string.IsNullOrWhiteSpace(patch.ResourceIdentifier.Identifier))
            {
                throw new ArgumentException(SampleServiceResources.ExceptionInvalidPatch);
            }

            if (null == patch.PatchRequest)
            {
                throw new ArgumentException(SampleServiceResources.ExceptionInvalidPatch);
            }

            PatchRequest2 patchRequest = patch.PatchRequest as PatchRequest2;

            if (null == patchRequest)
            {
                string unsupportedPatchTypeName = patch.GetType().FullName;
                throw new NotSupportedException(unsupportedPatchTypeName);
            }

            // call service
            TargetGroup target;

            try
            {
                // get group
                target = _storageService.RetrieveGroup(new Guid(patch.ResourceIdentifier.Identifier));

                // patch group
                Core2Group patched = (Core2Group)target;
                patched.Apply(patchRequest);

                // update user
                _storageService.UpdateGroup((TargetGroup)patched);
            }
            catch (Exception err)
            {
                switch (err.Message)
                {
                case "Conflict":
                    throw new HttpResponseException(HttpStatusCode.Conflict);

                case "NotFound":
                    throw new HttpResponseException(HttpStatusCode.NotFound);

                case "InvalidMemberType":
                    throw new HttpResponseException(HttpStatusCode.NotAcceptable);

                default:
                    throw new HttpResponseException(HttpStatusCode.InternalServerError);
                }
            }

            return(Task.CompletedTask);
        }