/// <summary> /// Constructor with specified controller name /// </summary> public MapToControllerAttribute(string controllerName, InteractingType iType, uint itemsPerPage = 0, bool allowAnonimus = true) { ControllerName = controllerName; InteractingType = iType; ItemsPerPage = itemsPerPage; AllowAnonimus = allowAnonimus; }
private static RequestDelegate GetHandler<T, TE>( Dictionary<string,List<AuthorizeAttribute>> restrictions, Dictionary<Type, EntityKeyDescribtion> entityKeys, DatabaseTypes dbType, string connString, InteractingType interactingType, bool allowAnonimus, string authentificationPath, string accessDeniedPath, Dictionary<string,RequestParamName> _requestParams, JsonSerializerOptions jsonSerializerOptions = null, Func<T> customDbContextFactory = null ) where T : DbContext, IDisposable where TE : class { return async (context) => { bool authResult = Authorization<TE>(context, HttpMethod.Get, restrictions, allowAnonimus,authentificationPath,accessDeniedPath); if (!authResult) { return; } var e = entityKeys; var QueryParams = RequestParams.RetriveQueryParam(context.Request.Query, _requestParams); IEnumerable<TE> queryResult; using (T dbcontext = CreateContext<T>(connString, dbType, customDbContextFactory)) { queryResult = GetDBQueryResult<T, TE>(dbcontext, QueryParams); } if (interactingType == InteractingType.JSON) { byte[] jsonUtf8Bytes; jsonUtf8Bytes = JsonSerializer.SerializeToUtf8Bytes(queryResult, jsonSerializerOptions); await context.Response.WriteAsync(System.Text.Encoding.UTF8.GetString(jsonUtf8Bytes)); } else if (interactingType == InteractingType.XML) { XmlRootAttribute a = new XmlRootAttribute("result"); // XmlSerializer does not support IEnumerable<T> XmlSerializer serializer = new XmlSerializer(typeof(List<TE>),a); StringWriter textWriter = new StringWriter(); serializer.Serialize(textWriter,queryResult.ToList()); await context.Response.WriteAsync(textWriter.ToString()); await textWriter.DisposeAsync(); } }; }
private static RequestDelegate DeleteHandler<T, TE>( Dictionary<string,List<AuthorizeAttribute>> restrictions, DatabaseTypes dbType, string connString, InteractingType interactingType, string authentificationPath, string accessDeniedPath, JsonSerializerOptions jsonSerializerOptions = null, MethodInfo dbContextBeforeSaveChangesMethod = null, Func<T> customDbContextFactory = null ) where T : DbContext, IDisposable where TE : class { return async (context) => { bool authResult = Authorization<TE>(context, HttpMethod.Delete, restrictions, false,authentificationPath,accessDeniedPath); if (!authResult) { return; } TE recivedData; List<TE> recivedDataList; var mi = GetActionBeforeDelete<TE>(); string Reason =""; using (T dbcontext = CreateContext<T>(connString, dbType, customDbContextFactory)) { if (interactingType == InteractingType.JSON) { using (var reader = new StreamReader(context.Request.Body)) { var body = await reader.ReadToEndAsync(); try { recivedData = JsonSerializer.Deserialize<TE>(body); if (recivedData != null) { if (CheckAllowed<T, TE>(dbcontext, recivedData, mi, out Reason)) { dbcontext.Set<TE>().Remove(recivedData); DoBeforeContextSaveChanges<T>(dbContextBeforeSaveChangesMethod, dbcontext); await dbcontext.SaveChangesAsync(); await context.Response.WriteAsync("Deleted"); } else { await context.Response.WriteAsync(Reason); } } } catch { // It can be array try { recivedDataList = JsonSerializer.Deserialize<List<TE>>(body); if (recivedDataList != null) { if (CheckAllowedList<T, TE>(dbcontext, recivedDataList, mi, out Reason)) { dbcontext.Set<TE>().RemoveRange(recivedDataList); DoBeforeContextSaveChanges<T>(dbContextBeforeSaveChangesMethod, dbcontext); await dbcontext.SaveChangesAsync(); await context.Response.WriteAsync("Deleted"); } else { await context.Response.WriteAsync(Reason); } } } catch { } } } } else if (interactingType == InteractingType.XML) { using (var reader = new StreamReader(context.Request.Body)) { var body = await reader.ReadToEndAsync(); Stream stream = GenerateStreamFromString(body); XmlRootAttribute a = new XmlRootAttribute("result"); XmlSerializer serializer = new XmlSerializer(typeof(List<TE>),a); var recivedDataL = (List<TE>)serializer.Deserialize(stream); await stream.DisposeAsync(); if (recivedDataL != null && recivedDataL.Count > 0) { if (CheckAllowedList<T, TE>(dbcontext, recivedDataL, mi, out Reason)) { dbcontext.Set<TE>().RemoveRange(recivedDataL); DoBeforeContextSaveChanges<T>(dbContextBeforeSaveChangesMethod, dbcontext); await dbcontext.SaveChangesAsync(); await context.Response.WriteAsync("Deleted"); } else { await context.Response.WriteAsync(Reason); } } // Do something } } } }; }