Guest User

Untitled

a guest
Feb 14th, 2019
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.31 KB | None | 0 0
  1. import Route from '@ember/routing/route';
  2.  
  3. export default Route.extend({
  4. model: function(){
  5. return this.store.createRecord('user');
  6. }
  7. });
  8.  
  9. import Controller from '@ember/controller';
  10.  
  11. export default Controller.extend({
  12. actions: {
  13. saveUser: function(){
  14. this.get('model').save().then(user => this.transitionToRoute('users.user', user));
  15. }
  16. }
  17. });
  18.  
  19. export default DS.Model.extend({
  20. username: DS.attr('string'),
  21. password: DS.attr('string'),
  22. firstName: DS.attr('string'),
  23. lastName: DS.attr('string'),
  24. roles: DS.attr('string'),
  25. pic: DS.attr('string'),
  26. });
  27.  
  28. <div class="col-md-12">
  29. <h4 class='form-label'><b>Add new pet</b></h4>
  30. <form role="form" id="addUserForm">
  31. <div class="form-group">
  32. <label for="username">UserName:</label>
  33. {{input class="form-control" id="username" value=model.username}}
  34. </div>
  35. <div class="form-group">
  36. <label for="password">password:</label>
  37. {{input class="form-control" id="password" value=model.password}}
  38. </div>
  39. <div class="form-group">
  40. <label for="firstName">firstName:</label>
  41. {{input class="form-control" id="firstName" value=model.firstName}}
  42. </div>
  43.  
  44. <div class="form-group">
  45. <label for="lastName">lastName</label>
  46. {{input class="form-control" id="lastName" value=model.lastName}}
  47. </div>
  48.  
  49. <div class="form-group">
  50. <label for="roles">roles</label>
  51. {{input class="form-control" id="roles" value=model.roles}}
  52. </div>
  53.  
  54. <div class="form-group">
  55. <label for="pic">pic</label>
  56. {{input class="form-control" id="pic" value=model.pic}}
  57. </div>
  58. <button type="submit" class="btn btn-default" {{action 'saveUser'}}>Submit</button>
  59. </form>
  60. </div>
  61.  
  62. import (
  63. "github.com/rs/cors"
  64. "log"
  65. "net/http"
  66. )
  67.  
  68. func main() {
  69. r := NewRouter()
  70.  
  71. c := cors.New(cors.Options{
  72. AllowedMethods: []string{"GET","POST", "PATCH", "DELETE", "OPTIONS"},
  73. AllowedOrigins: []string{"*", "*", "*"},
  74. AllowCredentials: false,
  75. AllowedHeaders: []string{"application/json", "Content-Type, Access-Control-Allow-Origin"},
  76. OptionsPassthrough: true,
  77. Debug: true,
  78.  
  79. })
  80.  
  81. handler := c.Handler(r)
  82.  
  83. log.Fatal(http.ListenAndServe(":8000", handler))
  84. }
  85.  
  86. type User struct {
  87. Id int `jsonapi:"primary,users"`
  88. Username string `jsonapi:"attr,username"`
  89. Password string `jsonapi:"attr,password"`
  90. FirstName string `jsonapi:"attr,firstname"`
  91. LastName string `jsonapi:"attr,lastname"`
  92. Role string `jsonapi:"attr,role"`
  93. Pic string `jsonapi:"attr,pic"`
  94. }
  95.  
  96. func ModifyUser(w http.ResponseWriter, r *http.Request) {
  97. fmt.Println("Modify Handler")
  98. jsonApiRuntime := jsonapi.NewRuntime().Instrument("/")
  99.  
  100. vars := mux.Vars(r)
  101. fmt.Println(vars["id"])
  102.  
  103. id, err := strconv.Atoi(vars["id"])
  104.  
  105. if err != nil {
  106. fmt.Println("Invalid User")
  107. return
  108. }
  109.  
  110. user := ReadUser(&models.User{Id: id})
  111. if err := jsonApiRuntime.UnmarshalPayload(r.Body, user); err != nil {
  112. http.Error(w, err.Error(), 500)
  113. fmt.Println(err.Error())
  114.  
  115. return
  116. }
  117.  
  118. UpdateUser(user)
  119.  
  120. if err := jsonApiRuntime.MarshalPayload(w, user); err != nil {
  121. http.Error(w, err.Error(), 500)
  122. }
  123.  
  124. w.WriteHeader(201)
  125. w.Header().Set("Content-Type", "application/vnd.api+json")
  126. }
Add Comment
Please, Sign In to add comment