From b7d92d368e763ea36e8cce54d410e0aa170e9000 Mon Sep 17 00:00:00 2001 From: alexanderkohler1 <alexander1.kohler@student.reutlingen-university.de> Date: Sun, 15 Jun 2025 20:54:38 +0200 Subject: [PATCH] =?UTF-8?q?Web-Controller=20um=20K=C3=BCrzel-Unterst=C3=BC?= =?UTF-8?q?tzung=20erweitert?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - max8 Validator für Kürzel-Längenbegrenzung hinzugefügt - Datum-Parsing für Formulare verbessert - Kürzel-Feld in Create- und Update-Controllern integriert --- src/app/project/web/web.go | 40 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/src/app/project/web/web.go b/src/app/project/web/web.go index 17509a5..5ee8347 100644 --- a/src/app/project/web/web.go +++ b/src/app/project/web/web.go @@ -41,6 +41,16 @@ type ProjectDetailData struct { func RegisterController(appCtx *hctx.AppCtx, webCtx *web.Ctx, mongoManager *project.MongoManager) { log.Println("INFO: Registering project controllers...") + // Add max8 validator for string length validation (needed for project shortcut field) + appCtx.Validator.AddFunc("max8", func(value any) error { + if str, ok := value.(string); ok { + if len(str) > 8 { + return errors.New("harmony.error.validation.max") + } + } + return nil + }) + // Initialize the data access layer (repository) and business logic layer (service) // Repository handles MongoDB operations, Service handles business rules projectRepo := project.NewMongoProjectRepository(mongoManager.GetDatabase()) @@ -166,6 +176,21 @@ func projectCreateController(appCtx *hctx.AppCtx, webCtx *web.Ctx, service *proj return io.Error(web.ErrInternal, err) } + // Manual parsing for date fields since web.ReadForm doesn't support time.Time + request := io.Request() + if err := request.ParseForm(); err == nil { + if startDateStr := request.FormValue("StartDate"); startDateStr != "" { + if startDate, parseErr := time.Parse("2006-01-02", startDateStr); parseErr == nil { + toCreate.StartDate = startDate + } + } + if endDateStr := request.FormValue("EndDate"); endDateStr != "" { + if endDate, parseErr := time.Parse("2006-01-02", endDateStr); parseErr == nil { + toCreate.EndDate = endDate + } + } + } + // If there are validation errors, redisplay the form with error messages if validationErrs != nil { log.Printf("WARN: Project creation validation failed: %d errors", len(validationErrs)) @@ -325,6 +350,21 @@ func projectUpdateController(appCtx *hctx.AppCtx, webCtx *web.Ctx, service *proj return io.InlineError(web.ErrInternal, err) } + // Manual parsing for date fields since web.ReadForm doesn't support time.Time + request := io.Request() + if err := request.ParseForm(); err == nil { + if startDateStr := request.FormValue("StartDate"); startDateStr != "" { + if startDate, parseErr := time.Parse("2006-01-02", startDateStr); parseErr == nil { + toUpdate.StartDate = startDate + } + } + if endDateStr := request.FormValue("EndDate"); endDateStr != "" { + if endDate, parseErr := time.Parse("2006-01-02", endDateStr); parseErr == nil { + toUpdate.EndDate = endDate + } + } + } + // If validation fails, redisplay the form with error messages if validationErrs != nil { log.Printf("WARN: Project update validation failed: %d errors", len(validationErrs)) -- GitLab