Browse Source

Updated README, minor changes

main
Unbewohnte 3 years ago
parent
commit
3496370e0d
  1. 9
      README.md
  2. 11
      dbHandle/handle.go

9
README.md

@ -1,7 +1,7 @@
# CRUD api
## A simple CRUD api written in Go
API has implementation of "GET", "POST", "PATCH", "DELETE" http methods, allowing to Create, Read, Update and Delete objects in sqlite3 database via json input.
API has implementation of "GET", "POST", "PATCH", "DELETE" http methods, allowing to Read, Create, Update and Delete objects in sqlite3 database via json input.
---
@ -13,6 +13,13 @@ Implemented:
- **PATCH**
- **DELETE**
## Examples
`curl localhost:8000/randomdata` - to get EVERYTHING (obviously a bad idea if you have lots of data)
`curl localhost:8000/randomdata -H "content-type:application/json" -d '{"title":"This is a title","text":"This is a text"}' -X POST` - to create a new RandomData (IDs are created automatically from 1-∞)
`curl localhost:8000/randomdata/1` - to get the first RandomData you`ve created
`curl localhost:8000/randomdata/1 -H "content-type:application/json" -d {"title":"This is an updated title","text":"This is an updated text"}' -X PUT` - to update the first RandomData
`curl localhost:8000/randomdata/1 -X DELETE` - to delete the first RandomData
---
It`s not a recommended or even the correct way of doing a CRUD api of such sort, I'm just practicing

11
dbHandle/handle.go

@ -1,7 +1,6 @@
package dbhandle
import (
"encoding/json"
"fmt"
"io"
"log"
@ -126,8 +125,7 @@ func (db *DB) HandleGlobalWeb(w http.ResponseWriter, r *http.Request) {
return
}
var randomData randomdata.RandomData
err = json.Unmarshal(body, &randomData)
randomData, err := randomdata.FromJson(body)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
return
@ -137,7 +135,7 @@ func (db *DB) HandleGlobalWeb(w http.ResponseWriter, r *http.Request) {
randomData.DateCreated = time.Now().UTC().Unix()
randomData.LastUpdated = time.Now().UTC().Unix()
err = db.Create(randomData)
err = db.Create(*randomData)
if err != nil {
log.Printf("Could not create a row: %s", err)
}
@ -216,8 +214,7 @@ func (db *DB) HandleSpecificWeb(w http.ResponseWriter, r *http.Request) {
return
}
var randomData randomdata.RandomData
err = json.Unmarshal(body, &randomData)
randomData, err := randomdata.FromJson(body)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
return
@ -226,7 +223,7 @@ func (db *DB) HandleSpecificWeb(w http.ResponseWriter, r *http.Request) {
// create date created, last updated
randomData.LastUpdated = time.Now().UTC().Unix()
err = db.UpdateSpecific(uint(providedID), randomData)
err = db.UpdateSpecific(uint(providedID), *randomData)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
log.Printf("Could not update a RandomData: %s\n", err)

Loading…
Cancel
Save