-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #64 from hngprojects/dev
Send to staging
- Loading branch information
Showing
37 changed files
with
1,559 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package migrations | ||
|
||
import "github.com/hngprojects/hng_boilerplate_golang_web/internal/models" | ||
|
||
func AuthMigrationModels() []interface{} { | ||
return []interface{}{ | ||
models.Organisation{}, | ||
models.Profile{}, | ||
models.Product{}, | ||
models.User{}, | ||
} // an array of db models, example: User{} | ||
} | ||
|
||
func AlterColumnModels() []AlterColumn { | ||
return []AlterColumn{} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package models | ||
|
||
import ( | ||
"time" | ||
|
||
"gorm.io/gorm" | ||
|
||
"github.com/hngprojects/hng_boilerplate_golang_web/pkg/repository/storage/postgresql" | ||
) | ||
|
||
type Organisation struct { | ||
ID string `gorm:"type:uuid;primaryKey;unique;not null" json:"id"` | ||
Name string `gorm:"type:varchar(255);not null" json:"name"` | ||
Description string `gorm:"type:text" json:"description"` | ||
Email string `gorm:"type:varchar(255);unique" json:"email"` | ||
State string `gorm:"type:varchar(255)" json:"state"` | ||
Industry string `gorm:"type:varchar(255)" json:"industry"` | ||
Type string `gorm:"type:varchar(255)" json:"type"` | ||
Address string `gorm:"type:varchar(255)" json:"address"` | ||
Country string `gorm:"type:varchar(255)" json:"country"` | ||
OwnerID string `gorm:"type:uuid;" json:"owner_id"` | ||
Users []User `gorm:"many2many:user_organisations;foreignKey:ID;joinForeignKey:org_id;References:ID;joinReferences:user_id"` | ||
CreatedAt time.Time `gorm:"column:created_at; not null; autoCreateTime" json:"created_at"` | ||
UpdatedAt time.Time `gorm:"column:updated_at; null; autoUpdateTime" json:"updated_at"` | ||
} | ||
|
||
type CreateOrgRequestModel struct { | ||
Name string `json:"name" validate:"required,min=2,max=255"` | ||
Description string `json:"description" ` | ||
Email string `json:"email" validate:"required"` | ||
State string `json:"state" validate:"required"` | ||
Industry string `json:"industry" validate:"required"` | ||
Type string `json:"type" validate:"required"` | ||
Address string `json:"address" validate:"required"` | ||
Country string `json:"country" validate:"required"` | ||
} | ||
|
||
func (c *Organisation) CreateOrganisation(db *gorm.DB) error { | ||
|
||
err := postgresql.CreateOneRecord(db, &c) | ||
|
||
if err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package models | ||
|
||
import "time" | ||
|
||
type Product struct { | ||
ID string `gorm:"type:uuid;primaryKey" json:"product_id"` | ||
Name string `gorm:"column:name; type:varchar(255); not null" json:"name"` | ||
Description string `gorm:"column:description;type:text;" json:"description"` | ||
OwnerID string `gorm:"type:uuid;" json:"owner_id"` | ||
CreatedAt time.Time `gorm:"column:created_at; not null; autoCreateTime" json:"created_at"` | ||
UpdatedAt time.Time `gorm:"column:updated_at; null; autoUpdateTime" json:"updated_at"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package models | ||
|
||
import "time" | ||
|
||
type Profile struct { | ||
ID string `gorm:"type:uuid;primary_key" json:"profile_id"` | ||
FirstName string `gorm:"column:first_name; type:text; not null" json:"first_name"` | ||
LastName string `gorm:"column:last_name; type:text;not null" json:"last_name"` | ||
Phone string `gorm:"type:varchar(255)" json:"phone"` | ||
AvatarURL string `gorm:"type:varchar(255)" json:"avatar_url"` | ||
Userid string `gorm:"type:uuid;" json:"user_id"` | ||
CreatedAt time.Time `gorm:"column:created_at; not null; autoCreateTime" json:"created_at"` | ||
UpdatedAt time.Time `gorm:"column:updated_at; null; autoUpdateTime" json:"updated_at"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package seed | ||
|
||
import ( | ||
"fmt" | ||
|
||
"gorm.io/gorm" | ||
|
||
"github.com/hngprojects/hng_boilerplate_golang_web/internal/models" | ||
"github.com/hngprojects/hng_boilerplate_golang_web/pkg/repository/storage/postgresql" | ||
"github.com/hngprojects/hng_boilerplate_golang_web/utility" | ||
) | ||
|
||
func SeedDatabase(db *gorm.DB) { | ||
// instantiate uuid | ||
|
||
Userid1 := utility.GenerateUUID() | ||
user1 := models.User{ | ||
ID: Userid1, | ||
Name: "John Doe", | ||
Email: "[email protected]", | ||
Password: utility.RandomString(20), | ||
Profile: models.Profile{ | ||
ID: utility.GenerateUUID(), | ||
FirstName: "John", | ||
LastName: "Doe", | ||
Phone: "1234567890", | ||
AvatarURL: "http://example.com/avatar.jpg", | ||
}, | ||
Products: []models.Product{ | ||
{ID: utility.GenerateUUID(), Name: "Product1", Description: "Description1", OwnerID: Userid1}, | ||
{ID: utility.GenerateUUID(), Name: "Product2", Description: "Description2", OwnerID: Userid1}, | ||
}, | ||
} | ||
|
||
Userid2 := utility.GenerateUUID() | ||
user2 := models.User{ | ||
ID: Userid2, | ||
Name: "Jane Doe", | ||
Password: utility.RandomString(20), | ||
Email: "[email protected]", | ||
Profile: models.Profile{ | ||
ID: utility.GenerateUUID(), | ||
FirstName: "Jane", | ||
LastName: "Doe", | ||
Phone: "0987654321", | ||
AvatarURL: "http://example.com/avatar2.jpg", | ||
}, | ||
Products: []models.Product{ | ||
{ID: utility.GenerateUUID(), Name: "Product3", Description: "Description3", OwnerID: Userid2}, | ||
{ID: utility.GenerateUUID(), Name: "Product4", Description: "Description4", OwnerID: Userid2}, | ||
}, | ||
} | ||
|
||
organisations := []models.Organisation{ | ||
{ID: utility.GenerateUUID(), Name: "Org1", Email: fmt.Sprintf(utility.RandomString(4)+"@email.com"),Description: "Description1", OwnerID: Userid1}, | ||
{ID: utility.GenerateUUID(), Name: "Org2", Email: fmt.Sprintf(utility.RandomString(4)+"@email.com"),Description: "Description2", OwnerID: Userid1}, | ||
{ID: utility.GenerateUUID(), Name: "Org3", Email: fmt.Sprintf(utility.RandomString(4)+"@email.com"),Description: "Description3", OwnerID: Userid2}, | ||
} | ||
|
||
var existingUser models.User | ||
if err := db.Preload("Profile").Preload("Products").Where("email = ?", user1.Email).First(&existingUser).Error; err != nil { | ||
if err == gorm.ErrRecordNotFound { | ||
postgresql.CreateOneRecord(db, &user1) | ||
postgresql.CreateOneRecord(db, &user2) | ||
for _, org := range organisations { | ||
postgresql.CreateOneRecord(db, &org) | ||
} | ||
fmt.Println("Users and organisations seeded.") | ||
|
||
// Add users to organisations | ||
existingUser.AddUserToOrganisation(db, &user1, []interface{}{&organisations[0], &organisations[1]}) | ||
existingUser.AddUserToOrganisation(db, &user2, []interface{}{&organisations[0], &organisations[1], &organisations[2]}) | ||
fmt.Println("Users added to organisations.") | ||
|
||
} else { | ||
fmt.Println("An error occurred: ", err) | ||
} | ||
} else { | ||
fmt.Println("Users already exist, skipping seeding.") | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package models | ||
|
||
import ( | ||
"time" | ||
|
||
"gorm.io/gorm" | ||
|
||
"github.com/hngprojects/hng_boilerplate_golang_web/pkg/repository/storage/postgresql" | ||
) | ||
|
||
type User struct { | ||
ID string `gorm:"type:uuid;primaryKey;unique;not null" json:"id"` | ||
Name string `gorm:"column:name; type:varchar(255)" json:"name"` | ||
Email string `gorm:"column:email; type:varchar(255)" json:"email"` | ||
Password string `gorm:"column:password; type:text; not null" json:"-"` | ||
Profile Profile `gorm:"foreignKey:Userid;constraint:OnUpdate:CASCADE,OnDelete:SET NULL;" json:"profile"` | ||
Organisations []Organisation `gorm:"many2many:user_organisations;;constraint:OnUpdate:CASCADE,OnDelete:SET NULL;" json:"organisations" ` // many to many relationship | ||
Products []Product `gorm:"foreignKey:OwnerID" json:"products"` | ||
CreatedAt time.Time `gorm:"column:created_at; not null; autoCreateTime" json:"created_at"` | ||
UpdatedAt time.Time `gorm:"column:updated_at; null; autoUpdateTime" json:"updated_at"` | ||
} | ||
|
||
type CreateUserRequestModel struct { | ||
Email string `json:"email" validate:"required"` | ||
Password string `json:"password" validate:"required"` | ||
FirstName string `json:"first_name" validate:"required"` | ||
LastName string `json:"last_name" validate:"required"` | ||
UserName string `json:"username" validate:"required"` | ||
PhoneNumber string `json:"phone_number"` | ||
} | ||
|
||
type LoginRequestModel struct { | ||
Email string `json:"email" validate:"required"` | ||
Password string `json:"password" validate:"required"` | ||
} | ||
|
||
func (u *User) AddUserToOrganisation(db *gorm.DB, user interface{}, orgs []interface{}) error { | ||
|
||
// Add user to organisation | ||
err := db.Model(user).Association("Organisations").Append(orgs...) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (u *User) GetUserByID(db *gorm.DB, userID string) (User, error) { | ||
var user User | ||
|
||
if err := db.Preload("Profile").Preload("Products").Preload("Organisations").Where("id = ?", userID).First(&user).Error; err != nil { | ||
return user, err | ||
} | ||
|
||
return user, nil | ||
} | ||
|
||
func (u *User) CreateUser(db *gorm.DB) error { | ||
|
||
err := postgresql.CreateOneRecord(db, &u) | ||
|
||
if err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.