update report function

This commit is contained in:
Adrian Zürcher
2026-02-11 13:42:02 +01:00
parent 81e05851bc
commit f2d20e83d6
5 changed files with 34 additions and 88 deletions

View File

@@ -3,6 +3,7 @@ package api
import (
"net/http"
"slices"
"strings"
"gitea.tecamino.com/paadi/memberDB/models"
"github.com/gin-gonic/gin"
@@ -31,6 +32,13 @@ func (a *APIHandler) GetReport(c *gin.Context) {
return
}
if len(request.Date) == 0 {
c.JSON(http.StatusOK, gin.H{
"data": report,
})
return
}
var events []models.Event
if len(request.Name) > 0 {
@@ -38,6 +46,7 @@ func (a *APIHandler) GetReport(c *gin.Context) {
} else {
events, err = a.DbHandler.GetEvent(0)
}
if err != nil {
a.logger.Error("GetReport", err)
c.JSON(http.StatusBadRequest, gin.H{
@@ -53,40 +62,15 @@ func (a *APIHandler) GetReport(c *gin.Context) {
addedMember := make(map[uint]bool)
for _, e := range events {
// skip if date lower or not equal from date as well as greater than to date
if (e.Date < request.Date.From || e.Date > request.Date.From) && e.Date != request.Date.From {
continue
}
if len(events) == 0 {
c.JSON(http.StatusOK, gin.H{})
return
}
// skip if day not selected
switch e.Day {
case "Monday":
if !request.Weekdays.Monday {
continue
}
case "Tuesday":
if !request.Weekdays.Tuesday {
continue
}
case "Wednesday":
if !request.Weekdays.Wednesday {
continue
}
case "Thursday":
if !request.Weekdays.Thursday {
continue
}
case "Friday":
if !request.Weekdays.Friday {
continue
}
case "Saturday":
if !request.Weekdays.Saturday {
continue
}
case "Sunday":
if !request.Weekdays.Sunday {
for _, e := range events {
for _, d := range request.Date {
if !strings.Contains(e.Date, d) {
continue
}
}
@@ -136,8 +120,13 @@ func (a *APIHandler) GetReport(c *gin.Context) {
for _, m := range members {
if _, ok := addedMember[m.Id]; ok {
continue
} else if !slices.Contains(request.Groups, m.Group.Id) {
continue
} else if len(request.Groups) > 0 {
if m.Group == nil {
continue
} else if !slices.Contains(request.Groups, m.Group.Id) {
continue
}
}
report.NonAttendees = append(report.NonAttendees, &m)
}

View File

@@ -3,6 +3,7 @@ package handlers
import (
"errors"
"slices"
"strings"
"gitea.tecamino.com/paadi/memberDB/models"
)
@@ -17,6 +18,10 @@ func (dh *DatabaseHandler) GetReport(filter models.ReportFilter) (report models.
return report, errors.New("database not opened")
}
if len(filter.Date) == 0 {
return
}
var events []models.Event
if len(filter.Name) > 0 {
@@ -36,39 +41,9 @@ func (dh *DatabaseHandler) GetReport(filter models.ReportFilter) (report models.
addedMember := make(map[uint]bool)
for _, e := range events {
// skip if date lower or not equal from date as well as greater than to date
if (e.Date < filter.Date.From || e.Date > filter.Date.From) && e.Date != filter.Date.From {
continue
}
// skip if day not selected
switch e.Day {
case "Monday":
if !filter.Weekdays.Monday {
continue
}
case "Tuesday":
if !filter.Weekdays.Tuesday {
continue
}
case "Wednesday":
if !filter.Weekdays.Wednesday {
continue
}
case "Thursday":
if !filter.Weekdays.Thursday {
continue
}
case "Friday":
if !filter.Weekdays.Friday {
continue
}
case "Saturday":
if !filter.Weekdays.Saturday {
continue
}
case "Sunday":
if !filter.Weekdays.Sunday {
for _, d := range filter.Date {
if !strings.Contains(e.Date, d) {
continue
}
}

View File

@@ -1,6 +0,0 @@
package models
type Date struct {
From string ` json:"from,omitempty"`
To string ` json:"to,omitempty"`
}

View File

@@ -1,10 +1,9 @@
package models
type ReportFilter struct {
Weekdays Weekdays `json:"weekdays"`
Date Date `json:"date"`
Groups []uint `json:"groupIds,omitempty"`
Name []string `json:"name,omitempty"`
Date []string `json:"date"`
Groups []uint `json:"groupIds,omitempty"`
Name []string `json:"name,omitempty"`
}
type Report struct {

View File

@@ -1,11 +0,0 @@
package models
type Weekdays struct {
Monday bool `json:"monday,omitempty"`
Tuesday bool `json:"tuesday,omitempty"`
Wednesday bool `json:"wednesday,omitempty"`
Thursday bool `json:"thursday,omitempty"`
Friday bool `json:"friday,omitempty"`
Saturday bool `json:"saturday,omitempty"`
Sunday bool `json:"sunday,omitempty"`
}