auto logout
This commit is contained in:
61
services/staff/staff.routes.go
Normal file
61
services/staff/staff.routes.go
Normal file
@@ -0,0 +1,61 @@
|
||||
package staff
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
"github.com/go-playground/validator/v10"
|
||||
"github.com/gorilla/mux"
|
||||
"sismedika.com/sas/westone/services/auth"
|
||||
"sismedika.com/sas/westone/types"
|
||||
"sismedika.com/sas/westone/utils"
|
||||
)
|
||||
|
||||
type Handler struct {
|
||||
store types.StaffStore
|
||||
authStore types.OauthStore
|
||||
}
|
||||
|
||||
func NewHandler(store types.StaffStore, authStore types.OauthStore) *Handler {
|
||||
return &Handler{store: store, authStore: authStore}
|
||||
}
|
||||
|
||||
func (h *Handler) RegisterRoutes(router *mux.Router) {
|
||||
staffRoute := router.PathPrefix("/staff").Subrouter()
|
||||
staffRoute.Use(auth.WithAuthStore(h.authStore))
|
||||
staffRoute.HandleFunc("/getstaff", h.handelGetStaff).Methods(http.MethodPost)
|
||||
staffRoute.HandleFunc("/", h.coba).Methods(http.MethodPost)
|
||||
}
|
||||
|
||||
func (h *Handler) coba(w http.ResponseWriter, r *http.Request) {
|
||||
var payload types.GetStaffPayload
|
||||
if err := utils.ParseJSON(r, &payload); err != nil {
|
||||
utils.WriteError(w, http.StatusBadRequest, err)
|
||||
return
|
||||
}
|
||||
fmt.Printf("payload: %+v\n", payload)
|
||||
fmt.Printf("COBA")
|
||||
|
||||
utils.WriteJSON(w, http.StatusOK, payload)
|
||||
}
|
||||
func (h *Handler) handelGetStaff(w http.ResponseWriter, r *http.Request) {
|
||||
var payload types.GetStaffPayload
|
||||
if err := utils.ParseJSON(r, &payload); err != nil {
|
||||
utils.WriteError(w, http.StatusBadRequest, err)
|
||||
return
|
||||
}
|
||||
|
||||
if err := utils.Validate.Struct(payload); err != nil {
|
||||
erros := err.(validator.ValidationErrors)
|
||||
utils.WriteError(w, http.StatusBadRequest, fmt.Errorf("invalid payload: %v", erros))
|
||||
return
|
||||
}
|
||||
|
||||
response, err := h.store.GetStaff(payload.StaffName)
|
||||
if err != nil {
|
||||
utils.WriteError(w, http.StatusInternalServerError, err)
|
||||
return
|
||||
}
|
||||
|
||||
utils.WriteJSON(w, http.StatusOK, response)
|
||||
}
|
||||
34
services/staff/staff.stores.go
Normal file
34
services/staff/staff.stores.go
Normal file
@@ -0,0 +1,34 @@
|
||||
package staff
|
||||
|
||||
import (
|
||||
"github.com/jmoiron/sqlx"
|
||||
"sismedika.com/sas/westone/types"
|
||||
)
|
||||
|
||||
type Store struct {
|
||||
db *sqlx.DB
|
||||
}
|
||||
|
||||
func NewStore(db *sqlx.DB) *Store {
|
||||
return &Store{db: db}
|
||||
}
|
||||
func (s *Store) GetStaff(name string) (*types.Staff, error) {
|
||||
term := new(types.Staff)
|
||||
|
||||
Keyword := "%" + name + "%"
|
||||
qry := `SELECT
|
||||
M_StaffID,
|
||||
M_StaffName,
|
||||
M_StaffEmail,
|
||||
M_StaffNakesID,
|
||||
M_StaffIsActive
|
||||
FROM m_staff
|
||||
WHERE (M_StaffName LIKE ? OR M_StaffEmail LIKE ? )
|
||||
AND M_StaffIsActive = 'Y'
|
||||
`
|
||||
if err := s.db.Get(term, qry, Keyword, Keyword); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return term, nil
|
||||
}
|
||||
Reference in New Issue
Block a user