add basic paper model/view/controller
All checks were successful
Go / build (push) Successful in 12m50s
All checks were successful
Go / build (push) Successful in 12m50s
paper weight is missing
This commit is contained in:
@@ -15,6 +15,12 @@ type ConfigController interface {
|
|||||||
AddConfigHandler(*gin.Context)
|
AddConfigHandler(*gin.Context)
|
||||||
ConfigHandler(*gin.Context)
|
ConfigHandler(*gin.Context)
|
||||||
ConfigView(*gin.Context)
|
ConfigView(*gin.Context)
|
||||||
|
|
||||||
|
GetAllPaper(*gin.Context)
|
||||||
|
PaperView(*gin.Context)
|
||||||
|
PaperHandler(*gin.Context)
|
||||||
|
AddPaperHandler(*gin.Context)
|
||||||
|
|
||||||
CreateTag(*gin.Context)
|
CreateTag(*gin.Context)
|
||||||
GetAllTags(*gin.Context)
|
GetAllTags(*gin.Context)
|
||||||
TagView(*gin.Context)
|
TagView(*gin.Context)
|
||||||
@@ -108,6 +114,99 @@ func (rc *configController) ConfigHandler(ctx *gin.Context) {
|
|||||||
rc.ConfigView(ctx)
|
rc.ConfigView(ctx)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (rc *configController) PaperHandler(ctx *gin.Context) {
|
||||||
|
newPaper, err := models.NewPaper(ctx)
|
||||||
|
action := ctx.PostForm("action")
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println(err)
|
||||||
|
ctx.HTML(http.StatusBadRequest, "paperview.html", gin.H{"error": err})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
paper, err := repositories.Papers.GetById(ctx.Param("id"))
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println(err)
|
||||||
|
ctx.HTML(http.StatusBadRequest, "paperview.html", gin.H{"error": err})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if action == "update" {
|
||||||
|
paper.Name = newPaper.Name
|
||||||
|
paper.Brand = newPaper.Brand
|
||||||
|
paper.Size = newPaper.Size
|
||||||
|
paper.Price = newPaper.Price
|
||||||
|
paper, err = repositories.Papers.Update(paper)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println(err)
|
||||||
|
ctx.HTML(http.StatusBadRequest, "paperview.html", gin.H{"error": err})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if action == "delete" {
|
||||||
|
repositories.Papers.DeleteById(ctx.Param("id"))
|
||||||
|
}
|
||||||
|
|
||||||
|
rc.PaperView(ctx)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (rc *configController) AddPaperHandler(c *gin.Context) {
|
||||||
|
paper, err := models.NewPaper(c)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println(err)
|
||||||
|
c.HTML(http.StatusBadRequest, "paperview.html", gin.H{"error": err})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err = repositories.Papers.Create(paper)
|
||||||
|
if err != nil {
|
||||||
|
data := CreateSessionData(c, gin.H{
|
||||||
|
"error": err,
|
||||||
|
"success": "",
|
||||||
|
})
|
||||||
|
|
||||||
|
c.HTML(http.StatusOK, "paperview.html", data)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
rc.PaperView(c)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (rc *configController) PaperView(c *gin.Context) {
|
||||||
|
papers, err := repositories.Papers.GetAll()
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
c.HTML(http.StatusBadRequest, "paperview.html", gin.H{"data": gin.H{"error": err}})
|
||||||
|
}
|
||||||
|
|
||||||
|
data := CreateSessionData(c, gin.H{
|
||||||
|
"paper": papers,
|
||||||
|
})
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
c.HTML(http.StatusBadRequest, "paperview.html", data)
|
||||||
|
}
|
||||||
|
|
||||||
|
c.HTML(http.StatusOK, "paperview.html", data)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (rc *configController) GetAllPaper(c *gin.Context) {
|
||||||
|
papers, err := repositories.Papers.GetAll()
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
ReplyError(c, fmt.Errorf("Could not query Papers"))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
c.JSON(http.StatusOK, papers)
|
||||||
|
}
|
||||||
|
|
||||||
|
//////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
func (rc *configController) TagHandler(ctx *gin.Context) {
|
func (rc *configController) TagHandler(ctx *gin.Context) {
|
||||||
name := ctx.PostForm("name")
|
name := ctx.PostForm("name")
|
||||||
color := ctx.PostForm("color")
|
color := ctx.PostForm("color")
|
||||||
|
|||||||
6
main.go
6
main.go
@@ -76,6 +76,12 @@ func main() {
|
|||||||
viewRoutes.POST("/tags/:id", authValidator.RequireAdmin, configController.TagHandler)
|
viewRoutes.POST("/tags/:id", authValidator.RequireAdmin, configController.TagHandler)
|
||||||
viewRoutes.GET("/tags/:id", userController.TagView)
|
viewRoutes.GET("/tags/:id", userController.TagView)
|
||||||
viewRoutes.POST("/tags", authValidator.RequireAdmin, configController.AddTagHandler)
|
viewRoutes.POST("/tags", authValidator.RequireAdmin, configController.AddTagHandler)
|
||||||
|
|
||||||
|
viewRoutes.GET("/paper", authValidator.RequireAdmin, configController.PaperView)
|
||||||
|
viewRoutes.POST("/paper/:id", authValidator.RequireAdmin, configController.PaperHandler)
|
||||||
|
viewRoutes.GET("/paper/:id", userController.TagView)
|
||||||
|
viewRoutes.POST("/paper", authValidator.RequireAdmin, configController.AddPaperHandler)
|
||||||
|
|
||||||
viewRoutes.GET("/cart", authValidator.RequireAuth, cartItemController.CartItemView)
|
viewRoutes.GET("/cart", authValidator.RequireAuth, cartItemController.CartItemView)
|
||||||
viewRoutes.POST("/cart", authValidator.RequireAuth, cartItemController.AddItemHandler)
|
viewRoutes.POST("/cart", authValidator.RequireAuth, cartItemController.AddItemHandler)
|
||||||
viewRoutes.POST("/cart/delete", authValidator.RequireAuth, cartItemController.DeleteItemHandler)
|
viewRoutes.POST("/cart/delete", authValidator.RequireAuth, cartItemController.DeleteItemHandler)
|
||||||
|
|||||||
74
models/paper.go
Normal file
74
models/paper.go
Normal file
@@ -0,0 +1,74 @@
|
|||||||
|
package models
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
|
"gorm.io/gorm"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
type PaperSize string
|
||||||
|
|
||||||
|
const (
|
||||||
|
A3 PaperSize = "A3"
|
||||||
|
A4 PaperSize = "A4"
|
||||||
|
A5 PaperSize = "A5"
|
||||||
|
SRA3 PaperSize = "SRA3"
|
||||||
|
)
|
||||||
|
|
||||||
|
func ParseSize(s string) (c PaperSize, err error) {
|
||||||
|
s = strings.ToUpper(s)
|
||||||
|
if s == "A3" {
|
||||||
|
return A3, nil
|
||||||
|
} else if s == "A4" {
|
||||||
|
return A4, nil
|
||||||
|
} else if s == "A5" {
|
||||||
|
return A5, nil
|
||||||
|
} else if s == "SRA3" {
|
||||||
|
return SRA3, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
return c, fmt.Errorf("Cannot parse category %s", s)
|
||||||
|
}
|
||||||
|
|
||||||
|
type Paper struct {
|
||||||
|
gorm.Model
|
||||||
|
Name string `json:"name" binding:"required" gorm:"not null"`
|
||||||
|
Brand string `json:"brand" binding:"required"`
|
||||||
|
Size PaperSize `json:"size" binding:"required"`
|
||||||
|
Price float64 `json:"price" binding:"required"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewPaper(ctx *gin.Context) (Paper, error) {
|
||||||
|
name := ctx.PostForm("name")
|
||||||
|
brand := ctx.PostForm("brand")
|
||||||
|
sizeTmp := ctx.PostForm("size")
|
||||||
|
priceTmp := ctx.PostForm("price")
|
||||||
|
|
||||||
|
price, err := strconv.ParseFloat(priceTmp, 64)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return Paper{}, fmt.Errorf("Couldnt parse Price")
|
||||||
|
}
|
||||||
|
|
||||||
|
size, err := ParseSize(sizeTmp)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return Paper{}, fmt.Errorf("Couldnt parse Size")
|
||||||
|
}
|
||||||
|
|
||||||
|
if name == "" || brand == "" {
|
||||||
|
return Paper{}, fmt.Errorf("Name or brand empty")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Convert the price string to float64
|
||||||
|
tag := Paper{
|
||||||
|
Name: name,
|
||||||
|
Brand: brand,
|
||||||
|
Size: size,
|
||||||
|
Price: price,
|
||||||
|
}
|
||||||
|
|
||||||
|
return tag, nil
|
||||||
|
}
|
||||||
82
repositories/paperRepository.go
Normal file
82
repositories/paperRepository.go
Normal file
@@ -0,0 +1,82 @@
|
|||||||
|
package repositories
|
||||||
|
|
||||||
|
import (
|
||||||
|
"strconv"
|
||||||
|
|
||||||
|
"gorm.io/gorm"
|
||||||
|
|
||||||
|
"git.dynamicdiscord.de/kalipso/zineshop/models"
|
||||||
|
)
|
||||||
|
|
||||||
|
type PaperRepository interface {
|
||||||
|
Create(models.Paper) (models.Paper, error)
|
||||||
|
GetAll() ([]models.Paper, error)
|
||||||
|
GetById(string) (models.Paper, error)
|
||||||
|
//GetByShopItemId(string) (models.Paper, error)
|
||||||
|
Update(models.Paper) (models.Paper, error)
|
||||||
|
DeleteById(string) error
|
||||||
|
}
|
||||||
|
|
||||||
|
type GORMPaperRepository struct {
|
||||||
|
DB *gorm.DB
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewGORMPaperRepository(db *gorm.DB) PaperRepository {
|
||||||
|
return &GORMPaperRepository{
|
||||||
|
DB: db,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *GORMPaperRepository) Create(tag models.Paper) (models.Paper, error) {
|
||||||
|
result := t.DB.Create(&tag)
|
||||||
|
|
||||||
|
if result.Error != nil {
|
||||||
|
return models.Paper{}, result.Error
|
||||||
|
}
|
||||||
|
|
||||||
|
return tag, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *GORMPaperRepository) GetAll() ([]models.Paper, error) {
|
||||||
|
var tags []models.Paper
|
||||||
|
result := t.DB.Find(&tags)
|
||||||
|
|
||||||
|
return tags, result.Error
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *GORMPaperRepository) GetById(id string) (models.Paper, error) {
|
||||||
|
tagId, err := strconv.Atoi(id)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return models.Paper{}, err
|
||||||
|
}
|
||||||
|
|
||||||
|
var tag models.Paper
|
||||||
|
result := t.DB.First(&tag, uint(tagId))
|
||||||
|
|
||||||
|
if result.Error != nil {
|
||||||
|
return models.Paper{}, result.Error
|
||||||
|
}
|
||||||
|
|
||||||
|
return tag, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *GORMPaperRepository) Update(tag models.Paper) (models.Paper, error) {
|
||||||
|
result := t.DB.Save(&tag)
|
||||||
|
if result.Error != nil {
|
||||||
|
return models.Paper{}, result.Error
|
||||||
|
}
|
||||||
|
|
||||||
|
return tag, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *GORMPaperRepository) DeleteById(id string) error {
|
||||||
|
tagId, err := strconv.Atoi(id)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
result := t.DB.Delete(&models.Paper{}, tagId)
|
||||||
|
return result.Error
|
||||||
|
}
|
||||||
@@ -16,6 +16,7 @@ var (
|
|||||||
Orders OrderRepository
|
Orders OrderRepository
|
||||||
Tokens RegisterTokenRepository
|
Tokens RegisterTokenRepository
|
||||||
ConfigOptions ConfigRepository
|
ConfigOptions ConfigRepository
|
||||||
|
Papers PaperRepository
|
||||||
)
|
)
|
||||||
|
|
||||||
func InitRepositories() {
|
func InitRepositories() {
|
||||||
@@ -45,4 +46,5 @@ func InitRepositories() {
|
|||||||
Orders = NewGORMOrderRepository(db)
|
Orders = NewGORMOrderRepository(db)
|
||||||
Tokens = NewGORMRegisterTokenRepository(db)
|
Tokens = NewGORMRegisterTokenRepository(db)
|
||||||
ConfigOptions = NewGORMConfigRepository(db)
|
ConfigOptions = NewGORMConfigRepository(db)
|
||||||
|
Papers = NewGORMPaperRepository(db)
|
||||||
}
|
}
|
||||||
|
|||||||
37
views/paperview.html
Normal file
37
views/paperview.html
Normal file
@@ -0,0 +1,37 @@
|
|||||||
|
{{ template "header.html" . }}
|
||||||
|
|
||||||
|
|
||||||
|
<div class="flex min-h-full flex-col justify-center px-6 py-12 lg:px-8">
|
||||||
|
<div class="sm:mx-auto sm:w-full sm:max-w-sm">
|
||||||
|
<img class="mx-auto h-10 w-auto" src="/static/img/logo-black.png" alt="Your Company">
|
||||||
|
<h2 class="mt-10 text-center text-2xl/9 font-bold tracking-tight text-gray-900">Edit Paper</h2>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="mt-10 sm:mx-auto sm:w-full sm:max-w-sm">
|
||||||
|
{{ range .data.paper }}
|
||||||
|
<form action="/paper/{{ .ID }}" method="POST">
|
||||||
|
<div class="max-w-md mx-auto mt-4">
|
||||||
|
<div class="flex">
|
||||||
|
|
||||||
|
<input type="text" id="name" name="name" value="{{ .Name }}" class="flex-grow border border-gray-300 rounded-l-md p-2 focus:outline-none focus:ring focus:ring-blue-500">
|
||||||
|
<input type="text" id="brand" name="brand" value="{{ .Brand }}" class="flex-grow border border-gray-300 rounded-l-md p-2 focus:outline-none focus:ring focus:ring-blue-500">
|
||||||
|
<input type="text" id="size" name="size" value="{{ .Size }}" class="flex-grow border border-gray-300 rounded-l-md p-2 focus:outline-none focus:ring focus:ring-blue-500">
|
||||||
|
<input type="number" step="0.01" min="0.00" id="price" name="price" value="{{ .Price }}" class="flex-grow border border-gray-300 rounded-l-md p-2 focus:outline-none focus:ring focus:ring-blue-500">
|
||||||
|
<button type="submit" name="action" value="update" class="bg-blue-600 text-white ml-4 mr-4 rounded px-4 hover:bg-blue-700">Update</button>
|
||||||
|
<button type="submit" name="action" value="delete" class="bg-red-800 text-white rounded px-4 hover:bg-red-900">Delete</button>
|
||||||
|
</div>
|
||||||
|
</form>
|
||||||
|
{{ end }}
|
||||||
|
<form action="/paper" method="POST">
|
||||||
|
<div class="max-w-md mx-auto mt-4">
|
||||||
|
<div class="flex">
|
||||||
|
<input type="text" id="name" name="name" placeholder="name" class="flex-grow border border-gray-300 rounded-l-md p-2 focus:outline-none focus:ring focus:ring-blue-500">
|
||||||
|
<input type="text" id="brand" name="brand" placeholder="brand" class="flex-grow border border-gray-300 rounded-l-md p-2 focus:outline-none focus:ring focus:ring-blue-500">
|
||||||
|
<input type="text" id="size" name="size" placeholder="size" class="flex-grow border border-gray-300 rounded-l-md p-2 focus:outline-none focus:ring focus:ring-blue-500">
|
||||||
|
<input type="number" step="0.01" min="0.00" id="price" name="price" placeholder="price per sheet" class="flex-grow border border-gray-300 rounded-l-md p-2 focus:outline-none focus:ring focus:ring-blue-500">
|
||||||
|
<button type="submit" class="bg-green-600 text-white ml-4 mr-4 rounded px-4 hover:bg-green-700">Add</button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</form>
|
||||||
|
</div>
|
||||||
|
{{ template "footer.html" . }}
|
||||||
Reference in New Issue
Block a user