66 "net/http"
77 "strconv"
88 "strings"
9+ "sync"
910)
1011
1112//CreateName create new name on database of type NameType
@@ -25,14 +26,27 @@ func CreateName(c *gin.Context) {
2526 return
2627 }
2728
29+ //Check the cache
30+ preloadTable := checkCache (c )
31+
32+ //Check if there's an exact name on the database
33+ for _ , name := range preloadTable {
34+ if name .Name == input .Name {
35+ c .JSON (http .StatusBadRequest , gin.H {"Message" : " Duplicate entry " + name .Name })
36+ return
37+ }
38+ }
39+
2840 //create name
2941 n , err := input .CreateName ()
3042 if err != nil {
3143 c .JSON (http .StatusInternalServerError , gin.H {"Error" : err .Error ()})
3244 return
3345 }
3446
47+ clearCache (c )
3548 c .JSON (http .StatusOK , n )
49+ return
3650}
3751
3852//GetID read name by id
@@ -82,32 +96,21 @@ func GetName(c *gin.Context) {
8296func GetMetaphoneMatch (c * gin.Context ) {
8397 var nameType models.NameType
8498
85- //Check the cache
86- var preloadTable []models.NameType
87- cache , existKey := c .Get ("nameTypes" )
88- if existKey {
89- preloadTable = cache .([]models.NameType )
90- } else {
91- allNames , err := nameType .GetAllNames ()
92- if err != nil {
93- c .JSON (http .StatusInternalServerError , gin.H {"Message" : "Error on caching all name types" })
94- return
95- }
96- preloadTable = allNames
97- }
98-
9999 //name to be searched
100100 name := c .Params .ByName ("name" )
101101
102+ //Check the cache
103+ preloadTable := checkCache (c )
104+
102105 //search similar names
103106 canonicalEntity , err := nameType .GetSimilarMatch (name , preloadTable )
104107 if err != nil {
105108 c .JSON (http .StatusNotFound , gin.H {"Message" : "Couldn't find canonical entity" })
106109 return
110+ } else {
111+ c .JSON (200 , canonicalEntity )
112+ return
107113 }
108-
109- c .JSON (200 , canonicalEntity )
110- return
111114}
112115
113116//UpdateName update name by id
@@ -151,22 +154,30 @@ func UpdateName(c *gin.Context) {
151154 c .JSON (http .StatusBadRequest , gin.H {"Message" : "Every item on json is the same on the database id " + param })
152155 return
153156 } else {
154- if input .Name != "" {
157+ if input .Name != "" && input . Name == name . Name {
155158 name .Name = input .Name
156159 }
157- if input .Classification != "" {
160+ if input .Classification != "" && input . Classification == name . Classification {
158161 name .Classification = input .Classification
159162 }
160- if input .Metaphone != "" {
163+ if input .Metaphone != "" && input . Metaphone == name . Metaphone {
161164 name .Metaphone = input .Metaphone
162165 }
163- if input .NameVariations != "" {
166+ if input .NameVariations != "" && input . NameVariations == name . NameVariations {
164167 name .NameVariations = input .NameVariations
165168 }
166169
167- db .Save (name )
170+ r := db .Save (name )
171+ if r .Error != nil {
172+ c .JSON (http .StatusBadRequest , gin.H {"Error" : r .Error })
173+ return
174+ }
175+
168176 c .JSON (http .StatusOK , name )
177+ clearCache (c )
178+ return
169179 }
180+
170181}
171182
172183//DeleteName delete name off database by id
@@ -197,4 +208,35 @@ func DeleteName(c *gin.Context) {
197208 }
198209
199210 c .JSON (http .StatusOK , gin.H {"Delete" : "id " + param + " was deleted" })
211+ clearCache (c )
212+ return
213+ }
214+
215+ func checkCache (c * gin.Context ) []models.NameType {
216+ var nameType models.NameType
217+ var preloadTable []models.NameType
218+
219+ cache , existKey := c .Get ("nameTypes" )
220+ if existKey {
221+ preloadTable = cache .([]models.NameType )
222+ } else {
223+ allNames , err := nameType .GetAllNames ()
224+ if err != nil {
225+ c .JSON (http .StatusInternalServerError , gin.H {"Message" : "Error on caching all name types" })
226+ return nil
227+ }
228+ preloadTable = allNames
229+ c .Set ("nameTypes" , preloadTable )
230+ }
231+
232+ return preloadTable
233+ }
234+
235+ func clearCache (c * gin.Context ) {
236+ cache , exist := c .Get ("nameTypes" )
237+ if exist {
238+ if cm , ok := cache .(sync.Map ); ok {
239+ cm .Delete ("preloadTable" )
240+ }
241+ }
200242}
0 commit comments