From f35e5db1a99adbc65533ca036bf38a7b48cc8e04 Mon Sep 17 00:00:00 2001 From: qowevisa Date: Fri, 2 Aug 2024 23:36:55 +0300 Subject: [PATCH] Create user in database --- db/user.go | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 db/user.go diff --git a/db/user.go b/db/user.go new file mode 100644 index 0000000..7bea1f5 --- /dev/null +++ b/db/user.go @@ -0,0 +1,29 @@ +package db + +import ( + "errors" + + "gorm.io/gorm" +) + +type User struct { + gorm.Model + Username string + Password string +} + +var ( + ERROR_USER_USERNAME_NOT_UNIQUE = errors.New("Username already persists in database") +) + +func (u *User) BeforeCreate(ctx *gorm.DB) error { + var duplicate User + err := ctx.Find(&duplicate, User{Username: u.Username}).Error + if err != nil { + return err + } + if duplicate.ID != 0 { + return ERROR_USER_USERNAME_NOT_UNIQUE + } + return nil +}