Skip to content

Commit

Permalink
Merge pull request #187 from DHBW-SE-2023/bugfix/student-name-unique-…
Browse files Browse the repository at this point in the history
…for-course

Database: Student's name is unique for course
  • Loading branch information
leandergmeiner committed Mar 31, 2024
2 parents 844318d + c078c9e commit 7eea977
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 7 deletions.
8 changes: 4 additions & 4 deletions internal/backend/database/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ type Student struct {
gorm.Model
FirstName string `gorm:"check:FirstName!='';type:varchar(64)"`
LastName string `gorm:"check:LastName!='';type:varchar(64)"`
FullName string `gorm:"check:FullName!='';type:varchar(128)"`
CourseID uint
IsImmatriculated bool
FullName string `gorm:"check:FullName!='';type:varchar(128);uniqueIndex:namecourseunique;index;not null"`
CourseID uint `gorm:"uniqueIndex:namecourseunique;not null"`
IsImmatriculated bool `gorm:"default:true"`
}

type Attendance struct {
Expand Down Expand Up @@ -66,7 +66,7 @@ func (item *BackendDatabase) ConnectDatabase() error {
}

// Ensure that the file exists
fd, err := os.OpenFile(dbPath, os.O_CREATE|os.O_EXCL|os.O_RDWR, 0600)
fd, err := os.OpenFile(dbPath, os.O_APPEND|os.O_CREATE|os.O_EXCL|os.O_RDWR, 0600)
if err == nil {
fd.Close()
}
Expand Down
2 changes: 2 additions & 0 deletions internal/frontend/main/pages/settings/email.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,8 @@ func ConfigureForm(w fyne.Window, alive bool, mailConnection string, mailUser st
buttonArea := container.NewAdaptiveGrid(2, submitButton, restartButton)
form := &widget.Form{
Items: []*widget.FormItem{ // we can specify items in the constructor
{Text: "Beschreibung", Widget: widget.NewLabel("Hier können sie das EMail Postfach hinterlegen, an welches")},
{Text: "", Widget: widget.NewLabel("die Kurssprecher Anwesenheitslisten schicken werden.")},
{Text: "Status", Widget: serverStatus},
{Text: "E-Mail Server", Widget: server},
{Text: "E-Mail User", Widget: username},
Expand Down
5 changes: 3 additions & 2 deletions internal/frontend/main/pages/students.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,14 +100,15 @@ func ValidateInput(studentEntry *widget.Entry, courseEntry *widget.Entry, confir
re, _ := regexp.Compile(`^[a-zA-Z]+(\s[a-zA-Z]+)+$`)
if !re.MatchString(s) {
validStudent = false
return errors.New("Die Eingabe entspricht nicht den Bedingungen(min. 1x Leerzeichen, nur Buchstaben!")
return errors.New("die eingabe entspricht nicht den bedingungen(min. 1x leerzeichen, nur buchstaben)")
} else {
validStudent = true
}
return nil
}
studentEntry.OnChanged = func(s string) {
if len(s) > 30 {
// Max student name length is 128
if len(s) > 128 {
s = s[0:10]
studentEntry.SetText(s)
}
Expand Down
2 changes: 1 addition & 1 deletion internal/frontend/main/pages/verification.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (

func VerificationScreen(w fyne.Window, img []byte, course int, courseTable *fyne.Container, optional ...time.Time) fyne.CanvasObject {
header := ReturnVerificationHeader()
description := canvas.NewText("Überprüfen sie die dargestellte Liste und wählen gegebenfalls Anwesende Studenten aus:", color.Black)
description := canvas.NewText("Vergleichen sie die links dargestellte Unterschriftenliste mit den erfassten heutigen Anwesenheiten:", color.Black)
description.TextSize = 16
description.TextStyle = fyne.TextStyle{Bold: true}
image := RotateImage(img)
Expand Down
21 changes: 21 additions & 0 deletions test/database_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,27 @@ func TestConnectDatabase(t *testing.T) {
}
}

func TestInsertTwoStudentsWithSameName(t *testing.T) {
conn, _ := setupDatabase()
defer clearDatabase(t, conn)

c, _ := conn.CourseByName("TIK22")

// This student already exists in the database
s := backend.Student{
FirstName: "Max",
LastName: "Mustermann",
IsImmatriculated: true,
CourseID: c.ID,
}

_, err := conn.InsertStudent(s)
if err == nil {
t.Fatalf("Expected to fail. Two students in the same course may not have the same name")
}

}

func TestInsertAttendanceList(t *testing.T) {
conn, _ := setupDatabase()
defer clearDatabase(t, conn)
Expand Down

0 comments on commit 7eea977

Please sign in to comment.