Ejemplo n.º 1
0
        public static deleteResponseType CreateDeleteResponse(deleteStatus status)
        {
            deleteResponseType deleteResponse = new deleteResponseType();

            deleteResponse.deletes = new deleteStatus[] { status };
            return(deleteResponse);
        }
Ejemplo n.º 2
0
        public static deleteResponseType CreateDeleteResponse(deleteStatus status)
        {
            var deleteResponse = new deleteResponseType {
                deletes = new[] { status }
            };

            return(deleteResponse);
        }
Ejemplo n.º 3
0
        public static deleteStatus CreateDelete(HttpStatusCode statusCode, string id, errorType error = null)
        {
            deleteStatus status = new deleteStatus();

            status.statusCode = ((int)statusCode).ToString();
            status.id         = id;
            if (error != null)
            {
                status.error = error;
            }
            return(status);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// <see cref="IProvider{TTSingle,TMultiple,TPrimaryKey}.Delete(deleteRequestType, string[], string[])">Delete</see>
        /// </summary>
        public virtual IHttpActionResult Delete(
            deleteRequestType deleteRequest,
            [MatrixParameter] string[] zoneId    = null,
            [MatrixParameter] string[] contextId = null)
        {
            if (!AuthenticationService.VerifyAuthenticationHeader(Request.Headers, out string sessionToken))
            {
                return(Unauthorized());
            }

            // Check ACLs and return StatusCode(HttpStatusCode.Forbidden) if appropriate.
            if (!AuthorisationService.IsAuthorised(Request.Headers, sessionToken, $"{TypeName}s", RightType.DELETE))
            {
                return(StatusCode(HttpStatusCode.Forbidden));
            }

            if ((zoneId != null && zoneId.Length != 1) || (contextId != null && contextId.Length != 1))
            {
                return(BadRequest($"Request failed for object {TypeName} as Zone and/or Context are invalid."));
            }

            ICollection <deleteStatus> deleteStatuses = new List <deleteStatus>();

            try
            {
                foreach (deleteIdType deleteId in deleteRequest.deletes)
                {
                    var status = new deleteStatus
                    {
                        id = deleteId.id
                    };

                    try
                    {
                        RequestParameter[] requestParameters = GetQueryParameters(Request);
                        Service.Delete(deleteId.id, zoneId?[0], contextId?[0], requestParameters);
                        status.statusCode = ((int)HttpStatusCode.NoContent).ToString();
                    }
                    catch (ArgumentException e)
                    {
                        status.error = ProviderUtils.CreateError(
                            HttpStatusCode.BadRequest,
                            TypeName,
                            $"Invalid argument: id={deleteId.id}.\n{e.Message}");
                        status.statusCode = ((int)HttpStatusCode.BadRequest).ToString();
                    }
                    catch (DeleteException e)
                    {
                        status.error = ProviderUtils.CreateError(
                            HttpStatusCode.BadRequest,
                            TypeName,
                            $"Request failed for object {TypeName} with ID of {deleteId.id}.\n{e.Message}");
                        status.statusCode = ((int)HttpStatusCode.BadRequest).ToString();
                    }
                    catch (NotFoundException e)
                    {
                        status.error = ProviderUtils.CreateError(
                            HttpStatusCode.NotFound,
                            TypeName,
                            $"Object {TypeName} with ID of {deleteId.id} not found.\n{e.Message}");
                        status.statusCode = ((int)HttpStatusCode.NotFound).ToString();
                    }
                    catch (Exception e)
                    {
                        status.error = ProviderUtils.CreateError(
                            HttpStatusCode.InternalServerError,
                            TypeName,
                            $"Request failed for object {TypeName} with ID of {deleteId.id}.\n{e.Message}");
                        status.statusCode = ((int)HttpStatusCode.InternalServerError).ToString();
                    }

                    deleteStatuses.Add(status);
                }
            }
            catch (Exception)
            {
                // Need to ignore exceptions otherwise it would not be possible to return status records of processed objects.
            }

            var deleteResponse = new deleteResponseType {
                deletes = deleteStatuses.ToArray()
            };

            return(Ok(deleteResponse));
        }
        public void ExplicitResponseMapperTest()
        {
            // Error.
            ResponseError srcError = new ResponseError {
                Code = 123, Description = "Err desc", Id = "42", Message = "Error occurred", Scope = "request"
            };
            errorType destError = MapperFactory.CreateInstance <ResponseError, errorType>(srcError);

            // Create.
            CreateStatus srcCreateStatus = new CreateStatus {
                AdvisoryId = "src456", Error = srcError, Id = "cr8", StatusCode = "200"
            };
            createType             destCreateStatus  = MapperFactory.CreateInstance <CreateStatus, createType>(srcCreateStatus);
            MultipleCreateResponse srcCreateResponse = new MultipleCreateResponse {
                StatusRecords = new List <CreateStatus> {
                    srcCreateStatus
                }
            };
            createResponseType destCreateResponse = MapperFactory.CreateInstance <MultipleCreateResponse, createResponseType>(srcCreateResponse);
            int index = 0;

            // Assert that the mapping was successful.
            foreach (CreateStatus record in srcCreateResponse.StatusRecords)
            {
                Assert.AreEqual(record.AdvisoryId, destCreateResponse.creates[index].advisoryId);
                Assert.AreEqual(record.Error.Code, destCreateResponse.creates[index].error.code);
                Assert.AreEqual(record.Error.Description, destCreateResponse.creates[index].error.description);
                Assert.AreEqual(record.Error.Id, destCreateResponse.creates[index].error.id);
                Assert.AreEqual(record.Error.Message, destCreateResponse.creates[index].error.message);
                Assert.AreEqual(record.Error.Scope, destCreateResponse.creates[index].error.scope);
                Assert.AreEqual(record.Id, destCreateResponse.creates[index].id);
                Assert.AreEqual(record.StatusCode, destCreateResponse.creates[index].statusCode);
                index++;
            }

            // Delete.
            DeleteStatus srcDeleteStatus = new DeleteStatus {
                Error = srcError, Id = "del8", StatusCode = "300"
            };
            deleteStatus           destDeleteStatus  = MapperFactory.CreateInstance <DeleteStatus, deleteStatus>(srcDeleteStatus);
            MultipleDeleteResponse srcDeleteResponse = new MultipleDeleteResponse {
                StatusRecords = new List <DeleteStatus> {
                    srcDeleteStatus
                }
            };
            deleteResponseType destDeleteResponse = MapperFactory.CreateInstance <MultipleDeleteResponse, deleteResponseType>(srcDeleteResponse);

            index = 0;

            // Assert that the mapping was successful.
            foreach (DeleteStatus record in srcDeleteResponse.StatusRecords)
            {
                Assert.AreEqual(record.Error.Code, destDeleteResponse.deletes[index].error.code);
                Assert.AreEqual(record.Error.Description, destDeleteResponse.deletes[index].error.description);
                Assert.AreEqual(record.Error.Id, destDeleteResponse.deletes[index].error.id);
                Assert.AreEqual(record.Error.Message, destDeleteResponse.deletes[index].error.message);
                Assert.AreEqual(record.Error.Scope, destDeleteResponse.deletes[index].error.scope);
                Assert.AreEqual(record.Id, destDeleteResponse.deletes[index].id);
                Assert.AreEqual(record.StatusCode, destDeleteResponse.deletes[index].statusCode);
                index++;
            }

            // Update.
            UpdateStatus srcUpdateStatus = new UpdateStatus {
                Error = srcError, Id = "up8", StatusCode = "400"
            };
            updateType             destUpdateStatus  = MapperFactory.CreateInstance <UpdateStatus, updateType>(srcUpdateStatus);
            MultipleUpdateResponse srcUpdateResponse = new MultipleUpdateResponse {
                StatusRecords = new List <UpdateStatus> {
                    srcUpdateStatus
                }
            };
            updateResponseType destUpdateResponse = MapperFactory.CreateInstance <MultipleUpdateResponse, updateResponseType>(srcUpdateResponse);

            index = 0;

            // Assert that the mapping was successful.
            foreach (UpdateStatus record in srcUpdateResponse.StatusRecords)
            {
                Assert.AreEqual(record.Error.Code, destUpdateResponse.updates[index].error.code);
                Assert.AreEqual(record.Error.Description, destUpdateResponse.updates[index].error.description);
                Assert.AreEqual(record.Error.Id, destUpdateResponse.updates[index].error.id);
                Assert.AreEqual(record.Error.Message, destUpdateResponse.updates[index].error.message);
                Assert.AreEqual(record.Error.Scope, destUpdateResponse.updates[index].error.scope);
                Assert.AreEqual(record.Id, destUpdateResponse.updates[index].id);
                Assert.AreEqual(record.StatusCode, destUpdateResponse.updates[index].statusCode);
                index++;
            }
        }
        /// <summary>
        /// <see cref="IProvider{TTSingle,TMultiple,TPrimaryKey}.Delete(deleteRequestType, string[], string[])">Delete</see>
        /// </summary>
        public virtual IHttpActionResult Delete(deleteRequestType deleteRequest, [MatrixParameter] string[] zoneId = null, [MatrixParameter] string[] contextId = null)
        {
            if (!authService.VerifyAuthenticationHeader(Request.Headers))
            {
                return(Unauthorized());
            }

            // Check ACLs and return StatusCode(HttpStatusCode.Forbidden) if appropriate.

            if ((zoneId != null && zoneId.Length != 1) || (contextId != null && contextId.Length != 1))
            {
                return(BadRequest("Request failed for object " + typeof(TSingle).Name + " as Zone and/or Context are invalid."));
            }

            IHttpActionResult          result;
            ICollection <deleteStatus> deleteStatuses = new List <deleteStatus>();

            try
            {
                foreach (deleteIdType deleteId in deleteRequest.deletes)
                {
                    deleteStatus status = new deleteStatus();
                    status.id = deleteId.id;

                    try
                    {
                        service.Delete(deleteId.id, zoneId: (zoneId == null ? null : zoneId[0]), contextId: (contextId == null ? null : contextId[0]));
                        status.statusCode = ((int)HttpStatusCode.NoContent).ToString();
                    }
                    catch (ArgumentException e)
                    {
                        status.error      = ProviderUtils.CreateError(HttpStatusCode.BadRequest, typeof(TSingle).Name, "Invalid argument: id=" + deleteId.id + ".\n" + e.Message);
                        status.statusCode = ((int)HttpStatusCode.BadRequest).ToString();
                    }
                    catch (DeleteException e)
                    {
                        status.error      = ProviderUtils.CreateError(HttpStatusCode.BadRequest, typeof(TSingle).Name, "Request failed for object " + typeof(TSingle).Name + " with ID of " + deleteId.id + ".\n " + e.Message);
                        status.statusCode = ((int)HttpStatusCode.BadRequest).ToString();
                    }
                    catch (NotFoundException e)
                    {
                        status.error      = ProviderUtils.CreateError(HttpStatusCode.NotFound, typeof(TSingle).Name, "Object " + typeof(TSingle).Name + " with ID of " + deleteId.id + " not found.\n" + e.Message);
                        status.statusCode = ((int)HttpStatusCode.NotFound).ToString();
                    }
                    catch (Exception e)
                    {
                        status.error      = ProviderUtils.CreateError(HttpStatusCode.InternalServerError, typeof(TSingle).Name, "Request failed for object " + typeof(TSingle).Name + " with ID of " + deleteId.id + ".\n " + e.Message);
                        status.statusCode = ((int)HttpStatusCode.InternalServerError).ToString();
                    }

                    deleteStatuses.Add(status);
                }
            }
            catch (Exception)
            {
                // Need to ignore exceptions otherwise it would not be possible to return status records of processed objects.
            }

            deleteResponseType deleteResponse = new deleteResponseType {
                deletes = deleteStatuses.ToArray()
            };

            result = Ok(deleteResponse);

            return(result);
        }