Skip to content

Commit

Permalink
#39 Fixed Delete Issue (#40)
Browse files Browse the repository at this point in the history
Co-authored-by: Emrah Kondur <[email protected]>
  • Loading branch information
ekondur and Emrah Kondur authored Jun 14, 2024
1 parent d5d7900 commit c2a052d
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 39 deletions.
12 changes: 12 additions & 0 deletions RedisUI/Models/LayoutModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System.Collections.Generic;

namespace RedisUI.Models
{
public class LayoutModel
{
public string Section { get; set; }
public string DbSize { get; set; }
public int CurrentDb { get; set; }
public List<string> DbList { get; set; }
}
}
7 changes: 7 additions & 0 deletions RedisUI/Models/PostModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace RedisUI.Models
{
internal class PostModel
{
public string DelKey { get; set; }
}
}
34 changes: 13 additions & 21 deletions RedisUI/Pages/Layout.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
using RedisUI.Contents;
using RedisUI.Models;
using System.Text;

namespace RedisUI.Pages
{
public static class Layout
{
public static string Build(string section, string dbSize, int db, RedisUISettings settings)
public static string Build(LayoutModel model, RedisUISettings settings)
{
var dbList = new StringBuilder();
foreach (var item in model.DbList)
{
dbList.Append($"<li><a class=\"dropdown-item\" id=\"nav{item}\" href=\"javascript:setdb({item});\">{item}</a></li>");
}

return $@"<!DOCTYPE html>
<html lang=""en"">
<head>
Expand Down Expand Up @@ -78,29 +86,14 @@ function setSize(size){{
<ul class=""navbar-nav me-auto mb-2 mb-lg-0"">
<a class=""navbar-brand"" title=""Keys"">
{Icons.KeyLg}
{dbSize}
{model.DbSize}
</a>
<li class=""nav-item dropdown"">
<a id=""dblink"" class=""nav-link dropdown-toggle"" href=""#"" role=""button"" data-bs-toggle=""dropdown"" aria-expanded=""false"">
DB ({db})
DB ({model.CurrentDb})
</a>
<ul class=""dropdown-menu"">
<li><a class=""dropdown-item"" id=""nav0"" href=""javascript:setdb(0);"">0</a></li>
<li><a class=""dropdown-item"" id=""nav1"" href=""javascript:setdb(1);"">1</a></li>
<li><a class=""dropdown-item"" id=""nav2"" href=""javascript:setdb(2);"">2</a></li>
<li><a class=""dropdown-item"" id=""nav3"" href=""javascript:setdb(3);"">3</a></li>
<li><a class=""dropdown-item"" id=""nav4"" href=""javascript:setdb(4);"">4</a></li>
<li><a class=""dropdown-item"" id=""nav5"" href=""javascript:setdb(5);"">5</a></li>
<li><a class=""dropdown-item"" id=""nav6"" href=""javascript:setdb(6);"">6</a></li>
<li><a class=""dropdown-item"" id=""nav7"" href=""javascript:setdb(7);"">7</a></li>
<li><a class=""dropdown-item"" id=""nav8"" href=""javascript:setdb(8);"">8</a></li>
<li><a class=""dropdown-item"" id=""nav9"" href=""javascript:setdb(9);"">9</a></li>
<li><a class=""dropdown-item"" id=""nav10"" href=""javascript:setdb(10);"">10</a></li>
<li><a class=""dropdown-item"" id=""nav11"" href=""javascript:setdb(11);"">11</a></li>
<li><a class=""dropdown-item"" id=""nav12"" href=""javascript:setdb(12);"">12</a></li>
<li><a class=""dropdown-item"" id=""nav13"" href=""javascript:setdb(13);"">13</a></li>
<li><a class=""dropdown-item"" id=""nav14"" href=""javascript:setdb(14);"">14</a></li>
<li><a class=""dropdown-item"" id=""nav15"" href=""javascript:setdb(15);"">15</a></li>
{dbList}
</ul>
</li>
</ul>
Expand All @@ -113,7 +106,7 @@ function setSize(size){{
<div class=""container"">
<br/>
{section}
{model.Section}
</div>
<div class=""container"">
Expand All @@ -128,7 +121,6 @@ function setSize(size){{
</html>
";


}
}
}
23 changes: 20 additions & 3 deletions RedisUI/Pages/Main.cs
Original file line number Diff line number Diff line change
Expand Up @@ -182,11 +182,13 @@ function confirmDelete(del){{
let currentSize = 10;
let currentKey = '';
let currentDb = 0;
let currentPage = 0;
var searchParams = new URLSearchParams(window.location.search);
var paramDb = searchParams.get('db');
var paramKey = searchParams.get('key');
var paramSize = searchParams.get('size');
var paramPage = searchParams.get('page');
if (paramDb) {{
currentDb = paramDb;
Expand All @@ -200,13 +202,28 @@ function confirmDelete(del){{
currentSize = paramSize;
}}
if (paramPage) {{
currentPage = paramPage;
}}
var currentPath = window.location.href.replace(window.location.search, '');
newQueryString = ""&db="" + currentDb + ""&size="" + currentSize + ""&key="" + currentKey + ""&del="" + del;
newQueryString = ""&db="" + currentDb + ""&size="" + currentSize + ""&key="" + currentKey + ""&page="" + currentPage;
newUrl = currentPath + (currentPath.indexOf('?') !== -1 ? '&' : '?') + newQueryString;
window.location = newUrl.replace('#', '');
fetch(newUrl, {{
method: 'POST',
body: JSON.stringify({{
DelKey: del,
}}),
headers: {{
'Content-type': 'application/json; charset=UTF-8'
}}
}})
.then(function(response) {{
window.location = newUrl.replace('#', '');
}});
}}
}};
Expand Down
53 changes: 38 additions & 15 deletions RedisUI/RedisUIMiddleware.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
using RedisUI.Models;
using RedisUI.Helpers;
using System;
using System.IO;
using System.Text.Json;

namespace RedisUI
{
Expand Down Expand Up @@ -50,17 +52,24 @@ public async Task InvokeAsync(HttpContext context)

var dbSize = await redisDb.ExecuteAsync("DBSIZE");

if (context.Request.Path.ToString() == $"{_settings.Path}/statistics")
var keyspace = await redisDb.ExecuteAsync("INFO", "KEYSPACE");
var keyspaces = keyspace
.ToString()
.Replace("# Keyspace", "")
.Split(new string[] { "\r\n" }, StringSplitOptions.None)
.Where(item => !string.IsNullOrEmpty(item))
.Select(KeyspaceModel.Instance)
.ToList();

var layoutModel = new LayoutModel
{
var keyspace = await redisDb.ExecuteAsync("INFO", "KEYSPACE");
var keyspaces = keyspace
.ToString()
.Replace("# Keyspace", "")
.Split(new string[] { "\r\n" }, StringSplitOptions.None)
.Where(item => !string.IsNullOrEmpty(item))
.Select(item => KeyspaceModel.Instance(item))
.ToList();
DbList = keyspaces.Select(x => x.Db).ToList(),
CurrentDb = currentDb,
DbSize = dbSize.ToString()
};

if (context.Request.Path.ToString() == $"{_settings.Path}/statistics")
{
var serverInfo = await redisDb.ExecuteAsync("INFO", "SERVER");
var memoryInfo = await redisDb.ExecuteAsync("INFO", "MEMORY");
var statsInfo = await redisDb.ExecuteAsync("INFO", "STATS");
Expand All @@ -75,7 +84,9 @@ public async Task InvokeAsync(HttpContext context)
AllInfo = allInfo.ToString().ToInfo()
};

await context.Response.WriteAsync(Layout.Build(Statistics.Build(model), dbSize.ToString(), currentDb, _settings));
layoutModel.Section = Statistics.Build(model);

await context.Response.WriteAsync(Layout.Build(layoutModel, _settings));
return;
}

Expand All @@ -86,11 +97,21 @@ public async Task InvokeAsync(HttpContext context)

var searchKey = context.Request.Query["key"].ToString();

var delKey = context.Request.Query["del"].ToString();

if (!string.IsNullOrEmpty(delKey))
context.Request.EnableBuffering();
context.Request.Body.Seek(0, SeekOrigin.Begin);
using (var stream = new StreamReader(context.Request.Body))
{
await redisDb.ExecuteAsync("DEL", delKey);
string body = await stream.ReadToEndAsync();

if (!string.IsNullOrEmpty(body))
{
var postModel = JsonSerializer.Deserialize<PostModel>(body);

if (postModel != null)
{
await redisDb.ExecuteAsync("DEL", postModel.DelKey);
}
}
}

RedisResult result;
Expand Down Expand Up @@ -142,7 +163,9 @@ public async Task InvokeAsync(HttpContext context)
}
}

await context.Response.WriteAsync(Layout.Build(Main.Build(keys, keys.Count > 0 ? long.Parse((string)innerResult[0]) : 0), dbSize.ToString(), currentDb, _settings));
layoutModel.Section = Main.Build(keys, keys.Count > 0 ? long.Parse((string)innerResult[0]) : 0);

await context.Response.WriteAsync(Layout.Build(layoutModel, _settings));
}
}
}

0 comments on commit c2a052d

Please sign in to comment.