From 84fcab202f694338e9c6f354900d1bb27704d04c Mon Sep 17 00:00:00 2001 From: Erik Winter Date: Mon, 25 Dec 2023 10:54:21 +0100 Subject: [PATCH] deploy api and make ids unique --- .gitignore | 3 +- Makefile | 7 +++-- cmd/api-service/client/client.go | 49 -------------------------------- cmd/api-service/server/sqlite.go | 19 +++++++++++++ 4 files changed, 26 insertions(+), 52 deletions(-) delete mode 100644 cmd/api-service/client/client.go diff --git a/.gitignore b/.gitignore index 7d6f92a..19b7853 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ *.db -emdb \ No newline at end of file +emdb +emdb-api \ No newline at end of file diff --git a/Makefile b/Makefile index f6a3c43..2719dfa 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,9 @@ -run-server: +run-api: go run ./cmd/api-service/service.go -apikey localOnly run-tui: - go run ./cmd/terminal-client/main.go \ No newline at end of file + go run ./cmd/terminal-client/main.go + +build-api: + go build -o emdb-api ./cmd/api-service/service.go \ No newline at end of file diff --git a/cmd/api-service/client/client.go b/cmd/api-service/client/client.go deleted file mode 100644 index 997c555..0000000 --- a/cmd/api-service/client/client.go +++ /dev/null @@ -1,49 +0,0 @@ -package client - -import ( - "bytes" - "encoding/json" - "fmt" - "net/http" - "time" - - "ewintr.nl/emdb/movie" -) - -type Client struct { - apiKey string - url string - c *http.Client -} - -func New(url, apiKey string) *Client { - return &Client{ - apiKey: apiKey, - url: url, - c: &http.Client{Timeout: 10 * time.Second}, - } -} - -func (c *Client) Store(m *movie.Movie) error { - url := fmt.Sprintf("%s/movie/%s", c.url, m.ID) - bodyJSON, err := json.Marshal(m) - if err != nil { - return err - } - body := bytes.NewBuffer(bodyJSON) - req, err := http.NewRequest(http.MethodPut, url, body) - if err != nil { - return err - } - req.Header.Set("Authorization", c.apiKey) - req.Header.Set("Content-Type", "application/json") - resp, err := c.c.Do(req) - if err != nil { - return err - } - if resp.StatusCode != http.StatusOK { - return fmt.Errorf("unexpected status code: %d", resp.StatusCode) - } - - return nil -} diff --git a/cmd/api-service/server/sqlite.go b/cmd/api-service/server/sqlite.go index 81f48d9..bda69db 100644 --- a/cmd/api-service/server/sqlite.go +++ b/cmd/api-service/server/sqlite.go @@ -29,6 +29,25 @@ var sqliteMigrations = []sqliteMigration{ `INSERT INTO system (latest_sync) VALUES (0)`, `ALTER TABLE movie ADD COLUMN tmdb_id INTEGER NOT NULL DEFAULT 0`, `ALTER TABLE movie ADD COLUMN summary TEXT NOT NULL DEFAULT ""`, + `BEGIN TRANSACTION; + CREATE TABLE movie_new ( + "id" TEXT UNIQUE NOT NULL, + "imdb_id" TEXT UNIQUE NOT NULL DEFAULT "", + "tmdb_id" INTEGER UNIQUE NOT NULL DEFAULT 0, + "title" TEXT NOT NULL DEFAULT "", + "english_title" TEXT NOT NULL DEFAULT "", + "year" INTEGER NOT NULL DEFAULT 0, + "directors" TEXT NOT NULL DEFAULT "", + "summary" TEXT NOT NULL DEFAULT "", + "watched_on" TEXT NOT NULL DEFAULT "", + "rating" INTEGER NOT NULL DEFAULT 0, + "comment" TEXT NOT NULL DEFAULT "" + ); + INSERT INTO movie_new (id, imdb_id, tmdb_id, title, english_title, year, directors, summary, watched_on, rating, comment) + SELECT id, imdb_id, tmdb_id, title, english_title, year, directors, summary, watched_on, rating, comment FROM movie; + DROP TABLE movie; + ALTER TABLE movie_new RENAME TO movie; + COMMIT`, } var (