poprawa połączenia z bazą, pierwszy endpoint
This commit is contained in:
parent
f13947defa
commit
43e77b3700
14
server/.vscode/launch.json
vendored
Normal file
14
server/.vscode/launch.json
vendored
Normal file
@ -0,0 +1,14 @@
|
||||
{
|
||||
"version": "0.2.0",
|
||||
"configurations": [
|
||||
{
|
||||
"name": "Launch",
|
||||
"type": "go",
|
||||
"request": "launch",
|
||||
"mode": "auto",
|
||||
"program": "${fileDirname}",
|
||||
"env": {},
|
||||
"args": []
|
||||
}
|
||||
]
|
||||
}
|
32
server/controllers/companiesController.go
Normal file
32
server/controllers/companiesController.go
Normal file
@ -0,0 +1,32 @@
|
||||
package controllers
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"ExpiryDatesManager/server/model"
|
||||
"ExpiryDatesManager/server/utils"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
var GetCompanies = func(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
data := model.GetCompanies()
|
||||
resp := utils.Message(true, "success")
|
||||
resp["data"] = data
|
||||
utils.Respond(w, resp)
|
||||
}
|
||||
|
||||
var CreateCompany = func(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
company := &model.Company{}
|
||||
|
||||
err := json.NewDecoder(r.Body).Decode(company)
|
||||
if err != nil {
|
||||
utils.Respond(w, utils.Message(false, "Error while decoding request body"))
|
||||
return
|
||||
}
|
||||
|
||||
data := company.Create()
|
||||
resp := utils.Message(true, "success")
|
||||
resp["data"] = data
|
||||
utils.Respond(w, resp)
|
||||
}
|
15
server/controllers/productsController.go
Normal file
15
server/controllers/productsController.go
Normal file
@ -0,0 +1,15 @@
|
||||
package controllers
|
||||
|
||||
import (
|
||||
"ExpiryDatesManager/server/model"
|
||||
"ExpiryDatesManager/server/utils"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
var GetProducts = func(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
data := model.GetProducts()
|
||||
resp := utils.Message(true, "success")
|
||||
resp["data"] = data
|
||||
utils.Respond(w, resp)
|
||||
}
|
@ -2,16 +2,26 @@ package main
|
||||
|
||||
import (
|
||||
"ExpiryDatesManager/server/model"
|
||||
"ExpiryDatesManager/server/controllers"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"github.com/gorilla/mux"
|
||||
)
|
||||
|
||||
func main() {
|
||||
fmt.Printf("hello, world\n")
|
||||
|
||||
|
||||
router := mux.NewRouter()
|
||||
|
||||
router.HandleFunc("/api/products", controllers.GetProducts).Methods("GET")
|
||||
router.HandleFunc("/api/companies", controllers.GetCompanies).Methods("GET")
|
||||
router.HandleFunc("/api/companies", controllers.CreateCompany).Methods("POST")
|
||||
|
||||
defer model.GetDB().Close()
|
||||
|
||||
fmt.Println("udalo sie")
|
||||
err := http.ListenAndServe(":"+"8000", router)
|
||||
if err != nil {
|
||||
fmt.Print(err)
|
||||
}
|
||||
|
||||
fmt.Println("udalo sie")
|
||||
}
|
||||
|
@ -19,7 +19,7 @@ func init() {
|
||||
username := os.Getenv("db_name")
|
||||
password := os.Getenv("db_pass")
|
||||
|
||||
connectionstring := fmt.Sprintf("%s:%s@tcp(127.0.0.1:3306)/expirydates", username, password)
|
||||
connectionstring := fmt.Sprintf("%s:%s@tcp(127.0.0.1:3306)/expirydates?charset=utf8&parseTime=True&loc=Local", username, password)
|
||||
|
||||
conn, err := gorm.Open("mysql", connectionstring)
|
||||
if err != nil {
|
||||
@ -32,6 +32,9 @@ func init() {
|
||||
db.Model(&Report{}).AddForeignKey("company_id", "companies(id)", "RESTRICT", "RESTRICT")
|
||||
db.Model(&ReportPosition{}).AddForeignKey("report_id", "reports(id)", "RESTRICT", "RESTRICT")
|
||||
db.Model(&ReportPosition{}).AddForeignKey("product_expiry_position_id", "product_expiry_positions(id)", "RESTRICT", "RESTRICT")
|
||||
|
||||
db.LogMode(true)
|
||||
db = db.Set("gorm:auto_preload", true)
|
||||
}
|
||||
|
||||
func GetDB() *gorm.DB {
|
||||
|
@ -7,4 +7,20 @@ import (
|
||||
type Company struct {
|
||||
gorm.Model
|
||||
Name string
|
||||
}
|
||||
|
||||
func GetCompanies() ([]*Company) {
|
||||
|
||||
comapnies := make([]*Company, 0)
|
||||
GetDB().Find(&comapnies)
|
||||
|
||||
return comapnies
|
||||
}
|
||||
|
||||
func (company *Company) Create() (*Company) {
|
||||
|
||||
GetDB().Create(company)
|
||||
GetDB().Save(company)
|
||||
|
||||
return company;
|
||||
}
|
@ -7,7 +7,15 @@ import (
|
||||
type Product struct {
|
||||
gorm.Model
|
||||
Name string
|
||||
Company Company
|
||||
Company Company `json:",omitempty"`
|
||||
CompanyID uint
|
||||
Code string `gorm:"unique; unique_index"`
|
||||
}
|
||||
|
||||
func GetProducts() ([]*Product) {
|
||||
|
||||
products := make([]*Product, 0)
|
||||
GetDB().Find(&products)
|
||||
|
||||
return products
|
||||
}
|
15
server/utils/httpUtils.go
Normal file
15
server/utils/httpUtils.go
Normal file
@ -0,0 +1,15 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func Message(status bool, message string) (map[string]interface{}) {
|
||||
return map[string]interface{} {"status" : status, "message" : message}
|
||||
}
|
||||
|
||||
func Respond(w http.ResponseWriter, data map[string] interface{}) {
|
||||
w.Header().Add("Content-Type", "application/json")
|
||||
json.NewEncoder(w).Encode(data)
|
||||
}
|
Loading…
Reference in New Issue
Block a user