mirror of
https://github.com/hazemKrimi/crimson-vault.git
synced 2026-05-01 18:20:27 +00:00
wip: versioned migrations using atlas
This commit is contained in:
@@ -29,8 +29,6 @@ func (api *API) Initialize() {
|
||||
ech := echo.New()
|
||||
|
||||
db.Connect(api.ConfigDirectory)
|
||||
db.MigrateClients()
|
||||
db.MigrateUsers()
|
||||
|
||||
api.instance = ech
|
||||
api.db = db
|
||||
|
||||
@@ -33,7 +33,11 @@ func (api *API) CreateClientHandler(context echo.Context) error {
|
||||
return err
|
||||
}
|
||||
|
||||
client := api.db.CreateClient(id, body)
|
||||
client, err := api.db.CreateClient(id, body)
|
||||
|
||||
if err != nil {
|
||||
return types.Error{Code: http.StatusInternalServerError, Cause: err, Messages: []string{"Unexpected error creating User!"}}
|
||||
}
|
||||
|
||||
return context.JSON(http.StatusOK, client)
|
||||
}
|
||||
|
||||
@@ -6,11 +6,7 @@ import (
|
||||
"github.com/hazemKrimi/crimson-vault/internal/types"
|
||||
)
|
||||
|
||||
func (db *DB) MigrateClients() {
|
||||
db.instance.AutoMigrate(&types.Client{})
|
||||
}
|
||||
|
||||
func (db *DB) CreateClient(userId uuid.UUID, body types.CreateClientRequestBody) types.Client {
|
||||
func (db *DB) CreateClient(userId uuid.UUID, body types.CreateClientRequestBody) (types.Client, error) {
|
||||
client := types.Client{
|
||||
ID: uuid.New().String(),
|
||||
UserID: userId.String(),
|
||||
@@ -23,8 +19,13 @@ func (db *DB) CreateClient(userId uuid.UUID, body types.CreateClientRequestBody)
|
||||
Email: body.Email,
|
||||
}
|
||||
|
||||
db.instance.Create(&client)
|
||||
return client
|
||||
result := db.instance.Create(&client)
|
||||
|
||||
if result.Error != nil {
|
||||
return types.Client{}, result.Error
|
||||
}
|
||||
|
||||
return client, nil
|
||||
}
|
||||
|
||||
func (db *DB) GetClients(userId uuid.UUID) ([]types.Client, error) {
|
||||
|
||||
@@ -9,10 +9,6 @@ import (
|
||||
"github.com/hazemKrimi/crimson-vault/internal/types"
|
||||
)
|
||||
|
||||
func (db *DB) MigrateUsers() {
|
||||
db.instance.AutoMigrate(&types.User{})
|
||||
}
|
||||
|
||||
func (db *DB) CreateUser(body types.CreateUserRequestBody) (types.User, error) {
|
||||
user := types.User{
|
||||
ID: uuid.New().String(),
|
||||
@@ -38,7 +34,7 @@ func (db *DB) CreateUser(body types.CreateUserRequestBody) (types.User, error) {
|
||||
func (db *DB) GetUsers() ([]types.User, error) {
|
||||
var users []types.User
|
||||
|
||||
result := db.instance.Find(&users)
|
||||
result := db.instance.Model(&types.User{}).Preload("Clients").Find(&users)
|
||||
|
||||
if result.Error != nil {
|
||||
return nil, result.Error
|
||||
@@ -48,7 +44,7 @@ func (db *DB) GetUsers() ([]types.User, error) {
|
||||
}
|
||||
|
||||
func (db *DB) GetUserById(id uuid.UUID, user *types.User) error {
|
||||
result := db.instance.Where("id = ?", id).First(user)
|
||||
result := db.instance.Model(&types.User{}).Preload("Clients").Where("id = ?", id).First(user)
|
||||
|
||||
if result.Error != nil {
|
||||
return result.Error
|
||||
|
||||
@@ -7,8 +7,8 @@ import (
|
||||
)
|
||||
|
||||
type Client struct {
|
||||
ID string `json:"id" gorm:"primaryKey"`
|
||||
UserID string `json:"userId"`
|
||||
ID string `json:"id" gorm:"type:varchar(255);primaryKey"`
|
||||
UserID string `json:"userId" gorm:"type:varchar(255)"`
|
||||
CreatedAt time.Time `json:"createAt"`
|
||||
UpdatedAt time.Time `json:"updatedAt"`
|
||||
DeletedAt gorm.DeletedAt `json:"deletedAt" gorm:"index"`
|
||||
|
||||
@@ -7,7 +7,7 @@ import (
|
||||
)
|
||||
|
||||
type User struct {
|
||||
ID string `json:"id" gorm:"primaryKey"`
|
||||
ID string `json:"id" gorm:"type:varchar(255);primaryKey"`
|
||||
SessionID string `json:"-"`
|
||||
CreatedAt time.Time `json:"createAt"`
|
||||
UpdatedAt time.Time `json:"updatedAt"`
|
||||
@@ -22,7 +22,7 @@ type User struct {
|
||||
Email string `json:"email"`
|
||||
Username string `json:"username" gorm:"unique"`
|
||||
Password string `json:"-"`
|
||||
Clients []Client `json:"clients" gorm:"constraint:OnUpdate:CASCADE,onDelete:CASCADE"`
|
||||
Clients []Client `json:"clients" gorm:"constraint:onDelete:CASCADE"`
|
||||
}
|
||||
|
||||
type CreateUserRequestBody struct {
|
||||
|
||||
Reference in New Issue
Block a user