Просмотр исходного кода

Create sample users in seed function

master
Immanuel Onyeka 1 год назад
Родитель
Сommit
3b00be6374
2 измененных файлов: 139 добавлений и 23 удалений
  1. +7
    -5
      migrations/0_29092022_setup_tables.sql
  2. +132
    -18
      skouter.go

+ 7
- 5
migrations/0_29092022_setup_tables.sql Просмотреть файл

@@ -28,26 +28,28 @@ CREATE TABLE user (
email VARCHAR(40) UNIQUE NOT NULL, email VARCHAR(40) UNIQUE NOT NULL,
first_name VARCHAR(30) NOT NULL, first_name VARCHAR(30) NOT NULL,
last_name VARCHAR(30) NOT NULL, last_name VARCHAR(30) NOT NULL,
phone VARCHAR(20) NOT NULL,
phone VARCHAR(20) NOT NULL DEFAULT '',
address INT NOT NULL, address INT NOT NULL,
password CHAR(64) NOT NULL, password CHAR(64) NOT NULL,
verified BOOLEAN, verified BOOLEAN,
branch_id INT NULL,
branch_id INT DEFAULT NULL,
avatar BLOB(102400) NOT NULL DEFAULT 0, avatar BLOB(102400) NOT NULL DEFAULT 0,
letterhead BLOB(102400) NOT NULL DEFAULT 0, letterhead BLOB(102400) NOT NULL DEFAULT 0,
/* The password should be a SHA256 hash in hex format. It's length doesn't /* The password should be a SHA256 hash in hex format. It's length doesn't
* vary */ * vary */
country ENUM('Canada', 'USA'),
country ENUM('Canada', 'USA') NOT NULL,
title ENUM('Loan Officer', title ENUM('Loan Officer',
'Branch Manager', 'Branch Manager',
'Mortgage Broker', 'Mortgage Broker',
'Other'),
'Other') NOT NULL,
status ENUM('Trial', status ENUM('Trial',
'Free', 'Free',
'Subscribed', 'Subscribed',
'Branch', 'Branch',
'Admin'),
'Admin') DEFAULT 'Trial',
role ENUM('User', 'Manager', 'Admin') NOT NULL, role ENUM('User', 'Manager', 'Admin') NOT NULL,
created DATETIME,
last_login DATETIME,
PRIMARY KEY (`id`), PRIMARY KEY (`id`),
FOREIGN KEY (branch_id) REFERENCES branch(id), FOREIGN KEY (branch_id) REFERENCES branch(id),
FOREIGN KEY (address) REFERENCES address(id) FOREIGN KEY (address) REFERENCES address(id)


+ 132
- 18
skouter.go Просмотреть файл

@@ -58,7 +58,7 @@ type User struct {
LastName string `json:"lastName"` LastName string `json:"lastName"`
Phone string `json:"phone"` Phone string `json:"phone"`
Address Address `json:"address"` Address Address `json:"address"`
BranchId int `json:"branchId"`
Branch Branch `json:"branch"`
Status string `json:"status"` Status string `json:"status"`
Country string `json:"country"` Country string `json:"country"`
Title string `json:"title"` Title string `json:"title"`
@@ -475,10 +475,10 @@ func getFeesTemp(w http.ResponseWriter, db *sql.DB, r *http.Request) {
var fees []FeeTemplate var fees []FeeTemplate
claims, err := getClaims(r) claims, err := getClaims(r)
if err != nil { w.WriteHeader(500); return } if err != nil { w.WriteHeader(500); return }
users, err := queryUsers(db, claims.Id)
user, err := queryUser(db, claims.Id)
if err != nil { w.WriteHeader(422); return } if err != nil { w.WriteHeader(422); return }


fees, err = fetchFeesTemp(db, claims.Id, users[0].BranchId)
fees, err = fetchFeesTemp(db, claims.Id, user.Branch.Id)
json.NewEncoder(w).Encode(fees) json.NewEncoder(w).Encode(fees)
} }


@@ -925,6 +925,66 @@ func queryBranch(db *sql.DB, id int) ( Branch, error ) {
return branch, err return branch, err
} }


func queryUser(db *sql.DB, id int) (User, error ) {
var user User
var query string
var err error

query = `SELECT
u.id,
u.email,
u.first_name,
u.last_name,
coalesce(u.branch_id, 0),
u.country,
u.title,
coalesce(u.status, ''),
u.verified,
u.role,
u.address,
u.phone
FROM user u WHERE u.id = CASE @e := ? WHEN 0 THEN u.id ELSE @e END
`
row := db.QueryRow(query, id)


if err != nil {
return user, err
}

err = row.Scan(
&user.Id,
&user.Email,
&user.FirstName,
&user.LastName,
&user.Branch.Id,
&user.Country,
&user.Title,
&user.Status,
&user.Verified,
&user.Role,
&user.Address.Id,
&user.Phone,
)
if err != nil {
return user, err
}
user.Address, err = queryAddress(db, user.Address.Id)
if err != nil {
return user, err
}
user.Branch, err = queryBranch(db, user.Branch.Id)
if err != nil {
return user, err
}

return user, nil
}

// Can probably be deleted.
func queryUsers(db *sql.DB, id int) ( []User, error ) { func queryUsers(db *sql.DB, id int) ( []User, error ) {
var users []User var users []User
var query string var query string
@@ -936,10 +996,10 @@ func queryUsers(db *sql.DB, id int) ( []User, error ) {
u.email, u.email,
u.first_name, u.first_name,
u.last_name, u.last_name,
u.branch_id,
coalesce(u.branch_id, 0),
u.country, u.country,
u.title, u.title,
u.status,
coalesce(u.status, ''),
u.verified, u.verified,
u.role, u.role,
u.address, u.address,
@@ -963,7 +1023,7 @@ func queryUsers(db *sql.DB, id int) ( []User, error ) {
&user.Email, &user.Email,
&user.FirstName, &user.FirstName,
&user.LastName, &user.LastName,
&user.BranchId,
&user.Branch.Id,
&user.Country, &user.Country,
&user.Title, &user.Title,
&user.Status, &user.Status,
@@ -977,6 +1037,11 @@ func queryUsers(db *sql.DB, id int) ( []User, error ) {
} }
user.Address, err = queryAddress(db, user.Address.Id) user.Address, err = queryAddress(db, user.Address.Id)
if err != nil {
return users, err
}
user.Branch, err = queryBranch(db, user.Branch.Id)
if err != nil { if err != nil {
return users, err return users, err
} }
@@ -1044,32 +1109,45 @@ func insertUser(db *sql.DB, user User) (User, error){
first_name, first_name,
last_name, last_name,
password, password,
created,
role, role,
title,
status,
verified, verified,
address, address,
country,
branch_id,
phone,
created,
last_login last_login
) )
VALUES (?, ?, ?, sha2(?, 256), NOW(), ?, ?, ?, NOW())
VALUES (?, ?, ?, sha2(?, 256), ?, ?, ?, ?, ?, ?,
CASE @b := ? WHEN 0 THEN NULL ELSE @b END,
?, NOW(), NOW())
RETURNING id RETURNING id
` `
row = db.QueryRow(query, row = db.QueryRow(query,
user.Email, user.Email,
user.FirstName, user.FirstName,
user.LastName, user.LastName,
user.Password, user.Password,
user.Role, user.Role,
user.Title,
user.Status,
user.Verified, user.Verified,
user.Address.Id, user.Address.Id,
user.Country,
user.Branch.Id,
user.Phone,
) )


err = row.Scan(&id) err = row.Scan(&id)
if err != nil { return User{}, err } if err != nil { return User{}, err }


users, err := queryUsers(db, id)
user, err = queryUser(db, id)
if err != nil { return User{}, err } if err != nil { return User{}, err }


return users[0], nil
return user, nil
} }


func updateAddress(address Address, db *sql.DB) error { func updateAddress(address Address, db *sql.DB) error {
@@ -1126,9 +1204,9 @@ func updateUser(user User, db *sql.DB) error {
func getUser(w http.ResponseWriter, db *sql.DB, r *http.Request) { func getUser(w http.ResponseWriter, db *sql.DB, r *http.Request) {
claims, err := getClaims(r) claims, err := getClaims(r)
if err != nil { w.WriteHeader(500); return } if err != nil { w.WriteHeader(500); return }
users, err := queryUsers(db, claims.Id)
user, err := queryUser(db, claims.Id)
if err != nil { w.WriteHeader(422); log.Println(err); return } if err != nil { w.WriteHeader(422); log.Println(err); return }
json.NewEncoder(w).Encode(users)
json.NewEncoder(w).Encode(user)
} }


func getUsers(w http.ResponseWriter, db *sql.DB, r *http.Request) { func getUsers(w http.ResponseWriter, db *sql.DB, r *http.Request) {
@@ -1953,12 +2031,12 @@ func checkPdf(w http.ResponseWriter, r *http.Request) {
// claims, err := getClaims(r) // claims, err := getClaims(r)
if err != nil { w.WriteHeader(500); return } if err != nil { w.WriteHeader(500); return }
users, err := queryUsers(db, 1)
user, err := queryUser(db, 1)
info := Report{ info := Report{
Title: "test PDF", Title: "test PDF",
Name: "idk-random-name", Name: "idk-random-name",
User: users[0],
User: user,
Estimate: estimates[0], Estimate: estimates[0],
} }
avatar, err := fetchAvatar(db, info.User.Id) avatar, err := fetchAvatar(db, info.User.Id)
@@ -2011,12 +2089,12 @@ func getPdf(w http.ResponseWriter, db *sql.DB, r *http.Request) {
log.Println(err) log.Println(err)
return return
} }
users, err := queryUsers(db, claims.Id)
user, err := queryUser(db, claims.Id)
info := Report{ info := Report{
Title: "test PDF", Title: "test PDF",
Name: "idk-random-name", Name: "idk-random-name",
User: users[0],
User: user,
Estimate: estimate, Estimate: estimate,
} }
avatar, err := fetchAvatar(db, info.User.Id) avatar, err := fetchAvatar(db, info.User.Id)
@@ -2245,7 +2323,7 @@ func dbSeed() {
addresses[i].Id = id addresses[i].Id = id
} }
branches := make([]Branch, 3)
branches := make([]Branch, 4)
for i := range branches { for i := range branches {
branches[i].Name = gofakeit.Street() branches[i].Name = gofakeit.Street()
branches[i].Type = "NMLS" branches[i].Type = "NMLS"
@@ -2255,7 +2333,43 @@ func dbSeed() {
branches[i].Address.Id = gofakeit.Number(1, 5) branches[i].Address.Id = gofakeit.Number(1, 5)
id, err := insertBranch(db, branches[i]) id, err := insertBranch(db, branches[i])
if err != nil {log.Println(err); break} if err != nil {log.Println(err); break}
branches[i].Address.Id = id
branches[i].Id = id
}
users := make([]User, 10)
for i := range users {
p := gofakeit.Person()
users[i].FirstName = p.FirstName
users[i].LastName = p.LastName
users[i].Email = p.Contact.Email
users[i].Phone = p.Contact.Phone
users[i].Branch = branches[gofakeit.Number(0, 3)]
users[i].Address = addresses[gofakeit.Number(1, 9)]
// users[i].Letterhead = gofakeit.ImagePng(400, 200)
// users[i].Avatar = gofakeit.ImagePng(200, 200)
users[i].Country = []string{"Canada", "USA"}[gofakeit.Number(0, 1)]
users[i].Password = "test123"
users[i].Verified = true
users[i].Title = "Loan Officer"
users[i].Status = "Subscribed"
users[i].Role = "User"
}
users[0].Email = "test@example.com"
users[0].Email = "test@example.com"
users[1].Email = "test2@example.com"
users[1].Status = "Branch"
users[1].Role = "Manager"
users[2].Email = "test3@example.com"
users[2].Status = "Free"
users[2].Role = "Admin"
for i := range users {
u, err := insertUser(db, users[i])
if err != nil {log.Println(err); break}
users[i].Id = u.Id
} }
} }




Загрузка…
Отмена
Сохранить