Browse Source

GetSpecific RandomData from db

main
Unbewohnte 3 years ago
parent
commit
20481f7110
  1. 1
      dbHandle/db.go
  2. 70
      dbHandle/handle.go
  3. 6
      randomData/randomData.go

1
dbHandle/db.go

@ -7,4 +7,5 @@ type DB struct {
} }
const drivername string = "sqlite3" const drivername string = "sqlite3"
var tableName string var tableName string

70
dbHandle/handle.go

@ -6,11 +6,14 @@ import (
"io" "io"
"log" "log"
"net/http" "net/http"
"strconv"
"strings"
randomdata "github.com/Unbewohnte/crud-api/randomData" randomdata "github.com/Unbewohnte/crud-api/randomData"
_ "github.com/mattn/go-sqlite3" _ "github.com/mattn/go-sqlite3"
) )
// Collect and return all rows in db
func (db *DB) GetEverything() ([]*randomdata.RandomData, error) { func (db *DB) GetEverything() ([]*randomdata.RandomData, error) {
rows, err := db.Query(fmt.Sprintf("SELECT * FROM %s", tableName)) rows, err := db.Query(fmt.Sprintf("SELECT * FROM %s", tableName))
if err != nil { if err != nil {
@ -37,18 +40,44 @@ func (db *DB) GetEverything() ([]*randomdata.RandomData, error) {
return contents, nil return contents, nil
} }
func (db *DB) GetSpecific() (*randomdata.RandomData, error) { // Get `RandomData` from db with given id
func (db *DB) GetSpecific(id uint) (*randomdata.RandomData, error) {
row, err := db.Query(fmt.Sprintf("SELECT * FROM %s WHERE id=%d", tableName, id))
if err != nil {
return nil, err
}
// there should be only one row, because we looked for a specific ID
for row.Next() {
var id uint
var title string
var text string
row.Scan(&id, &title, &text)
var randomData = randomdata.RandomData{
ID: id,
Title: title,
Text: text,
}
return &randomData, nil
}
return nil, nil return nil, nil
} }
func (db *DB) DeleteSpecific() error { // Delete `RandomData` from db with given id
func (db *DB) DeleteSpecific(id uint) error {
return nil return nil
} }
func (db *DB) PatchSpecific() error { // Edit `Randomdata` from db with given id
func (db *DB) PatchSpecific(id uint) error {
return nil return nil
} }
// Create a new `RandomData` row in db
func (db *DB) Create(rd randomdata.RandomData) error { func (db *DB) Create(rd randomdata.RandomData) error {
_, err := db.Exec(fmt.Sprintf("INSERT INTO %s (title, text) VALUES (?, ?)", tableName), rd.Title, rd.Text) _, err := db.Exec(fmt.Sprintf("INSERT INTO %s (title, text) VALUES (?, ?)", tableName), rd.Title, rd.Text)
if err != nil { if err != nil {
@ -58,6 +87,7 @@ func (db *DB) Create(rd randomdata.RandomData) error {
return nil return nil
} }
// Handler function for all `RandomData`s in database
func (db *DB) HandleGlobalWeb(w http.ResponseWriter, r *http.Request) { func (db *DB) HandleGlobalWeb(w http.ResponseWriter, r *http.Request) {
switch r.Method { switch r.Method {
@ -96,7 +126,7 @@ func (db *DB) HandleGlobalWeb(w http.ResponseWriter, r *http.Request) {
log.Printf("Could not retrieve db contents: %s\n", err) log.Printf("Could not retrieve db contents: %s\n", err)
return return
} }
randomDatasJsonBytes, err := randomdata.ToJsonAll(randomDatas) randomDatasJsonBytes, err := randomdata.ToJsonAll(randomDatas, true)
if err != nil { if err != nil {
w.WriteHeader(http.StatusInternalServerError) w.WriteHeader(http.StatusInternalServerError)
log.Printf("Could not convert to json: %s\n", err) log.Printf("Could not convert to json: %s\n", err)
@ -113,6 +143,38 @@ func (db *DB) HandleGlobalWeb(w http.ResponseWriter, r *http.Request) {
} }
// Handler function for a specific `RandomData` in database
func (db *DB) HandleSpecificWeb(w http.ResponseWriter, r *http.Request) { func (db *DB) HandleSpecificWeb(w http.ResponseWriter, r *http.Request) {
providedIDstr := strings.Split(r.RequestURI, "/")[2]
providedID, err := strconv.ParseUint(providedIDstr, 10, 32)
if err != nil {
// most likely a bad id
w.WriteHeader(http.StatusBadRequest)
return
}
switch r.Method {
case http.MethodGet:
randomData, err := db.GetSpecific(uint(providedID))
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
log.Printf("Could not retrieve a specific RandomData: %s\n", err)
return
}
rdJsonBytes, err := randomData.ToJson()
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
log.Printf("Could not convert RandomData to Json: %s\n", err)
}
w.Write(rdJsonBytes)
return
default:
w.WriteHeader(http.StatusMethodNotAllowed)
return
}
} }

6
randomData/randomData.go

@ -35,7 +35,7 @@ func (rd *RandomData) ToJson() ([]byte, error) {
} }
// Convert all rdatas to []byte // Convert all rdatas to []byte
func ToJsonAll(rdatas []*RandomData) ([]byte, error) { func ToJsonAll(rdatas []*RandomData, addNewlines bool) ([]byte, error) {
var rdatasBytes []byte var rdatasBytes []byte
for _, rdata := range rdatas { for _, rdata := range rdatas {
jsonBytes, err := rdata.ToJson() jsonBytes, err := rdata.ToJson()
@ -43,6 +43,10 @@ func ToJsonAll(rdatas []*RandomData) ([]byte, error) {
return nil, err return nil, err
} }
rdatasBytes = append(rdatasBytes, jsonBytes...) rdatasBytes = append(rdatasBytes, jsonBytes...)
if addNewlines {
rdatasBytes = append(rdatasBytes, 10)
}
} }
return rdatasBytes, nil return rdatasBytes, nil

Loading…
Cancel
Save