Commit 617ac106 authored by rima anggraini's avatar rima anggraini

initial commit

parents
package main
//modelnya
type User struct {
FullName string `json:"Name"`
Age int
}
// func main() {
// fmt.Println("---------DECODE JSON KE STRUCT-----------")
// var jsonString = `{"Name": "john wick", "Age": 27}`
// var jsonData = []byte(jsonString)
// var data User
// var err = json.Unmarshal(jsonData, &data)
// if err != nil {
// fmt.Println(err.Error())
// return
// }
// fmt.Println("user :", data.FullName)
// fmt.Println("age :", data.Age)
// fmt.Println("---------DECODE JSON KE INTERFACE-----------")
// var data1 map[string]interface{}
// json.Unmarshal(jsonData, &data1)
// fmt.Println("user :", data1["Name"])
// fmt.Println("age :", data1["Age"])
// }
package main
import (
"database/sql"
"encoding/json"
"fmt"
"log"
"net/http"
_ "github.com/go-sql-driver/mysql"
)
type student struct {
ID string
Name string
Grade int
}
type product struct {
ProductID uint64
ProductName string
Stock int
}
type Response struct {
Status int `json:"status"`
Message string `json:"message"`
Data []product
}
type productCategory struct {
ProductID int
ProductName string
Stock int
}
type category struct {
CategoryID int
CategoryName string
}
type CategoryArr struct {
CategoryName string
DataProduct []productCategory
}
type ResponseCategory struct {
Status string
Message string
data CategoryArr
}
var data = []student{
student{"E001", "ethan", 21},
student{"W001", "wick", 22},
student{"B001", "bourne", 23},
student{"B002", "bond", 23},
}
func users(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
if r.Method == "POST" {
var result, err = json.Marshal(data)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Write(result)
return
}
http.Error(w, "", http.StatusBadRequest)
}
func connect() (*sql.DB, error) {
db, err := sql.Open("mysql", "root:@tcp(localhost:3306)/test")
if err != nil {
return nil, err
}
fmt.Println(db)
return db, nil
}
func user(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
if r.Method == "POST" {
var id = r.FormValue("id")
var result []byte
var err error
for _, each := range data {
if each.ID == id {
result, err = json.Marshal(each)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Write(result)
return
}
}
http.Error(w, "User not found", http.StatusBadRequest)
return
}
http.Error(w, "", http.StatusBadRequest)
}
func products(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
if r.Method == "POST" {
db, err := connect()
if err != nil {
fmt.Println(err.Error())
return
}
defer db.Close()
fmt.Println("OK")
rows, err := db.Query("select ProductID, ProductName, Stock from Product")
if err != nil {
fmt.Println(err.Error())
return
}
defer rows.Close()
var products product
var arr_product []product
var response Response
if err != nil {
log.Print(err)
}
for rows.Next() {
if err := rows.Scan(&products.ProductID, &products.ProductName, &products.Stock); err != nil {
log.Fatal(err.Error())
} else {
arr_product = append(arr_product, products)
}
}
response.Status = 1
response.Message = "Success"
response.Data = arr_product
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(response)
return
}
http.Error(w, "", http.StatusBadRequest)
}
func categoryfunc(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
if r.Method == "POST" {
db, err := connect()
if err != nil {
fmt.Println(err.Error())
return
}
defer db.Close()
fmt.Println("OK")
var productsresult productCategory
var arrayProduct []productCategory
var response ResponseCategory
productCategory, err := db.Query("select ProductID, ProductName, Stock from Product WHERE CategoryID = 1")
fmt.Println("-----------------")
if err != nil {
log.Print(err)
}
for productCategory.Next() {
if err := productCategory.Scan(productsresult.ProductID, productsresult.ProductName, productsresult.Stock); err != nil {
log.Fatal(err.Error())
} else {
arrayProduct = append(arrayProduct, productsresult)
}
}
fmt.Println("Masuk sini---------")
var datacat = CategoryArr{
CategoryName: "Makanan",
DataProduct: arrayProduct,
}
response.Status = "OK"
response.Message = "Success"
response.data = datacat
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(response)
return
}
http.Error(w, "", http.StatusBadRequest)
}
func sqlQuery() {
db, err := connect()
if err != nil {
fmt.Println(err.Error())
return
}
defer db.Close()
fmt.Println("OK")
rows, err := db.Query("select ProductID, ProductName, Stock from Product")
if err != nil {
fmt.Println(err.Error())
return
}
defer rows.Close()
var result []product
for rows.Next() {
var each = product{}
var err = rows.Scan(&each.ProductID, &each.ProductName, &each.Stock)
if err != nil {
fmt.Println(err.Error())
return
}
result = append(result, each)
}
if err = rows.Err(); err != nil {
fmt.Println(err.Error())
return
}
for _, each := range result {
fmt.Println(each.ProductName)
}
}
func main() {
http.HandleFunc("/users", users)
http.HandleFunc("/user", user)
http.HandleFunc("/product", products)
http.HandleFunc("/category", categoryfunc)
sqlQuery()
fmt.Println("starting web server at http://localhost:8080/")
http.ListenAndServe(":8080", nil)
}
mysql @ 877a9775
Subproject commit 877a9775f06853f611fb2d4e817d92479242d1cd
mux @ d83b6ffe
Subproject commit d83b6ffe499a29cc05fc977988d0392851779620
package main
type Products struct {
Id int64 `form:"id" json:"ProductId"`
ProductName string `form:"firstname" json:"Productname"`
Stock int `form:"lastname" json:"Stock"`
}
type Response struct {
Status int `json:"status"`
Message string `json:"message"`
Data []Products
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment