Skip to content

Commit

Permalink
add users form
Browse files Browse the repository at this point in the history
  • Loading branch information
darcys22 committed Nov 13, 2021
1 parent abba6e8 commit 5ca8de2
Show file tree
Hide file tree
Showing 18 changed files with 195 additions and 119 deletions.
6 changes: 6 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,14 @@ build-go: ## Build all Go binaries.
@echo "build go files"
$(GO) run build.go build

build-debug-go: ## Build all Go binaries.
@echo "build debug go files"
$(GO) run build.go debug

build: build-go

debug: build-debug-go

run: ## Run Server
@GO111MODULE=on ./bin/linux-amd64/godbledger-web

Expand Down
3 changes: 2 additions & 1 deletion backend/api/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ package api

import (
m "github.com/darcys22/godbledger-web/backend/models"
"github.com/darcys22/godbledger-web/backend/auth"
"github.com/gin-gonic/gin"
"net/http"
)

func NewUser(c *gin.Context) {
func NewUser(ctx *gin.Context) {
var new_user m.PostNewUserCommand

if err := ctx.BindJSON(&new_user); err != nil {
Expand Down
14 changes: 8 additions & 6 deletions backend/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,17 @@ var log = logrus.WithField("prefix", "API")

func mapStatic(m *gin.Engine, dir string, prefix string) {
headers := func() gin.HandlerFunc {
return func(c *gin.Context) {
c.Writer.Header().Set("Cache-Control", "public, max-age=3600")
c.Next()
return func(ctx *gin.Context) {
ctx.Writer.Header().Set("Cache-Control", "public, max-age=3600")
ctx.Next()
}
}

if setting.Env == setting.DEV {
headers = func() gin.HandlerFunc {
return func(c *gin.Context) {
c.Writer.Header().Set("Cache-Control", "max-age=0, must-revalidate, no-cache")
c.Next()
return func(ctx *gin.Context) {
ctx.Writer.Header().Set("Cache-Control", "max-age=0, must-revalidate, no-cache")
ctx.Next()
}
}
}
Expand Down Expand Up @@ -102,6 +102,8 @@ func NewGin() *gin.Engine {

m.LoadHTMLGlob(path.Join(setting.StaticRootPath, "views/*.html"))

InitLoginHandler()
InitUsersDatabase()
register(m)

return m
Expand Down
32 changes: 18 additions & 14 deletions backend/api/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,34 @@ import (
"net/http"
)

func Index(c *gin.Context) {
c.HTML(http.StatusOK, "index.html", nil)
func Index(ctx *gin.Context) {
ctx.HTML(http.StatusOK, "index.html", nil)
}

func Reports(c *gin.Context) {
c.HTML(http.StatusOK, "reports.html", nil)
func Reports(ctx *gin.Context) {
ctx.HTML(http.StatusOK, "reports.html", nil)
}

func Reconcile(c *gin.Context) {
c.HTML(http.StatusOK, "reconcile.html", nil)
func Reconcile(ctx *gin.Context) {
ctx.HTML(http.StatusOK, "reconcile.html", nil)
}

func Accounts(c *gin.Context) {
c.HTML(http.StatusOK, "accounts.html", nil)
func Accounts(ctx *gin.Context) {
ctx.HTML(http.StatusOK, "accounts.html", nil)
}

func Modules(c *gin.Context) {
c.HTML(http.StatusOK, "modules.html", nil)
func Modules(ctx *gin.Context) {
ctx.HTML(http.StatusOK, "modules.html", nil)
}

func User(c *gin.Context) {
c.HTML(http.StatusOK, "user.html", nil)
func User(ctx *gin.Context) {
ctx.HTML(http.StatusOK, "user.html", nil)
}

func Admin(c *gin.Context) {
c.HTML(http.StatusOK, "admin.html", nil)
func Admin(ctx *gin.Context) {
ctx.HTML(http.StatusOK, "admin.html", nil)
}

func LoginView(ctx *gin.Context) {
ctx.HTML(http.StatusOK, "login.view.html", nil)
}
46 changes: 23 additions & 23 deletions backend/api/journal.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,66 +6,66 @@ import (
"net/http"
)

func GetJournals(c *gin.Context) {
func GetJournals(ctx *gin.Context) {
journalsModel := m.NewJournalsListing()
err := journalsModel.SearchJournals()
if err != nil {
log.Errorf("Could not get journal listing (%v)", err)
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
ctx.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
}
c.JSON(http.StatusOK, journalsModel)
ctx.JSON(http.StatusOK, journalsModel)
}

func PostJournal(c *gin.Context) {
func PostJournal(ctx *gin.Context) {
var journal m.PostJournalCommand

if err := c.BindJSON(&journal); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
if err := ctx.BindJSON(&journal); err != nil {
ctx.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}

if err := journal.Save(); err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
ctx.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
}
c.JSON(http.StatusOK, journal)
ctx.JSON(http.StatusOK, journal)
}

func DeleteJournal(c *gin.Context) {
id := c.Params.ByName("id")
func DeleteJournal(ctx *gin.Context) {
id := ctx.Params.ByName("id")

if err := m.DeleteJournalCommand(id); err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
ctx.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
}
c.String(http.StatusOK, "Success")
ctx.String(http.StatusOK, "Success")
}

func GetJournal(c *gin.Context) {
id := c.Params.ByName("id")
func GetJournal(ctx *gin.Context) {
id := ctx.Params.ByName("id")

journal, err := m.GetJournalCommand(id)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
ctx.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
}

c.JSON(http.StatusOK, journal)
ctx.JSON(http.StatusOK, journal)
}

func EditJournal(c *gin.Context) {
id := c.Params.ByName("id")
func EditJournal(ctx *gin.Context) {
id := ctx.Params.ByName("id")
var journal m.PostJournalCommand

if err := c.BindJSON(&journal); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
if err := ctx.BindJSON(&journal); err != nil {
ctx.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}

if err := journal.Save(); err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
ctx.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
}

if err := m.DeleteJournalCommand(id); err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
ctx.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
}

c.JSON(http.StatusOK, journal)
ctx.JSON(http.StatusOK, journal)
}
32 changes: 8 additions & 24 deletions backend/api/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,24 @@ import (
"net/url"

"github.com/darcys22/godbledger-web/backend/auth"
"github.com/darcys22/godbledger-web/backend/setting"
)

var (
loginService auth.LoginService = auth.StaticLoginService()
jwtService auth.JWTService = auth.JWTAuthService()
loginController LoginController = LoginHandler(loginService, jwtService)
loginService auth.LoginService
jwtService auth.JWTService
loginController LoginController
)

type LoginController struct {
loginService auth.LoginService
jwtService auth.JWTService
}

func LoginHandler(loginService auth.LoginService,
jwtService auth.JWTService) LoginController {
return LoginController{
func InitLoginHandler() {
loginService = auth.StaticLoginService(setting.GetConfig())
jwtService = auth.JWTAuthService()
loginController = LoginController{
loginService: loginService,
jwtService: jwtService,
}
Expand All @@ -46,24 +48,6 @@ func (controller *LoginController) Login(ctx *gin.Context) string {
return ""
}

func (controller *LoginController) NewUser(ctx *gin.Context) string {
var credential LoginCredentials
err := ctx.ShouldBind(&credential)
if err != nil {
return "no data found"
}
//isUserAuthenticated := controller.loginService.NewUser(credential.Email, credential.Password)
//if isUserAuthenticated {
//return controller.jwtService.GenerateToken(credential.Email, true)

//}
return ""
}

func LoginView(ctx *gin.Context) {
ctx.HTML(http.StatusOK, "login.view.html", nil)
}

func Login(ctx *gin.Context) {
token := loginController.Login(ctx)
if token != "" {
Expand Down
14 changes: 7 additions & 7 deletions backend/api/reconcile.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,22 @@ import (
"github.com/gin-gonic/gin"
)

func GetExternalAccountListing(c *gin.Context) {
m.GetExternalAccountListing(c)
func GetExternalAccountListing(ctx *gin.Context) {
m.GetExternalAccountListing(ctx)
}

func GetUnreconciledTransactions(c *gin.Context) {
func GetUnreconciledTransactions(ctx *gin.Context) {
var request m.UnreconciledTransactionsRequest

if err := c.BindJSON(&request); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
if err := ctx.BindJSON(&request); err != nil {
ctx.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}

err, unreconciledTransactionsResult := m.UnreconciledTransactions(request)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
ctx.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
}

c.JSON(http.StatusOK, unreconciledTransactionsResult)
ctx.JSON(http.StatusOK, unreconciledTransactionsResult)
}
7 changes: 6 additions & 1 deletion backend/api/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,21 @@ import (
"github.com/darcys22/godbledger-web/backend/auth"
m "github.com/darcys22/godbledger-web/backend/models"
"github.com/darcys22/godbledger-web/backend/models/sqlite"
"github.com/darcys22/godbledger-web/backend/setting"

"github.com/gin-gonic/gin"
"github.com/dgrijalva/jwt-go"

)

var (
users sqlite.UserModel = sqlite.New("sqlite.db")
users sqlite.UserModel
)

func InitUsersDatabase() {
users = sqlite.New("sqlite.db", setting.GetConfig())
}

func respondWithError(ctx *gin.Context, message interface{}) {
log.Debugf("Error processing JWT: %v", message)
ctx.Abort()
Expand Down
13 changes: 3 additions & 10 deletions backend/auth/LoginService.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/dgrijalva/jwt-go"
"github.com/darcys22/godbledger-web/backend/models/sqlite"
"github.com/darcys22/godbledger-web/backend/setting"
)

type LoginService interface {
Expand All @@ -16,8 +17,8 @@ type loginInformation struct {
users *sqlite.UserModel
}

func StaticLoginService() LoginService {
database := sqlite.New("sqlite.db")
func StaticLoginService(cfg *setting.Cfg) LoginService {
database := sqlite.New("sqlite.db", cfg)

return &loginInformation{users: &database}
}
Expand All @@ -30,14 +31,6 @@ func (info *loginInformation) LoginUser(email string, password string) bool {
return true
}

func (info *loginInformation) NewUser(email string, password string) bool {
//_, err := info.users.New(email, password)
//if err != nil {
//return false
//}
return true
}

//JWT service
type JWTService interface {
GenerateToken(email string, isUser bool) string
Expand Down
Loading

0 comments on commit 5ca8de2

Please sign in to comment.