Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
M
Myaktion Go
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Emanuel Petrinovic
Myaktion Go
Commits
470d6133
Commit
470d6133
authored
2 months ago
by
Emanuel Petrinovic
Browse files
Options
Downloads
Patches
Plain Diff
Hausaufgaben fertig
parent
e2487a07
No related branches found
No related tags found
No related merge requests found
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
Calls.md
+32
-0
32 additions, 0 deletions
Calls.md
src/myaktion/handler/campaign.go
+88
-0
88 additions, 0 deletions
src/myaktion/handler/campaign.go
src/myaktion/main.go
+6
-0
6 additions, 0 deletions
src/myaktion/main.go
src/myaktion/service/campaign.go
+49
-0
49 additions, 0 deletions
src/myaktion/service/campaign.go
with
175 additions
and
0 deletions
Calls.md
0 → 100644
+
32
−
0
View file @
470d6133
### Campaign POST
```
curl -H "Content-Type: application/json" -d '{"name":"Covid","organizerName":"Martin","donationMinimum":2,"targetAmount":100, "account":{"name":"Martin","bankName":"DKB","number":"123456"}}' localhost:8000/campaigns
```
### 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
```
### Campaign all GET
```
curl localhost:8000/campaigns
```
### Campaign one GET
```
curl localhost:8000/campaigns/1
```
### Campaign Delete
```
curl -X DELETE localhost:8000/campaigns/1
```
### Donation POST:
```
curl -X POST -H "Content-Type: application/json" -d '{"amount": 50.0, "donorName": "Max Mustermann", "receiptRequested": true, "status": "TRANSFERRED", "account": {"name": "Max Mustermann", "bankname": "Bank AG", "number": "1234567890"}}' localhost:8000/campaigns/1/donation
```
This diff is collapsed.
Click to expand it.
src/myaktion/handler/campaign.go
+
88
−
0
View file @
470d6133
...
...
@@ -24,6 +24,46 @@ func CreateCampaign(w http.ResponseWriter, r *http.Request) {
sendJson
(
w
,
campaign
)
}
func
UpdateCampaign
(
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
}
error
:=
service
.
UpdateCampaign
(
campaign
,
id
)
if
error
!=
nil
{
http
.
Error
(
w
,
error
.
Error
(),
http
.
StatusInternalServerError
)
return
}
sendJson
(
w
,
campaign
)
}
func
GetCampaign
(
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
:=
service
.
GetCampaign
(
id
)
if
err
!=
nil
{
log
.
Printf
(
"Error calling service GetCampagin: %v"
,
err
)
http
.
Error
(
w
,
err
.
Error
(),
http
.
StatusInternalServerError
)
return
}
sendJson
(
w
,
campaign
)
}
func
GetCampaigns
(
w
http
.
ResponseWriter
,
_
*
http
.
Request
)
{
campaigns
,
err
:=
service
.
GetCampaigns
()
if
err
!=
nil
{
...
...
@@ -34,6 +74,43 @@ func GetCampaigns(w http.ResponseWriter, _ *http.Request) {
sendJson
(
w
,
campaigns
)
}
func
DeleteCampaign
(
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
}
var
campaign
model
.
Campaign
err
=
service
.
DeleteCampaign
(
&
campaign
,
id
)
if
err
!=
nil
{
log
.
Printf
(
"Error calling service DeleteCampaign: %v"
,
err
)
http
.
Error
(
w
,
err
.
Error
(),
http
.
StatusInternalServerError
)
return
}
}
func
AddDonation
(
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
}
donation
,
err
:=
getDonation
(
r
)
if
err
!=
nil
{
http
.
Error
(
w
,
err
.
Error
(),
http
.
StatusBadRequest
)
return
}
err
=
service
.
AddDonation
(
donation
,
id
)
}
func
getCampaign
(
r
*
http
.
Request
)
(
*
model
.
Campaign
,
error
)
{
var
campaign
model
.
Campaign
err
:=
json
.
NewDecoder
(
r
.
Body
)
.
Decode
(
&
campaign
)
...
...
@@ -43,3 +120,14 @@ func getCampaign(r *http.Request) (*model.Campaign, error) {
}
return
&
campaign
,
nil
}
func
getDonation
(
r
*
http
.
Request
)
(
*
model
.
Donation
,
error
)
{
var
donation
model
.
Donation
err
:=
json
.
NewDecoder
(
r
.
Body
)
.
Decode
(
&
donation
)
if
err
!=
nil
{
log
.
Errorf
(
"Can't serialize request body to donation struct: %v"
,
err
)
return
nil
,
err
}
return
&
donation
,
nil
}
This diff is collapsed.
Click to expand it.
src/myaktion/main.go
+
6
−
0
View file @
470d6133
...
...
@@ -24,8 +24,14 @@ func main() {
log
.
Println
(
"Starting My-Aktion API server"
)
router
:=
mux
.
NewRouter
()
router
.
HandleFunc
(
"/health"
,
handler
.
Health
)
.
Methods
(
"GET"
)
router
.
HandleFunc
(
"/campaigns"
,
handler
.
CreateCampaign
)
.
Methods
(
"POST"
)
router
.
HandleFunc
(
"/campaigns/{id}"
,
handler
.
UpdateCampaign
)
.
Methods
(
"PUT"
)
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}/donation"
,
handler
.
AddDonation
)
.
Methods
(
"POST"
)
if
err
:=
http
.
ListenAndServe
(
":8000"
,
router
);
err
!=
nil
{
log
.
Fatal
(
err
)
}
...
...
This diff is collapsed.
Click to expand it.
src/myaktion/service/campaign.go
+
49
−
0
View file @
470d6133
...
...
@@ -29,3 +29,52 @@ func GetCampaigns() ([]model.Campaign, error) {
log
.
Tracef
(
"Retrieved: %v"
,
campaigns
)
return
campaigns
,
nil
}
func
GetCampaign
(
id
uint
)
(
*
model
.
Campaign
,
error
)
{
var
campaign
*
model
.
Campaign
result
:=
db
.
DB
.
First
(
&
campaign
,
id
)
if
result
.
Error
!=
nil
{
return
nil
,
result
.
Error
}
log
.
Tracef
(
"Retrieved: %v"
,
campaign
)
return
campaign
,
nil
}
func
UpdateCampaign
(
campaign
*
model
.
Campaign
,
id
uint
)
error
{
result
:=
db
.
DB
.
Model
(
&
campaign
)
.
Where
(
"id = ?"
,
id
)
.
Updates
(
campaign
)
if
result
.
Error
!=
nil
{
return
result
.
Error
}
return
nil
}
func
DeleteCampaign
(
campaign
*
model
.
Campaign
,
id
uint
)
error
{
result
:=
db
.
DB
.
Model
(
&
campaign
)
.
Where
(
"id = ?"
,
id
)
.
Delete
(
campaign
)
if
result
.
Error
!=
nil
{
return
result
.
Error
}
log
.
Infof
(
"Successfully deleted campaign with ID %v in database."
,
campaign
.
ID
)
log
.
Tracef
(
"Deleted: %v"
,
campaign
)
return
nil
}
func
AddDonation
(
donation
*
model
.
Donation
,
id
uint
)
error
{
donation
.
CampaignID
=
id
result
:=
db
.
DB
.
Create
(
&
donation
)
if
result
.
Error
!=
nil
{
return
result
.
Error
}
log
.
Infof
(
"Successfully stored new donation in database."
)
log
.
Tracef
(
"Stored: %v"
,
donation
)
return
nil
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment