-
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 #43 from Micah-Shallom/main
[FEAT] Database Setup - Models, and Seeding
- Loading branch information
Showing
19 changed files
with
367 additions
and
12 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,14 @@ | ||
package models | ||
|
||
import ( | ||
"time" | ||
) | ||
|
||
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"` | ||
Users []User `gorm:"many2many:user_organisations;foreignKey:ID;joinForeignKey:org_id;References:ID;joinReferences:user_id" json:"users"` | ||
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,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,80 @@ | ||
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]", | ||
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", | ||
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", Description: "Description1"}, | ||
{ID: utility.GenerateUUID(), Name: "Org2", Description: "Description2"}, | ||
{ID: utility.GenerateUUID(), Name: "Org3", Description: "Description3"}, | ||
} | ||
|
||
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,40 @@ | ||
package models | ||
|
||
import ( | ||
"time" | ||
|
||
"gorm.io/gorm" | ||
) | ||
|
||
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"` | ||
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"` | ||
} | ||
|
||
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 | ||
} |
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,55 @@ | ||
package invite | ||
|
||
import ( | ||
"net/http" | ||
|
||
"github.com/gin-gonic/gin" | ||
"github.com/go-playground/validator/v10" | ||
|
||
"github.com/hngprojects/hng_boilerplate_golang_web/external/request" | ||
"github.com/hngprojects/hng_boilerplate_golang_web/pkg/repository/storage" | ||
"github.com/hngprojects/hng_boilerplate_golang_web/utility" | ||
) | ||
|
||
type Controller struct { | ||
Db *storage.Database | ||
Validator *validator.Validate | ||
Logger *utility.Logger | ||
ExtReq request.ExternalRequest | ||
} | ||
|
||
type InvitationRequest struct { | ||
Emails []string `json:"emails" validate:"required"` | ||
OrgID string `json:"org_id" binding:"uuid"` | ||
} | ||
|
||
|
||
|
||
func (base *Controller) PostInvite(c *gin.Context) { | ||
|
||
var inviteReq InvitationRequest | ||
|
||
if err := c.ShouldBindJSON(&inviteReq); err != nil { | ||
rd := utility.BuildErrorResponse(http.StatusBadRequest, "error", "Failed to parse request body", err, nil) | ||
c.JSON(http.StatusBadRequest, rd) | ||
return | ||
} | ||
|
||
err := base.Validator.Struct(&inviteReq) | ||
if err != nil { | ||
rd := utility.BuildErrorResponse(http.StatusBadRequest,"error", "Validation failed", utility.ValidationResponse(err, base.Validator), nil) | ||
c.JSON(http.StatusBadRequest, rd) | ||
return | ||
} | ||
|
||
// if err != nil { | ||
// rd := utility.BuildErrorResponse(http.StatusNotFound, "error", err.Error(), err, nil) | ||
// c.JSON(http.StatusInternalServerError, rd) | ||
// return | ||
// } | ||
|
||
base.Logger.Info("invite posted successfully") | ||
rd := utility.BuildSuccessResponse(http.StatusOK, "", "invite url") | ||
|
||
c.JSON(http.StatusOK, rd) | ||
} |
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,38 @@ | ||
package seed | ||
|
||
import ( | ||
"net/http" | ||
|
||
"github.com/gin-gonic/gin" | ||
"github.com/go-playground/validator/v10" | ||
|
||
"github.com/hngprojects/hng_boilerplate_golang_web/external/request" | ||
"github.com/hngprojects/hng_boilerplate_golang_web/pkg/repository/storage" | ||
"github.com/hngprojects/hng_boilerplate_golang_web/services/seed" | ||
"github.com/hngprojects/hng_boilerplate_golang_web/utility" | ||
) | ||
|
||
type Controller struct { | ||
Db *storage.Database | ||
Validator *validator.Validate | ||
Logger *utility.Logger | ||
ExtReq request.ExternalRequest | ||
} | ||
|
||
func (base *Controller) GetUser(c *gin.Context) { | ||
//get the user_id from the URL | ||
userIDStr := c.Param("user_id") | ||
|
||
user, err := seed.GetUser(userIDStr, base.Db.Postgresql) | ||
|
||
if err != nil { | ||
rd := utility.BuildErrorResponse(http.StatusNotFound, "error", err.Error(), err, nil) | ||
c.JSON(http.StatusInternalServerError, rd) | ||
return | ||
} | ||
|
||
base.Logger.Info("user fetched successfully") | ||
rd := utility.BuildSuccessResponse(http.StatusOK, "", user) | ||
|
||
c.JSON(http.StatusOK, rd) | ||
} |
Oops, something went wrong.