From 547fce395416c0083d955849d78dd77d250deaff Mon Sep 17 00:00:00 2001 From: Bekir Pehlivan Date: Wed, 18 Oct 2023 17:25:58 +0300 Subject: [PATCH] Added table view for the user --- controller/admin_controller.go | 17 +++++++------ controller/login_controller.go | 14 +++++------ controller/search_controller.go | 22 +++++++++++++---- internal/search.go | 13 ++++++---- internal/sync_loop.go | 4 +-- templates/admin_navbar.html | 2 +- templates/index.html | 44 +++++++++++++++------------------ templates/table_user.html | 14 +++++++++++ 8 files changed, 79 insertions(+), 51 deletions(-) create mode 100644 templates/table_user.html diff --git a/controller/admin_controller.go b/controller/admin_controller.go index 78ceed0..f5874d8 100644 --- a/controller/admin_controller.go +++ b/controller/admin_controller.go @@ -1,7 +1,10 @@ package controller import ( + "net/http" + "github.com/gin-gonic/gin" + ginsession "github.com/go-session/gin-session" ) type TableData struct { @@ -9,13 +12,13 @@ type TableData struct { } func HandleAdmin(ctx *gin.Context) { - // store := ginsession.FromContext(ctx) - // _, ok := store.Get("user") - // if !ok { - // // User is not logged in, redirect them to the login page - // ctx.Redirect(http.StatusTemporaryRedirect, "/login") - // return - // } + store := ginsession.FromContext(ctx) + _, ok := store.Get("user") + if !ok { + // User is not logged in, redirect them to the login page + ctx.Redirect(http.StatusTemporaryRedirect, "/login") + return + } ctx.HTML(200, "admin.html", nil) } diff --git a/controller/login_controller.go b/controller/login_controller.go index 214b318..27f1d9d 100644 --- a/controller/login_controller.go +++ b/controller/login_controller.go @@ -10,13 +10,13 @@ import ( ) func HandleLogin(ctx *gin.Context) { - // store := ginsession.FromContext(ctx) - // _, ok := store.Get("user") - // if ok { - // // User is not logged in, redirect them to the login page - // ctx.Redirect(http.StatusTemporaryRedirect, "/") - // return - // } + store := ginsession.FromContext(ctx) + _, ok := store.Get("user") + if ok { + // User is not logged in, redirect them to the login page + ctx.Redirect(http.StatusTemporaryRedirect, "/") + return + } ctx.HTML(200, "login.html", nil) } diff --git a/controller/search_controller.go b/controller/search_controller.go index bc5eda2..72ed194 100644 --- a/controller/search_controller.go +++ b/controller/search_controller.go @@ -8,13 +8,25 @@ import ( func HandleSearch(ctx *gin.Context) { searchQuery := ctx.PostForm("search-input") + exact := ctx.Query("exact") + var data internal.PageData - if searchQuery == "" { - HandleQueue(ctx) - return - } + if exact == "true" { + + sourceCreds := internal.Credentials{ + Server: ctx.Query("source_server"), + Account: ctx.Query("source_account"), + } + destCreds := internal.Credentials{ + Server: ctx.Query("destination_server"), + Account: ctx.Query("destination_account"), + } - data := internal.GetSearchData(searchQuery) + data = internal.GetSearchData("", true, sourceCreds, destCreds) + } else { + + data = internal.GetSearchData(searchQuery, false, internal.Credentials{}, internal.Credentials{}) + } ctx.HTML(200, "tbody.html", data) } diff --git a/internal/search.go b/internal/search.go index 4946be0..b6282c7 100644 --- a/internal/search.go +++ b/internal/search.go @@ -7,8 +7,13 @@ import ( "github.com/lithammer/fuzzysearch/fuzzy" ) -func GetSearchData(searchQuery string) PageData { - results := searchInQueue(searchQuery) +func GetSearchData(searchQuery string, isExact bool, sourceDetails, destinationDetails Credentials) PageData { + var results []*Task + if isExact { + results = searchExactCredentials(sourceDetails, destinationDetails) + } else { + results = searchInQueue(searchQuery) + } data := PageData{ Index: 1, @@ -69,10 +74,8 @@ func searchExactCredentials(sourceDetails, destinationDetails Credentials) []*Ta task := e.Value.(*Task) if task.SourceAccount == sourceDetails.Account && task.SourceServer == sourceDetails.Server && - task.SourcePassword == sourceDetails.Password && task.DestinationAccount == destinationDetails.Account && - task.DestinationServer == destinationDetails.Server && - task.DestinationPassword == destinationDetails.Password { + task.DestinationServer == destinationDetails.Server { results = append(results, task) } } diff --git a/internal/sync_loop.go b/internal/sync_loop.go index 2e6e4b4..c59c7c3 100644 --- a/internal/sync_loop.go +++ b/internal/sync_loop.go @@ -16,8 +16,8 @@ func processPendingTasks() { continue } - //syncIMAP(task) - simulateTask(task) + syncIMAP(task) + // simulateTask(task) time.Sleep(100 * time.Millisecond) } } diff --git a/templates/admin_navbar.html b/templates/admin_navbar.html index c1affd5..a8eb95e 100644 --- a/templates/admin_navbar.html +++ b/templates/admin_navbar.html @@ -6,7 +6,7 @@ diff --git a/templates/index.html b/templates/index.html index 17c360e..9825843 100644 --- a/templates/index.html +++ b/templates/index.html @@ -64,12 +64,12 @@

Source Details

-
-
@@ -88,13 +88,13 @@

Destination Details

-
-
@@ -121,22 +121,9 @@

User queue

form="credentials_form">
- - - - - - - - - - - - - - - -
IndexSource ServerSource AccountDestination ServerDestination AccountStatusActions
+ + {{template "table_user.html"}} +
@@ -226,20 +213,29 @@

User queue

document.addEventListener('alpine:init', () => { Alpine.data('data', () => ({ + source_server: '', + source_account: '', + destination_server: '', + destination_account: '', source_validated: false, destination_validated: false, + showQueue(){ + if (this.source_validated === true && this.destination_validated === true) { + htmx.ajax('POST', "/api/search?exact=true&source_server="+this.source_server+"&source_account="+this.source_account+"&destination_server="+this.destination_server+"&destination_account="+this.destination_account+"", {target:'#table-body',swap:'innerHTML'}) + } + }, update_source_status(result) { - this.source_validated = result + this.source_validated = result; + this.showQueue(); }, update_destination_status(result) { - this.destination_validated = result + this.destination_validated = result; + this.showQueue(); }, })) }) - - \ No newline at end of file diff --git a/templates/table_user.html b/templates/table_user.html new file mode 100644 index 0000000..3f64268 --- /dev/null +++ b/templates/table_user.html @@ -0,0 +1,14 @@ + + + Index + Source Server + Source Account + Destination Server + Destination Account + Status + Actions + + + + {{template "tbody.html" .}} + \ No newline at end of file