models

package module
v0.0.0-...-d463927 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 13, 2017 License: MIT Imports: 11 Imported by: 0

README

Model templates with gorp orm for gin

Build Status Coverage Status Go Report Card

Description

Data model templates with gorp orm for gin

Usage

Create a "models" directory in your project

cp gin-model-template/*.go myproject/models/

agent.go and user.go are tables templates. Feel free to rename files and fix XXX tags. Test with go test

repo.go contains database parameters.

In your main.go project import ./models

Sample :

  package main
    
  import (
      "fmt"
      "github.com/gin-gonic/gin"
      . "./models"
  )
    
  func main() {
    r := gin.Default()
    
    r.Use(Database("test.sqlite3"))
  
    v1 := r.Group("api/v1")
    {
        v1.GET("/users", GetUsers)
        v1.GET("/users/:id", GetUser)
        v1.POST("/users", PostUser)
        v1.PUT("/users/:id", UpdateUser)
        v1.DELETE("/users/:id", DeleteUser)
        v1.OPTIONS("/users", Options)     // POST
        v1.OPTIONS("/users/:id", Options) // PUT, DELETE
     ...

## TODO

Sample

Licence

MIT License

Copyright (c) 2016 Yves Agostini

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

yves+github@yvesago.net

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Database

func Database(connString string) gin.HandlerFunc

Database gin Middlware to select database

func DeleteAgent

func DeleteAgent(c *gin.Context)

DeleteAgent by id

func DeleteUser

func DeleteUser(c *gin.Context)

DeleteUser delete one user by id

func GetAgent

func GetAgent(c *gin.Context)

GetAgent return one agent by id

func GetAgents

func GetAgents(c *gin.Context)

GetAgents return all agents filtered by URL query

func GetUser

func GetUser(c *gin.Context)

GetUser return one user by id

func GetUsers

func GetUsers(c *gin.Context)

GetUsers return all users filtered by URL query

func InitDb

func InitDb(dbName string) *gorp.DbMap

InitDb set or create db

func ParseQuery

func ParseQuery(q map[string][]string) string

ParseQuery parse query to set select SQL query

func PostAgent

func PostAgent(c *gin.Context)

PostAgent create and return agent

func PostUser

func PostUser(c *gin.Context)

PostUser create and return one user

func UpdateAgent

func UpdateAgent(c *gin.Context)

UpdateAgent by id

func UpdateUser

func UpdateUser(c *gin.Context)

UpdateUser update one user by id

Types

type Agent

type Agent struct {
	Id         int64     `db:"id" json:"id"`
	Name       string    `db:"name" json:"name"`
	IP         string    `db:"ip" json:"ip"`
	FileSurvey string    `db:"filesurvey" json:"filesurvey"`
	Role       string    `db:"role" json:"role"`
	Status     string    `db:"status" json:"status"`
	Created    time.Time `db:"created" json:"created"` // or int64
	Updated    time.Time `db:"updated" json:"updated"`
}

XXX custom struct name and fields Agent db and json type

func (*Agent) PreInsert

func (a *Agent) PreInsert(s gorp.SqlExecutor) error

PreInsert set created an updated time before insert in db

func (*Agent) PreUpdate

func (a *Agent) PreUpdate(s gorp.SqlExecutor) error

PreUpdate set updated time before insert in db

type User

type User struct {
	Id      int64     `db:"id" json:"id"`
	Name    string    `db:"name" json:"name"`
	Email   string    `db:"email" json:"mail"`
	Status  string    `db:"status" json:"status"`
	Comment string    `db:"comment, size:16384" json:"comment"`
	Pass    string    `db:"pass" json:"pass"`
	Created time.Time `db:"created" json:"created"` // or int64
	Updated time.Time `db:"updated" json:"updated"`
}

XXX custom struct name and fields User db and json type

func (*User) PreInsert

func (a *User) PreInsert(s gorp.SqlExecutor) error

PreInsert set created an updated time before insert in db

func (*User) PreUpdate

func (a *User) PreUpdate(s gorp.SqlExecutor) error

PreUpdate set updated time before insert in db

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL