Skip to content

Commit 4983bc8

Browse files
committed
- Refactor getSimilarNames. We partition it into a method of NameType and a function on crud file
1 parent 04956cb commit 4983bc8

File tree

7 files changed

+329
-381
lines changed

7 files changed

+329
-381
lines changed

controllers/crud.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package controllers
22

33
import (
4+
"github.com/Darklabel91/API_Names/database"
45
"github.com/Darklabel91/API_Names/models"
56
"github.com/gin-gonic/gin"
67
"net/http"
@@ -78,6 +79,35 @@ func GetName(c *gin.Context) {
7879
return
7980
}
8081

82+
//GetMetaphoneMatch read name by metaphone
83+
func GetMetaphoneMatch(c *gin.Context) {
84+
//Check the cache
85+
var preloadTable []models.NameType
86+
cache, existKey := c.Get("nameTypes")
87+
if existKey {
88+
preloadTable = cache.([]models.NameType)
89+
} else {
90+
if err := database.DB.Find(&preloadTable).Error; err != nil {
91+
c.JSON(http.StatusInternalServerError, gin.H{"error": "failed to preload nameTypes"})
92+
return
93+
}
94+
}
95+
96+
//name to be searched
97+
name := c.Params.ByName("name")
98+
99+
//search similar names
100+
var nameType models.NameType
101+
canonicalEntity, err := nameType.GetSimilarMatch(name, preloadTable)
102+
if err != nil {
103+
c.JSON(http.StatusInternalServerError, gin.H{"Message": err})
104+
return
105+
}
106+
107+
c.JSON(200, canonicalEntity)
108+
return
109+
}
110+
81111
//UpdateName update name by id
82112
func UpdateName(c *gin.Context) {
83113
//convert id string into int

controllers/getSimilarNames.go

Lines changed: 0 additions & 306 deletions
This file was deleted.

0 commit comments

Comments
 (0)