Advertisement
Guest User

Untitled

a guest
Jun 15th, 2019
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 1.27 KB | None | 0 0
  1. func Create(w http.ResponseWriter, r *http.Request) {
  2.     w.Header().Set("Content-Type", "application/json")
  3.  
  4.     // Parse body
  5.     decoder := json.NewDecoder(r.Body)
  6.     var newFSM mongo.FSM
  7.     err := decoder.Decode(&newFSM)
  8.     if err != nil {
  9.         fmt.Printf("Error: %s", err)
  10.         w.Write([]byte("Error parse JSON"))
  11.         return
  12.     }
  13.  
  14.     // Create FSM
  15.     FSM, err := fsm.New()
  16.     if err != nil {
  17.         fmt.Printf("Error: %s", err)
  18.         w.Write([]byte("Error create a new FSM"))
  19.         return
  20.     }
  21.  
  22.     FSM.Import(newFSM.FSM)
  23.  
  24.     // Add transition
  25.     // ...code
  26.     // as example
  27.     FSM.AddStateTransitionRules("a", "b", "c")
  28.     FSM.AddStateTransitionRules("b", "d", "e")
  29.     // Add event
  30.     FSM.AddEvent("start", "a")
  31.     FSM.AddEvent("to b", "b")
  32.     // ...code
  33.     // Add CB
  34.     // ...code
  35.     // Add state
  36.     // ...code
  37.  
  38.     resultFSM, err := FSM.MarshalJSON()
  39.     if err != nil {
  40.         fmt.Printf("Error: %s", err)
  41.         w.Write([]byte("Error parse a result FSM"))
  42.         return
  43.     }
  44.     json.Unmarshal(resultFSM, &newFSM.FSM)
  45.  
  46.     id, err := mongo.Cfg.Add(newFSM)
  47.     if err != nil {
  48.         fmt.Printf("Error: %s", err)
  49.         w.Write([]byte("Error create new FSM"))
  50.         return
  51.     }
  52.     newFSM.Id = *id
  53.  
  54.     b, err := json.Marshal(newFSM)
  55.     if err != nil {
  56.         fmt.Printf("Error: %s", err)
  57.         w.Write([]byte("Error parse JSON"))
  58.         return
  59.     }
  60.  
  61.     w.Write([]byte(string(b)))
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement