all repos — piggy @ bbb48c59b31feefe1c469fb20c41527184610062

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 deleteBookmakersId(w http.ResponseWriter, r *http.Request) {
 60	id, err := getId(r)
 61	if err != nil {
 62		new400Error(w, err)
 63		return
 64	}
 65
 66	err = app.DB.Delete(&app.Bookmaker{}, id).Error
 67	if err != nil {
 68		log.Println("could not delete bookmaker: " + err.Error())
 69		new500Error(w, err)
 70		return
 71	}
 72
 73	jsonResponse(w, []uint{id})
 74}
 75
 76func getAccounts(w http.ResponseWriter, r *http.Request) {
 77	var accounts []app.Account
 78	err := app.DB.Find(&accounts).Error
 79	if err != nil {
 80		log.Println("could not get accounts: " + err.Error())
 81		new500Error(w, err)
 82		return
 83	}
 84
 85	jsonResponse(w, accounts)
 86}
 87
 88func getAccountsId(w http.ResponseWriter, r *http.Request) {
 89	id, err := getId(r)
 90	if err != nil {
 91		new400Error(w, err)
 92		return
 93	}
 94
 95	var account app.Account
 96	err = app.DB.First(&account, id).Error
 97	if err != nil {
 98		log.Println("could not get account: " + err.Error())
 99		new500Error(w, err)
100		return
101	}
102
103	jsonResponse(w, account)
104}
105
106func postAccounts(w http.ResponseWriter, r *http.Request) {
107	var account app.Account
108	err := json.NewDecoder(r.Body).Decode(&account)
109	if err != nil {
110		log.Println("could not decode account JSON: " + err.Error())
111		new400Error(w, err)
112		return
113	}
114	err = app.DB.Save(&account).Error
115	if err != nil {
116		log.Println("could not save account: " + err.Error())
117		new500Error(w, err)
118		return
119	}
120
121	jsonResponse(w, account)
122}
123
124func deleteAccountsId(w http.ResponseWriter, r *http.Request) {
125	id, err := getId(r)
126	if err != nil {
127		new400Error(w, err)
128		return
129	}
130
131	err = app.DB.Delete(&app.Account{}, id).Error
132	if err != nil {
133		log.Println("could not delete account: " + err.Error())
134		new500Error(w, err)
135		return
136	}
137
138	jsonResponse(w, []uint{id})
139}
140
141func getRecords(w http.ResponseWriter, r *http.Request) {
142	records, _, err := app.GetRecords()
143	if err != nil {
144		log.Println("could not get records: " + err.Error())
145		new500Error(w, err)
146	}
147
148	jsonResponse(w, records)
149}
150
151func getRecordsId(w http.ResponseWriter, r *http.Request) {
152	id, err := getId(r)
153	if err != nil {
154		new400Error(w, err)
155		return
156	}
157
158	record, err := app.GetRecord(id)
159	if err != nil {
160		log.Println("could not get record: " + err.Error())
161		new500Error(w, err)
162	}
163
164	jsonResponse(w, record)
165}
166
167func postRecords(w http.ResponseWriter, r *http.Request) {
168	var record app.Record
169	err := json.NewDecoder(r.Body).Decode(&record)
170	if err != nil {
171		log.Println("could not decode record JSON: " + err.Error())
172		new400Error(w, err)
173		return
174	}
175	err = app.DB.Save(&record).Error
176	if err != nil {
177		log.Println("could not save record: " + err.Error())
178		new500Error(w, err)
179		return
180	}
181
182	jsonResponse(w, record)
183}
184
185func deleteRecordsId(w http.ResponseWriter, r *http.Request) {
186	id, err := getId(r)
187	if err != nil {
188		new400Error(w, err)
189		return
190	}
191
192	err = app.DB.Delete(&app.Record{}, id).Error
193	if err != nil {
194		log.Println("could not delete account: " + err.Error())
195		new500Error(w, err)
196		return
197	}
198
199	jsonResponse(w, []uint{id})
200}