Skip to content
Snippets Groups Projects
Verified Commit 171cd46c authored by Janne Mareike Koschinski's avatar Janne Mareike Koschinski
Browse files

Implement mutation APIs and prepare for implementing a new UI

parent ffe3b69d
Branches
No related tags found
1 merge request!1Replace entire project structure
Showing with 39692 additions and 40 deletions
package api
import (
"database/sql"
"git.kuschku.de/justjanne/imghost-frontend/environment"
"github.com/gorilla/mux"
"net/http"
)
func DeleteAlbum(env environment.FrontendEnvironment) http.Handler {
return http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
vars := mux.Vars(request)
album, err := env.Repositories.Albums.Get(vars["albumId"])
if err == sql.ErrNoRows {
http.NotFound(writer, request)
return
} else if err != nil {
http.Error(writer, err.Error(), http.StatusInternalServerError)
return
}
err = env.Repositories.Albums.Delete(album)
if err != nil {
http.Error(writer, err.Error(), http.StatusInternalServerError)
return
}
writer.WriteHeader(http.StatusNoContent)
})
}
package api
import (
"database/sql"
"encoding/json"
"git.kuschku.de/justjanne/imghost-frontend/environment"
"git.kuschku.de/justjanne/imghost-frontend/model"
"github.com/gorilla/mux"
"net/http"
)
func ReorderAlbum(env environment.FrontendEnvironment) http.Handler {
return http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
var err error
vars := mux.Vars(request)
album, err := env.Repositories.Albums.Get(vars["albumId"])
if err == sql.ErrNoRows {
http.NotFound(writer, request)
return
} else if err != nil {
http.Error(writer, err.Error(), http.StatusInternalServerError)
return
}
var changes []model.AlbumImage
err = json.NewDecoder(request.Body).Decode(&changes)
if err != nil {
http.Error(writer, err.Error(), http.StatusInternalServerError)
return
}
for index, image := range changes {
image.Album = album.Id
err = env.Repositories.AlbumImages.Reorder(image, index)
if err != nil {
http.Error(writer, err.Error(), http.StatusInternalServerError)
return
}
}
writer.WriteHeader(http.StatusNoContent)
})
}
package api
import (
"database/sql"
"encoding/json"
"git.kuschku.de/justjanne/imghost-frontend/environment"
"git.kuschku.de/justjanne/imghost-frontend/model"
"github.com/gorilla/mux"
"net/http"
)
func UpdateAlbum(env environment.FrontendEnvironment) http.Handler {
return http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
var err error
vars := mux.Vars(request)
album, err := env.Repositories.Albums.Get(vars["albumId"])
if err == sql.ErrNoRows {
http.NotFound(writer, request)
return
} else if err != nil {
http.Error(writer, err.Error(), http.StatusInternalServerError)
return
}
var changes model.Album
err = json.NewDecoder(request.Body).Decode(&changes)
if err != nil {
http.Error(writer, err.Error(), http.StatusInternalServerError)
return
}
album.Title = changes.Title
album.Description = changes.Description
err = env.Repositories.Albums.Update(album)
if err != nil {
http.Error(writer, err.Error(), http.StatusInternalServerError)
return
}
writer.WriteHeader(http.StatusNoContent)
})
}
package api
import (
"database/sql"
"git.kuschku.de/justjanne/imghost-frontend/environment"
"github.com/gorilla/mux"
"net/http"
)
func DeleteAlbumImage(env environment.FrontendEnvironment) http.Handler {
return http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
vars := mux.Vars(request)
albumImage, err := env.Repositories.AlbumImages.Get(vars["albumId"], vars["imageId"])
if err == sql.ErrNoRows {
http.NotFound(writer, request)
return
} else if err != nil {
http.Error(writer, err.Error(), http.StatusInternalServerError)
return
}
err = env.Repositories.AlbumImages.Delete(albumImage)
if err != nil {
http.Error(writer, err.Error(), http.StatusInternalServerError)
return
}
writer.WriteHeader(http.StatusNoContent)
})
}
package api
import (
"database/sql"
"encoding/json"
"git.kuschku.de/justjanne/imghost-frontend/environment"
"git.kuschku.de/justjanne/imghost-frontend/model"
"github.com/gorilla/mux"
"net/http"
)
func UpdateAlbumImage(env environment.FrontendEnvironment) http.Handler {
return http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
var err error
vars := mux.Vars(request)
image, err := env.Repositories.AlbumImages.Get(
vars["albumId"],
vars["imageId"])
if err == sql.ErrNoRows {
http.NotFound(writer, request)
return
} else if err != nil {
http.Error(writer, err.Error(), http.StatusInternalServerError)
return
}
var changes model.AlbumImage
err = json.NewDecoder(request.Body).Decode(&changes)
if err != nil {
http.Error(writer, err.Error(), http.StatusInternalServerError)
return
}
image.Title = changes.Title
image.Description = changes.Description
err = env.Repositories.AlbumImages.Update(image)
if err != nil {
http.Error(writer, err.Error(), http.StatusInternalServerError)
return
}
writer.WriteHeader(http.StatusNoContent)
})
}
package api
import (
"database/sql"
"git.kuschku.de/justjanne/imghost-frontend/environment"
"github.com/gorilla/mux"
"net/http"
)
func DeleteImage(env environment.FrontendEnvironment) http.Handler {
return http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
var err error
vars := mux.Vars(request)
image, err := env.Repositories.Images.Get(vars["imageId"])
if err == sql.ErrNoRows {
http.NotFound(writer, request)
return
} else if err != nil {
http.Error(writer, err.Error(), http.StatusInternalServerError)
return
}
err = env.Repositories.Images.Delete(image)
if err != nil {
http.Error(writer, err.Error(), http.StatusInternalServerError)
return
}
writer.WriteHeader(http.StatusNoContent)
})
}
......@@ -10,21 +10,26 @@ import (
"net/http"
)
func EnrichImageInfo(env environment.FrontendEnvironment, image model.Image) (info model.ImageInfo, err error) {
info.Image = image
info.State, err = env.Repositories.ImageStates.Get(image.Id)
if err != nil {
return
}
imageUrl, err := env.Storage.UrlFor(context.Background(), env.Configuration.Storage.ImageBucket, info.Image.Id)
if err != nil {
return
}
info.Url = imageUrl.String()
return
}
func GetImage(env environment.FrontendEnvironment) http.Handler {
return http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
var err error
vars := mux.Vars(request)
var info model.ImageInfo
info.Image, err = env.Repositories.Images.Get(vars["imageId"])
if err == sql.ErrNoRows {
http.NotFound(writer, request)
return
} else if err != nil {
http.Error(writer, err.Error(), http.StatusInternalServerError)
return
}
info.State, err = env.Repositories.ImageStates.Get(vars["imageId"])
image, err := env.Repositories.Images.Get(vars["imageId"])
if err == sql.ErrNoRows {
http.NotFound(writer, request)
return
......@@ -32,12 +37,11 @@ func GetImage(env environment.FrontendEnvironment) http.Handler {
http.Error(writer, err.Error(), http.StatusInternalServerError)
return
}
imageUrl, err := env.Storage.UrlFor(context.Background(), env.Configuration.Storage.ImageBucket, info.Image.Id)
info, err := EnrichImageInfo(env, image)
if err != nil {
http.Error(writer, err.Error(), http.StatusInternalServerError)
return
}
info.Url = imageUrl.String()
util.ReturnJson(writer, info)
})
......
package api
import (
"database/sql"
"git.kuschku.de/justjanne/imghost-frontend/auth"
"git.kuschku.de/justjanne/imghost-frontend/environment"
"git.kuschku.de/justjanne/imghost-frontend/model"
"git.kuschku.de/justjanne/imghost-frontend/util"
"net/http"
)
......@@ -15,14 +15,15 @@ func ListImages(env environment.FrontendEnvironment) http.Handler {
http.Error(writer, err.Error(), http.StatusUnauthorized)
}
images, err := env.Repositories.Images.List(user)
if err == sql.ErrNoRows {
http.NotFound(writer, request)
return
} else if err != nil {
var infos []model.ImageInfo
for _, image := range images {
info, err := EnrichImageInfo(env, image)
if err != nil {
http.Error(writer, err.Error(), http.StatusInternalServerError)
return
}
infos = append(infos, info)
}
util.ReturnJson(writer, images)
util.ReturnJson(writer, infos)
})
}
package api
import (
"database/sql"
"encoding/json"
"git.kuschku.de/justjanne/imghost-frontend/environment"
"git.kuschku.de/justjanne/imghost-frontend/model"
"github.com/gorilla/mux"
"net/http"
)
func UpdateImage(env environment.FrontendEnvironment) http.Handler {
return http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
var err error
vars := mux.Vars(request)
image, err := env.Repositories.Images.Get(vars["imageId"])
if err == sql.ErrNoRows {
http.NotFound(writer, request)
return
} else if err != nil {
http.Error(writer, err.Error(), http.StatusInternalServerError)
return
}
var changes model.Image
err = json.NewDecoder(request.Body).Decode(&changes)
if err != nil {
http.Error(writer, err.Error(), http.StatusInternalServerError)
return
}
image.Title = changes.Title
image.Description = changes.Description
err = env.Repositories.Images.Update(image)
if err != nil {
http.Error(writer, err.Error(), http.StatusInternalServerError)
return
}
writer.WriteHeader(http.StatusNoContent)
})
}
......@@ -33,33 +33,52 @@ func main() {
// Image API
router.Handle(
"/api/v1/images",
api.ListImages(env)).Methods(http.MethodGet)
api.ListImages(env)).Methods(http.MethodGet, http.MethodOptions)
router.Handle(
"/api/v1/images",
api.UploadImage(env)).Methods(http.MethodPost)
api.UploadImage(env)).Methods(http.MethodPost, http.MethodOptions)
router.Handle(
"/api/v1/images/{imageId}",
api.GetImage(env)).Methods(http.MethodGet)
api.GetImage(env)).Methods(http.MethodGet, http.MethodOptions)
router.Handle(
"/api/v1/images/{imageId}",
api.UpdateImage(env)).Methods(http.MethodPost, http.MethodOptions)
router.Handle(
"/api/v1/images/{imageId}",
api.DeleteImage(env)).Methods(http.MethodDelete, http.MethodOptions)
// Album API
router.Handle(
"/api/v1/albums",
api.ListAlbums(env)).Methods(http.MethodGet)
api.ListAlbums(env)).Methods(http.MethodGet, http.MethodOptions)
router.Handle(
"/api/v1/albums/{albumId}",
api.GetAlbum(env)).Methods(http.MethodGet, http.MethodOptions)
router.Handle(
"/api/v1/albums/{albumId}",
api.UpdateAlbum(env)).Methods(http.MethodPost, http.MethodOptions)
router.Handle(
"/api/v1/albums/{imageId}",
api.GetAlbum(env)).Methods(http.MethodGet)
"/api/v1/albums/{albumId}/reorder",
api.ReorderAlbum(env)).Methods(http.MethodPost, http.MethodOptions)
router.Handle(
"/api/v1/albums/{albumId}",
api.DeleteAlbum(env)).Methods(http.MethodDelete, http.MethodOptions)
// Album Image API
router.Handle(
"/api/v1/albums/{albumId}/images",
api.ListAlbumImages(env)).Methods(http.MethodGet)
api.ListAlbumImages(env)).Methods(http.MethodGet, http.MethodOptions)
router.Handle(
"/api/v1/albums/{albumId}/images/{imageId}",
api.GetAlbumImage(env)).Methods(http.MethodGet)
// TODO: Implement mutating API methods
api.GetAlbumImage(env)).Methods(http.MethodGet, http.MethodOptions)
router.Handle(
"/api/v1/albums/{albumId}/images/{imageId}",
api.UpdateAlbumImage(env)).Methods(http.MethodPost, http.MethodOptions)
router.Handle(
"/api/v1/albums/{albumId}/images/{imageId}",
api.DeleteAlbumImage(env)).Methods(http.MethodDelete, http.MethodOptions)
if err = http.ListenAndServe(":8080", util.MethodOverride(router)); err != nil {
if err = http.ListenAndServe(":8080", util.CorsWrapper(router)); err != nil {
panic(err)
}
}
{
"name": "imghost-frontend",
"lockfileVersion": 2,
"requires": true,
"packages": {
"": {
"dependencies": {
"axios": "^0.21.1"
}
},
"node_modules/axios": {
"version": "0.21.1",
"resolved": "https://registry.npmjs.org/axios/-/axios-0.21.1.tgz",
"integrity": "sha512-dKQiRHxGD9PPRIUNIWvZhPTPpl1rf/OxTYKsqKUDjBwYylTvV7SjSHJb9ratfyzM6wCdLCOYLzs73qpg5c4iGA==",
"dependencies": {
"follow-redirects": "^1.10.0"
}
},
"node_modules/follow-redirects": {
"version": "1.14.1",
"resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.14.1.tgz",
"integrity": "sha512-HWqDgT7ZEkqRzBvc2s64vSZ/hfOceEol3ac/7tKwzuvEyWx3/4UegXh5oBOIotkGsObyk3xznnSRVADBgWSQVg==",
"funding": [
{
"type": "individual",
"url": "https://github.com/sponsors/RubenVerborgh"
}
],
"engines": {
"node": ">=4.0"
},
"peerDependenciesMeta": {
"debug": {
"optional": true
}
}
}
},
"dependencies": {
"axios": {
"version": "0.21.1",
"resolved": "https://registry.npmjs.org/axios/-/axios-0.21.1.tgz",
"integrity": "sha512-dKQiRHxGD9PPRIUNIWvZhPTPpl1rf/OxTYKsqKUDjBwYylTvV7SjSHJb9ratfyzM6wCdLCOYLzs73qpg5c4iGA==",
"requires": {
"follow-redirects": "^1.10.0"
}
},
"follow-redirects": {
"version": "1.14.1",
"resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.14.1.tgz",
"integrity": "sha512-HWqDgT7ZEkqRzBvc2s64vSZ/hfOceEol3ac/7tKwzuvEyWx3/4UegXh5oBOIotkGsObyk3xznnSRVADBgWSQVg=="
}
}
}
{
"dependencies": {
"axios": "^0.21.1"
}
}
......@@ -138,26 +138,26 @@ func (repo AlbumImages) Update(changed model.AlbumImage) (err error) {
return
}
func (repo AlbumImages) Delete(changed model.AlbumImage) (err error) {
_, err = repo.stmtDelete.Exec(map[string]interface{}{
func (repo AlbumImages) Reorder(changed model.AlbumImage, position int) (err error) {
_, err = repo.stmtReorder.Exec(map[string]interface{}{
"albumId": changed.Album,
"imageId": changed.Image,
"position": position,
})
return
}
func (repo AlbumImages) DeleteAll(changed model.AlbumImage) (err error) {
_, err = repo.stmtDeleteAll.Exec(map[string]interface{}{
func (repo AlbumImages) Delete(changed model.AlbumImage) (err error) {
_, err = repo.stmtDelete.Exec(map[string]interface{}{
"albumId": changed.Album,
"imageId": changed.Image,
})
return
}
func (repo AlbumImages) Reorder(changed model.AlbumImage, position int) (err error) {
func (repo AlbumImages) DeleteAll(changed model.AlbumImage) (err error) {
_, err = repo.stmtDeleteAll.Exec(map[string]interface{}{
"albumId": changed.Album,
"imageId": changed.Image,
"position": position,
})
return
}
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.
# dependencies
/node_modules
/.pnp
.pnp.js
# testing
/coverage
# production
/build
# misc
.DS_Store
.env.local
.env.development.local
.env.test.local
.env.production.local
npm-debug.log*
yarn-debug.log*
yarn-error.log*
# Getting Started with Create React App
This project was bootstrapped with [Create React App](https://github.com/facebook/create-react-app).
## Available Scripts
In the project directory, you can run:
### `npm start`
Runs the app in the development mode.\
Open [http://localhost:3000](http://localhost:3000) to view it in the browser.
The page will reload if you make edits.\
You will also see any lint errors in the console.
### `npm test`
Launches the test runner in the interactive watch mode.\
See the section about [running tests](https://facebook.github.io/create-react-app/docs/running-tests) for more information.
### `npm run build`
Builds the app for production to the `build` folder.\
It correctly bundles React in production mode and optimizes the build for the best performance.
The build is minified and the filenames include the hashes.\
Your app is ready to be deployed!
See the section about [deployment](https://facebook.github.io/create-react-app/docs/deployment) for more information.
### `npm run eject`
**Note: this is a one-way operation. Once you `eject`, you can’t go back!**
If you aren’t satisfied with the build tool and configuration choices, you can `eject` at any time. This command will remove the single build dependency from your project.
Instead, it will copy all the configuration files and the transitive dependencies (webpack, Babel, ESLint, etc) right into your project so you have full control over them. All of the commands except `eject` will still work, but they will point to the copied scripts so you can tweak them. At this point you’re on your own.
You don’t have to ever use `eject`. The curated feature set is suitable for small and middle deployments, and you shouldn’t feel obligated to use this feature. However we understand that this tool wouldn’t be useful if you couldn’t customize it when you are ready for it.
## Learn More
You can learn more in the [Create React App documentation](https://facebook.github.io/create-react-app/docs/getting-started).
To learn React, check out the [React documentation](https://reactjs.org/).
This diff is collapsed.
{
"name": "ui",
"version": "0.1.0",
"private": true,
"dependencies": {
"@material-ui/core": "^4.12.3",
"@material-ui/icons": "^4.11.2",
"@testing-library/jest-dom": "^5.14.1",
"@testing-library/react": "^11.2.7",
"@testing-library/user-event": "^12.8.3",
"@types/jest": "^26.0.24",
"@types/node": "^12.20.17",
"@types/react": "^17.0.15",
"@types/react-dom": "^17.0.9",
"react": "^17.0.2",
"react-dom": "^17.0.2",
"react-query": "^3.19.1",
"react-router-dom": "^5.2.0",
"react-scripts": "4.0.3",
"typescript": "^4.3.5",
"web-vitals": "^1.1.2"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"eslintConfig": {
"extends": [
"react-app",
"react-app/jest"
]
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
}
}
ui/public/favicon.ico

3.78 KiB

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<link rel="icon" href="%PUBLIC_URL%/favicon.ico" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="theme-color" content="#000000" />
<meta
name="description"
content="Web site created using create-react-app"
/>
<link rel="apple-touch-icon" href="%PUBLIC_URL%/logo192.png" />
<!--
manifest.json provides metadata used when your web app is installed on a
user's mobile device or desktop. See https://developers.google.com/web/fundamentals/web-app-manifest/
-->
<link rel="manifest" href="%PUBLIC_URL%/manifest.json" />
<!--
Notice the use of %PUBLIC_URL% in the tags above.
It will be replaced with the URL of the `public` folder during the build.
Only files inside the `public` folder can be referenced from the HTML.
Unlike "/favicon.ico" or "favicon.ico", "%PUBLIC_URL%/favicon.ico" will
work correctly both with client-side routing and a non-root public URL.
Learn how to configure a non-root public URL by running `npm run build`.
-->
<title>React App</title>
</head>
<body>
<noscript>You need to enable JavaScript to run this app.</noscript>
<div id="root"></div>
<!--
This HTML file is a template.
If you open it directly in the browser, you will see an empty page.
You can add webfonts, meta tags, or analytics to this file.
The build step will place the bundled scripts into the <body> tag.
To begin the development, run `npm start` or `yarn start`.
To create a production bundle, use `npm run build` or `yarn build`.
-->
</body>
</html>
ui/public/logo192.png

5.22 KiB

0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment