controller added

Registrierung hinzugefügt
This commit is contained in:
2023-11-12 20:28:09 +01:00
parent d2cd4c3a41
commit 7c3d3e03bc
13 changed files with 429 additions and 30 deletions

14
app/models/Category.go Normal file
View File

@@ -0,0 +1,14 @@
package models
import "time"
type Category struct {
ID string
ParentID string
Section Section
SectionID string
Products []Product `gorm:"many2many:product_categories;"`
Name string
CreatedAt time.Time
UpdatedAt time.Time
}

11
app/models/Section.go Normal file
View File

@@ -0,0 +1,11 @@
package models
import "time"
type Section struct {
ID string
Name string
CreatedAt time.Time
UpdatedAt time.Time
Categories []Category
}

14
app/models/models.go Normal file
View File

@@ -0,0 +1,14 @@
package models
type Model struct {
Model interface{}
}
func RegisterModels() []Model {
return []Model{
{Model: User{}},
{Model: Product{}},
{Model: Category{}},
{Model: Section{}},
}
}

16
app/models/product.go Normal file
View File

@@ -0,0 +1,16 @@
package models
import (
"gorm.io/gorm"
"time"
)
type Product struct {
ID string
ParentID string
Name string
Categories []Category `gorm:"many2many:product_categories;"`
CreatedAt time.Time
UpdatedAt time.Time
DeletedAt gorm.DeletedAt
}

View File

@@ -2,6 +2,7 @@ package models
import (
"gorm.io/gorm"
"strings"
"time"
)
@@ -19,9 +20,7 @@ type User struct {
}
func (u *User) FindByID(db *gorm.DB, userID string) (*User, error) {
var user User
err := db.Debug().Model(User{}).Where("id = ?", userID).
First(&user).Error
if err != nil {
@@ -29,3 +28,29 @@ func (u *User) FindByID(db *gorm.DB, userID string) (*User, error) {
}
return &user, nil
}
func (u *User) FindByEmail(db *gorm.DB, email string) (*User, error) {
var user User
err := db.Debug().Model(User{}).Where("LOWER(email) = ?", strings.ToLower(email)).
First(&user).Error
if err != nil {
return nil, err
}
return &user, nil
}
func (u *User) CreateUser(db *gorm.DB, param *User) (*User, error) {
user := &User{
ID: param.ID,
FirstName: param.FirstName,
LastName: param.LastName,
Email: param.Email,
Password: param.Password,
}
err := db.Debug().Create(&user).Error
if err != nil {
return nil, err
}
return user, nil
}