Try to implement new transaction
This commit is contained in:
parent
4d1b883974
commit
adce350e0c
86
http/http.go
86
http/http.go
@ -14,6 +14,8 @@ import (
|
|||||||
"git.javil.eu/jacob1123/budgeteer/web"
|
"git.javil.eu/jacob1123/budgeteer/web"
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
|
"github.com/google/uuid"
|
||||||
|
"github.com/jackc/pgtype"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Handler handles incoming requests
|
// Handler handles incoming requests
|
||||||
@ -44,26 +46,98 @@ func (h *Handler) Serve() {
|
|||||||
router.GET("/", func(c *gin.Context) { c.HTML(http.StatusOK, "index", nil) })
|
router.GET("/", func(c *gin.Context) { c.HTML(http.StatusOK, "index", nil) })
|
||||||
router.GET("/login", h.login)
|
router.GET("/login", h.login)
|
||||||
router.GET("/register", h.register)
|
router.GET("/register", h.register)
|
||||||
router.GET("/dashboard", h.dashboard)
|
authenticatedFrontend := router.Group("")
|
||||||
router.GET("/budget/:budgetid", h.budget)
|
{
|
||||||
|
authenticatedFrontend.Use(h.verifyLoginWithRedirect)
|
||||||
|
authenticatedFrontend.GET("/dashboard", h.dashboard)
|
||||||
|
authenticatedFrontend.GET("/budget/:budgetid", h.budget)
|
||||||
|
}
|
||||||
api := router.Group("/api/v1")
|
api := router.Group("/api/v1")
|
||||||
{
|
{
|
||||||
user := api.Group("/user")
|
user := api.Group("/user")
|
||||||
{
|
{
|
||||||
user.GET("/logout", logout)
|
unauthenticated := user.Group("")
|
||||||
user.GET("/login", func(c *gin.Context) { c.Redirect(http.StatusPermanentRedirect, "/login") })
|
{
|
||||||
user.POST("/login", h.loginPost)
|
unauthenticated.GET("/login", func(c *gin.Context) { c.Redirect(http.StatusPermanentRedirect, "/login") })
|
||||||
user.POST("/register", h.registerPost)
|
unauthenticated.POST("/login", h.loginPost)
|
||||||
|
unauthenticated.POST("/register", h.registerPost)
|
||||||
|
}
|
||||||
|
authenticated := user.Group("")
|
||||||
|
{
|
||||||
|
authenticated.Use(h.verifyLoginWithRedirect)
|
||||||
|
authenticated.GET("/logout", logout)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
budget := api.Group("/budget")
|
budget := api.Group("/budget")
|
||||||
{
|
{
|
||||||
|
budget.Use(h.verifyLoginWithRedirect)
|
||||||
budget.POST("/new", h.newBudget)
|
budget.POST("/new", h.newBudget)
|
||||||
}
|
}
|
||||||
|
transaction := api.Group("/transaction")
|
||||||
|
{
|
||||||
|
transaction.Use(h.verifyLoginWithRedirect)
|
||||||
|
transaction.POST("/new", h.newTransaction)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
router.Run(":1323")
|
router.Run(":1323")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (h *Handler) verifyLoginWithRedirect(c *gin.Context) {
|
||||||
|
_, err := h.verifyLogin(c)
|
||||||
|
if err != nil {
|
||||||
|
c.Redirect(http.StatusTemporaryRedirect, "/login")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
c.Next()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *Handler) newTransaction(c *gin.Context) {
|
||||||
|
|
||||||
|
transactionMemo, succ := c.GetPostForm("memo")
|
||||||
|
if !succ {
|
||||||
|
c.AbortWithStatus(http.StatusNotAcceptable)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
transactionAccount, succ := c.GetPostForm("account_id")
|
||||||
|
if !succ {
|
||||||
|
c.AbortWithStatus(http.StatusNotAcceptable)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
transactionAccountID, err := uuid.Parse(transactionAccount)
|
||||||
|
if !succ {
|
||||||
|
c.AbortWithStatus(http.StatusNotAcceptable)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
transactionDate, succ := c.GetPostForm("date")
|
||||||
|
if !succ {
|
||||||
|
c.AbortWithStatus(http.StatusNotAcceptable)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
transactionDateValue, err := time.Parse("2006-01-02", transactionDate)
|
||||||
|
if err != nil {
|
||||||
|
c.AbortWithStatus(http.StatusNotAcceptable)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
new := postgres.CreateTransactionParams{
|
||||||
|
Memo: transactionMemo,
|
||||||
|
Date: transactionDateValue,
|
||||||
|
Amount: pgtype.Numeric{},
|
||||||
|
AccountID: transactionAccountID,
|
||||||
|
}
|
||||||
|
_, err = h.Service.DB.CreateTransaction(c.Request.Context(), new)
|
||||||
|
if err != nil {
|
||||||
|
c.AbortWithError(http.StatusInternalServerError, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func (h *Handler) newBudget(c *gin.Context) {
|
func (h *Handler) newBudget(c *gin.Context) {
|
||||||
token, err := h.verifyLogin(c)
|
token, err := h.verifyLogin(c)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -25,7 +25,7 @@
|
|||||||
</tr>
|
</tr>
|
||||||
{{end}}
|
{{end}}
|
||||||
</table>
|
</table>
|
||||||
{{template "budget-new"}}
|
{{template "transaction-new"}}
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
{{end}}
|
{{end}}
|
44
web/transaction-new.html
Normal file
44
web/transaction-new.html
Normal file
@ -0,0 +1,44 @@
|
|||||||
|
{{define "transaction-new"}}
|
||||||
|
<div id="newbudgetmodal" class="modal fade">
|
||||||
|
<div class="modal-dialog" role="document">
|
||||||
|
<script>
|
||||||
|
$(document).ready(function () {
|
||||||
|
$('#errorcreatingbudget').hide();
|
||||||
|
$('#newbudgetform').ajaxForm({
|
||||||
|
error: function() {
|
||||||
|
$('#errorcreatingbudget').show();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
</script>
|
||||||
|
<div class="modal-content">
|
||||||
|
<div class="modal-header">
|
||||||
|
<h5 class="modal-title">New Transaction</h5>
|
||||||
|
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
|
||||||
|
<span aria-hidden="true">×</span>
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
<form id="newbudgetform" action="/api/v1/transaction/new" method="POST">
|
||||||
|
<div class="modal-body">
|
||||||
|
<div class="form-group">
|
||||||
|
<label for="date">Date</label>
|
||||||
|
<input type="date" name="date" class="form-control" value="{{.Now}}" />
|
||||||
|
</div>
|
||||||
|
<div class="form-group">
|
||||||
|
<label for="memo">Memo</label>
|
||||||
|
<input type="text" name="memo" class="form-control" />
|
||||||
|
</div>
|
||||||
|
<div class="form-group">
|
||||||
|
<label for="amount">Amount</label>
|
||||||
|
<input type="number" name="amount" class="form-control" placeholder="0.00" />
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<div class="modal-footer">
|
||||||
|
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
|
||||||
|
<input type="submit" class="btn btn-primary" value="Create" class="form-control" />
|
||||||
|
</div>
|
||||||
|
</form>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
{{end}}
|
Loading…
x
Reference in New Issue
Block a user