From acdee880e449dbe5ec379a37ec0ee5d742c46b89 Mon Sep 17 00:00:00 2001 From: Florian Hoss Date: Wed, 30 Mar 2022 15:11:58 +0200 Subject: [PATCH] registering user works --- Lab01/app/.idea/saveactions_settings.xml | 13 ++++++ Lab01/app/database/types.go | 10 +++-- Lab01/app/templates/register.tmpl | 54 ++++++++++++------------ Lab01/app/webpage/webpage.go | 3 +- 4 files changed, 48 insertions(+), 32 deletions(-) create mode 100644 Lab01/app/.idea/saveactions_settings.xml diff --git a/Lab01/app/.idea/saveactions_settings.xml b/Lab01/app/.idea/saveactions_settings.xml new file mode 100644 index 0000000..66129c1 --- /dev/null +++ b/Lab01/app/.idea/saveactions_settings.xml @@ -0,0 +1,13 @@ + + + + + + \ No newline at end of file diff --git a/Lab01/app/database/types.go b/Lab01/app/database/types.go index a0078b8..d4e11f0 100644 --- a/Lab01/app/database/types.go +++ b/Lab01/app/database/types.go @@ -1,6 +1,8 @@ package database -import "gorm.io/gorm" +import ( + "gorm.io/gorm" +) type Database struct { ORM *gorm.DB @@ -8,7 +10,7 @@ type Database struct { } type User struct { - ID int - Username string - Password string + Username string `gorm:"primaryKey"` + Password string + CreatedAt int64 `gorm:"autoCreateTime"` } diff --git a/Lab01/app/templates/register.tmpl b/Lab01/app/templates/register.tmpl index b64084b..3f1632f 100644 --- a/Lab01/app/templates/register.tmpl +++ b/Lab01/app/templates/register.tmpl @@ -9,37 +9,37 @@ {{template "navbar" .}}
-
-
- - -
The username needs to be unique
-
-
- - -
The password needs to be different from the username
-
- -
+
+ + +
The username needs to be unique
+
+
+ + +
The password needs to be different from the username
+
+
- + \ No newline at end of file diff --git a/Lab01/app/webpage/webpage.go b/Lab01/app/webpage/webpage.go index e6da9a9..1c33e94 100644 --- a/Lab01/app/webpage/webpage.go +++ b/Lab01/app/webpage/webpage.go @@ -45,7 +45,8 @@ func (wp *Webpage) defineRoutes() { user := database.User{Username: username, Password: password} result := wp.Database.ORM.Create(&user) if result.Error != nil { - c.JSON(200, gin.H{"message": "cannot create user"}) + c.JSON(400, gin.H{"message": result.Error.Error()}) + return } c.JSON(200, gin.H{"message": "user registered"}) })