Exemplo n.º 1
0
        protected IHttpResponse GetResponseData <T>(IHttpResponse response, CRUD.CrudTypes crudFlags)
        {
            Ensure.ArgumentNotNull(nameof(response), response);

            if (response.IsError)
            {
                response = ProcessResponse(response);
                return(response);
            }

            Ensure.ArgumentNotNull(nameof(response.Data), response.Data);

            string data = (string)response.Data;

            //To handle the return of raw data without throwing exception
            if (typeof(T) == typeof(string))
            {
                return(new HttpResponse(HttpStatusCode.OK, data, response.ResponseHeaders));
            }

            if (crudFlags.HasFlag(CRUD.CrudTypes.Create) || crudFlags.HasFlag(CRUD.CrudTypes.Delete))
            {
                var converted = JsonConvert.DeserializeObject <T>(data, JsonHelpers.DefaultSerializerSettings);
                return(new HttpResponse(HttpStatusCode.OK, converted, response.ResponseHeaders));
            }

            // Default Read
            var jo   = JObject.Parse(data);
            var path = jo.First.Path;

            if ("Items".Equals(path, StringComparison.CurrentCultureIgnoreCase))
            {
                var convertedItems = JsonConvert.DeserializeObject <TargetProcessReadResponseWrapper <T> >
                                         (data, JsonHelpers.DefaultSerializerSettings).Items;
                return(new HttpResponse(HttpStatusCode.OK, convertedItems, response.ResponseHeaders));
            }
            var convertedItem = JsonConvert.DeserializeObject <T>(data, JsonHelpers.DefaultSerializerSettings);

            return(new HttpResponse(HttpStatusCode.OK, convertedItem, response.ResponseHeaders));
        }
Exemplo n.º 2
0
        private static string GetMessageForEntity <T>(CRUD.CrudTypes crudFlags)
        {
            var crudAttribute = GetCannotCreateReadUpdateDeleteAttribute <T>() ?? GetCrudBaseAttribute <T>();

            if (!crudAttribute.CanCreate && !crudAttribute.CanDelete && !crudAttribute.CanRead && !crudAttribute.CanUpdate)
            {
                return(CrudMessages.AllProhibited);
            }

            if (crudFlags.HasFlag(CRUD.CrudTypes.All)) //TODO check this or change to admin
            {
                return(CrudMessages.AdminProhibited);
            }

            if (crudFlags.HasFlag(CRUD.CrudTypes.Create) && !crudAttribute.CanCreate)
            {
                return(CrudMessages.CreateProhibited);
            }

            if (crudFlags.HasFlag(CRUD.CrudTypes.Read) && !crudAttribute.CanRead)
            {
                return(CrudMessages.ReadProhibited);
            }

            if (crudFlags.HasFlag(CRUD.CrudTypes.Update) && !crudAttribute.CanUpdate)
            {
                return(CrudMessages.UpdateProhibited);
            }

            if (crudFlags.HasFlag(CRUD.CrudTypes.Delete) && !crudAttribute.CanDelete)
            {
                return(CrudMessages.DeleteProhibited);
            }



            return(CrudMessages.UnknownError);
        }
Exemplo n.º 3
0
        private static IRequiredDataResponse VerifyEntityProperties(Entity entity, CRUD.CrudTypes crudFlags)
        {
            Ensure.ArgumentNotNull(nameof(entity), entity);

            if (crudFlags.HasFlag(CRUD.CrudTypes.Create))
            {
                return(VerifyEntityPropertiesForCreate(entity));
            }
            if (crudFlags.HasFlag(CRUD.CrudTypes.Read))
            {
                return(VerifyEntityPropertiesForRead(entity));
            }
            if (crudFlags.HasFlag(CRUD.CrudTypes.Update))
            {
                return(VerifyEntityPropertiesForUpdate(entity));
            }
            if (crudFlags.HasFlag(CRUD.CrudTypes.Delete))
            {
                return(VerifyEntityPropertiesForDelete(entity));
            }
            return(new RequiredDataResponse {
                IsError = true, Message = CrudMessages.UnknownError
            });
        }
Exemplo n.º 4
0
        private static bool VerifyEntity <T>(CRUD.CrudTypes crudFlags)
        {
            var crudAttribute = GetCannotCreateReadUpdateDeleteAttribute <T>() ?? GetCrudBaseAttribute <T>();

            if (crudFlags.HasFlag(CRUD.CrudTypes.Create))
            {
                return(crudAttribute?.CanCreate ?? true);
            }

            if (crudFlags.HasFlag(CRUD.CrudTypes.Read))
            {
                return(crudAttribute.CanRead);
            }
            if (crudFlags.HasFlag(CRUD.CrudTypes.Update))
            {
                return(crudAttribute.CanUpdate);
            }

            if (crudFlags.HasFlag(CRUD.CrudTypes.Delete))
            {
                return(crudAttribute.CanDelete);
            }
            return(false);
        }