refactor: perform clean up
This commit is contained in:
parent
c15cdea57d
commit
a7e427ca14
13 changed files with 133 additions and 85 deletions
|
|
@ -10,8 +10,6 @@ import (
|
|||
"git.patanix.de/git/kettlebell-app/internal/data"
|
||||
)
|
||||
|
||||
// TrainingPayload ist die JSON-Struktur, die an das Backend gesendet wird.
|
||||
// Die `json:"..."`-Tags stellen sicher, dass die Feldnamen im JSON korrekt sind.
|
||||
type TrainingPayload struct {
|
||||
Reps int `json:"reps"`
|
||||
Rest float64 `json:"rest"`
|
||||
|
|
@ -19,17 +17,14 @@ type TrainingPayload struct {
|
|||
UUID string `json:"uuid"`
|
||||
}
|
||||
|
||||
// ApiService kümmert sich um die Kommunikation mit dem Backend.
|
||||
type ApiService struct {
|
||||
client *http.Client
|
||||
endpoint string
|
||||
uuid string
|
||||
}
|
||||
|
||||
// NewApiService erstellt einen neuen Service für die API-Kommunikation.
|
||||
func NewApiService(appUUID string) *ApiService {
|
||||
return &ApiService{
|
||||
// Erstellt einen HTTP-Client mit einem 5-Sekunden-Timeout, genau wie in deiner Flutter-App.
|
||||
client: &http.Client{
|
||||
Timeout: 5 * time.Second,
|
||||
},
|
||||
|
|
@ -38,15 +33,12 @@ func NewApiService(appUUID string) *ApiService {
|
|||
}
|
||||
}
|
||||
|
||||
// SendTrainingData sendet eine abgeschlossene Trainingseinheit an das Backend.
|
||||
func (s *ApiService) SendTrainingData(session *data.TrainingSession) {
|
||||
// Berechnung für 'rest' durchführen.
|
||||
var rest float64
|
||||
if session.Sets > 0 {
|
||||
rest = float64(session.Duration) / float64(session.Sets)
|
||||
}
|
||||
|
||||
// Die zu sendenden Daten vorbereiten.
|
||||
payload := TrainingPayload{
|
||||
Reps: int(session.RepsPerSet),
|
||||
Rest: rest,
|
||||
|
|
@ -54,14 +46,12 @@ func (s *ApiService) SendTrainingData(session *data.TrainingSession) {
|
|||
UUID: s.uuid,
|
||||
}
|
||||
|
||||
// Daten in JSON umwandeln.
|
||||
jsonData, err := json.Marshal(payload)
|
||||
if err != nil {
|
||||
log.Printf("API Fehler: Konnte Payload nicht in JSON umwandeln: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
// Den HTTP-Request erstellen.
|
||||
req, err := http.NewRequest("POST", s.endpoint, bytes.NewBuffer(jsonData))
|
||||
if err != nil {
|
||||
log.Printf("API Fehler: Konnte Request nicht erstellen: %v", err)
|
||||
|
|
@ -69,7 +59,6 @@ func (s *ApiService) SendTrainingData(session *data.TrainingSession) {
|
|||
}
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
|
||||
// Request senden.
|
||||
log.Printf("Sende Training an Backend: %s", string(jsonData))
|
||||
resp, err := s.client.Do(req)
|
||||
if err != nil {
|
||||
|
|
@ -78,7 +67,6 @@ func (s *ApiService) SendTrainingData(session *data.TrainingSession) {
|
|||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
// Antwort des Servers prüfen.
|
||||
if resp.StatusCode == http.StatusOK || resp.StatusCode == http.StatusCreated {
|
||||
log.Println("Training erfolgreich an Backend gesendet.")
|
||||
} else {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue