Advertisement
kreangkrai

dvdrental_2

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