Unbewohnte
3 years ago
11 changed files with 302 additions and 378 deletions
@ -1,23 +0,0 @@ |
|||||||
[ |
|
||||||
{ |
|
||||||
"ID": 1618064636424100339, |
|
||||||
"DateCreated": "2021-04-10T14:23:56.424100065Z", |
|
||||||
"LastUpdated": "2021-04-10T14:23:56.424100065Z", |
|
||||||
"title": "FUMO?", |
|
||||||
"text": "FUMO" |
|
||||||
}, |
|
||||||
{ |
|
||||||
"ID": 1618064651615612586, |
|
||||||
"DateCreated": "2021-04-10T14:24:11.615612068Z", |
|
||||||
"LastUpdated": "2021-04-10T14:24:11.615612068Z", |
|
||||||
"title": "Title", |
|
||||||
"text": "text" |
|
||||||
}, |
|
||||||
{ |
|
||||||
"ID": 1618065099475731301, |
|
||||||
"DateCreated": "2021-04-10T14:31:39.47573104Z", |
|
||||||
"LastUpdated": "2021-04-10T14:31:39.47573104Z", |
|
||||||
"title": "link", |
|
||||||
"text": "https://youtu.be/dQw4w9WgXcQ" |
|
||||||
} |
|
||||||
] |
|
@ -0,0 +1,49 @@ |
|||||||
|
package dbhandle |
||||||
|
|
||||||
|
import ( |
||||||
|
"database/sql" |
||||||
|
"os" |
||||||
|
"path/filepath" |
||||||
|
|
||||||
|
_ "github.com/mattn/go-sqlite3" |
||||||
|
) |
||||||
|
|
||||||
|
// full path to the local database that has been created by `CreateLocalDB`
|
||||||
|
var dbpath string |
||||||
|
|
||||||
|
// Creates a local database file in the same directory
|
||||||
|
// as executable if does not exist already. Creates a table "randomdata" with
|
||||||
|
// such structure: (id PRIMARY KEY, title TEXT, text TEXT), which
|
||||||
|
// represents underlying fields of `RandomData`
|
||||||
|
func CreateLocalDB(dbName string) (*DB, error) { |
||||||
|
// double check if dbName is actually just a name, not a path
|
||||||
|
dbName = filepath.Base(dbName) |
||||||
|
|
||||||
|
executablePath, err := os.Executable() |
||||||
|
if err != nil { |
||||||
|
return nil, err |
||||||
|
} |
||||||
|
exeDir := filepath.Dir(executablePath) |
||||||
|
|
||||||
|
dbpath = filepath.Join(exeDir, dbName) |
||||||
|
|
||||||
|
// create db if does not exist
|
||||||
|
dbfile, err := os.OpenFile(dbpath, os.O_CREATE, os.ModePerm) |
||||||
|
if err != nil { |
||||||
|
return nil, err |
||||||
|
} |
||||||
|
dbfile.Close() |
||||||
|
|
||||||
|
// create table that suits `RandomData` struct
|
||||||
|
db, err := sql.Open(drivername, dbpath) |
||||||
|
if err != nil { |
||||||
|
return nil, err |
||||||
|
} |
||||||
|
|
||||||
|
_, err = db.Exec("CREATE TABLE IF NOT EXISTS randomdata (id INTEGER PRIMARY KEY, title TEXT, text TEXT)") |
||||||
|
if err != nil { |
||||||
|
return nil, err |
||||||
|
} |
||||||
|
|
||||||
|
return &DB{db}, nil |
||||||
|
} |
@ -0,0 +1,9 @@ |
|||||||
|
package dbhandle |
||||||
|
|
||||||
|
import "database/sql" |
||||||
|
|
||||||
|
type DB struct { |
||||||
|
*sql.DB |
||||||
|
} |
||||||
|
|
||||||
|
const drivername string = "sqlite3" |
@ -0,0 +1,29 @@ |
|||||||
|
package dbhandle |
||||||
|
|
||||||
|
import ( |
||||||
|
"net/http" |
||||||
|
|
||||||
|
randomdata "github.com/Unbewohnte/crud-api/randomData" |
||||||
|
_ "github.com/mattn/go-sqlite3" |
||||||
|
) |
||||||
|
|
||||||
|
func (db *DB) GetEverything() ([]*randomdata.RandomData, error) |
||||||
|
|
||||||
|
func (db *DB) GetSpecific() (*randomdata.RandomData, error) |
||||||
|
|
||||||
|
func (db *DB) DeleteSpecific() error |
||||||
|
|
||||||
|
func (db *DB) PatchSpecific() error |
||||||
|
|
||||||
|
func (db *DB) Create(rd randomdata.RandomData) error { |
||||||
|
_, err := db.Exec("INSERT INTO randomdata (title, text) VALUES (?, ?)", rd.Title, rd.Text) |
||||||
|
if err != nil { |
||||||
|
return err |
||||||
|
} |
||||||
|
|
||||||
|
return nil |
||||||
|
} |
||||||
|
|
||||||
|
func (db *DB) HandleSpecificWeb(w http.ResponseWriter, r *http.Request) |
||||||
|
|
||||||
|
func (db *DB) HandleGlobalWeb(w http.ResponseWriter, r *http.Request) |
@ -0,0 +1,5 @@ |
|||||||
|
module github.com/Unbewohnte/crud-api |
||||||
|
|
||||||
|
go 1.17 |
||||||
|
|
||||||
|
require github.com/mattn/go-sqlite3 v1.14.8 |
@ -0,0 +1,26 @@ |
|||||||
|
package logs |
||||||
|
|
||||||
|
import ( |
||||||
|
"log" |
||||||
|
"os" |
||||||
|
"path/filepath" |
||||||
|
) |
||||||
|
|
||||||
|
// Create a logfile in the same directory as executable and set output
|
||||||
|
// of `log` package to it
|
||||||
|
func SetUp() error { |
||||||
|
executablePath, err := os.Executable() |
||||||
|
if err != nil { |
||||||
|
return err |
||||||
|
} |
||||||
|
exeDir := filepath.Dir(executablePath) |
||||||
|
|
||||||
|
logfile, err := os.Create(filepath.Join(exeDir, "logs.log")) |
||||||
|
if err != nil { |
||||||
|
return err |
||||||
|
} |
||||||
|
|
||||||
|
log.SetOutput(logfile) |
||||||
|
|
||||||
|
return nil |
||||||
|
} |
@ -1,370 +1,168 @@ |
|||||||
package main |
package main |
||||||
|
|
||||||
import ( |
import ( |
||||||
"encoding/json" |
"flag" |
||||||
"fmt" |
"fmt" |
||||||
"io" |
|
||||||
"log" |
"log" |
||||||
"net/http" |
"net/http" |
||||||
"os" |
|
||||||
"path/filepath" |
|
||||||
"strconv" |
|
||||||
"strings" |
|
||||||
"time" |
"time" |
||||||
) |
|
||||||
|
|
||||||
type RandomData struct { |
|
||||||
// unexported for json
|
|
||||||
ID int64 |
|
||||||
DateCreated time.Time |
|
||||||
LastUpdated time.Time |
|
||||||
// exported for json
|
|
||||||
Title string `json:"title"` |
|
||||||
Text string `json:"text"` |
|
||||||
} |
|
||||||
|
|
||||||
type randomDataHandler struct { |
|
||||||
dbFilepath string |
|
||||||
} |
|
||||||
|
|
||||||
func InitLogs() { |
dbhandle "github.com/Unbewohnte/crud-api/dbHandle" |
||||||
var logsDir string = filepath.Join(".", "logs") |
"github.com/Unbewohnte/crud-api/logs" |
||||||
|
) |
||||||
err := os.MkdirAll(logsDir, os.ModePerm) |
|
||||||
if err != nil { |
|
||||||
panic(err) |
|
||||||
} |
|
||||||
logfile, err := os.Create(filepath.Join(logsDir, "logs.log")) |
|
||||||
if err != nil { |
|
||||||
panic(err) |
|
||||||
} |
|
||||||
log.SetOutput(logfile) |
|
||||||
} |
|
||||||
|
|
||||||
func homepage(w http.ResponseWriter, r *http.Request) { |
|
||||||
helpMessage := ` |
|
||||||
<h1>CRUD api in Go's standart library</h1> |
|
||||||
<ul> |
|
||||||
<li> (GET) <a href="/randomdata">/randomdata</a> - to get all database </li> |
|
||||||
<li> (GET) /randomdata/{id} - to get specific random data under corresponding id </li> |
|
||||||
<li> (POST) /randomdata - to create random data</li> |
|
||||||
<li> (DELETE) /randomdata/{id} - to delete specified random data </li> |
|
||||||
<li> (PUT) /randomdata/{id} - to update random data with given id </li> |
|
||||||
</ul> |
|
||||||
` |
|
||||||
fmt.Fprint(w, helpMessage) |
|
||||||
} |
|
||||||
|
|
||||||
func newDatabaseHandler() *randomDataHandler { |
var ( |
||||||
dbDirpath := filepath.Join(".", "database") |
port *uint = flag.Uint("port", 8080, "Specifies a port on which the helping page will be served") |
||||||
err := os.MkdirAll(dbDirpath, os.ModePerm) |
dbname string = "database.db" |
||||||
if err != nil { |
) |
||||||
panic(err) |
|
||||||
} |
|
||||||
dbFilepath := filepath.Join(dbDirpath, "database.json") |
|
||||||
|
|
||||||
dbFile, err := os.OpenFile(dbFilepath, os.O_CREATE, os.ModePerm) |
func init() { |
||||||
|
// set up logs, parse flags
|
||||||
|
err := logs.SetUp() |
||||||
if err != nil { |
if err != nil { |
||||||
panic(err) |
panic(err) |
||||||
} |
} |
||||||
defer dbFile.Close() |
|
||||||
|
|
||||||
log.Println("Successfully created new database handler") |
|
||||||
|
|
||||||
return &randomDataHandler{ |
|
||||||
dbFilepath: dbFilepath, |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
func (dbHandler *randomDataHandler) writeRandomData(newData RandomData) error { |
|
||||||
dbBytes, err := dbHandler.readDatabase() |
|
||||||
if err != nil { |
|
||||||
log.Println("Error reading db (writeRandomData) : ", err) |
|
||||||
} |
|
||||||
|
|
||||||
var db []RandomData |
|
||||||
|
|
||||||
err = json.Unmarshal(dbBytes, &db) |
|
||||||
if err != nil { |
|
||||||
log.Println("Error unmarshalling db (writeRandomData) : ", err) |
|
||||||
} |
|
||||||
|
|
||||||
db = append(db, newData) |
|
||||||
|
|
||||||
dbFile, err := os.OpenFile(dbHandler.dbFilepath, os.O_WRONLY, 0644) |
|
||||||
if err != nil { |
|
||||||
log.Println("Error opening db file (writeRandomData) : ", err) |
|
||||||
} |
|
||||||
defer dbFile.Close() |
|
||||||
|
|
||||||
jsonBytes, err := json.MarshalIndent(db, "", " ") |
|
||||||
if err != nil { |
|
||||||
log.Println("Error marshalling db (writeRandomData) : ", err) |
|
||||||
} |
|
||||||
|
|
||||||
dbFile.Write(jsonBytes) |
|
||||||
|
|
||||||
return nil |
|
||||||
} |
|
||||||
|
|
||||||
func (dbHandler *randomDataHandler) readDatabase() ([]byte, error) { |
|
||||||
dbBytes, err := os.ReadFile(dbHandler.dbFilepath) |
|
||||||
if err != nil { |
|
||||||
log.Println("Error reading db (readDatabase) : ", err) |
|
||||||
return nil, err |
|
||||||
} |
|
||||||
return dbBytes, nil |
|
||||||
} |
|
||||||
|
|
||||||
func (dbHandler *randomDataHandler) removeRandomData(id int64) error { |
|
||||||
dbBytes, err := dbHandler.readDatabase() |
|
||||||
if err != nil { |
|
||||||
return err |
|
||||||
} |
|
||||||
|
|
||||||
var db []RandomData |
|
||||||
err = json.Unmarshal(dbBytes, &db) |
|
||||||
if err != nil { |
|
||||||
return err |
|
||||||
} |
|
||||||
|
|
||||||
var counter int64 = 0 |
flag.Parse() |
||||||
for _, randomData := range db { |
|
||||||
if id == randomData.ID { |
|
||||||
db = append(db[:counter], db[counter+1:]...) |
|
||||||
err = dbHandler.writeDB(db) |
|
||||||
if err != nil { |
|
||||||
return err |
|
||||||
} |
|
||||||
} |
|
||||||
counter++ |
|
||||||
} |
|
||||||
return nil |
|
||||||
} |
} |
||||||
|
|
||||||
func (dbHandler *randomDataHandler) writeDB(db []RandomData) error { |
func main() { |
||||||
jsonEncodedDB, err := json.MarshalIndent(db, "", " ") |
// create a local db file
|
||||||
if err != nil { |
db, err := dbhandle.CreateLocalDB(dbname) |
||||||
return err |
|
||||||
} |
|
||||||
|
|
||||||
dbFile, err := os.OpenFile(dbHandler.dbFilepath, os.O_WRONLY|os.O_TRUNC, 0644) |
|
||||||
if err != nil { |
|
||||||
return err |
|
||||||
} |
|
||||||
defer dbFile.Close() |
|
||||||
|
|
||||||
dbFile.Write(jsonEncodedDB) |
|
||||||
|
|
||||||
return nil |
|
||||||
} |
|
||||||
|
|
||||||
func (dbHandler *randomDataHandler) get(w http.ResponseWriter, r *http.Request) { |
|
||||||
dbBytes, err := dbHandler.readDatabase() |
|
||||||
if err != nil { |
|
||||||
log.Println("Error reading db (get) : ", err) |
|
||||||
w.WriteHeader(http.StatusInternalServerError) |
|
||||||
return |
|
||||||
} |
|
||||||
|
|
||||||
w.Write(dbBytes) |
|
||||||
} |
|
||||||
|
|
||||||
func (dbHandler *randomDataHandler) create(w http.ResponseWriter, r *http.Request) { |
|
||||||
if r.Header.Get("content-type") != "application/json" { |
|
||||||
w.WriteHeader(http.StatusUnsupportedMediaType) |
|
||||||
w.Write([]byte(fmt.Sprintf("Got `%s` instead of `application/json`", r.Header.Get("content-type")))) |
|
||||||
return |
|
||||||
} |
|
||||||
requestBody, err := io.ReadAll(r.Body) |
|
||||||
if err != nil { |
|
||||||
log.Println("Error reading http request (create) : ", err) |
|
||||||
w.WriteHeader(http.StatusBadRequest) |
|
||||||
return |
|
||||||
} |
|
||||||
defer r.Body.Close() |
|
||||||
|
|
||||||
var newRandomData RandomData |
|
||||||
err = json.Unmarshal(requestBody, &newRandomData) |
|
||||||
if err != nil { |
|
||||||
log.Printf("Error unmarshalling http request (create) : %q \n", err) |
|
||||||
w.WriteHeader(http.StatusBadRequest) |
|
||||||
return |
|
||||||
} |
|
||||||
|
|
||||||
newRandomData.DateCreated = time.Now().UTC() |
|
||||||
newRandomData.LastUpdated = newRandomData.DateCreated |
|
||||||
newRandomData.ID = time.Now().UTC().UnixNano() |
|
||||||
|
|
||||||
err = dbHandler.writeRandomData(newRandomData) |
|
||||||
if err != nil { |
|
||||||
log.Println("Error writing RandomData (create): ", err) |
|
||||||
w.WriteHeader(http.StatusInternalServerError) |
|
||||||
return |
|
||||||
} |
|
||||||
|
|
||||||
w.WriteHeader(http.StatusOK) |
|
||||||
|
|
||||||
log.Println("Successfuly added to db : ", newRandomData) |
|
||||||
} |
|
||||||
|
|
||||||
func (dbHandler *randomDataHandler) getSpecificRandomData(w http.ResponseWriter, r *http.Request) { |
|
||||||
givenID := strings.Split(r.URL.String(), "/")[2] |
|
||||||
|
|
||||||
dbBytes, err := dbHandler.readDatabase() |
|
||||||
if err != nil { |
|
||||||
log.Println("Error reading db (getSpecificRandomData) : ", err) |
|
||||||
w.WriteHeader(http.StatusInternalServerError) |
|
||||||
return |
|
||||||
} |
|
||||||
|
|
||||||
var db []RandomData |
|
||||||
|
|
||||||
err = json.Unmarshal(dbBytes, &db) |
|
||||||
if err != nil { |
|
||||||
log.Println("Error unmarshalling database (getSpecificRandomData) : ", err) |
|
||||||
w.WriteHeader(http.StatusInternalServerError) |
|
||||||
return |
|
||||||
} |
|
||||||
|
|
||||||
int64GivenID, _ := strconv.ParseInt(givenID, 10, 64) |
|
||||||
for _, randomData := range db { |
|
||||||
if int64GivenID == randomData.ID { |
|
||||||
response, err := json.MarshalIndent(randomData, "", " ") |
|
||||||
if err != nil { |
|
||||||
log.Println("Error marshaling response(getSpecificRandomData) : ", err) |
|
||||||
w.WriteHeader(http.StatusInternalServerError) |
|
||||||
return |
|
||||||
} |
|
||||||
|
|
||||||
w.Write(response) |
|
||||||
return |
|
||||||
} |
|
||||||
} |
|
||||||
w.WriteHeader(http.StatusNotFound) |
|
||||||
} |
|
||||||
|
|
||||||
func (dbHandler *randomDataHandler) updateSpecificRandomData(w http.ResponseWriter, r *http.Request) { |
|
||||||
if r.Header.Get("content-type") != "application/json" { |
|
||||||
w.WriteHeader(http.StatusUnsupportedMediaType) |
|
||||||
w.Write([]byte(fmt.Sprintf("Got `%s` instead of `application/json`", r.Header.Get("content-type")))) |
|
||||||
return |
|
||||||
} |
|
||||||
|
|
||||||
requestBody, err := io.ReadAll(r.Body) |
|
||||||
if err != nil { |
|
||||||
log.Println("Error reading http request (create) : ", err) |
|
||||||
w.WriteHeader(http.StatusBadRequest) |
|
||||||
r.Body.Close() |
|
||||||
return |
|
||||||
} |
|
||||||
defer r.Body.Close() |
|
||||||
|
|
||||||
var givenUpdatedRandomData RandomData |
|
||||||
err = json.Unmarshal(requestBody, &givenUpdatedRandomData) |
|
||||||
if err != nil { |
|
||||||
log.Println("Error unmarshalling request body (updateSpecificRandomData) : ", err) |
|
||||||
return |
|
||||||
} |
|
||||||
|
|
||||||
givenID := strings.Split(r.URL.String(), "/")[2] |
|
||||||
|
|
||||||
dbBytes, err := dbHandler.readDatabase() |
|
||||||
if err != nil { |
|
||||||
log.Println("Error reading db (updateSpecificRandomData) : ", err) |
|
||||||
w.WriteHeader(http.StatusInternalServerError) |
|
||||||
return |
|
||||||
} |
|
||||||
var db []RandomData |
|
||||||
|
|
||||||
err = json.Unmarshal(dbBytes, &db) |
|
||||||
if err != nil { |
|
||||||
log.Println("Error unmarshalling database (update) : ", err) |
|
||||||
return |
|
||||||
} |
|
||||||
|
|
||||||
int64GivenID, _ := strconv.ParseInt(givenID, 10, 64) |
|
||||||
var counter int64 |
|
||||||
for _, randomData := range db { |
|
||||||
if int64GivenID == randomData.ID { |
|
||||||
var updatedRandomData RandomData |
|
||||||
|
|
||||||
updatedRandomData = givenUpdatedRandomData |
|
||||||
updatedRandomData.ID = randomData.ID |
|
||||||
updatedRandomData.DateCreated = randomData.DateCreated |
|
||||||
updatedRandomData.LastUpdated = time.Now().UTC() |
|
||||||
|
|
||||||
dbHandler.removeRandomData(int64GivenID) |
|
||||||
dbHandler.writeRandomData(updatedRandomData) |
|
||||||
|
|
||||||
log.Printf("Successfully updated RandomData with id %v \n", updatedRandomData.ID) |
|
||||||
return |
|
||||||
} |
|
||||||
counter++ |
|
||||||
} |
|
||||||
|
|
||||||
w.WriteHeader(http.StatusNotFound) |
|
||||||
} |
|
||||||
|
|
||||||
func (dbHandler *randomDataHandler) deleteSpecificRandomData(w http.ResponseWriter, r *http.Request) { |
|
||||||
givenID := strings.Split(r.URL.String(), "/")[2] |
|
||||||
|
|
||||||
int64GivenID, _ := strconv.ParseInt(givenID, 10, 64) |
|
||||||
|
|
||||||
err := dbHandler.removeRandomData(int64GivenID) |
|
||||||
if err != nil { |
if err != nil { |
||||||
log.Println("Error removing RandomData (deleteSpecificRandomData) : ", err) |
log.Fatalf("error setting up a database: %s", err) |
||||||
w.WriteHeader(http.StatusInternalServerError) |
|
||||||
return |
|
||||||
} |
} |
||||||
|
|
||||||
w.WriteHeader(http.StatusOK) |
mux := http.NewServeMux() |
||||||
|
mux.HandleFunc("/", helpPage) |
||||||
log.Printf("Successfully deleted RandomData with id %v \n", int64GivenID) |
mux.HandleFunc("/randomdata", db.HandleGlobalWeb) |
||||||
} |
mux.HandleFunc("/randomdata/", db.HandleSpecificWeb) |
||||||
|
|
||||||
func (dbHandler *randomDataHandler) handle(w http.ResponseWriter, r *http.Request) { |
|
||||||
switch r.Method { |
|
||||||
case "GET": |
|
||||||
dbHandler.get(w, r) |
|
||||||
case "POST": |
|
||||||
dbHandler.create(w, r) |
|
||||||
default: |
|
||||||
w.WriteHeader(http.StatusMethodNotAllowed) |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
func (dbHandler *randomDataHandler) handleSpecific(w http.ResponseWriter, r *http.Request) { |
|
||||||
switch r.Method { |
|
||||||
case "GET": |
|
||||||
dbHandler.getSpecificRandomData(w, r) |
|
||||||
case "PUT": |
|
||||||
dbHandler.updateSpecificRandomData(w, r) |
|
||||||
case "DELETE": |
|
||||||
dbHandler.deleteSpecificRandomData(w, r) |
|
||||||
default: |
|
||||||
w.WriteHeader(http.StatusMethodNotAllowed) |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
func init() { |
|
||||||
InitLogs() |
|
||||||
} |
|
||||||
|
|
||||||
func main() { |
|
||||||
|
|
||||||
databaseHandler := newDatabaseHandler() |
|
||||||
|
|
||||||
servemux := http.NewServeMux() |
|
||||||
servemux.HandleFunc("/", homepage) |
|
||||||
servemux.HandleFunc("/randomdata", databaseHandler.handle) |
|
||||||
servemux.HandleFunc("/randomdata/", databaseHandler.handleSpecific) |
|
||||||
|
|
||||||
server := &http.Server{ |
server := &http.Server{ |
||||||
Addr: ":8000", |
Addr: fmt.Sprintf(":%d", *port), |
||||||
Handler: servemux, |
Handler: mux, |
||||||
ReadTimeout: 5 * time.Second, |
ReadTimeout: 5 * time.Second, |
||||||
WriteTimeout: 5 * time.Second, |
WriteTimeout: 5 * time.Second, |
||||||
} |
} |
||||||
|
log.Printf("Starting on port %d\n", *port) |
||||||
|
fmt.Printf("Starting on port %d\n", *port) |
||||||
|
|
||||||
log.Fatal(server.ListenAndServe()) |
log.Fatal(server.ListenAndServe()) |
||||||
} |
} |
||||||
|
|
||||||
|
func helpPage(w http.ResponseWriter, r *http.Request) { |
||||||
|
helpMessage := ` |
||||||
|
<h1>CRUD api</h1> |
||||||
|
<ul> |
||||||
|
<li> (GET) <a href="/randomdata">/randomdata</a> - to get all info from database (obviously a bad idea in serious projects ᗜˬᗜ) </li> |
||||||
|
<li> (GET) /randomdata/{id} - to get specific random data under corresponding id </li> |
||||||
|
<li> (POST) /randomdata - to create random data </li> |
||||||
|
<li> (DELETE) /randomdata/{id} - to delete specified random data </li> |
||||||
|
<li> (PUT) /randomdata/{id} - to update random data with given id </li> |
||||||
|
</ul> |
||||||
|
` |
||||||
|
fmt.Fprint(w, helpMessage) |
||||||
|
} |
||||||
|
|
||||||
|
// func (dbHandler *randomDataHandler) updateSpecificRandomData(w http.ResponseWriter, r *http.Request) {
|
||||||
|
// if r.Header.Get("content-type") != "application/json" {
|
||||||
|
// w.WriteHeader(http.StatusUnsupportedMediaType)
|
||||||
|
// w.Write([]byte(fmt.Sprintf("Got `%s` instead of `application/json`", r.Header.Get("content-type"))))
|
||||||
|
// return
|
||||||
|
// }
|
||||||
|
|
||||||
|
// requestBody, err := io.ReadAll(r.Body)
|
||||||
|
// if err != nil {
|
||||||
|
// log.Println("Error reading http request (create) : ", err)
|
||||||
|
// w.WriteHeader(http.StatusBadRequest)
|
||||||
|
// r.Body.Close()
|
||||||
|
// return
|
||||||
|
// }
|
||||||
|
// defer r.Body.Close()
|
||||||
|
|
||||||
|
// var givenUpdatedRandomData RandomData
|
||||||
|
// err = json.Unmarshal(requestBody, &givenUpdatedRandomData)
|
||||||
|
// if err != nil {
|
||||||
|
// log.Println("Error unmarshalling request body (updateSpecificRandomData) : ", err)
|
||||||
|
// return
|
||||||
|
// }
|
||||||
|
|
||||||
|
// givenID := strings.Split(r.URL.String(), "/")[2]
|
||||||
|
|
||||||
|
// dbBytes, err := dbHandler.readDatabase()
|
||||||
|
// if err != nil {
|
||||||
|
// log.Println("Error reading db (updateSpecificRandomData) : ", err)
|
||||||
|
// w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
// return
|
||||||
|
// }
|
||||||
|
// var db []RandomData
|
||||||
|
|
||||||
|
// err = json.Unmarshal(dbBytes, &db)
|
||||||
|
// if err != nil {
|
||||||
|
// log.Println("Error unmarshalling database (update) : ", err)
|
||||||
|
// return
|
||||||
|
// }
|
||||||
|
|
||||||
|
// int64GivenID, _ := strconv.ParseInt(givenID, 10, 64)
|
||||||
|
// var counter int64
|
||||||
|
// for _, randomData := range db {
|
||||||
|
// if int64GivenID == randomData.ID {
|
||||||
|
// var updatedRandomData RandomData
|
||||||
|
|
||||||
|
// updatedRandomData = givenUpdatedRandomData
|
||||||
|
// updatedRandomData.ID = randomData.ID
|
||||||
|
// updatedRandomData.DateCreated = randomData.DateCreated
|
||||||
|
// updatedRandomData.LastUpdated = time.Now().UTC()
|
||||||
|
|
||||||
|
// dbHandler.removeRandomData(int64GivenID)
|
||||||
|
// dbHandler.writeRandomData(updatedRandomData)
|
||||||
|
|
||||||
|
// log.Printf("Successfully updated RandomData with id %v \n", updatedRandomData.ID)
|
||||||
|
// return
|
||||||
|
// }
|
||||||
|
// counter++
|
||||||
|
// }
|
||||||
|
|
||||||
|
// w.WriteHeader(http.StatusNotFound)
|
||||||
|
// }
|
||||||
|
|
||||||
|
// func (dbHandler *randomDataHandler) deleteSpecificRandomData(w http.ResponseWriter, r *http.Request) {
|
||||||
|
// givenID := strings.Split(r.URL.String(), "/")[2]
|
||||||
|
|
||||||
|
// int64GivenID, _ := strconv.ParseInt(givenID, 10, 64)
|
||||||
|
|
||||||
|
// err := dbHandler.removeRandomData(int64GivenID)
|
||||||
|
// if err != nil {
|
||||||
|
// log.Println("Error removing RandomData (deleteSpecificRandomData) : ", err)
|
||||||
|
// w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
// return
|
||||||
|
// }
|
||||||
|
|
||||||
|
// w.WriteHeader(http.StatusOK)
|
||||||
|
|
||||||
|
// log.Printf("Successfully deleted RandomData with id %v \n", int64GivenID)
|
||||||
|
// }
|
||||||
|
|
||||||
|
// func (dbHandler *randomDataHandler) handle(w http.ResponseWriter, r *http.Request) {
|
||||||
|
// switch r.Method {
|
||||||
|
// case "GET":
|
||||||
|
// dbHandler.get(w, r)
|
||||||
|
// case "POST":
|
||||||
|
// dbHandler.create(w, r)
|
||||||
|
// default:
|
||||||
|
// w.WriteHeader(http.StatusMethodNotAllowed)
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
|
||||||
|
// func (dbHandler *randomDataHandler) handleSpecific(w http.ResponseWriter, r *http.Request) {
|
||||||
|
// switch r.Method {
|
||||||
|
// case "GET":
|
||||||
|
// dbHandler.getSpecificRandomData(w, r)
|
||||||
|
// case "PUT":
|
||||||
|
// dbHandler.updateSpecificRandomData(w, r)
|
||||||
|
// case "DELETE":
|
||||||
|
// dbHandler.deleteSpecificRandomData(w, r)
|
||||||
|
// default:
|
||||||
|
// w.WriteHeader(http.StatusMethodNotAllowed)
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
@ -0,0 +1,35 @@ |
|||||||
|
package randomdata |
||||||
|
|
||||||
|
import ( |
||||||
|
"encoding/json" |
||||||
|
"time" |
||||||
|
) |
||||||
|
|
||||||
|
// The `bridge` between input values and a record in db
|
||||||
|
type RandomData struct { |
||||||
|
DateCreated int64 |
||||||
|
LastUpdated int64 |
||||||
|
Title string `json:"title"` |
||||||
|
Text string `json:"text"` |
||||||
|
} |
||||||
|
|
||||||
|
// Create a new `RandomData`
|
||||||
|
func New(title string, text string) *RandomData { |
||||||
|
return &RandomData{ |
||||||
|
DateCreated: time.Now().UTC().Unix(), |
||||||
|
LastUpdated: time.Now().UTC().Unix(), |
||||||
|
Title: title, |
||||||
|
Text: text, |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
// Unmarshal `RandomData` from Json encoded bytes
|
||||||
|
func FromJson(jsonBytes []byte) (*RandomData, error) { |
||||||
|
var randomData RandomData |
||||||
|
err := json.Unmarshal(jsonBytes, &randomData) |
||||||
|
if err != nil { |
||||||
|
return nil, err |
||||||
|
} |
||||||
|
|
||||||
|
return &randomData, nil |
||||||
|
} |
Loading…
Reference in new issue