github.com/jancarloviray/community@v0.41.1-0.20170124221257-33a66c87cf2f/core/api/endpoint/org_endpoint.go (about) 1 // Copyright 2016 Documize Inc. <legal@documize.com>. All rights reserved. 2 // 3 // This software (Documize Community Edition) is licensed under 4 // GNU AGPL v3 http://www.gnu.org/licenses/agpl-3.0.en.html 5 // 6 // You can operate outside the AGPL restrictions by purchasing 7 // Documize Enterprise Edition and obtaining a commercial license 8 // by contacting <sales@documize.com>. 9 // 10 // https://documize.com 11 12 package endpoint 13 14 import ( 15 // "bytes" 16 "database/sql" 17 "encoding/json" 18 "io/ioutil" 19 "net/http" 20 21 "github.com/documize/community/core/api/entity" 22 "github.com/documize/community/core/api/request" 23 "github.com/documize/community/core/log" 24 "github.com/documize/community/core/utility" 25 26 "github.com/gorilla/mux" 27 ) 28 29 // GetOrganization returns the requested organization. 30 func GetOrganization(w http.ResponseWriter, r *http.Request) { 31 method := "GetOrganization" 32 p := request.GetPersister(r) 33 34 params := mux.Vars(r) 35 orgID := params["orgID"] 36 37 if orgID != p.Context.OrgID { 38 writeForbiddenError(w) 39 return 40 } 41 42 org, err := p.GetOrganization(p.Context.OrgID) 43 44 if err != nil && err != sql.ErrNoRows { 45 writeServerError(w, method, err) 46 return 47 } 48 49 json, err := json.Marshal(org) 50 51 if err != nil { 52 writeJSONMarshalError(w, method, "organization", err) 53 return 54 } 55 56 writeSuccessBytes(w, json) 57 } 58 59 // UpdateOrganization saves organization amends. 60 func UpdateOrganization(w http.ResponseWriter, r *http.Request) { 61 method := "UpdateOrganization" 62 p := request.GetPersister(r) 63 64 if !p.Context.Administrator { 65 writeForbiddenError(w) 66 return 67 } 68 69 defer utility.Close(r.Body) 70 body, err := ioutil.ReadAll(r.Body) 71 72 if err != nil { 73 writePayloadError(w, method, err) 74 return 75 } 76 77 var org = entity.Organization{} 78 err = json.Unmarshal(body, &org) 79 80 org.RefID = p.Context.OrgID 81 82 tx, err := request.Db.Beginx() 83 84 if err != nil { 85 writeTransactionError(w, method, err) 86 return 87 } 88 89 p.Context.Transaction = tx 90 91 err = p.UpdateOrganization(org) 92 93 if err != nil { 94 log.IfErr(tx.Rollback()) 95 writeGeneralSQLError(w, method, err) 96 return 97 } 98 99 log.IfErr(tx.Commit()) 100 101 json, err := json.Marshal(org) 102 103 if err != nil { 104 writeJSONMarshalError(w, method, "organization", err) 105 return 106 } 107 108 writeSuccessBytes(w, json) 109 }