Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
mnvx committed Aug 13, 2020
0 parents commit 58db669
Show file tree
Hide file tree
Showing 10 changed files with 599 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.idea
vendor
coverage.out
157 changes: 157 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
# CRUD for GORM

## Features

- [x] ORM: GORM
- [x] Base CRUD service
- [x] Base CRUD repository
- [x] Base CRUD controller

## How to use

```bash
package controller

import (
"github.com/gin-gonic/gin"
"github.com/zubroide/gorm-crud/common"
"github.com/zubroide/gorm-crud/repository"
"github.com/zubroide/gorm-crud/service"
"net/http"
"reflect"
"strconv"
)


type ParametersHydratorInterface interface {
Hydrate(context *gin.Context) (repository.ListParametersInterface, error)
}

type BaseParametersHydrator struct {
logger common.LoggerInterface
ParametersHydratorInterface
}

func NewBaseParametersHydrator(logger common.LoggerInterface) ParametersHydratorInterface {
return &BaseParametersHydrator{logger: logger}
}

func (c BaseParametersHydrator) Hydrate(context *gin.Context) (repository.ListParametersInterface, error) {
var parameters repository.CrudListParameters
err := context.ShouldBindQuery(&parameters)
return &parameters, err
}


type CrudControllerInterface interface {
BaseControllerInterface
Create(context *gin.Context)
Get(context *gin.Context)
List(context *gin.Context)
Update(context *gin.Context)
Delete(context *gin.Context)
}

type CrudController struct {
CrudControllerInterface
*BaseController
service service.CrudServiceInterface
parametersHydrator ParametersHydratorInterface
}

func NewCrudController(service service.CrudServiceInterface, parametersHydrator ParametersHydratorInterface, logger common.LoggerInterface) *CrudController {
controller := NewBaseController(logger)
return &CrudController{BaseController: controller, service: service, parametersHydrator: parametersHydrator}
}

func (c CrudController) Get(context *gin.Context) {
recordId, err := strconv.Atoi(context.Params.ByName("id"))
if err != nil {
c.replyError(context, "Please specify record id", http.StatusBadRequest)
return
}

data, err := c.service.GetItem(uint(recordId))

if err != nil {
c.replyError(context, "Record not found", http.StatusNotFound)
return
}

c.replySuccess(context, data)
}

func (c CrudController) List(context *gin.Context) {
parameters, err := c.parametersHydrator.Hydrate(context)

if err != nil {
c.replyError(context, "Cant parse request parameters", http.StatusBadRequest)
return
}

data, err := c.service.GetList(parameters)

if err != nil {
c.replyError(context, "Data not found", http.StatusBadRequest)
return
}

c.replySuccess(context, data)
}

func (c CrudController) Create(context *gin.Context) {
model := c.service.GetModel()
data := reflect.New(reflect.TypeOf(model).Elem()).Interface()
if err := context.ShouldBindJSON(data); err != nil {
c.replyError(context, "Cant parse request", http.StatusBadRequest)
return
}
data = c.service.Create(data)
c.replySuccess(context, data)
}

func (c CrudController) Update(context *gin.Context) {
recordId, err := strconv.Atoi(context.Params.ByName("id"))
if err != nil {
c.replyError(context, "Cant parse request", http.StatusBadRequest)
return
}

data, err := c.service.GetItem(uint(recordId))
if err != nil {
c.replyError(context, "Data not found", http.StatusBadRequest)
return
}

if err := context.ShouldBindJSON(data); err != nil {
c.replyError(context, "Cant parse request", http.StatusBadRequest)
return
}
data = c.service.Update(data)

c.replySuccess(context, data)
}

func (c CrudController) Delete(context *gin.Context) {
recordId, err := strconv.Atoi(context.Params.ByName("id"))
if err != nil {
c.replyError(context, "Please specify record id", http.StatusBadRequest)
return
}

err = c.service.Delete(uint(recordId))
if err != nil {
c.replyError(context, "Data not found", http.StatusBadRequest)
return
}

c.replySuccess(context, nil)
}
```
## Requirements
- Go 1.12+
# License
MIT
5 changes: 5 additions & 0 deletions common/logger.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package common

type LoggerInterface interface {
Error(message string)
}
4 changes: 4 additions & 0 deletions entity/common.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package entity

type InterfaceEntity interface {
}
5 changes: 5 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module github.com/zubroide/gorm-crud

go 1.12

require github.com/jinzhu/gorm v1.9.12
19 changes: 19 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
github.com/denisenkom/go-mssqldb v0.0.0-20191124224453-732737034ffd/go.mod h1:xbL0rPBG9cCiLr28tMa8zpbdarY27NDyej4t/EjAShU=
github.com/erikstmartin/go-testdb v0.0.0-20160219214506-8d10e4a1bae5/go.mod h1:a2zkGnVExMxdzMo3M0Hi/3sEU+cWnZpSni0O6/Yb/P0=
github.com/go-sql-driver/mysql v1.4.1/go.mod h1:zAC/RDZ24gD3HViQzih4MyKcchzm+sOG5ZlKdlhCg5w=
github.com/golang-sql/civil v0.0.0-20190719163853-cb61b32ac6fe/go.mod h1:8vg3r2VgvsThLBIFL93Qb5yWzgyZWhEmBwUJWevAkK0=
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/jinzhu/gorm v1.9.12/go.mod h1:vhTjlKSJUTWNtcbQtrMBFCxy7eXTzeCAzfL5fBZT/Qs=
github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc=
github.com/jinzhu/now v1.0.1/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8=
github.com/lib/pq v1.1.1/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
github.com/mattn/go-sqlite3 v2.0.1+incompatible/go.mod h1:FPy6KqzDD04eiIsT53CuJW3U88zkxoIYsOqkbpncsNc=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20190325154230-a5d413f7728c/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20191205180655-e7c4368fe9dd/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
google.golang.org/appengine v1.4.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4=
19 changes: 19 additions & 0 deletions repository/base_repository.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package repository

import (
"github.com/jinzhu/gorm"
"github.com/zubroide/gorm-crud/common"
)

type BaseRepositoryInterface interface {
}

type BaseRepository struct {
BaseRepositoryInterface
db *gorm.DB
logger common.LoggerInterface
}

func NewBaseRepository(db *gorm.DB, logger common.LoggerInterface) BaseRepositoryInterface {
return &BaseRepository{db: db, logger: logger}
}
Loading

0 comments on commit 58db669

Please sign in to comment.