Skip to content

Commit 6e43031

Browse files
committed
- minor bug fix and better comments on database file
1 parent 4f11452 commit 6e43031

File tree

4 files changed

+29
-28
lines changed

4 files changed

+29
-28
lines changed

controllers/controller.go

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package controllers
22

33
import (
44
"errors"
5-
"fmt"
65
"github.com/Darklabel91/API_Names/database"
76
"github.com/Darklabel91/API_Names/models"
87
Metaphone "github.com/Darklabel91/metaphone-br"
@@ -38,7 +37,7 @@ func Signup(c *gin.Context) {
3837

3938
//create the user
4039
user := models.User{Email: body.Email, Password: string(hash), IP: c.ClientIP()}
41-
result := database.Db.Create(&user)
40+
result := database.DB.Create(&user)
4241

4342
if result.Error != nil {
4443
c.JSON(http.StatusBadRequest, gin.H{"Message": "Email already registered"})
@@ -61,7 +60,7 @@ func Login(c *gin.Context) {
6160

6261
// Look up requested user
6362
var user models.User
64-
database.Db.First(&user, "email = ?", body.Email)
63+
database.DB.First(&user, "email = ?", body.Email)
6564

6665
if user.ID == 0 {
6766
c.JSON(http.StatusBadRequest, gin.H{"Message": "Invalid email or password"})
@@ -124,7 +123,7 @@ func CreateName(c *gin.Context) {
124123
return
125124
}
126125

127-
database.Db.Create(&name)
126+
database.DB.Create(&name)
128127
c.JSON(http.StatusOK, name)
129128
}
130129

@@ -133,7 +132,7 @@ func GetID(c *gin.Context) {
133132
var name models.NameType
134133

135134
id := c.Params.ByName("id")
136-
database.Db.First(&name, id)
135+
database.DB.First(&name, id)
137136

138137
if name.ID == 0 {
139138
c.JSON(http.StatusNotFound, gin.H{"Not found": "name id not found"})
@@ -148,14 +147,14 @@ func DeleteName(c *gin.Context) {
148147
var name models.NameType
149148

150149
id := c.Params.ByName("id")
151-
database.Db.First(&name, id)
150+
database.DB.First(&name, id)
152151

153152
if name.ID == 0 {
154153
c.JSON(http.StatusNotFound, gin.H{"Not found": "name id not found"})
155154
return
156155
}
157156

158-
database.Db.Delete(&name, id)
157+
database.DB.Delete(&name, id)
159158
c.JSON(http.StatusOK, gin.H{"Delete": "name id " + id + " was deleted"})
160159
}
161160

@@ -164,7 +163,7 @@ func UpdateName(c *gin.Context) {
164163
var name models.NameType
165164

166165
id := c.Param("id")
167-
database.Db.First(&name, id)
166+
database.DB.First(&name, id)
168167

169168
if name.ID == 0 {
170169
c.JSON(http.StatusNotFound, gin.H{"Not found": "name id not found"})
@@ -176,7 +175,7 @@ func UpdateName(c *gin.Context) {
176175
return
177176
}
178177

179-
database.Db.Model(&name).UpdateColumns(name)
178+
database.DB.Model(&name).UpdateColumns(name)
180179
c.JSON(http.StatusOK, name)
181180
}
182181

@@ -185,7 +184,7 @@ func GetName(c *gin.Context) {
185184
var name models.NameType
186185

187186
n := c.Params.ByName("name")
188-
database.Db.Raw("select * from name_types where name = ?", strings.ToUpper(n)).Find(&name)
187+
database.DB.Raw("select * from name_types where name = ?", strings.ToUpper(n)).Find(&name)
189188

190189
if name.ID == 0 {
191190
c.JSON(http.StatusNotFound, gin.H{"Not found": "name not found"})
@@ -199,7 +198,7 @@ func GetName(c *gin.Context) {
199198
//GetTrustedIPs return all IPS from user's on the database
200199
func GetTrustedIPs() []string {
201200
var users []models.User
202-
if err := database.Db.Find(&users).Error; err != nil {
201+
if err := database.DB.Find(&users).Error; err != nil {
203202
return nil
204203
}
205204

@@ -208,8 +207,6 @@ func GetTrustedIPs() []string {
208207
ips = append(ips, user.IP)
209208
}
210209

211-
fmt.Println(ips)
212-
213210
return ips
214211
}
215212

@@ -233,7 +230,7 @@ func SearchSimilarNames(c *gin.Context) {
233230
nameMetaphone := Metaphone.Pack(name)
234231

235232
//search perfect match
236-
database.Db.Raw("select * from name_types where name = ?", strings.ToUpper(name)).Find(&metaphoneNames)
233+
database.DB.Raw("select * from name_types where name = ?", strings.ToUpper(name)).Find(&metaphoneNames)
237234
if len(metaphoneNames) == 1 {
238235
r := models.MetaphoneR{
239236
ID: metaphoneNames[0].ID,
@@ -250,7 +247,7 @@ func SearchSimilarNames(c *gin.Context) {
250247
}
251248

252249
//find all metaphoneNames matching metaphone
253-
database.Db.Raw("select * from name_types where metaphone = ?", nameMetaphone).Find(&metaphoneNames)
250+
database.DB.Raw("select * from name_types where metaphone = ?", nameMetaphone).Find(&metaphoneNames)
254251
similarNames := findNames(metaphoneNames, name, levenshtein)
255252

256253
//for recall purposes we can't only search for metaphone exact match's if no similar word is found
@@ -341,7 +338,7 @@ func findCanonical(name string, matchingMetaphoneNames []models.NameType, nameVa
341338
for _, similarName := range nameVariations {
342339
sn := strings.ToUpper(similarName)
343340
if sn == n {
344-
database.Db.Raw("select * from name_types where name = ?", sn).Find(&canonicalEntity)
341+
database.DB.Raw("select * from name_types where name = ?", sn).Find(&canonicalEntity)
345342
if canonicalEntity.ID != 0 {
346343
return canonicalEntity, nil
347344
}
@@ -350,7 +347,7 @@ func findCanonical(name string, matchingMetaphoneNames []models.NameType, nameVa
350347

351348
//in case of failure on other attempts, we search every nameVariations directly on database
352349
for _, similarName := range nameVariations {
353-
database.Db.Raw("select * from name_types where name = ?", strings.ToUpper(similarName)).Find(&canonicalEntity)
350+
database.DB.Raw("select * from name_types where name = ?", strings.ToUpper(similarName)).Find(&canonicalEntity)
354351
if canonicalEntity.ID != 0 {
355352
return canonicalEntity, nil
356353
}

database/db.go

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,12 @@ import (
1717
"time"
1818
)
1919

20-
var Db *gorm.DB
20+
var DB *gorm.DB
2121

22-
func InitDb() *gorm.DB {
23-
Db = connectDB()
24-
if Db == nil {
22+
//InitDB set up all database environment
23+
func InitDB() *gorm.DB {
24+
DB = connectDB()
25+
if DB == nil {
2526
return nil
2627
}
2728

@@ -35,9 +36,10 @@ func InitDb() *gorm.DB {
3536
return nil
3637
}
3738

38-
return Db
39+
return DB
3940
}
4041

42+
//connectDB open connection and migrate tables ORM
4143
func connectDB() *gorm.DB {
4244
//load .env file
4345
err := godotenv.Load()
@@ -87,6 +89,7 @@ func connectDB() *gorm.DB {
8789
return db
8890
}
8991

92+
//createDatabase runs create database script
9093
func createDatabase(host, username, password, dbName string) error {
9194
// Set up the MySQL DSN string
9295
dsn := fmt.Sprintf("%s:%s@tcp(%s)/?charset=utf8mb4&parseTime=True&loc=Local", username, password, host)
@@ -114,9 +117,10 @@ func createDatabase(host, username, password, dbName string) error {
114117
return nil
115118
}
116119

120+
//createRoot creates a user directly from the server
117121
func createRoot() error {
118122
var user models.User
119-
Db.Raw("select * from users where id = 1").Find(&user)
123+
DB.Raw("select * from users where id = 1").Find(&user)
120124

121125
if user.ID == 0 {
122126
hash, err := bcrypt.GenerateFromPassword([]byte(os.Getenv("SECRET")), 10)
@@ -130,7 +134,7 @@ func createRoot() error {
130134
IP: getOutboundIP(),
131135
}
132136

133-
Db.Create(&userRoot)
137+
DB.Create(&userRoot)
134138

135139
fmt.Println("- Created first user")
136140
return nil
@@ -155,7 +159,7 @@ func getOutboundIP() string {
155159
//uploadCSVNameTypes
156160
func uploadCSVNameTypes() error {
157161
var name models.NameType
158-
Db.Raw("SELECT * FROM name_types WHERE id = 1").Find(&name)
162+
DB.Raw("SELECT * FROM name_types WHERE id = 1").Find(&name)
159163

160164
if name.ID == 0 {
161165
start := time.Now()
@@ -190,7 +194,7 @@ func uploadCSVNameTypes() error {
190194
Metaphone: metaphone.Pack(row[0]),
191195
NameVariations: row[3],
192196
}
193-
if err = Db.Create(&nameType).Error; err != nil {
197+
if err = DB.Create(&nameType).Error; err != nil {
194198
return errors.New("error creating NameType:" + err.Error())
195199
}
196200
}

main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77
)
88

99
func main() {
10-
r := database.InitDb()
10+
r := database.InitDB()
1111
if r == nil {
1212
return
1313
}

routes/routes.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ func waitGroupID(c *gin.Context) {
9393
//preloadNameTypes for better response time we load all records of the table
9494
func preloadNameTypes() gin.HandlerFunc {
9595
var nameTypes []models.NameType
96-
if err := database.Db.Find(&nameTypes).Error; err != nil {
96+
if err := database.DB.Find(&nameTypes).Error; err != nil {
9797
return nil
9898
}
9999

0 commit comments

Comments
 (0)