mirror of
https://gitlab.dit.htwk-leipzig.de/htwk-software/htwkalender.git
synced 2025-07-16 09:38:49 +02:00
feat:#51 added mapping for room schedule request
This commit is contained in:
@ -31,12 +31,14 @@ func (m Events) Contains(event Event) bool {
|
||||
}
|
||||
|
||||
type AnonymizedEventDTO struct {
|
||||
Day string `db:"Day" json:"day"`
|
||||
Week string `db:"Week" json:"week"`
|
||||
Start types.DateTime `db:"start" json:"start"`
|
||||
End types.DateTime `db:"end" json:"end"`
|
||||
Rooms string `db:"Rooms" json:"rooms"`
|
||||
Free bool `json:"free"`
|
||||
Day string `db:"Day" json:"day"`
|
||||
Week string `db:"Week" json:"week"`
|
||||
Start types.DateTime `db:"start" json:"start"`
|
||||
End types.DateTime `db:"end" json:"end"`
|
||||
Rooms string `db:"Rooms" json:"rooms"`
|
||||
Name string `db:"Name" json:"name"`
|
||||
EventType string `db:"EventType" json:"eventType"`
|
||||
Free bool `json:"free"`
|
||||
}
|
||||
|
||||
type Event struct {
|
||||
@ -86,12 +88,14 @@ func (e *Event) SetCourse(course string) Event {
|
||||
// AnonymizeEvent Creates an AnonymizedEventDTO from an Event hiding all sensitive data
|
||||
func (e *Event) AnonymizeEvent() AnonymizedEventDTO {
|
||||
return AnonymizedEventDTO{
|
||||
Day: e.Day,
|
||||
Week: e.Week,
|
||||
Start: e.Start,
|
||||
End: e.End,
|
||||
Rooms: e.Rooms,
|
||||
Free: strings.Contains(strings.ToLower(e.Name), "zur freien verfügung"),
|
||||
Day: e.Day,
|
||||
Week: e.Week,
|
||||
Start: e.Start,
|
||||
End: e.End,
|
||||
Rooms: e.Rooms,
|
||||
Name: e.Name,
|
||||
EventType: e.EventType,
|
||||
Free: strings.Contains(strings.ToLower(e.Name), "zur freien verfügung"),
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -174,22 +174,22 @@ func TestEventAnonymizeEvent(t *testing.T) {
|
||||
{
|
||||
name: "event anonymize empty event",
|
||||
fields: fields{},
|
||||
want: AnonymizedEventDTO{Day: "", Week: "", Start: types.DateTime{}, End: types.DateTime{}, Rooms: "", Free: false},
|
||||
want: AnonymizedEventDTO{Day: "", Week: "", Start: types.DateTime{}, End: types.DateTime{}, Rooms: "", Name: "", EventType: "", Free: false},
|
||||
},
|
||||
{
|
||||
name: "event anonymize one event",
|
||||
fields: fields{Name: "Event", Day: "test", Week: "test", Rooms: "test"},
|
||||
want: AnonymizedEventDTO{Day: "test", Week: "test", Start: types.DateTime{}, End: types.DateTime{}, Rooms: "test", Free: false},
|
||||
want: AnonymizedEventDTO{Day: "test", Week: "test", Start: types.DateTime{}, End: types.DateTime{}, Rooms: "test", Name: "Event", EventType: "", Free: false},
|
||||
},
|
||||
{
|
||||
name: "event anonymize one event with free",
|
||||
fields: fields{Name: "Räume zur freien Verfügung", Day: "test", Week: "test", Rooms: "test", Course: "test"},
|
||||
want: AnonymizedEventDTO{Day: "test", Week: "test", Start: types.DateTime{}, End: types.DateTime{}, Rooms: "test", Free: true},
|
||||
want: AnonymizedEventDTO{Day: "test", Week: "test", Start: types.DateTime{}, End: types.DateTime{}, Rooms: "test", Name: "Räume zur freien Verfügung", EventType: "", Free: true},
|
||||
},
|
||||
{
|
||||
name: "event anonymize another free event",
|
||||
fields: fields{Name: "Zur freien Verfügung", Day: "Montag", Week: "5", Start: types.DateTime{}, End: types.DateTime{}, Rooms: "TR_A1.28-S", Course: "42INM-3"},
|
||||
want: AnonymizedEventDTO{Day: "Montag", Week: "5", Start: types.DateTime{}, End: types.DateTime{}, Rooms: "TR_A1.28-S", Free: true},
|
||||
want: AnonymizedEventDTO{Day: "Montag", Week: "5", Start: types.DateTime{}, End: types.DateTime{}, Rooms: "TR_A1.28-S", Name: "Zur freien Verfügung", EventType: "", Free: true},
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
|
@ -199,7 +199,8 @@ func AddRoutes(services serviceModel.Service) {
|
||||
roomParam := c.QueryParam("room")
|
||||
to := c.QueryParam("to")
|
||||
from := c.QueryParam("from")
|
||||
roomSchedule, err := room.GetRoomSchedule(services.App, roomParam, from, to)
|
||||
mapped := c.QueryParam("mapped")
|
||||
roomSchedule, err := room.GetRoomSchedule(services.App, roomParam, from, to, mapped)
|
||||
if err != nil {
|
||||
slog.Error("Failed to get room schedule:", "error", err)
|
||||
return c.JSON(http.StatusBadRequest, "Failed to get room schedule")
|
||||
|
24
services/data-manager/service/functions/roomMapping.go
Normal file
24
services/data-manager/service/functions/roomMapping.go
Normal file
@ -0,0 +1,24 @@
|
||||
package functions
|
||||
|
||||
import (
|
||||
"regexp"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func MapRoom(room string) string {
|
||||
// remove dots from room string
|
||||
room = strings.ReplaceAll(room, ".", "")
|
||||
|
||||
// Regular expression pattern to match room identifiers
|
||||
// The pattern looks for strings that start with two uppercase letters, optionally followed by an underscore,
|
||||
// followed by 1 to 3 digits, and ending with "-[A-Z]"
|
||||
re := regexp.MustCompile(`\b[A-ZÄÖÜ]{2}([_]+[A-ZÄÖÜ])?[0-9]{1,4}[a-zäöü]?-[A-ZÄÖÜ]\b`)
|
||||
|
||||
// Use the ReplaceAllStringFunc to process each match
|
||||
result := re.ReplaceAllStringFunc(room, func(match string) string {
|
||||
// Remove the last two characters (i.e., "-<letter>")
|
||||
return match[:len(match)-2]
|
||||
})
|
||||
|
||||
return result
|
||||
}
|
36
services/data-manager/service/functions/roomMapping_test.go
Normal file
36
services/data-manager/service/functions/roomMapping_test.go
Normal file
@ -0,0 +1,36 @@
|
||||
package functions
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestMapRoom(t *testing.T) {
|
||||
type args struct {
|
||||
room string
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
args args
|
||||
want string
|
||||
}{
|
||||
{
|
||||
name: "Test 1 MapRoom",
|
||||
args: args{
|
||||
room: "H.1.1",
|
||||
},
|
||||
want: "H11",
|
||||
},
|
||||
{
|
||||
name: "Test Treftsbau MapRoom",
|
||||
args: args{
|
||||
room: "TR_L3.03-S,TR_L3.02-S,TR_L2.13-L,TR_L2.05-S,TR_L1.14-H,TR_L1.07-H,TR_L1.06-B,TR_L0.14-S,TR_Innenhof_FF,TR_C1.62-L,TR_B1.50-S,TR_B1.49-S,TR_B1.48-S,TR_B1.46-S,TR_B1.45-S,TR_B0.71-L,TR_B0.70-L,TR_B0.67-L,TR_A_Cafeteria,TR_A2.28-L,TR_A1.40-F,TR_A1.37-S,TR_A1.34-S,TR_A1.29-H,TR_A1.28-S,TR_A1.27-S,TR_A1.26-S,TR_A1.25-S,TR_A1.24-H,TR_A0.34-L,TR_A0.33-L,TR_A0.32.2-L,TR_A0.32.1-L,TR_A0.31.1-L,NI_FoyerK_F,NI_Foyer1_F,NI104-L,NI103-L,NI102-L,NI070-L,GU319-L,GU318-L,GU317b-L,GU317a-L,GU313-L,GU309-L,GU301-L,GU225-L,GU224-L,GU219-L,GU203-L,GU202-L,GU201-L,GU014-L,GU013-L,GU012-L,GU011-L,GU010-L,GU009-L,GU001-L,FÖ306-S,FÖ305-S,FÖ304-S",
|
||||
},
|
||||
want: "TR_L303,TR_L302,TR_L213,TR_L205,TR_L114,TR_L107,TR_L106,TR_L014,TR_Innenhof_FF,TR_C162,TR_B150,TR_B149,TR_B148,TR_B146,TR_B145,TR_B071,TR_B070,TR_B067,TR_A_Cafeteria,TR_A228,TR_A140,TR_A137,TR_A134,TR_A129,TR_A128,TR_A127,TR_A126,TR_A125,TR_A124,TR_A034,TR_A033,TR_A0322,TR_A0321,TR_A0311,NI_FoyerK_F,NI_Foyer1_F,NI104,NI103,NI102,NI070,GU319,GU318,GU317b,GU317a,GU313,GU309,GU301,GU225,GU224,GU219,GU203,GU202,GU201,GU014,GU013,GU012,GU011,GU010,GU009,GU001,FÖ306,FÖ305,FÖ304",
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if got := MapRoom(tt.args.room); got != tt.want {
|
||||
t.Errorf("MapRoom() = %v, want %v", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
@ -42,12 +42,25 @@ func GetRoomScheduleForDay(app *pocketbase.PocketBase, room string, date string)
|
||||
return anonymizedRoomSchedule, nil
|
||||
}
|
||||
|
||||
func GetRoomSchedule(app *pocketbase.PocketBase, room string, from string, to string) ([]model.AnonymizedEventDTO, error) {
|
||||
func GetRoomSchedule(app *pocketbase.PocketBase, room string, from string, to string, mapped string) ([]model.AnonymizedEventDTO, error) {
|
||||
|
||||
if mapped == "true" {
|
||||
room = functions.MapRoom(room)
|
||||
}
|
||||
|
||||
roomSchedule, err := db.GetRoomSchedule(app, room, from, to)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
anonymizedRoomSchedule := anonymizeRooms(roomSchedule)
|
||||
|
||||
// If mapped is "true", map the rooms again after anonymization
|
||||
if mapped == "true" {
|
||||
for i := range anonymizedRoomSchedule {
|
||||
anonymizedRoomSchedule[i].Rooms = functions.MapRoom(anonymizedRoomSchedule[i].Rooms)
|
||||
}
|
||||
}
|
||||
|
||||
return anonymizedRoomSchedule, nil
|
||||
}
|
||||
|
||||
|
@ -56,12 +56,14 @@ func TestAnonymizeRooms(t *testing.T) {
|
||||
},
|
||||
want: []model.AnonymizedEventDTO{
|
||||
{
|
||||
Day: "Montag",
|
||||
Week: "52",
|
||||
Start: types.DateTime{},
|
||||
End: types.DateTime{},
|
||||
Rooms: "Room",
|
||||
Free: false,
|
||||
Day: "Montag",
|
||||
Week: "52",
|
||||
Start: types.DateTime{},
|
||||
End: types.DateTime{},
|
||||
Rooms: "Room",
|
||||
Name: "Secret",
|
||||
EventType: "V",
|
||||
Free: false,
|
||||
},
|
||||
},
|
||||
},
|
||||
@ -112,20 +114,24 @@ func TestAnonymizeRooms(t *testing.T) {
|
||||
},
|
||||
want: []model.AnonymizedEventDTO{
|
||||
{
|
||||
Day: "Montag",
|
||||
Week: "51",
|
||||
Start: types.DateTime{},
|
||||
End: types.DateTime{},
|
||||
Rooms: "Room",
|
||||
Free: false,
|
||||
Day: "Montag",
|
||||
Week: "51",
|
||||
Start: types.DateTime{},
|
||||
End: types.DateTime{},
|
||||
Rooms: "Room",
|
||||
Name: "Incognito",
|
||||
EventType: "V",
|
||||
Free: false,
|
||||
},
|
||||
{
|
||||
Day: "Dienstag",
|
||||
Week: "52",
|
||||
Start: types.DateTime{},
|
||||
End: types.DateTime{},
|
||||
Rooms: "Room",
|
||||
Free: false,
|
||||
Day: "Dienstag",
|
||||
Week: "52",
|
||||
Start: types.DateTime{},
|
||||
End: types.DateTime{},
|
||||
Rooms: "Room",
|
||||
Name: "Private",
|
||||
EventType: "S",
|
||||
Free: false,
|
||||
},
|
||||
},
|
||||
},
|
||||
|
Reference in New Issue
Block a user