kreangkrai

dvdrental_4

Mar 18th, 2019
432
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 2.27 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4.     "github.com/gin-gonic/gin"
  5.     "go101/go001/model"
  6.     "log"
  7.     "net/http"
  8.     "time"
  9. )
  10.  
  11. type Customers []Customer
  12. type Customer struct {
  13.     CustomerID int       `json:"customer_id"`
  14.     StoreID    int       `json:"store_id"`
  15.     FirstName  string    `json:"first_name"`
  16.     LastName   string    `json:"last_name"`
  17.     Email      string    `json:"email"`
  18.     AddressID  int       `json:"address_id"`
  19.     Activebool bool      `json:"activebool"`
  20.     CreateDate time.Time `json:"create_date"`
  21.     LastUpdate time.Time `json:"last_update"`
  22.     Active     int       `json:"active"`
  23. }
  24.  
  25. func main() {
  26.     r := gin.Default()
  27.     r.GET("/customers", func(c *gin.Context) {
  28.         limit := c.DefaultQuery("limit", "10")
  29.         customers := getCustomers(limit)
  30.         c.JSON(http.StatusOK, gin.H{
  31.             "data": customers,
  32.         })
  33.     })
  34.     r.GET("/customer/:id", func(c *gin.Context) {
  35.         customerID := c.Param("id")
  36.         customer := getCustomerByID(customerID)
  37.         c.JSON(http.StatusOK, gin.H{
  38.             "data": customer,
  39.         })
  40.     })
  41.     r.Run(":8880")
  42. }
  43.  
  44. func getCustomers(limit string) (customers Customers) {
  45.     rows, err := model.PgxConn.Query("select * from customer limit $1", limit)
  46.     if err != nil {
  47.         log.Fatalf("Unable to query: %v", err)
  48.     }
  49.     for rows.Next() {
  50.         customer := Customer{}
  51.  
  52.         rows.Scan(&customer.CustomerID, &customer.StoreID, &customer.FirstName, &customer.LastName, &customer.Email, &customer.AddressID, &customer.Activebool, &customer.CreateDate, &customer.LastUpdate, &customer.Active)
  53.  
  54.         customers = append(customers, customer)
  55.     }
  56.     return customers
  57. }
  58.  
  59. func getCustomerByID(id string) (customer Customer) {
  60.     rows, _ := model.PgxConn.Query("select * from customer as cus where cus.customer_id=$1", id)
  61.     for rows.Next() {
  62.         rows.Scan(&customer.CustomerID, &customer.StoreID, &customer.FirstName, &customer.LastName, &customer.Email, &customer.AddressID, &customer.Activebool, &customer.CreateDate, &customer.LastUpdate, &customer.Active)
  63.     }
  64.     return
  65. }
  66.  
  67.  
  68. package model
  69.  
  70. import (
  71.     "github.com/jackc/pgx"
  72.     "log"
  73. )
  74.  
  75. var PgxConn *pgx.Conn
  76.  
  77. func init() {
  78.     config := pgx.ConnConfig{Host: "172.17.0.2", Port: 5432, Database: "dvdrental", User: "postgres", Password: "toor123"}
  79.     conn, err := pgx.Connect(config)
  80.     if err != nil {
  81.         log.Fatalf("Unable to establish connection: %v", err)
  82.     }
  83.     PgxConn = conn
  84. }
Advertisement
Add Comment
Please, Sign In to add comment