Skip to content
Snippets Groups Projects
Commit 1d19e692 authored by albrecht's avatar albrecht
Browse files

erste laufende Version erstellt

parent 4e37f9af
No related branches found
No related tags found
No related merge requests found
Showing
with 759 additions and 90 deletions
# Go-mazon
# gomazon
ADD Product
curl -H "Content-Type: application/json" -d '{"description":"test","gesamtrating":4.0,"preis":2.38}' localhost:8080/product/new
## Getting started
To make it easy for you to get started with GitLab, here's a list of recommended next steps.
Already a pro? Just edit this README.md and make it your own. Want to make it easy? [Use the template at the bottom](#editing-this-readme)!
## Add your files
- [ ] [Create](https://docs.gitlab.com/ee/user/project/repository/web_editor.html#create-a-file) or [upload](https://docs.gitlab.com/ee/user/project/repository/web_editor.html#upload-a-file) files
- [ ] [Add files using the command line](https://docs.gitlab.com/ee/gitlab-basics/add-file.html#add-a-file-using-the-command-line) or push an existing Git repository with the following command:
```
cd existing_repo
git remote add origin https://gitlab.reutlingen-university.de/albrecht/go-mazon.git
git branch -M main
git push -uf origin main
```
## Integrate with your tools
- [ ] [Set up project integrations](https://gitlab.reutlingen-university.de/albrecht/go-mazon/-/settings/integrations)
## Collaborate with your team
- [ ] [Invite team members and collaborators](https://docs.gitlab.com/ee/user/project/members/)
- [ ] [Create a new merge request](https://docs.gitlab.com/ee/user/project/merge_requests/creating_merge_requests.html)
- [ ] [Automatically close issues from merge requests](https://docs.gitlab.com/ee/user/project/issues/managing_issues.html#closing-issues-automatically)
- [ ] [Enable merge request approvals](https://docs.gitlab.com/ee/user/project/merge_requests/approvals/)
- [ ] [Set auto-merge](https://docs.gitlab.com/ee/user/project/merge_requests/merge_when_pipeline_succeeds.html)
## Test and Deploy
Use the built-in continuous integration in GitLab.
- [ ] [Get started with GitLab CI/CD](https://docs.gitlab.com/ee/ci/quick_start/index.html)
- [ ] [Analyze your code for known vulnerabilities with Static Application Security Testing(SAST)](https://docs.gitlab.com/ee/user/application_security/sast/)
- [ ] [Deploy to Kubernetes, Amazon EC2, or Amazon ECS using Auto Deploy](https://docs.gitlab.com/ee/topics/autodevops/requirements.html)
- [ ] [Use pull-based deployments for improved Kubernetes management](https://docs.gitlab.com/ee/user/clusters/agent/)
- [ ] [Set up protected environments](https://docs.gitlab.com/ee/ci/environments/protected_environments.html)
***
# Editing this README
When you're ready to make this README your own, just edit this file and use the handy template below (or feel free to structure it however you want - this is just a starting point!). Thank you to [makeareadme.com](https://www.makeareadme.com/) for this template.
## Suggestions for a good README
Every project is different, so consider which of these sections apply to yours. The sections used in the template are suggestions for most open source projects. Also keep in mind that while a README can be too long and detailed, too long is better than too short. If you think your README is too long, consider utilizing another form of documentation rather than cutting out information.
## Name
Choose a self-explaining name for your project.
## Description
Let people know what your project can do specifically. Provide context and add a link to any reference visitors might be unfamiliar with. A list of Features or a Background subsection can also be added here. If there are alternatives to your project, this is a good place to list differentiating factors.
## Badges
On some READMEs, you may see small images that convey metadata, such as whether or not all the tests are passing for the project. You can use Shields to add some to your README. Many services also have instructions for adding a badge.
## Visuals
Depending on what you are making, it can be a good idea to include screenshots or even a video (you'll frequently see GIFs rather than actual videos). Tools like ttygif can help, but check out Asciinema for a more sophisticated method.
## Installation
Within a particular ecosystem, there may be a common way of installing things, such as using Yarn, NuGet, or Homebrew. However, consider the possibility that whoever is reading your README is a novice and would like more guidance. Listing specific steps helps remove ambiguity and gets people to using your project as quickly as possible. If it only runs in a specific context like a particular programming language version or operating system or has dependencies that have to be installed manually, also add a Requirements subsection.
## Usage
Use examples liberally, and show the expected output if you can. It's helpful to have inline the smallest example of usage that you can demonstrate, while providing links to more sophisticated examples if they are too long to reasonably include in the README.
## Support
Tell people where they can go to for help. It can be any combination of an issue tracker, a chat room, an email address, etc.
## Roadmap
If you have ideas for releases in the future, it is a good idea to list them in the README.
## Contributing
State if you are open to contributions and what your requirements are for accepting them.
For people who want to make changes to your project, it's helpful to have some documentation on how to get started. Perhaps there is a script that they should run or some environment variables that they need to set. Make these steps explicit. These instructions could also be useful to your future self.
You can also document commands to lint the code or run tests. These steps help to ensure high code quality and reduce the likelihood that the changes inadvertently break something. Having instructions for running tests is especially helpful if it requires external setup, such as starting a Selenium server for testing in a browser.
## Authors and acknowledgment
Show your appreciation to those who have contributed to the project.
## License
For open source projects, say how it is licensed.
## Project status
If you have run out of energy or time for your project, put a note at the top of the README saying that development has slowed down or stopped completely. Someone may choose to fork your project or volunteer to step in as a maintainer or owner, allowing your project to keep going. You can also make an explicit request for maintainers.
Read Product
curl localhost:8080/products
\ No newline at end of file
services:
gomazon:
build: ./src/gomazon
ports:
- "8080:8080"
environment:
- DB_CONNECT=mariadb:3306
mariadb:
image: mariadb:10.5
environment:
- MYSQL_ROOT_PASSWORD=root
- MYSQL_DATABASE=gomazon
#!/usr/bin/env bash
docker run -d -p 3306:3306 --name database -e MYSQL_ROOT_PASSWORD=root -e MYSQL_DATABASE=gomazon mariadb:10.5
\ No newline at end of file
#!/usr/bin/env bash
docker kill database && docker rm database
\ No newline at end of file
FROM golang:1.20-buster
WORKDIR /go/src/app
COPY . .
RUN go mod download
RUN go install
RUN chmod +x ./wait-for-it.sh ./docker-entrypoint.sh
ENTRYPOINT ["./docker-entrypoint.sh"]
CMD ["gomazon"]
EXPOSE 8080
package db
import (
"fmt"
"os"
log "github.com/sirupsen/logrus"
"gitlab.reutlingen-university.de/albrecht/gomazon/model"
"gorm.io/driver/mysql"
"gorm.io/gorm"
)
var DB *gorm.DB
func init() {
dsn := fmt.Sprintf("root:root@tcp(%s)/gomazon?charset=utf8&parseTime=True&loc=Local", os.Getenv("DB_CONNECT"))
log.Info("UsingDSN for DB:", dsn)
var err error
DB, err = gorm.Open(mysql.Open(dsn), &gorm.Config{})
if err != nil {
panic("failed to connect to database")
}
log.Info("Starting automatic migrations")
if err := DB.Debug().AutoMigrate(&model.BankAccount{}); err != nil {
panic(err)
}
if err := DB.Debug().AutoMigrate(&model.Produkt{}); err != nil {
panic(err)
}
if err := DB.Debug().AutoMigrate(&model.Warenkorb{}); err != nil {
panic(err)
}
if err := DB.Debug().AutoMigrate(&model.Bewertung{}); err != nil {
panic(err)
}
if err := DB.Debug().AutoMigrate(&model.Position{}); err != nil {
panic(err)
}
log.Info("Automatic migrations finished")
}
#!/bin/sh
set -e
# Wait for DB
if [ -n "$DB_CONNECT" ]; then
/go/src/app/wait-for-it.sh "$DB_CONNECT" -t 40
fi
# Run the main container command.
exec "$@"
module gitlab.reutlingen-university.de/albrecht/gomazon
go 1.20
require (
github.com/dgrijalva/jwt-go v3.2.0+incompatible
github.com/gin-gonic/gin v1.9.1
github.com/sirupsen/logrus v1.9.3
gorm.io/driver/mysql v1.5.1
gorm.io/gorm v1.25.2
)
require (
github.com/bytedance/sonic v1.9.2 // indirect
github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311 // indirect
github.com/gabriel-vasile/mimetype v1.4.2 // indirect
github.com/gin-contrib/sse v0.1.0 // indirect
github.com/go-playground/locales v0.14.1 // indirect
github.com/go-playground/universal-translator v0.18.1 // indirect
github.com/go-playground/validator/v10 v10.14.1 // indirect
github.com/go-sql-driver/mysql v1.7.1 // indirect
github.com/goccy/go-json v0.10.2 // indirect
github.com/jinzhu/inflection v1.0.0 // indirect
github.com/jinzhu/now v1.1.5 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/klauspost/cpuid/v2 v2.2.5 // indirect
github.com/leodido/go-urn v1.2.4 // indirect
github.com/mattn/go-isatty v0.0.19 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/pelletier/go-toml/v2 v2.0.8 // indirect
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
github.com/ugorji/go/codec v1.2.11 // indirect
golang.org/x/arch v0.4.0 // indirect
golang.org/x/crypto v0.11.0 // indirect
golang.org/x/net v0.12.0 // indirect
golang.org/x/sys v0.10.0 // indirect
golang.org/x/text v0.11.0 // indirect
google.golang.org/protobuf v1.31.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
github.com/bytedance/sonic v1.5.0/go.mod h1:ED5hyg4y6t3/9Ku1R6dU/4KyJ48DZ4jPhfY1O2AihPM=
github.com/bytedance/sonic v1.9.2 h1:GDaNjuWSGu09guE9Oql0MSTNhNCLlWwO8y/xM5BzcbM=
github.com/bytedance/sonic v1.9.2/go.mod h1:i736AoUSYt75HyZLoJW9ERYxcy6eaN6h4BZXU064P/U=
github.com/chenzhuoyu/base64x v0.0.0-20211019084208-fb5309c8db06/go.mod h1:DH46F32mSOjUmXrMHnKwZdA8wcEefY7UVqBKYGjpdQY=
github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311 h1:qSGYFH7+jGhDF8vLC+iwCD4WpbV1EBDSzWkJODFLams=
github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311/go.mod h1:b583jCggY9gE99b6G5LEC39OIiVsWj+R97kbl5odCEk=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dgrijalva/jwt-go v3.2.0+incompatible h1:7qlOGliEKZXTDg6OTjfoBKDXWrumCAMpl/TFQ4/5kLM=
github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ=
github.com/gabriel-vasile/mimetype v1.4.2 h1:w5qFW6JKBz9Y393Y4q372O9A7cUSequkh1Q7OhCmWKU=
github.com/gabriel-vasile/mimetype v1.4.2/go.mod h1:zApsH/mKG4w07erKIaJPFiX0Tsq9BFQgN3qGY5GnNgA=
github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE=
github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI=
github.com/gin-gonic/gin v1.9.1 h1:4idEAncQnU5cB7BeOkPtxjfCSye0AAm1R0RVIqJ+Jmg=
github.com/gin-gonic/gin v1.9.1/go.mod h1:hPrL7YrpYKXt5YId3A/Tnip5kqbEAP+KLuI3SUcPTeU=
github.com/go-playground/assert/v2 v2.2.0 h1:JvknZsQTYeFEAhQwI4qEt9cyV5ONwRHC+lYKSsYSR8s=
github.com/go-playground/locales v0.14.1 h1:EWaQ/wswjilfKLTECiXz7Rh+3BjFhfDFKv/oXslEjJA=
github.com/go-playground/locales v0.14.1/go.mod h1:hxrqLVvrK65+Rwrd5Fc6F2O76J/NuW9t0sjnWqG1slY=
github.com/go-playground/universal-translator v0.18.1 h1:Bcnm0ZwsGyWbCzImXv+pAJnYK9S473LQFuzCbDbfSFY=
github.com/go-playground/universal-translator v0.18.1/go.mod h1:xekY+UJKNuX9WP91TpwSH2VMlDf28Uj24BCp08ZFTUY=
github.com/go-playground/validator/v10 v10.14.1 h1:9c50NUPC30zyuKprjL3vNZ0m5oG+jU0zvx4AqHGnv4k=
github.com/go-playground/validator/v10 v10.14.1/go.mod h1:9iXMNT7sEkjXb0I+enO7QXmzG6QCsPWY4zveKFVRSyU=
github.com/go-sql-driver/mysql v1.7.0/go.mod h1:OXbVy3sEdcQ2Doequ6Z5BW6fXNQTmx+9S1MCJN5yJMI=
github.com/go-sql-driver/mysql v1.7.1 h1:lUIinVbN1DY0xBg0eMOzmmtGoHwWBbvnWubQUrtU8EI=
github.com/go-sql-driver/mysql v1.7.1/go.mod h1:OXbVy3sEdcQ2Doequ6Z5BW6fXNQTmx+9S1MCJN5yJMI=
github.com/goccy/go-json v0.10.2 h1:CrxCmQqYDkv1z7lO7Wbh2HN93uovUHgrECaO5ZrCXAU=
github.com/goccy/go-json v0.10.2/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I=
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E=
github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc=
github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ=
github.com/jinzhu/now v1.1.5/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8=
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg=
github.com/klauspost/cpuid/v2 v2.2.5 h1:0E5MSMDEoAulmXNFquVs//DdoomxaoTY1kUhbc/qbZg=
github.com/klauspost/cpuid/v2 v2.2.5/go.mod h1:Lcz8mBdAVJIBVzewtcLocK12l3Y+JytZYpaMropDUws=
github.com/leodido/go-urn v1.2.4 h1:XlAE/cm/ms7TE/VMVoduSpNBoyc2dOxHs5MZSwAN63Q=
github.com/leodido/go-urn v1.2.4/go.mod h1:7ZrI8mTSeBSHl/UaRyKQW1qZeMgak41ANeCNaVckg+4=
github.com/mattn/go-isatty v0.0.19 h1:JITubQf0MOLdlGRuRq+jtsDlekdYPia9ZFsB8h/APPA=
github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg=
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M=
github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk=
github.com/pelletier/go-toml/v2 v2.0.8 h1:0ctb6s9mE31h0/lhu+J6OPmVeDxJn+kYnJc2jZR9tGQ=
github.com/pelletier/go-toml/v2 v2.0.8/go.mod h1:vuYfssBdrU2XDZ9bYydBu6t+6a6PYNcZljzZR9VXg+4=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ=
github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/stretchr/testify v1.8.3 h1:RP3t2pwF7cMEbC1dqtB6poj3niw/9gnV4Cjg5oW5gtY=
github.com/stretchr/testify v1.8.3/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/twitchyliquid64/golang-asm v0.15.1 h1:SU5vSMR7hnwNxj24w34ZyCi/FmDZTkS4MhqMhdFk5YI=
github.com/twitchyliquid64/golang-asm v0.15.1/go.mod h1:a1lVb/DtPvCB8fslRZhAngC2+aY1QWCk3Cedj/Gdt08=
github.com/ugorji/go/codec v1.2.11 h1:BMaWp1Bb6fHwEtbplGBGJ498wD+LKlNSl25MjdZY4dU=
github.com/ugorji/go/codec v1.2.11/go.mod h1:UNopzCgEMSXjBc6AOMqYvWC1ktqTAfzJZUZgYf6w6lg=
golang.org/x/arch v0.0.0-20210923205945-b76863e36670/go.mod h1:5om86z9Hs0C8fWVUuoMHwpExlXzs5Tkyp9hOrfG7pp8=
golang.org/x/arch v0.4.0 h1:A8WCeEWhLwPBKNbFi5Wv5UTCBx5zzubnXDlMOFAzFMc=
golang.org/x/arch v0.4.0/go.mod h1:5om86z9Hs0C8fWVUuoMHwpExlXzs5Tkyp9hOrfG7pp8=
golang.org/x/crypto v0.11.0 h1:6Ewdq3tDic1mg5xRO4milcWCfMVQhI4NkqWWvqejpuA=
golang.org/x/crypto v0.11.0/go.mod h1:xgJhtzW8F9jGdVFWZESrid1U1bjeNy4zgy5cRr/CIio=
golang.org/x/net v0.12.0 h1:cfawfvKITfUsFCeJIHJrbSxpeu/E81khclypR0GVT50=
golang.org/x/net v0.12.0/go.mod h1:zEVYFnQC7m/vmpQFELhcD1EWkZlX69l4oqgmer6hfKA=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.10.0 h1:SqMFp9UcQJZa+pmYuAKjd9xq1f0j5rLcDIk0mj4qAsA=
golang.org/x/sys v0.10.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/text v0.11.0 h1:LAntKIrcmeSKERyiOh0XMV39LXS8IE9UL2yP7+f5ij4=
golang.org/x/text v0.11.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
google.golang.org/protobuf v1.31.0 h1:g0LDEJHgrBl9N9r17Ru3sqWhkIx2NB67okBHPwC7hs8=
google.golang.org/protobuf v1.31.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gorm.io/driver/mysql v1.5.1 h1:WUEH5VF9obL/lTtzjmML/5e6VfFR/788coz2uaVCAZw=
gorm.io/driver/mysql v1.5.1/go.mod h1:Jo3Xu7mMhCyj8dlrb3WoCaRd1FhsVh+yMXb1jUInf5o=
gorm.io/gorm v1.25.1/go.mod h1:L4uxeKpfBml98NYqVqwAdmV1a2nBtAec/cf3fpucW/k=
gorm.io/gorm v1.25.2 h1:gs1o6Vsa+oVKG/a9ElL3XgyGfghFfkKA2SInQaCyMho=
gorm.io/gorm v1.25.2/go.mod h1:L4uxeKpfBml98NYqVqwAdmV1a2nBtAec/cf3fpucW/k=
rsc.io/pdf v0.1.1/go.mod h1:n8OzWcQ6Sp37PL01nO98y4iUCRdTGarVfzxY20ICaU4=
package handler
import (
"net/http"
"time"
"github.com/gin-gonic/gin"
"gitlab.reutlingen-university.de/albrecht/gomazon/service"
)
// Handler-Funktion zum Generieren eines Admin-JWT-Tokens
func GenerateAdminTokenHandler(c *gin.Context) {
// Benutzername und isAdmin aus dem Request-Body erhalten
var requestBody struct {
Username string `json:"username"`
IsAdmin bool `json:"isAdmin"`
}
if err := c.ShouldBindJSON(&requestBody); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid request body"})
return
}
// Überprüfen, ob der Benutzername vorhanden ist
if requestBody.Username == "" {
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid username"})
return
}
// Überprüfen, ob isAdmin gesetzt ist
if _, ok := interface{}(requestBody.IsAdmin).(bool); !ok {
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid isAdmin value"})
return
}
// JWT-Token für den Benutzer generieren
expiration := time.Now().Add(30 * time.Minute)
token, err := service.GenerateAdminJWTToken(requestBody.Username, requestBody.IsAdmin, expiration)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to generate admin token"})
return
}
c.JSON(http.StatusOK, gin.H{"token": token})
}
package handler
import (
"net/http"
"github.com/gin-gonic/gin"
log "github.com/sirupsen/logrus"
"gitlab.reutlingen-university.de/albrecht/gomazon/model"
"gitlab.reutlingen-university.de/albrecht/gomazon/service"
)
type BankAccountHandler struct {
BankAccountService *service.BankAccountService
}
// Bankkonto erstellen
func (h *BankAccountHandler) CreateBankAccount(c *gin.Context) {
tokenString := c.GetHeader("Authorization")
username, _, err := service.ExtractTokenData(tokenString)
if err != nil {
log.Errorf(err.Error())
}
var bankAccount model.BankAccount
if err := c.ShouldBindJSON(&bankAccount); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
bankAccount.Username = username
createdAccount, err := h.BankAccountService.CreateBankAccount(bankAccount)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusCreated, createdAccount)
}
func (h *BankAccountHandler) UpdateBankAccount(c *gin.Context) {
tokenString := c.GetHeader("Authorization")
username, _, err := service.ExtractTokenData(tokenString)
if err != nil {
log.Errorf(err.Error())
}
var withdrawalData struct {
Amount float64 `json:"amount" binding:"required"`
}
if err := c.ShouldBindJSON(&withdrawalData); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
updatedAccount, err := h.BankAccountService.UpdateBankAccount(username, withdrawalData.Amount)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusOK, updatedAccount)
}
package handler
import (
"net/http"
"strconv"
"github.com/gin-gonic/gin"
log "github.com/sirupsen/logrus"
"gitlab.reutlingen-university.de/albrecht/gomazon/model"
"gitlab.reutlingen-university.de/albrecht/gomazon/service"
)
type BewertungHandler struct {
BewertungService *service.BewertungService
ProduktService *service.ProduktService
}
func (h *BewertungHandler) CreateBewertung(c *gin.Context) {
tokenString := c.GetHeader("Authorization")
username, _, err := service.ExtractTokenData(tokenString)
if err != nil {
log.Errorf(err.Error())
}
// Produkt-ID aus dem URL-Parameter extrahieren
produktID, err := strconv.Atoi(c.Param("id"))
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
// Bewertungsdaten aus dem Request-Body lesen
var bewertung *model.Bewertung
if err := c.ShouldBindJSON(&bewertung); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
bewertung.Username = username
// Bewertung erstellen
err = h.BewertungService.CreateBewertung(uint(produktID), bewertung)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusOK, bewertung)
}
func (h *BewertungHandler) DeleteBewertung(c *gin.Context) {
// Produkt-ID und Bewertungs-ID aus den URL-Parametern extrahieren
bewertungID, err := strconv.Atoi(c.Param("id"))
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid Bewertung ID"})
return
}
bewertung, err := h.ProduktService.GetBewertungByID(uint(bewertungID))
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid Bewertung ID"})
return
}
tokenString := c.GetHeader("Authorization")
username, admin, err := service.ExtractTokenData(tokenString)
if err != nil {
log.Errorf(err.Error())
}
if admin || (bewertung.Username == username) {
// Bewertung löschen
err = h.BewertungService.DeleteBewertung(uint(bewertungID))
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusOK, gin.H{"message": "Bewertung erfolgreich gelöscht"})
} else {
c.JSON(http.StatusBadRequest, gin.H{"error": "not authorized"})
log.Errorf("%v is not the owner (%v)", username, bewertung.Username)
return
}
}
func (h *BewertungHandler) ReadBewertungen(c *gin.Context) {
produkte, err := h.BewertungService.ProduktService.ReadBewertungen()
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusOK, produkte)
}
package handler
import (
"io"
"net/http"
)
func Health(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
io.WriteString(w, `{"alive": true}`)
}
package handler
import (
"net/http"
"strconv"
"github.com/gin-gonic/gin"
log "github.com/sirupsen/logrus"
"gitlab.reutlingen-university.de/albrecht/gomazon/model"
"gitlab.reutlingen-university.de/albrecht/gomazon/service"
)
type ProduktHandler struct {
ProduktService *service.ProduktService
}
func (h *ProduktHandler) CreateProdukt(c *gin.Context) {
tokenString := c.GetHeader("Authorization")
username, admin, err := service.ExtractTokenData(tokenString)
if !admin {
c.JSON(http.StatusBadRequest, gin.H{"error": "not authorized"})
log.Errorf("%v is not an admin", username)
return
}
if err != nil {
log.Errorf(err.Error())
}
var produkt model.Produkt
// Die Produktinformationen aus dem Request-Body binden
if err := c.ShouldBindJSON(&produkt); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
produkt.Gesamtrating = 0.0
// Das Produkt über den ProduktService erstellen
createdProdukt, err := h.ProduktService.CreateProdukt(produkt)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusOK, createdProdukt)
}
func (h *ProduktHandler) ReadProdukte(c *gin.Context) {
produkte, err := h.ProduktService.ReadProdukte()
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusOK, produkte)
}
func (h *ProduktHandler) UpdateProdukt(c *gin.Context) {
tokenString := c.GetHeader("Authorization")
username, admin, err := service.ExtractTokenData(tokenString)
if !admin {
c.JSON(http.StatusBadRequest, gin.H{"error": "not authorized"})
log.Errorf("%v is not an admin", username)
return
}
if err != nil {
log.Errorf(err.Error())
}
id, err := strconv.Atoi(c.Param("id"))
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "Ungültige ID"})
return
}
var produkt model.Produkt
if err := c.ShouldBindJSON(&produkt); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
updatedProdukt, err := h.ProduktService.UpdateProdukt(uint(id), produkt)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusOK, updatedProdukt)
}
func (h *ProduktHandler) DeleteProdukt(c *gin.Context) {
tokenString := c.GetHeader("Authorization")
username, admin, err := service.ExtractTokenData(tokenString)
if !admin {
c.JSON(http.StatusBadRequest, gin.H{"error": "not authorized"})
log.Errorf("%v is not an admin", username)
return
}
if err != nil {
log.Errorf(err.Error())
}
id, err := strconv.Atoi(c.Param("id"))
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "Ungültige ID"})
return
}
err = h.ProduktService.DeleteProdukt(uint(id))
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusOK, gin.H{"message": "Produkt erfolgreich gelöscht"})
}
func (h *ProduktHandler) GetProduktByID(c *gin.Context) {
id, err := strconv.Atoi(c.Param("id"))
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "Ungültige ID"})
return
}
produkt, err := h.ProduktService.GetProduktByID(uint(id))
if err != nil {
c.JSON(http.StatusNotFound, gin.H{"error": "Produkt nicht gefunden"})
return
}
c.JSON(http.StatusOK, produkt)
}
package handler
import (
"net/http"
"strconv"
"github.com/gin-gonic/gin"
log "github.com/sirupsen/logrus"
"gitlab.reutlingen-university.de/albrecht/gomazon/model"
"gitlab.reutlingen-university.de/albrecht/gomazon/service"
)
type WarenkorbHandler struct {
WarenkorbService *service.WarenkorbService
BankAccountService *service.BankAccountService
}
// Warenkorb erstellen
func (h *WarenkorbHandler) CreateWarenkorb(c *gin.Context) {
tokenString := c.GetHeader("Authorization")
username, _, err := service.ExtractTokenData(tokenString)
if err != nil {
log.Errorf(err.Error())
}
var warenkorb model.Warenkorb
// if err := c.ShouldBindJSON(&warenkorb); err != nil {
// c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
// return
// }
warenkorb.Username = username
createdWarenkorb, err := h.WarenkorbService.CreateWarenkorb(warenkorb)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusCreated, createdWarenkorb)
}
// Einzelnes Produkt zum Warenkorb hinzufügen
func (h *WarenkorbHandler) AddProductToWarenkorb(c *gin.Context) {
tokenString := c.GetHeader("Authorization")
username, _, err := service.ExtractTokenData(tokenString)
if err != nil {
log.Errorf(err.Error())
}
produktID, err := strconv.Atoi(c.Param("id"))
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid Product ID"})
return
}
var addProductData struct {
Menge int `json:"menge" binding:"required"`
}
if err := c.ShouldBindJSON(&addProductData); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
updatedWarenkorb, err := h.WarenkorbService.AddProductToWarenkorb(username, uint(produktID), addProductData.Menge)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusOK, updatedWarenkorb)
}
// Bezahlfunktion für den Warenkorb
func (h *WarenkorbHandler) Bezahlen(c *gin.Context) {
tokenString := c.GetHeader("Authorization")
username, _, err := service.ExtractTokenData(tokenString)
if err != nil {
log.Errorf(err.Error())
}
err = h.WarenkorbService.Bezahlen(username)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusOK, gin.H{"message": "Zahlung erfolgreich"})
}
package main
import (
"github.com/gin-gonic/gin"
"gitlab.reutlingen-university.de/albrecht/gomazon/handler"
"gitlab.reutlingen-university.de/albrecht/gomazon/service"
)
func main() {
router := gin.Default()
// Service erstellen
produktService := &service.ProduktService{}
bewertungService := &service.BewertungService{}
bankAccountService := &service.BankAccountService{}
warenkorbService := &service.WarenkorbService{}
// Handler erstellen und den Service zuweisen
produktHandler := &handler.ProduktHandler{ProduktService: produktService}
bewertungHandler := &handler.BewertungHandler{BewertungService: bewertungService}
bankAccountHandler := &handler.BankAccountHandler{BankAccountService: bankAccountService}
warenkorbHandler := &handler.WarenkorbHandler{WarenkorbService: warenkorbService}
// Initialer Test, wie Gin funktioniert
router.GET("/health", func(c *gin.Context) {
handler.Health(c.Writer, c.Request)
})
////Non-Admin-Funktionen
router.GET("/createJWT", handler.GenerateAdminTokenHandler) //generate jwt-token
//Produkte
router.POST("/products", produktHandler.CreateProdukt) //neues Produkt (Admin only)
router.PUT("/products/:id", produktHandler.UpdateProdukt) //update Produkt (Admin only)
router.DELETE("/products/:id", produktHandler.DeleteProdukt) //Delete Produkt (Admin only)
router.GET("/products", produktHandler.ReadProdukte) //alle Produkte
router.GET("/products/:id", produktHandler.GetProduktByID) //einzelnes Produkt
//Bewertung
router.POST("/products/:id/rating", bewertungHandler.CreateBewertung) //neue Bewertung
router.DELETE("/ratings/:id", bewertungHandler.DeleteBewertung) //Delete Bewertung (Admin only (oder eigene))
router.GET("/ratings", bewertungHandler.ReadBewertungen) //alle Bewertungen
//Bankkonto
router.POST("/bankaccounts", bankAccountHandler.CreateBankAccount) // neues Konto
router.PUT("/bankaccounts", bankAccountHandler.UpdateBankAccount) // Überweisung
//Warenkorb
router.POST("/warenkorb", warenkorbHandler.CreateWarenkorb) //neuer Warenkorb
router.POST("/warenkorb/produkt/:id", warenkorbHandler.AddProductToWarenkorb) //produkt hinzufügen
router.POST("/warenkorb/bezahlen", warenkorbHandler.Bezahlen) //bezahlen
router.Run(":8080")
}
package model
import "gorm.io/gorm"
type BankAccount struct {
gorm.Model
Iban string `gorm:"notNull"`
BankName string `gorm:"notNull"`
Balance float64 `gorm:"notNull"`
Username string `gorm:"unique"`
}
package model
import "gorm.io/gorm"
type Bewertung struct {
gorm.Model //benötigt, weil Bewertungen auch gelöscht werden sollen
Username string `gorm:"notNull"`
Inhalt string `gorm:"notNull"`
Bewertung int `gorm:"notNull"`
ProduktId uint
}
package model
import "gorm.io/gorm"
type Position struct {
gorm.Model
Menge int `gorm:"notNull"`
ProduktId uint `gorm:"notNull"`
Preis float64 `gorm:"notNull"`
Gesamtpreis float64 `gorm:"notNull"`
}
package model
import "gorm.io/gorm"
type Produkt struct {
gorm.Model
Name string `gorm:"notNull"`
Description string `gorm:"notNull;size:80"`
Gesamtrating float64 `gorm:"notNull;default:0.0"`
Preis float64 `gorm:"notNull"`
Bewertungen []Bewertung `gorm:"foreignKey:ProduktId;constraint:OnUpdate:CASCADE,OnDelete:CASCADE"`
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment