all repos — piggy @ main

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	"gorm.io/gorm"
 10)
 11
 12func getBookmakers(w http.ResponseWriter, r *http.Request) {
 13	var bookmakers []app.Bookmaker
 14	err := app.DB.Find(&bookmakers).Error
 15	if err != nil {
 16		log.Println("could not get bookmakers: " + err.Error())
 17		new500Error(w, err)
 18		return
 19	}
 20
 21	jsonResponse(w, bookmakers)
 22}
 23
 24func getBookmakersId(w http.ResponseWriter, r *http.Request) {
 25	id, err := getId(r)
 26	if err != nil {
 27		new400Error(w, err)
 28		return
 29	}
 30
 31	var bookmaker app.Bookmaker
 32	err = app.DB.First(&bookmaker, id).Error
 33	if err != nil {
 34		log.Println("could not get bookmaker: " + err.Error())
 35		new500Error(w, err)
 36		return
 37	}
 38
 39	jsonResponse(w, bookmaker)
 40}
 41
 42func postBookmakers(w http.ResponseWriter, r *http.Request) {
 43	var bookmaker app.Bookmaker
 44	err := json.NewDecoder(r.Body).Decode(&bookmaker)
 45	if err != nil {
 46		log.Println("could not decode bookmaker JSON: " + err.Error())
 47		new400Error(w, err)
 48		return
 49	}
 50	err = app.DB.Save(&bookmaker).Error
 51	if err != nil {
 52		log.Println("could not save bookmaker: " + err.Error())
 53		new500Error(w, err)
 54		return
 55	}
 56
 57	jsonResponse(w, bookmaker)
 58}
 59
 60func deleteBookmakersId(w http.ResponseWriter, r *http.Request) {
 61	id, err := getId(r)
 62	if err != nil {
 63		new400Error(w, err)
 64		return
 65	}
 66
 67	err = app.DB.Delete(&app.Bookmaker{}, id).Error
 68	if err != nil {
 69		log.Println("could not delete bookmaker: " + err.Error())
 70		new500Error(w, err)
 71		return
 72	}
 73
 74	jsonResponse(w, []uint{id})
 75}
 76
 77func getAccounts(w http.ResponseWriter, r *http.Request) {
 78	var accounts []app.Account
 79	err := app.DB.Find(&accounts).Error
 80	if err != nil {
 81		log.Println("could not get accounts: " + err.Error())
 82		new500Error(w, err)
 83		return
 84	}
 85
 86	jsonResponse(w, accounts)
 87}
 88
 89func getAccountsId(w http.ResponseWriter, r *http.Request) {
 90	id, err := getId(r)
 91	if err != nil {
 92		new400Error(w, err)
 93		return
 94	}
 95
 96	var account app.Account
 97	err = app.DB.First(&account, id).Error
 98	if err != nil {
 99		log.Println("could not get account: " + err.Error())
100		new500Error(w, err)
101		return
102	}
103
104	jsonResponse(w, account)
105}
106
107func postAccounts(w http.ResponseWriter, r *http.Request) {
108	var account app.Account
109	err := json.NewDecoder(r.Body).Decode(&account)
110	if err != nil {
111		log.Println("could not decode account JSON: " + err.Error())
112		new400Error(w, err)
113		return
114	}
115	err = app.DB.Save(&account).Error
116	if err != nil {
117		log.Println("could not save account: " + err.Error())
118		new500Error(w, err)
119		return
120	}
121
122	jsonResponse(w, account)
123}
124
125func deleteAccountsId(w http.ResponseWriter, r *http.Request) {
126	id, err := getId(r)
127	if err != nil {
128		new400Error(w, err)
129		return
130	}
131
132	err = app.DB.Delete(&app.Account{}, id).Error
133	if err != nil {
134		log.Println("could not delete account: " + err.Error())
135		new500Error(w, err)
136		return
137	}
138
139	jsonResponse(w, []uint{id})
140}
141
142func getRecords(w http.ResponseWriter, r *http.Request) {
143	records, _, err := app.GetRecords()
144	if err != nil {
145		log.Println("could not get records: " + err.Error())
146		new500Error(w, err)
147	}
148
149	jsonResponse(w, records)
150}
151
152func getRecordsId(w http.ResponseWriter, r *http.Request) {
153	id, err := getId(r)
154	if err != nil {
155		new400Error(w, err)
156		return
157	}
158
159	record, err := app.GetRecord(id)
160	if err != nil {
161		log.Println("could not get record: " + err.Error())
162		new500Error(w, err)
163	}
164
165	jsonResponse(w, record)
166}
167
168func postRecords(w http.ResponseWriter, r *http.Request) {
169	var record app.Record
170	err := json.NewDecoder(r.Body).Decode(&record)
171	if err != nil {
172		log.Println("could not decode record JSON: " + err.Error())
173		new400Error(w, err)
174		return
175	}
176
177	if record.ID != 0 {
178		err = app.DB.Delete(&app.Entry{}, "record_id = ?", record.ID).Error
179		if err != nil {
180			log.Println("could not delete outdated entries: " + err.Error())
181			new500Error(w, err)
182			return
183		}
184	}
185
186	err = app.DB.Session(&gorm.Session{FullSaveAssociations: true}).Save(&record).Error
187	if err != nil {
188		log.Println("could not save record: " + err.Error())
189		new500Error(w, err)
190		return
191	}
192
193	jsonResponse(w, record)
194}
195
196func deleteRecordsId(w http.ResponseWriter, r *http.Request) {
197	id, err := getId(r)
198	if err != nil {
199		new400Error(w, err)
200		return
201	}
202
203	err = app.DB.Delete(&app.Record{}, id).Error
204	if err != nil {
205		log.Println("could not delete account: " + err.Error())
206		new500Error(w, err)
207		return
208	}
209
210	jsonResponse(w, []uint{id})
211}