repository.go 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. package files
  2. import (
  3. // "errors"
  4. "net/http"
  5. "io/ioutil"
  6. // "strconv"
  7. // "strings"
  8. "git.mmnx.de/mmnx/cdn/module"
  9. "github.com/gorilla/mux"
  10. )
  11. //TODO test mux var
  12. // repository.go holds all basic HTTP API methods
  13. func filesIndex(w http.ResponseWriter, r *http.Request) {
  14. switch r.Method {
  15. case "GET":
  16. getFiles(w, r)
  17. // getContacts(w, r)
  18. break
  19. case "POST":
  20. // postContact(w, r)
  21. break
  22. case "PUT":
  23. // putContact(w, r)
  24. break
  25. }
  26. }
  27. func filesSingle(w http.ResponseWriter, r *http.Request) {
  28. switch r.Method {
  29. case "GET":
  30. // getContact(w, r)
  31. break
  32. case "PATCH":
  33. // patchContact(w, r)
  34. break
  35. case "DELETE":
  36. // deleteContact(w, r)
  37. break
  38. }
  39. }
  40. func filesRaw(w http.ResponseWriter, r *http.Request) {
  41. if r.Method == "GET" {
  42. fileID := mux.Vars(r)["fileID"]
  43. file := &File{}
  44. // get file from DB
  45. module.HandleError(module.ErrDatabase, db.Preload("Storage").Find(&file, "id = ?", fileID).Error)
  46. // read file contents
  47. dat, err := ioutil.ReadFile(file.Storage.AbsolutePath + file.RelativePath)
  48. module.HandleError(module.ErrReadingFile, err)
  49. // output file contents
  50. w.Write(dat)
  51. }
  52. }
  53. func getFiles(w http.ResponseWriter, r *http.Request) {
  54. files := []File{}
  55. module.HandleError(module.ErrDatabase, db.Preload("Storage").Find(&files).Error) // get db records
  56. module.JSONResp(w, files)
  57. }
  58. func mockFiles(w http.ResponseWriter, r *http.Request) {
  59. createTestData()
  60. // files := []File{}
  61. // for i := range people {
  62. // // "convert" us house-nr format to eu
  63. // street := strings.Title(people[i].Location["street"].(string))
  64. // streetSpl := strings.Split(street, " ")
  65. // houseNr, err := strconv.Atoi(streetSpl[0])
  66. // module.HandleError(module.ErrParsingInt, err)
  67. // houseNr = houseNr / 400
  68. // street = strings.Join(streetSpl[1:], " ") + " " + strconv.Itoa(houseNr)
  69. // con := Contact{
  70. // Prefix: func(t string) string {
  71. // if t == "mr" {
  72. // return "Hr."
  73. // }
  74. // return "Fr."
  75. // }(people[i].Name["title"]),
  76. // FirstName: strings.Title(people[i].Name["first"]),
  77. // LastName: strings.Title(people[i].Name["last"]),
  78. // Phones: []Phone{Phone{Type: "Mobil", Number: people[i].Cell}, Phone{Type: "Privat", Number: people[i].Phone}},
  79. // Emails: []Email{Email{Type: "Privat", Address: people[i].Email}},
  80. // Addresses: []Address{Address{Type: "Privat", Street: street, ZIPCode: strconv.FormatFloat(people[i].Location["postcode"].(float64), 'g', 5, 64), City: strings.Title(people[i].Location["city"].(string)), State: strings.Title(people[i].Location["state"].(string)), Country: "Deutschland"}},
  81. // }
  82. // cons = append(cons, con)
  83. // module.HandleError(module.ErrDatabase, db.Create(&con).Error) // create in DB
  84. // }
  85. module.JSONResp(w, "done")
  86. }
  87. // func getContacts(w http.ResponseWriter, r *http.Request) {
  88. // contacts := []Contact{}
  89. // module.HandleError(module.ErrDatabase, db.Preload("Emails").Preload("Phones").Preload("Addresses").Find(&contacts).Error) // get db records
  90. // module.JSONResp(w, contacts)
  91. // }
  92. // func getContact(w http.ResponseWriter, r *http.Request) {
  93. // // contact := Contact{}
  94. // //
  95. // // module.HandleError(module.ErrDatabase, db.Preload("Emails").Preload("Phones").Preload("Addresses").First(&contact, "id = ?", mux.Vars(r)["contactID"]).Error) // get DB record with relations
  96. // module.JSONResp(w, getFullContact(mux.Vars(r)["contactID"]))
  97. // }
  98. // func postContact(w http.ResponseWriter, r *http.Request) {
  99. // contact := Contact{}
  100. // module.JSONReq(r, &contact) // decode POST data
  101. // module.HandleError(module.ErrDatabase, db.Create(&contact).Error) // create in DB
  102. // module.JSONResp(w, getFullContact(strconv.Itoa(int(contact.ID))))
  103. // }
  104. // func putContact(w http.ResponseWriter, r *http.Request) {
  105. // contact := Contact{}
  106. // module.JSONReq(r, &contact) // decode POST data
  107. // module.HandleError(module.ErrDatabase, db.Save(&contact).Error) // update in DB
  108. // module.JSONResp(w, getFullContact(strconv.Itoa(int(contact.ID))))
  109. // }
  110. // func patchContact(w http.ResponseWriter, r *http.Request) {
  111. // var updates []map[string]interface{}
  112. // parsedUpdates := make(map[string]interface{})
  113. // contact := Contact{}
  114. // module.JSONReq(r, &updates) // decode POST data
  115. // for i := range updates {
  116. // for k, v := range updates[i] {
  117. // parsedUpdates[k] = v
  118. // }
  119. // }
  120. // module.HandleError(module.ErrDatabase, db.Find(&contact, "id = ?", mux.Vars(r)["contactID"]).Error) // get contact
  121. // module.HandleError(module.ErrDatabase, db.Model(&contact).Update(parsedUpdates).Error) // update
  122. // contact = Contact{}
  123. // module.JSONResp(w, getFullContact(mux.Vars(r)["contactID"]))
  124. // }
  125. // func deleteContact(w http.ResponseWriter, r *http.Request) {
  126. // count := -1
  127. // module.HandleError(module.ErrDatabase, db.Model(Contact{}).Where("id = ?", mux.Vars(r)["contactID"]).Count(&count).Error) // get contact count with contactID
  128. // if count != 1 { // contact has to be identifiable
  129. // module.HandleError(module.ErrDatabase, errors.New("record not found"))
  130. // }
  131. // module.HandleError(module.ErrDatabase, db.Delete(Contact{}, mux.Vars(r)["contactID"]).Error) // soft delete
  132. // status := make(map[string]string)
  133. // status["status"] = "ok"
  134. // module.JSONResp(w, status)
  135. // }
  136. // // mockContacts gets some random person data and fills the database with them
  137. // func mockContacts(w http.ResponseWriter, r *http.Request) {
  138. // people := createRandomTestData()
  139. // cons := []Contact{}
  140. // for i := range people {
  141. // // "convert" us house-nr format to eu
  142. // street := strings.Title(people[i].Location["street"].(string))
  143. // streetSpl := strings.Split(street, " ")
  144. // houseNr, err := strconv.Atoi(streetSpl[0])
  145. // module.HandleError(module.ErrParsingInt, err)
  146. // houseNr = houseNr / 400
  147. // street = strings.Join(streetSpl[1:], " ") + " " + strconv.Itoa(houseNr)
  148. // con := Contact{
  149. // Prefix: func(t string) string {
  150. // if t == "mr" {
  151. // return "Hr."
  152. // }
  153. // return "Fr."
  154. // }(people[i].Name["title"]),
  155. // FirstName: strings.Title(people[i].Name["first"]),
  156. // LastName: strings.Title(people[i].Name["last"]),
  157. // Phones: []Phone{Phone{Type: "Mobil", Number: people[i].Cell}, Phone{Type: "Privat", Number: people[i].Phone}},
  158. // Emails: []Email{Email{Type: "Privat", Address: people[i].Email}},
  159. // Addresses: []Address{Address{Type: "Privat", Street: street, ZIPCode: strconv.FormatFloat(people[i].Location["postcode"].(float64), 'g', 5, 64), City: strings.Title(people[i].Location["city"].(string)), State: strings.Title(people[i].Location["state"].(string)), Country: "Deutschland"}},
  160. // }
  161. // cons = append(cons, con)
  162. // module.HandleError(module.ErrDatabase, db.Create(&con).Error) // create in DB
  163. // }
  164. // module.JSONResp(w, cons)
  165. // }
  166. // // Helpers
  167. // func getFullContact(contactID string) Contact {
  168. // contact := Contact{}
  169. // module.HandleError(module.ErrDatabase, db.Preload("Emails").Preload("Phones").Preload("Addresses").First(&contact, "id = ?", contactID).Error) // get DB record with relations
  170. // return contact
  171. // }