Skip to content

Commit

Permalink
updated the changes
Browse files Browse the repository at this point in the history
Signed-off-by: Yaxhveer <yaxhcod@gmail.com>
  • Loading branch information
Yaxhveer committed Feb 4, 2024
1 parent b87eb97 commit d15a4aa
Show file tree
Hide file tree
Showing 2 changed files with 1 addition and 21 deletions.
18 changes: 1 addition & 17 deletions mux-sql/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,12 @@ package main

import (
"database/sql"

// tom: for Initialize
"encoding/json"
"fmt"
"log"

// tom: for route handlers
"encoding/json"
"net/http"
"strconv"

// tom: go get required
"github.com/gorilla/mux"
_ "github.com/lib/pq"
)
Expand All @@ -23,16 +18,11 @@ type App struct {
DB *sql.DB
}

// tom: initial function is empty, it's filled afterwards
// func (a *App) Initialize(user, password, dbname string) { }

// Initialize the app
func (a *App) Initialize(host, user, password, dbname string) error {

connectionString := fmt.Sprintf("host=%s port=%s user=%s "+
"password=%s dbname=%s sslmode=disable",
host, "5432", user, password, dbname)
// connectionString := fmt.Sprintf("user=%s password=%s dbname=%s sslmode=disable", user, password, dbname)
var err error
a.DB, err = sql.Open("postgres", connectionString)
if err != nil {
Expand All @@ -41,21 +31,15 @@ func (a *App) Initialize(host, user, password, dbname string) error {

a.Router = mux.NewRouter()

// tom: this line is added after initializeRoutes is created later on
a.initializeRoutes()
return err
}

// tom: initial version
// func (a *App) Run(addr string) { }
// improved version

// Run starts the app
func (a *App) Run(addr string) {
log.Fatal(http.ListenAndServe(addr, a.Router))
}

// tom: these are added later
func (a *App) getProduct(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
id, err := strconv.Atoi(vars["id"])
Expand Down
4 changes: 0 additions & 4 deletions mux-sql/model.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
// model.go

package main

import (
"context"
"database/sql"
// tom: errors is removed once functions are implemented
// "errors"
)

// tom: add backticks to json
type product struct {
ID int `json:"id"`
Name string `json:"name"`
Expand Down

0 comments on commit d15a4aa

Please # to comment.