Advertisement
kreangkrai

dvdrental_3

Mar 18th, 2019
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 2.38 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4.     "github.com/gin-gonic/gin"
  5.     "github.com/jackc/pgx"
  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.     config := pgx.ConnConfig{Host: "172.17.0.2", Port: 5432, Database: "dvdrental", User: "postgres", Password: "toor123"}
  46.     conn, err := pgx.Connect(config)
  47.     defer conn.Close()
  48.     if err != nil {
  49.         log.Fatalf("Unable to establish connection: %v", err)
  50.     }
  51.  
  52.     // Query customer
  53.     rows, _ := conn.Query("select * from customer limit $1", limit)
  54.     for rows.Next() {
  55.         customer := Customer{}
  56.  
  57.         rows.Scan(&customer.CustomerID, &customer.StoreID, &customer.FirstName, &customer.LastName, &customer.Email, &customer.AddressID, &customer.Activebool, &customer.CreateDate, &customer.LastUpdate, &customer.Active)
  58.  
  59.         customers = append(customers, customer)
  60.     }
  61.     return customers
  62. }
  63.  
  64. func getCustomerByID(id string) (customer Customer) {
  65.     config := pgx.ConnConfig{Host: "172.17.0.2", Port: 5432, Database: "dvdrental", User: "postgres", Password: "toor123"}
  66.     conn, err := pgx.Connect(config)
  67.     defer conn.Close()
  68.     if err != nil {
  69.         log.Fatalf("Unable to establish connection: %v", err)
  70.     }
  71.  
  72.     // Query customer
  73.     rows, _ := conn.Query("select * from customer as cus where cus.customer_id=$1", id)
  74.     for rows.Next() {
  75.         rows.Scan(&customer.CustomerID, &customer.StoreID, &customer.FirstName, &customer.LastName, &customer.Email, &customer.AddressID, &customer.Activebool, &customer.CreateDate, &customer.LastUpdate, &customer.Active)
  76.     }
  77.     return
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement