Skip to content

Commit

Permalink
add borrowReturnItemHandler
Browse files Browse the repository at this point in the history
  • Loading branch information
gpioblink committed Nov 19, 2020
1 parent e6c9a9c commit 7b051f4
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 9 deletions.
57 changes: 54 additions & 3 deletions server/handler/borrow.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ import (
"encoding/json"
"fmt"
"github.com/AizuGeekDojo/EnterLeaveSystem/server/db"
"io"
"log"
"net/http"
"strconv"
)

//UserAPIHandler handles http request for user management
Expand All @@ -24,8 +26,8 @@ func (h *Handler) BorrowAPIHandler(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case "GET":
getUserBorrowingHandler(w, r, h.DB)
//case "POST":
// createNewBorrowing(w, r, h.DB)
case "POST":
borrowReturnItemHandler(w, r, h.DB)
default:
w.WriteHeader(http.StatusMethodNotAllowed)
fmt.Fprintf(w, "Unexpected method")
Expand All @@ -39,11 +41,21 @@ type UserBorrowingInfo struct {
}

type UserBorrowingProduct struct {
ID string `json:"ID"`
ID int `json:"ID"`
BarCode string `json:"BarCode"`
Name string `json:"Name"`
}

type UserBorrowingTransaction struct {
UID string `json:"SID"`
Operations []UserBorrowingOperation `json:"Operations"`
}

type UserBorrowingOperation struct {
ItemID string `json:"ID"`
Operation string `json:"Op"`
}

func getUserBorrowingHandler(w http.ResponseWriter, r *http.Request, d *sql.DB) {
var userborrowingdat UserBorrowingInfo
r.ParseForm()
Expand Down Expand Up @@ -75,3 +87,42 @@ func getUserBorrowingHandler(w http.ResponseWriter, r *http.Request, d *sql.DB)
}
w.Write(retbyte)
}

func borrowReturnItemHandler(w http.ResponseWriter, r *http.Request, d *sql.DB) {
var usertrans UserBorrowingTransaction
r.ParseForm()

reqlen, err := strconv.Atoi(r.Header.Get("Content-Length"))
if err != nil {
w.WriteHeader(http.StatusBadRequest)
fmt.Fprintf(w, "Cannot get Content-Length: %v", err)
log.Printf("%v %v: Bad request: %v", r.Method, r.URL.Path, err)
return
}
body := make([]byte, reqlen)
n, err := r.Body.Read(body)
if err != nil {
if err != io.EOF || n == 0 {
w.WriteHeader(http.StatusBadRequest)
fmt.Fprintf(w, "Failed to read: %v", err)
log.Printf("%v %v: Bad request: %v", r.Method, r.URL.Path, err)
return
}
}
err = json.Unmarshal(body, &usertrans)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
fmt.Fprintf(w, "Failed to parse JSON: %v", err)
log.Printf("%v %v: Bad request: %v", r.Method, r.URL.Path, err)
return
}

retbyte, err := json.Marshal(usertrans)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
log.Printf("%v %v: json.Marshal error: %v", r.Method, r.URL.Path, err)
fmt.Fprintf(w, "{}")
return
}
w.Write(retbyte)
}
11 changes: 5 additions & 6 deletions server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (

"github.com/AizuGeekDojo/EnterLeaveSystem/server/db"
"github.com/AizuGeekDojo/EnterLeaveSystem/server/handler"
"github.com/AizuGeekDojo/EnterLeaveSystem/server/utils"
"golang.org/x/net/websocket"
)

Expand All @@ -29,13 +28,13 @@ func main() {
http.HandleFunc("/api/borrow", h.BorrowAPIHandler)

//Standby NFC card reader
go handler.ReadCard(d)
// go handler.ReadCard(d)

//Start cron
err = utils.CronInit(d)
if err != nil {
panic(err)
}
// err = utils.CronInit(d)
// if err != nil {
// panic(err)
// }

//Start web server
fmt.Println("Start server")
Expand Down

0 comments on commit 7b051f4

Please sign in to comment.