| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226 |
- package files
- import (
- // "errors"
- "net/http"
- "io/ioutil"
- // "strconv"
- // "strings"
- "git.mmnx.de/mmnx/cdn/module"
- "github.com/gorilla/mux"
- )
- //TODO test mux var
- // repository.go holds all basic HTTP API methods
- func filesIndex(w http.ResponseWriter, r *http.Request) {
- switch r.Method {
- case "GET":
- getFiles(w, r)
- // getContacts(w, r)
- break
- case "POST":
- // postContact(w, r)
- break
- case "PUT":
- // putContact(w, r)
- break
- }
- }
- func filesSingle(w http.ResponseWriter, r *http.Request) {
- switch r.Method {
- case "GET":
- // getContact(w, r)
- break
- case "PATCH":
- // patchContact(w, r)
- break
- case "DELETE":
- // deleteContact(w, r)
- break
- }
- }
- func filesRaw(w http.ResponseWriter, r *http.Request) {
- if r.Method == "GET" {
- fileID := mux.Vars(r)["fileID"]
- file := &File{}
- // get file from DB
- module.HandleError(module.ErrDatabase, db.Preload("Storage").Find(&file, "id = ?", fileID).Error)
- // read file contents
- dat, err := ioutil.ReadFile(file.Storage.AbsolutePath + file.RelativePath)
- module.HandleError(module.ErrReadingFile, err)
- // output file contents
- w.Write(dat)
- }
- }
- func getFiles(w http.ResponseWriter, r *http.Request) {
- files := []File{}
- module.HandleError(module.ErrDatabase, db.Preload("Storage").Find(&files).Error) // get db records
- module.JSONResp(w, files)
- }
- func mockFiles(w http.ResponseWriter, r *http.Request) {
- createTestData()
- // files := []File{}
- // for i := range people {
- // // "convert" us house-nr format to eu
- // street := strings.Title(people[i].Location["street"].(string))
- // streetSpl := strings.Split(street, " ")
- // houseNr, err := strconv.Atoi(streetSpl[0])
- // module.HandleError(module.ErrParsingInt, err)
- // houseNr = houseNr / 400
- // street = strings.Join(streetSpl[1:], " ") + " " + strconv.Itoa(houseNr)
- // con := Contact{
- // Prefix: func(t string) string {
- // if t == "mr" {
- // return "Hr."
- // }
- // return "Fr."
- // }(people[i].Name["title"]),
- // FirstName: strings.Title(people[i].Name["first"]),
- // LastName: strings.Title(people[i].Name["last"]),
- // Phones: []Phone{Phone{Type: "Mobil", Number: people[i].Cell}, Phone{Type: "Privat", Number: people[i].Phone}},
- // Emails: []Email{Email{Type: "Privat", Address: people[i].Email}},
- // 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"}},
- // }
- // cons = append(cons, con)
- // module.HandleError(module.ErrDatabase, db.Create(&con).Error) // create in DB
- // }
- module.JSONResp(w, "done")
- }
- // func getContacts(w http.ResponseWriter, r *http.Request) {
- // contacts := []Contact{}
- // module.HandleError(module.ErrDatabase, db.Preload("Emails").Preload("Phones").Preload("Addresses").Find(&contacts).Error) // get db records
- // module.JSONResp(w, contacts)
- // }
- // func getContact(w http.ResponseWriter, r *http.Request) {
- // // contact := Contact{}
- // //
- // // module.HandleError(module.ErrDatabase, db.Preload("Emails").Preload("Phones").Preload("Addresses").First(&contact, "id = ?", mux.Vars(r)["contactID"]).Error) // get DB record with relations
- // module.JSONResp(w, getFullContact(mux.Vars(r)["contactID"]))
- // }
- // func postContact(w http.ResponseWriter, r *http.Request) {
- // contact := Contact{}
- // module.JSONReq(r, &contact) // decode POST data
- // module.HandleError(module.ErrDatabase, db.Create(&contact).Error) // create in DB
- // module.JSONResp(w, getFullContact(strconv.Itoa(int(contact.ID))))
- // }
- // func putContact(w http.ResponseWriter, r *http.Request) {
- // contact := Contact{}
- // module.JSONReq(r, &contact) // decode POST data
- // module.HandleError(module.ErrDatabase, db.Save(&contact).Error) // update in DB
- // module.JSONResp(w, getFullContact(strconv.Itoa(int(contact.ID))))
- // }
- // func patchContact(w http.ResponseWriter, r *http.Request) {
- // var updates []map[string]interface{}
- // parsedUpdates := make(map[string]interface{})
- // contact := Contact{}
- // module.JSONReq(r, &updates) // decode POST data
- // for i := range updates {
- // for k, v := range updates[i] {
- // parsedUpdates[k] = v
- // }
- // }
- // module.HandleError(module.ErrDatabase, db.Find(&contact, "id = ?", mux.Vars(r)["contactID"]).Error) // get contact
- // module.HandleError(module.ErrDatabase, db.Model(&contact).Update(parsedUpdates).Error) // update
- // contact = Contact{}
- // module.JSONResp(w, getFullContact(mux.Vars(r)["contactID"]))
- // }
- // func deleteContact(w http.ResponseWriter, r *http.Request) {
- // count := -1
- // module.HandleError(module.ErrDatabase, db.Model(Contact{}).Where("id = ?", mux.Vars(r)["contactID"]).Count(&count).Error) // get contact count with contactID
- // if count != 1 { // contact has to be identifiable
- // module.HandleError(module.ErrDatabase, errors.New("record not found"))
- // }
- // module.HandleError(module.ErrDatabase, db.Delete(Contact{}, mux.Vars(r)["contactID"]).Error) // soft delete
- // status := make(map[string]string)
- // status["status"] = "ok"
- // module.JSONResp(w, status)
- // }
- // // mockContacts gets some random person data and fills the database with them
- // func mockContacts(w http.ResponseWriter, r *http.Request) {
- // people := createRandomTestData()
- // cons := []Contact{}
- // for i := range people {
- // // "convert" us house-nr format to eu
- // street := strings.Title(people[i].Location["street"].(string))
- // streetSpl := strings.Split(street, " ")
- // houseNr, err := strconv.Atoi(streetSpl[0])
- // module.HandleError(module.ErrParsingInt, err)
- // houseNr = houseNr / 400
- // street = strings.Join(streetSpl[1:], " ") + " " + strconv.Itoa(houseNr)
- // con := Contact{
- // Prefix: func(t string) string {
- // if t == "mr" {
- // return "Hr."
- // }
- // return "Fr."
- // }(people[i].Name["title"]),
- // FirstName: strings.Title(people[i].Name["first"]),
- // LastName: strings.Title(people[i].Name["last"]),
- // Phones: []Phone{Phone{Type: "Mobil", Number: people[i].Cell}, Phone{Type: "Privat", Number: people[i].Phone}},
- // Emails: []Email{Email{Type: "Privat", Address: people[i].Email}},
- // 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"}},
- // }
- // cons = append(cons, con)
- // module.HandleError(module.ErrDatabase, db.Create(&con).Error) // create in DB
- // }
- // module.JSONResp(w, cons)
- // }
- // // Helpers
- // func getFullContact(contactID string) Contact {
- // contact := Contact{}
- // module.HandleError(module.ErrDatabase, db.Preload("Emails").Preload("Phones").Preload("Addresses").First(&contact, "id = ?", contactID).Error) // get DB record with relations
- // return contact
- // }
|