Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/update products #53

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
14 changes: 1 addition & 13 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,25 +1,13 @@
language: go
go:
- "1.12.1"
- "1.14"

services:
- docker

notifications:
email: false

env:
- DEP_VERSION="0.5.0"

before_install:
# Download the binary to bin folder in $GOPATH
- curl -L -s https://github.com/golang/dep/releases/download/v${DEP_VERSION}/dep-linux-amd64 -o $GOPATH/bin/dep
# Make the binary executable
- chmod +x $GOPATH/bin/dep

install:
- dep ensure

script:
- go test -v -race ./...

Expand Down
19 changes: 11 additions & 8 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
FROM golang AS builder
FROM golang:1.14 AS builder

WORKDIR $GOPATH/src/github.com/freitagsrunde/k4ever-backend/
COPY . .
ENV GO111MODULE=on

RUN curl -L -s https://github.com/golang/dep/releases/download/v0.5.0/dep-linux-amd64 -o /go/bin/dep && \
go get -u github.com/go-swagger/go-swagger/cmd/swagger && \
RUN mkdir k4ever
WORKDIR /k4ever
COPY go.mod go.sum ./
RUN go mod download && \
go get -u github.com/gobuffalo/packr/packr && \
chmod +x /go/bin/dep && \
dep ensure && \
go generate && \
go get -u github.com/go-swagger/go-swagger/cmd/swagger

COPY . .

RUN go generate && \
packr && \
CGO_ENABLED=0 GOOS=linux go build -a -tags netgo -ldflags "-w -extldflags -static -X github.com/freitagsrunde/k4ever-backend/internal/context.GitCommit=$(git rev-parse HEAD) -X github.com/freitagsrunde/k4ever-backend/internal/context.GitBranch=$(git rev-parse --abbrev-ref HEAD) -X github.com/freitagsrunde/k4ever-backend/internal/context.BuildTime=$(date -u --iso-8601=seconds ) -X github.com/freitagsrunde/k4ever-backend/internal/context.version=0.0.1" -o /go/bin/k4ever

Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
VERSION=0.0.1
VERSION=0.0.2

all: build

Expand Down
31 changes: 31 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
module github.com/freitagsrunde/k4ever-backend

go 1.14

require (
github.com/appleboy/gin-jwt/v2 v2.6.3
github.com/denisenkom/go-mssqldb v0.0.0-20200206145737-bbfc9a55622e // indirect
github.com/erikstmartin/go-testdb v0.0.0-20160219214506-8d10e4a1bae5 // indirect
github.com/gin-contrib/cors v0.0.0-20170318125340-cf4846e6a636
github.com/gin-gonic/gin v1.5.0
github.com/go-sql-driver/mysql v1.5.0 // indirect
github.com/gobuffalo/packr v1.30.1
github.com/gofrs/uuid v3.2.0+incompatible // indirect
github.com/jinzhu/gorm v1.9.2
github.com/jinzhu/inflection v0.0.0-20180308033659-04140366298a // indirect
github.com/jinzhu/now v1.1.1 // indirect
github.com/lib/pq v1.0.0 // indirect
github.com/mattn/go-sqlite3 v1.10.0 // indirect
github.com/prometheus/client_golang v0.9.2
github.com/prometheus/client_model v0.0.0-20190129233127-fd36f4220a90 // indirect
github.com/prometheus/common v0.2.0 // indirect
github.com/prometheus/procfs v0.0.0-20190306233201-d0f344d83b0c // indirect
github.com/sirupsen/logrus v1.4.2
github.com/spf13/afero v1.2.0 // indirect
github.com/spf13/cobra v0.0.5
github.com/spf13/viper v1.3.2
github.com/stretchr/testify v1.4.0
golang.org/x/crypto v0.0.0-20190621222207-cc06ce4a13d4
gopkg.in/asn1-ber.v1 v1.0.0-20181015200546-f715ec2f112d // indirect
gopkg.in/ldap.v3 v3.0.2
)
224 changes: 224 additions & 0 deletions go.sum

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion internal/api/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ func NewApiTest() (app *gin.Engine, router *gin.RouterGroup, conf k4ever.Config)
app = gin.New()

CreateAuthMiddleware(conf)
app.POST("/api/v1/login/", AuthMiddleware.LoginHandler)
router = app.Group("/api/v1")
router.POST("/login/", AuthMiddleware.LoginHandler)
router.Use(AuthMiddleware.MiddlewareFunc())

// Create test user
Expand Down
8 changes: 6 additions & 2 deletions internal/api/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package api
import (
"time"

jwt "github.com/appleboy/gin-jwt"
jwt "github.com/appleboy/gin-jwt/v2"
"github.com/freitagsrunde/k4ever-backend/internal/k4ever"
"github.com/freitagsrunde/k4ever-backend/internal/models"
"github.com/freitagsrunde/k4ever-backend/internal/utils"
Expand All @@ -23,7 +23,8 @@ var configForAuth k4ever.Config

func CreateAuthMiddleware(config k4ever.Config) {
configForAuth = config
AuthMiddleware = &jwt.GinJWTMiddleware{
var err error
AuthMiddleware, err = jwt.New(&jwt.GinJWTMiddleware{
Realm: "emtpy", // TODO
Key: []byte("secret"), // TODO
Timeout: time.Hour,
Expand All @@ -40,6 +41,9 @@ func CreateAuthMiddleware(config k4ever.Config) {
},
//IdentityHandler: getIdentity,
Authenticator: authenticate,
})
if err != nil {
log.Error("Could not initialize auth middleware")
}
}

Expand Down
117 changes: 112 additions & 5 deletions internal/api/product.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ package api

import (
"net/http"
"strconv"

jwt "github.com/appleboy/gin-jwt"
jwt "github.com/appleboy/gin-jwt/v2"
"github.com/freitagsrunde/k4ever-backend/internal/k4ever"
"github.com/freitagsrunde/k4ever-backend/internal/models"
"github.com/freitagsrunde/k4ever-backend/internal/utils"
Expand All @@ -22,6 +23,8 @@ func ProductRoutesPrivate(router *gin.RouterGroup, config k4ever.Config) {
{
getProducts(products, config)
createProduct(products, config)
updateProduct(products, config)
deleteProduct(products, config)
buyProduct(products, config)
setProductImage(products, config)
}
Expand Down Expand Up @@ -163,15 +166,102 @@ func createProduct(router *gin.RouterGroup, config k4ever.Config) {
})
}

// swagger:route GET /products/{id}/image/ getProductImage
// swagger:route PUT /products/{id}/ updateProduct
//
// Not yet implemented
// Update a product
//
// Returns a product image or path to it (tbd)
// Produces:
// - application/json
//
// Consumes:
// - application/json
//
// Responses:
// default: GenericError
// 200: Product
// 400: GenericError
// 401: GenericError
// 500: GenericError
func updateProduct(router *gin.RouterGroup, config k4ever.Config) {
// swagger:parameters updateProduct
type ProductParam struct {
//in: body
// required: true
Product models.Product
}
router.PUT(":id/", func(c *gin.Context) {
if !utils.CheckRole(2, c) {
c.JSON(http.StatusUnauthorized, gin.H{"error": "Unauthorized"})
return
}
var product models.Product
if err := c.ShouldBindJSON(&product); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
uintID, err := strconv.ParseUint(c.Param("id"), 10, 32)
product.ID = uint(uintID)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "ID is not an int"})
return
}

if err = k4ever.UpdateProduct(&product, config); err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}

c.JSON(http.StatusOK, product)
})
}

// swager:route DELETE /products/{id}/ deleteProduct
//
// Delete a product
//
// Responses:
// default: GenericError
// 200: string
// 401: GenericError
// 500: GenericError
func deleteProduct(router *gin.RouterGroup, config k4ever.Config) {
// swagger:parameters deleteProduct
type deleteProductParams struct {
// in: path
// required: true
Id int
}
router.DELETE(":id/", func(c *gin.Context) {
if !utils.CheckRole(2, c) {
c.JSON(http.StatusUnauthorized, gin.H{"error": "Unauthorized"})
return
}
uintID, err := strconv.ParseUint(c.Param("id"), 10, 32)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "Id is not an int"})
return
}

if err = k4ever.DeleteProduct(uint(uintID), config); err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusOK, "Deleted")
})
}

// swagger:route PUT /products/{id}/image/ setProductImage
//
// set the product image for a single product
//
// Set the product image from the form value "file"
//
// Produces:
// - application/json
//
// Consumes:
// - multipart/form-data
//
// Security:
// jwt:
//
Expand All @@ -184,6 +274,10 @@ func setProductImage(router *gin.RouterGroup, config k4ever.Config) {
// in: path
// required: true
Id int `json:"id"`

// in: form
// required: true
File string `json:"file"`
}
router.PUT(":id/image/", func(c *gin.Context) {
if !utils.CheckRole(2, c) {
Expand Down Expand Up @@ -232,6 +326,10 @@ func buyProduct(router *gin.RouterGroup, config k4ever.Config) {
// in: path
// required: true
Id int `json:"id"`

// in: body
// required: false
Deposit bool `json:"deposit"`
}
router.POST(":id/buy/", func(c *gin.Context) {
if !utils.CheckRole(1, c) {
Expand All @@ -243,7 +341,16 @@ func buyProduct(router *gin.RouterGroup, config k4ever.Config) {
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": "Invalid token"})
return
}
purchase, err := k4ever.BuyProduct(c.Param("id"), username.(string), config)

var productParams buyProductParams
if c.Request.Body == http.NoBody {
if err := c.ShouldBindJSON(&productParams); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
}

purchase, err := k4ever.BuyProduct(c.Param("id"), productParams.Deposit, username.(string), config)
if err != nil {
if err.Error() == "record not found" {
c.AbortWithStatusJSON(http.StatusNotFound, gin.H{"error": "Record not found"})
Expand Down
5 changes: 3 additions & 2 deletions internal/api/product_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@ import (
func TestGetProducts(t *testing.T) {
app, router, conf := NewApiTest()

getProducts(router, conf)
products := router.Group("/products/")
getProducts(products, conf)

result := PerformRequest(app, "GET", "/api/v1")
result := PerformRequest(app, "GET", "/api/v1/products/")

assert.Equal(t, http.StatusOK, result.Code)
}
2 changes: 1 addition & 1 deletion internal/api/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"net/http"
"strings"

jwt "github.com/appleboy/gin-jwt"
jwt "github.com/appleboy/gin-jwt/v2"
"github.com/freitagsrunde/k4ever-backend/internal/k4ever"
"github.com/freitagsrunde/k4ever-backend/internal/models"
"github.com/freitagsrunde/k4ever-backend/internal/utils"
Expand Down
7 changes: 7 additions & 0 deletions internal/context/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,4 +147,11 @@ func (c *Config) MigrateDB() {
&models.History{},
&models.PurchaseItem{},
)

deposit := models.Product{}
deposit.Name = "Deposit"
deposit.Hidden = true
if err := db.Where(deposit).FirstOrCreate(&deposit).Error; err != nil {
os.Exit(1)
}
}
Loading