all repos — piggy @ aac7712a4650e29b43bbaec3b3ac295214a16367

Dead simple finance manager in Go, HTML and JS.

src/api/api.go (view raw)

  1package api
  2
  3import (
  4	"encoding/json"
  5	"log"
  6	"net/http"
  7
  8	"github.com/BiRabittoh/piggy/src/app"
  9)
 10
 11func getBookmakers(w http.ResponseWriter, r *http.Request) {
 12	var bookmakers []app.Bookmaker
 13	err := app.DB.Find(&bookmakers).Error
 14	if err != nil {
 15		log.Println("could not get bookmakers: " + err.Error())
 16		new500Error(w, err)
 17		return
 18	}
 19
 20	jsonResponse(w, bookmakers)
 21}
 22
 23func getBookmakersId(w http.ResponseWriter, r *http.Request) {
 24	id, err := getId(r)
 25	if err != nil {
 26		new400Error(w, err)
 27		return
 28	}
 29
 30	var bookmaker app.Bookmaker
 31	err = app.DB.First(&bookmaker, id).Error
 32	if err != nil {
 33		log.Println("could not get bookmaker: " + err.Error())
 34		new500Error(w, err)
 35		return
 36	}
 37
 38	jsonResponse(w, bookmaker)
 39}
 40
 41func postBookmakers(w http.ResponseWriter, r *http.Request) {
 42	var bookmaker app.Bookmaker
 43	err := json.NewDecoder(r.Body).Decode(&bookmaker)
 44	if err != nil {
 45		log.Println("could not decode bookmaker JSON: " + err.Error())
 46		new400Error(w, err)
 47		return
 48	}
 49	err = app.DB.Save(&bookmaker).Error
 50	if err != nil {
 51		log.Println("could not save bookmaker: " + err.Error())
 52		new500Error(w, err)
 53		return
 54	}
 55
 56	jsonResponse(w, bookmaker)
 57}
 58
 59func getAccounts(w http.ResponseWriter, r *http.Request) {
 60	var accounts []app.Account
 61	err := app.DB.Find(&accounts).Error
 62	if err != nil {
 63		log.Println("could not get accounts: " + err.Error())
 64		new500Error(w, err)
 65		return
 66	}
 67
 68	jsonResponse(w, accounts)
 69}
 70
 71func getAccountsId(w http.ResponseWriter, r *http.Request) {
 72	id, err := getId(r)
 73	if err != nil {
 74		new400Error(w, err)
 75		return
 76	}
 77
 78	var account app.Account
 79	err = app.DB.First(&account, id).Error
 80	if err != nil {
 81		log.Println("could not get account: " + err.Error())
 82		new500Error(w, err)
 83		return
 84	}
 85
 86	jsonResponse(w, account)
 87}
 88
 89func postAccounts(w http.ResponseWriter, r *http.Request) {
 90	var account app.Account
 91	err := json.NewDecoder(r.Body).Decode(&account)
 92	if err != nil {
 93		log.Println("could not decode account JSON: " + err.Error())
 94		new400Error(w, err)
 95		return
 96	}
 97	err = app.DB.Save(&account).Error
 98	if err != nil {
 99		log.Println("could not save account: " + err.Error())
100		new500Error(w, err)
101		return
102	}
103
104	jsonResponse(w, account)
105}
106
107func getRecords(w http.ResponseWriter, r *http.Request) {
108	records, _, err := app.GetRecords()
109	if err != nil {
110		log.Println("could not get records: " + err.Error())
111		new500Error(w, err)
112	}
113
114	jsonResponse(w, records)
115}
116
117func getRecordsId(w http.ResponseWriter, r *http.Request) {
118	id, err := getId(r)
119	if err != nil {
120		new400Error(w, err)
121		return
122	}
123
124	record, err := app.GetRecord(id)
125	if err != nil {
126		log.Println("could not get record: " + err.Error())
127		new500Error(w, err)
128	}
129
130	jsonResponse(w, record)
131}
132
133func postRecords(w http.ResponseWriter, r *http.Request) {
134	var record app.Record
135	err := json.NewDecoder(r.Body).Decode(&record)
136	if err != nil {
137		log.Println("could not decode record JSON: " + err.Error())
138		new400Error(w, err)
139		return
140	}
141	err = app.DB.Save(&record).Error
142	if err != nil {
143		log.Println("could not save record: " + err.Error())
144		new500Error(w, err)
145		return
146	}
147
148	jsonResponse(w, record)
149}