diff --git a/internal/dao/events.sql.go b/internal/dao/events.sql.go index c88c3a5..3b5289b 100644 --- a/internal/dao/events.sql.go +++ b/internal/dao/events.sql.go @@ -117,7 +117,7 @@ func (q *Queries) GetEvents(ctx context.Context, ids []int64) ([]Event, error) { const getEventsForCallsign = `-- name: GetEventsForCallsign :many SELECT id, created, stream_id, account_id, event_type, event_data FROM events -WHERE event_type = ?1 +WHERE event_type = ?1 AND json_extract(event_data, '$.callsign') = ?2 ` diff --git a/internal/handlers/account.go b/internal/handlers/account.go index 9e741fa..76024ee 100644 --- a/internal/handlers/account.go +++ b/internal/handlers/account.go @@ -8,6 +8,7 @@ import ( validator "github.com/go-playground/validator/v10" "github.com/ryanfaerman/netctl/internal/middleware" + "github.com/ryanfaerman/netctl/internal/models" "github.com/ryanfaerman/netctl/internal/services" "github.com/ryanfaerman/netctl/internal/views" "github.com/ryanfaerman/netctl/web/named" @@ -29,6 +30,7 @@ func (h account) Routes(r chi.Router) { }) r.Get(named.Route("account-profile", "/profile/{callsign}"), h.Show) + r.Get(named.Route("account-profile-self", "/profile"), h.Show) r.Get(named.Route("account-edit", "/profile/{callsign}/edit"), h.Edit) r.Post(named.Route("account-edit-save", "/profile/{callsign}/edit/-/save"), h.Update) } @@ -87,19 +89,32 @@ func (h account) Setup(w http.ResponseWriter, r *http.Request) { } func (h account) Show(w http.ResponseWriter, r *http.Request) { + var account *models.Account + callsign := chi.URLParam(r, "callsign") - ctx := services.CSRF.GetContext(r.Context(), r) + if callsign == "" { + a, err := services.Session.GetAccount(r.Context()) + if err != nil { + ErrorHandler(err)(w, r) + return + } + account = a + // get account from session + } else { + a, err := services.Account.FindByCallsign(r.Context(), callsign) + if err != nil { + ErrorHandler(err)(w, r) + return + } + account = a - a, err := services.Account.FindByCallsign(ctx, callsign) - if err != nil { - ErrorHandler(err)(w, r) - return } + ctx := services.CSRF.GetContext(r.Context(), r) - a.About = services.Markdown.MustRenderString(a.About) + account.About = services.Markdown.MustRenderString(account.About) v := views.Account{ - Account: a, + Account: account, } v.Profile().Render(ctx, w) } diff --git a/internal/middleware/path.go b/internal/middleware/path.go new file mode 100644 index 0000000..3ceebee --- /dev/null +++ b/internal/middleware/path.go @@ -0,0 +1,19 @@ +package middleware + +import ( + "fmt" + "net/http" + + "github.com/davecgh/go-spew/spew" + "github.com/go-chi/chi" + "github.com/ryanfaerman/netctl/web/named" +) + +func Path(next http.Handler) http.Handler { + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + c := chi.RouteContext(r.Context()) + spew.Dump(c.RoutePattern(), c.URLParams) + fmt.Println("lookup", named.Lookup(c.RoutePattern())) + next.ServeHTTP(w, r) + }) +} diff --git a/internal/services/session.go b/internal/services/session.go index 7ecdfe9..f42cec7 100644 --- a/internal/services/session.go +++ b/internal/services/session.go @@ -8,7 +8,6 @@ import ( "fmt" "net/http" - "github.com/davecgh/go-spew/spew" "github.com/mrz1836/postmark" "github.com/ryanfaerman/netctl/config" "github.com/ryanfaerman/netctl/internal/models" @@ -137,9 +136,7 @@ func (session) Destroy(ctx context.Context) error { return nil } -var ( - ErrNoAccountInSession = errors.New("no account in session") -) +var ErrNoAccountInSession = errors.New("no account in session") func (session) GetAccount(ctx context.Context) (*models.Account, error) { id, ok := global.session.Get(ctx, "account_id").(int64) @@ -154,7 +151,5 @@ func (s session) MustGetAccount(ctx context.Context) *models.Account { if err != nil { panic(err) } - spew.Dump(u) - spew.Dump(u.Ready()) return u } diff --git a/internal/sql/queries/events.sql b/internal/sql/queries/events.sql index bfc1d53..d15f9ee 100644 --- a/internal/sql/queries/events.sql +++ b/internal/sql/queries/events.sql @@ -40,5 +40,5 @@ DELETE FROM events_recovery WHERE id = ?1; -- name: GetEventsForCallsign :many SELECT * FROM events -WHERE event_type = ?1 +WHERE event_type = ?1 AND json_extract(event_data, '$.callsign') = @callsign; diff --git a/internal/views/account.templ b/internal/views/account.templ index d939161..7ddaa5d 100644 --- a/internal/views/account.templ +++ b/internal/views/account.templ @@ -7,11 +7,17 @@ type Account struct { Account *models.Account } +func (v Account) Nav() Menu { + return Menu{ + {Value: "Profile", URL: named.URLFor("account-profile", v.Account.Callsign().Call), Icon: IconAttrs{Name: "file-lines"}}, + {Value: "Billing", URL: "#", Icon: IconAttrs{Name: "list-check"}}, + {Value: "Reports", URL: "#", Icon: IconAttrs{Name: "receipt"}}, + {Value: "Settings", URL: "#", Icon: IconAttrs{Name: "sliders"}}, + } +} + templ (v Account) Profile() { - @Page() { -
- @v.LocalBar() -
+ @Page(v.Nav()) {

{ v.Account.Name }

@@ -28,10 +34,7 @@ templ (v Account) Profile() { } templ (v Account) Edit() { - @Page() { -
- @v.LocalBar() -
+ @Page(v.Nav()) {
"Editing { v.Account.Name }!" @@ -90,52 +93,3 @@ templ (v Account) EditFormWithErrors(input AccountEditFormInput, inputErrs Accou }) } } - -templ (v Account) LocalBar() { - -} diff --git a/internal/views/account_templ.go b/internal/views/account_templ.go index 2cbf173..47d7023 100644 --- a/internal/views/account_templ.go +++ b/internal/views/account_templ.go @@ -17,6 +17,15 @@ type Account struct { Account *models.Account } +func (v Account) Nav() Menu { + return Menu{ + {Value: "Profile", URL: named.URLFor("account-profile", v.Account.Callsign().Call), Icon: IconAttrs{Name: "file-lines"}}, + {Value: "Billing", URL: "#", Icon: IconAttrs{Name: "list-check"}}, + {Value: "Reports", URL: "#", Icon: IconAttrs{Name: "receipt"}}, + {Value: "Settings", URL: "#", Icon: IconAttrs{Name: "sliders"}}, + } +} + func (v Account) Profile() templ.Component { return templ.ComponentFunc(func(ctx context.Context, templ_7745c5c3_W io.Writer) (templ_7745c5c3_Err error) { templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templ_7745c5c3_W.(*bytes.Buffer) @@ -36,22 +45,14 @@ func (v Account) Profile() templ.Component { templ_7745c5c3_Buffer = templ.GetBuffer() defer templ.ReleaseBuffer(templ_7745c5c3_Buffer) } - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("
") - if templ_7745c5c3_Err != nil { - return templ_7745c5c3_Err - } - templ_7745c5c3_Err = v.LocalBar().Render(ctx, templ_7745c5c3_Buffer) - if templ_7745c5c3_Err != nil { - return templ_7745c5c3_Err - } - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("

") + _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("

") if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } var templ_7745c5c3_Var3 string templ_7745c5c3_Var3, templ_7745c5c3_Err = templ.JoinStringErrs(v.Account.Name) if templ_7745c5c3_Err != nil { - return templ.Error{Err: templ_7745c5c3_Err, FileName: `internal/views/account.templ`, Line: 16, Col: 24} + return templ.Error{Err: templ_7745c5c3_Err, FileName: `internal/views/account.templ`, Line: 22, Col: 24} } _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var3)) if templ_7745c5c3_Err != nil { @@ -101,7 +102,7 @@ func (v Account) Profile() templ.Component { } return templ_7745c5c3_Err }) - templ_7745c5c3_Err = Page().Render(templ.WithChildren(ctx, templ_7745c5c3_Var2), templ_7745c5c3_Buffer) + templ_7745c5c3_Err = Page(v.Nav()).Render(templ.WithChildren(ctx, templ_7745c5c3_Var2), templ_7745c5c3_Buffer) if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } @@ -131,15 +132,7 @@ func (v Account) Edit() templ.Component { templ_7745c5c3_Buffer = templ.GetBuffer() defer templ.ReleaseBuffer(templ_7745c5c3_Buffer) } - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("
") - if templ_7745c5c3_Err != nil { - return templ_7745c5c3_Err - } - templ_7745c5c3_Err = v.LocalBar().Render(ctx, templ_7745c5c3_Buffer) - if templ_7745c5c3_Err != nil { - return templ_7745c5c3_Err - } - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("
") + _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("
") if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } @@ -151,7 +144,7 @@ func (v Account) Edit() templ.Component { var templ_7745c5c3_Var10 string templ_7745c5c3_Var10, templ_7745c5c3_Err = templ.JoinStringErrs(v.Account.Name) if templ_7745c5c3_Err != nil { - return templ.Error{Err: templ_7745c5c3_Err, FileName: `internal/views/account.templ`, Line: 36, Col: 29} + return templ.Error{Err: templ_7745c5c3_Err, FileName: `internal/views/account.templ`, Line: 39, Col: 29} } _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var10)) if templ_7745c5c3_Err != nil { @@ -202,7 +195,7 @@ func (v Account) Edit() templ.Component { } return templ_7745c5c3_Err }) - templ_7745c5c3_Err = Page().Render(templ.WithChildren(ctx, templ_7745c5c3_Var8), templ_7745c5c3_Buffer) + templ_7745c5c3_Err = Page(v.Nav()).Render(templ.WithChildren(ctx, templ_7745c5c3_Var8), templ_7745c5c3_Buffer) if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } @@ -322,105 +315,3 @@ func (v Account) EditFormWithErrors(input AccountEditFormInput, inputErrs Accoun return templ_7745c5c3_Err }) } - -func (v Account) LocalBar() templ.Component { - return templ.ComponentFunc(func(ctx context.Context, templ_7745c5c3_W io.Writer) (templ_7745c5c3_Err error) { - templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templ_7745c5c3_W.(*bytes.Buffer) - if !templ_7745c5c3_IsBuffer { - templ_7745c5c3_Buffer = templ.GetBuffer() - defer templ.ReleaseBuffer(templ_7745c5c3_Buffer) - } - ctx = templ.InitializeContext(ctx) - templ_7745c5c3_Var18 := templ.GetChildren(ctx) - if templ_7745c5c3_Var18 == nil { - templ_7745c5c3_Var18 = templ.NopComponent - } - ctx = templ.ClearChildren(ctx) - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("
") - if templ_7745c5c3_Err != nil { - return templ_7745c5c3_Err - } - if !templ_7745c5c3_IsBuffer { - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteTo(templ_7745c5c3_W) - } - return templ_7745c5c3_Err - }) -} diff --git a/internal/views/dashboard.templ b/internal/views/dashboard.templ index e49e347..c5e5c1c 100644 --- a/internal/views/dashboard.templ +++ b/internal/views/dashboard.templ @@ -11,10 +11,12 @@ type Dashboard struct { } templ (v Dashboard) Authenticated() { - @Page() { - if v.Ready != nil { + if v.Ready != nil { + @BarePage() { @v.SetupAccount() - } else { + } + } else { + @Page(Menu{}) {