diff --git a/Calls.md b/Calls.md index b7419b6788cd2de07d75e9c9a8c51916f7c2c69f..bbf186ea473a4759596c0471cde566d3224aea79 100644 --- a/Calls.md +++ b/Calls.md @@ -6,7 +6,7 @@ curl -H "Content-Type: application/json" -d '{"name":"Covid","organizerName":"Ma ### Campaign PUT ``` -curl -X PUT -H "Content-Type: application/json" -d '{"name":"Covid","organizerName":"Martin","donationMinimum":2,"targetAmount":100, "account":{"name":"Bombardillo Crocodillo","bankName":"Sacur","number":"123456"}}' localhost:8000/campaigns/1 +curl -X PUT -H "Content-Type: application/json" -d '{"name":"Covid","organizerName":"Martin","donationMinimum":2,"targetAmount":100, "account":{"name":"Hans-Martin","bankName":"DKB","number":"123456"}}' localhost:8000/campaigns/1 ``` ### Campaign all GET diff --git a/docker-compose.yaml b/docker-compose.yaml new file mode 100644 index 0000000000000000000000000000000000000000..94da5576d9eef673f4b195113649a7abdccad941 --- /dev/null +++ b/docker-compose.yaml @@ -0,0 +1,25 @@ +version: "3" + +services: + myaktion: + build: ./src/myaktion + ports: + - "8000:8000" + environment: + - DB_CONNECT=mariadb:3306 + - LOG_LEVEL=info + depends_on: + mariadb: + condition: service_healthy + + mariadb: + image: mariadb:latest + environment: + - MYSQL_ROOT_PASSWORD=root + - MYSQL_DATABASE=myaktion + healthcheck: + test: ["CMD", "healthcheck.sh", "--connect", "--innodb_initialized"] + start_period: 10s + interval: 10s + timeout: 5s + retries: 3 \ No newline at end of file diff --git a/src/myaktion/Dockerfile b/src/myaktion/Dockerfile new file mode 100644 index 0000000000000000000000000000000000000000..8d993e7015f9a3b7a69d425a031c62abf766a148 --- /dev/null +++ b/src/myaktion/Dockerfile @@ -0,0 +1,13 @@ +FROM golang:1.24.3-alpine + +WORKDIR /go/src/app + +COPY . . + +RUN go mod download + +RUN go build -o myaktion . + +CMD ["./myaktion"] + +EXPOSE 8000 \ No newline at end of file diff --git a/src/myaktion/docker-entrypoint.sh b/src/myaktion/docker-entrypoint.sh new file mode 100644 index 0000000000000000000000000000000000000000..3974640b053e6f84a21c292c01e3674348445831 --- /dev/null +++ b/src/myaktion/docker-entrypoint.sh @@ -0,0 +1,182 @@ +#!/usr/bin/env bash +# Use this script to test if a given TCP host/port are available + +WAITFORIT_cmdname=${0##*/} + +echoerr() { if [[ $WAITFORIT_QUIET -ne 1 ]]; then echo "$@" 1>&2; fi } + +usage() +{ + cat << USAGE >&2 +Usage: + $WAITFORIT_cmdname host:port [-s] [-t timeout] [-- command args] + -h HOST | --host=HOST Host or IP under test + -p PORT | --port=PORT TCP port under test + Alternatively, you specify the host and port as host:port + -s | --strict Only execute subcommand if the test succeeds + -q | --quiet Don't output any status messages + -t TIMEOUT | --timeout=TIMEOUT + Timeout in seconds, zero for no timeout + -- COMMAND ARGS Execute command with args after the test finishes +USAGE + exit 1 +} + +wait_for() +{ + if [[ $WAITFORIT_TIMEOUT -gt 0 ]]; then + echoerr "$WAITFORIT_cmdname: waiting $WAITFORIT_TIMEOUT seconds for $WAITFORIT_HOST:$WAITFORIT_PORT" + else + echoerr "$WAITFORIT_cmdname: waiting for $WAITFORIT_HOST:$WAITFORIT_PORT without a timeout" + fi + WAITFORIT_start_ts=$(date +%s) + while : + do + if [[ $WAITFORIT_ISBUSY -eq 1 ]]; then + nc -z $WAITFORIT_HOST $WAITFORIT_PORT + WAITFORIT_result=$? + else + (echo -n > /dev/tcp/$WAITFORIT_HOST/$WAITFORIT_PORT) >/dev/null 2>&1 + WAITFORIT_result=$? + fi + if [[ $WAITFORIT_result -eq 0 ]]; then + WAITFORIT_end_ts=$(date +%s) + echoerr "$WAITFORIT_cmdname: $WAITFORIT_HOST:$WAITFORIT_PORT is available after $((WAITFORIT_end_ts - WAITFORIT_start_ts)) seconds" + break + fi + sleep 1 + done + return $WAITFORIT_result +} + +wait_for_wrapper() +{ + # In order to support SIGINT during timeout: http://unix.stackexchange.com/a/57692 + if [[ $WAITFORIT_QUIET -eq 1 ]]; then + timeout $WAITFORIT_BUSYTIMEFLAG $WAITFORIT_TIMEOUT $0 --quiet --child --host=$WAITFORIT_HOST --port=$WAITFORIT_PORT --timeout=$WAITFORIT_TIMEOUT & + else + timeout $WAITFORIT_BUSYTIMEFLAG $WAITFORIT_TIMEOUT $0 --child --host=$WAITFORIT_HOST --port=$WAITFORIT_PORT --timeout=$WAITFORIT_TIMEOUT & + fi + WAITFORIT_PID=$! + trap "kill -INT -$WAITFORIT_PID" INT + wait $WAITFORIT_PID + WAITFORIT_RESULT=$? + if [[ $WAITFORIT_RESULT -ne 0 ]]; then + echoerr "$WAITFORIT_cmdname: timeout occurred after waiting $WAITFORIT_TIMEOUT seconds for $WAITFORIT_HOST:$WAITFORIT_PORT" + fi + return $WAITFORIT_RESULT +} + +# process arguments +while [[ $# -gt 0 ]] +do + case "$1" in + *:* ) + WAITFORIT_hostport=(${1//:/ }) + WAITFORIT_HOST=${WAITFORIT_hostport[0]} + WAITFORIT_PORT=${WAITFORIT_hostport[1]} + shift 1 + ;; + --child) + WAITFORIT_CHILD=1 + shift 1 + ;; + -q | --quiet) + WAITFORIT_QUIET=1 + shift 1 + ;; + -s | --strict) + WAITFORIT_STRICT=1 + shift 1 + ;; + -h) + WAITFORIT_HOST="$2" + if [[ $WAITFORIT_HOST == "" ]]; then break; fi + shift 2 + ;; + --host=*) + WAITFORIT_HOST="${1#*=}" + shift 1 + ;; + -p) + WAITFORIT_PORT="$2" + if [[ $WAITFORIT_PORT == "" ]]; then break; fi + shift 2 + ;; + --port=*) + WAITFORIT_PORT="${1#*=}" + shift 1 + ;; + -t) + WAITFORIT_TIMEOUT="$2" + if [[ $WAITFORIT_TIMEOUT == "" ]]; then break; fi + shift 2 + ;; + --timeout=*) + WAITFORIT_TIMEOUT="${1#*=}" + shift 1 + ;; + --) + shift + WAITFORIT_CLI=("$@") + break + ;; + --help) + usage + ;; + *) + echoerr "Unknown argument: $1" + usage + ;; + esac +done + +if [[ "$WAITFORIT_HOST" == "" || "$WAITFORIT_PORT" == "" ]]; then + echoerr "Error: you need to provide a host and port to test." + usage +fi + +WAITFORIT_TIMEOUT=${WAITFORIT_TIMEOUT:-15} +WAITFORIT_STRICT=${WAITFORIT_STRICT:-0} +WAITFORIT_CHILD=${WAITFORIT_CHILD:-0} +WAITFORIT_QUIET=${WAITFORIT_QUIET:-0} + +# Check to see if timeout is from busybox? +WAITFORIT_TIMEOUT_PATH=$(type -p timeout) +WAITFORIT_TIMEOUT_PATH=$(realpath $WAITFORIT_TIMEOUT_PATH 2>/dev/null || readlink -f $WAITFORIT_TIMEOUT_PATH) + +WAITFORIT_BUSYTIMEFLAG="" +if [[ $WAITFORIT_TIMEOUT_PATH =~ "busybox" ]]; then + WAITFORIT_ISBUSY=1 + # Check if busybox timeout uses -t flag + # (recent Alpine versions don't support -t anymore) + if timeout &>/dev/stdout | grep -q -e '-t '; then + WAITFORIT_BUSYTIMEFLAG="-t" + fi +else + WAITFORIT_ISBUSY=0 +fi + +if [[ $WAITFORIT_CHILD -gt 0 ]]; then + wait_for + WAITFORIT_RESULT=$? + exit $WAITFORIT_RESULT +else + if [[ $WAITFORIT_TIMEOUT -gt 0 ]]; then + wait_for_wrapper + WAITFORIT_RESULT=$? + else + wait_for + WAITFORIT_RESULT=$? + fi +fi + +if [[ $WAITFORIT_CLI != "" ]]; then + if [[ $WAITFORIT_RESULT -ne 0 && $WAITFORIT_STRICT -eq 1 ]]; then + echoerr "$WAITFORIT_cmdname: strict mode, refusing to execute subprocess" + exit $WAITFORIT_RESULT + fi + exec "${WAITFORIT_CLI[@]}" +else + exit $WAITFORIT_RESULT +fi \ No newline at end of file diff --git a/src/myaktion/handler/campaign.go b/src/myaktion/handler/campaign.go index 9b220a71dc4758f1b97eaa19f8f360b218b72acf..db13402861063d10df0f24b0f11ae9aaa79a3d36 100644 --- a/src/myaktion/handler/campaign.go +++ b/src/myaktion/handler/campaign.go @@ -131,3 +131,31 @@ func getDonation(r *http.Request) (*model.Donation, error) { } return &donation, nil } + +func PatchCampaign(w http.ResponseWriter, r *http.Request) { + id, err := getId(r) + if err != nil { + log.Printf("Error retrieving ID from request: %v", err) + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + + campaign, err := getCampaign(r) + if err != nil { + http.Error(w, err.Error(), http.StatusBadRequest) + return + } + + err = service.PatchCampaign2(campaign, id) + /*m := make(map[string]any) + err = json.NewDecoder(r.Body).Decode(&m) + if err != nil { + log.Errorf("Can't serialize request body to donation struct: %v", err) + } + + err = service.PatchCampaign(&m, id) + if err != nil { + log.Errorf("cant patch stuff: %v", err) + }*/ + +} diff --git a/src/myaktion/main.go b/src/myaktion/main.go index 9c69c595a6d359df5338aa3ba7320e5c66cb1736..dd990e4f9c02b8b118d0f4bd3714c588dd84a5df 100644 --- a/src/myaktion/main.go +++ b/src/myaktion/main.go @@ -30,7 +30,7 @@ func main() { router.HandleFunc("/campaigns/{id}", handler.GetCampaign).Methods("GET") router.HandleFunc("/campaigns", handler.GetCampaigns).Methods("GET") router.HandleFunc("/campaigns/{id}", handler.DeleteCampaign).Methods("DELETE") - + router.HandleFunc("/campaigns/{id}", handler.PatchCampaign).Methods("PATCH") router.HandleFunc("/campaigns/{id}/donation", handler.AddDonation).Methods("POST") if err := http.ListenAndServe(":8000", router); err != nil { log.Fatal(err) diff --git a/src/myaktion/model/campaign.go b/src/myaktion/model/campaign.go index 84a981fc84d5a1ead0a654c0a2736f692c1d2565..a5315c4664a9862690c6bb2d8fa214576c16a97c 100644 --- a/src/myaktion/model/campaign.go +++ b/src/myaktion/model/campaign.go @@ -12,3 +12,12 @@ type Campaign struct { Donations []Donation `gorm:"foreignKey:CampaignID;constraint:OnUpdate:CASCADE,OnDelete:CASCADE"` Account Account `gorm:"embedded;embeddedPrefix:account_"` } + +func (u *Campaign) AfterFind(tx *gorm.DB) (err error) { + var totalAmount float64 + for _, donation := range u.Donations { + totalAmount += donation.Amount + } + u.AmmountDonatedSoFar = totalAmount + return +} diff --git a/src/myaktion/service/campaign.go b/src/myaktion/service/campaign.go index 3755b229f1d4eee4fecf138247b7cab55b2b0d14..36e077a99368a224595b8e2ffe529fe26288ac65 100644 --- a/src/myaktion/service/campaign.go +++ b/src/myaktion/service/campaign.go @@ -78,3 +78,40 @@ func AddDonation(donation *model.Donation, id uint) error { log.Tracef("Stored: %v", donation) return nil } + +func PatchCampaign(m *map[string]any, id uint) error { + + /*var newM = make(map[string]any) + for field, value := range *m{ + if (field == "Account"){ + newM = value + } + }*/ + result := db.DB.Model(model.Campaign{}).Where("id = ?", id).Updates(m) + /*for field, value := range *m { + result := db.DB.Model(model.Campaign{}).Where("id = ?", id).Select(field).Updates(map[string]interface{}{field: value}) + if result.Error != nil { + return result.Error + } + }*/ + + if result.Error != nil { + return result.Error + } + log.Infof("Successfully patched campaign.") + return nil +} + +func PatchCampaign2(campaign *model.Campaign, id uint) error { + + existingCampaign, error := GetCampaign(id) + if error != nil { + return error + } + + result := db.DB.Model(existingCampaign).Updates(campaign) + if result != nil { + return result.Error + } + return nil +}