Преглед на файлове

Create sample users in seed function

master
Immanuel Onyeka преди 1 година
родител
ревизия
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,
first_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,
password CHAR(64) NOT NULL,
verified BOOLEAN,
branch_id INT NULL,
branch_id INT DEFAULT NULL,
avatar 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
* vary */
country ENUM('Canada', 'USA'),
country ENUM('Canada', 'USA') NOT NULL,
title ENUM('Loan Officer',
'Branch Manager',
'Mortgage Broker',
'Other'),
'Other') NOT NULL,
status ENUM('Trial',
'Free',
'Subscribed',
'Branch',
'Admin'),
'Admin') DEFAULT 'Trial',
role ENUM('User', 'Manager', 'Admin') NOT NULL,
created DATETIME,
last_login DATETIME,
PRIMARY KEY (`id`),
FOREIGN KEY (branch_id) REFERENCES branch(id),
FOREIGN KEY (address) REFERENCES address(id)


+ 132
- 18
skouter.go Целия файл

@@ -58,7 +58,7 @@ type User struct {
LastName string `json:"lastName"`
Phone string `json:"phone"`
Address Address `json:"address"`
BranchId int `json:"branchId"`
Branch Branch `json:"branch"`
Status string `json:"status"`
Country string `json:"country"`
Title string `json:"title"`
@@ -475,10 +475,10 @@ func getFeesTemp(w http.ResponseWriter, db *sql.DB, r *http.Request) {
var fees []FeeTemplate
claims, err := getClaims(r)
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 }

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

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

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

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

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

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) {
claims, err := getClaims(r)
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 }
json.NewEncoder(w).Encode(users)
json.NewEncoder(w).Encode(user)
}

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)
if err != nil { w.WriteHeader(500); return }
users, err := queryUsers(db, 1)
user, err := queryUser(db, 1)
info := Report{
Title: "test PDF",
Name: "idk-random-name",
User: users[0],
User: user,
Estimate: estimates[0],
}
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)
return
}
users, err := queryUsers(db, claims.Id)
user, err := queryUser(db, claims.Id)
info := Report{
Title: "test PDF",
Name: "idk-random-name",
User: users[0],
User: user,
Estimate: estimate,
}
avatar, err := fetchAvatar(db, info.User.Id)
@@ -2245,7 +2323,7 @@ func dbSeed() {
addresses[i].Id = id
}
branches := make([]Branch, 3)
branches := make([]Branch, 4)
for i := range branches {
branches[i].Name = gofakeit.Street()
branches[i].Type = "NMLS"
@@ -2255,7 +2333,43 @@ func dbSeed() {
branches[i].Address.Id = gofakeit.Number(1, 5)
id, err := insertBranch(db, branches[i])
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
}
}



Loading…
Отказ
Запис