Skip to content

Commit 04806c1

Browse files
committed
- Better project organization, and now we created methods for each model. It's way faster now.
- So fast that we need to update the log every 10s
1 parent 08814be commit 04806c1

File tree

22 files changed

+549
-390
lines changed

22 files changed

+549
-390
lines changed

controllers/createName.go

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

controllers/crud.go

Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
1+
package controllers
2+
3+
import (
4+
"github.com/Darklabel91/API_Names/models"
5+
"github.com/gin-gonic/gin"
6+
"net/http"
7+
"strconv"
8+
"strings"
9+
)
10+
11+
//CreateName create new name on database of type NameType
12+
func CreateName(c *gin.Context) {
13+
//name is passed by middlewares
14+
nameValue, ok := c.Get("name")
15+
if !ok {
16+
c.JSON(http.StatusBadRequest, gin.H{"Message": "name is not present on middlewares"})
17+
return
18+
}
19+
20+
//parse nameValue into models.NameTypeInput
21+
var input models.NameType
22+
input, ok = nameValue.(models.NameType)
23+
if !ok {
24+
c.JSON(http.StatusBadRequest, gin.H{"Message": "failed to parse name"})
25+
return
26+
}
27+
28+
//create name
29+
n, err := input.CreateName()
30+
if err != nil {
31+
c.JSON(http.StatusInternalServerError, gin.H{"Error": err.Error()})
32+
return
33+
}
34+
35+
c.JSON(http.StatusOK, n)
36+
}
37+
38+
//GetID read name by id
39+
func GetID(c *gin.Context) {
40+
var name models.NameType
41+
42+
param := c.Params.ByName("id")
43+
id, err := strconv.Atoi(param)
44+
if err != nil {
45+
c.JSON(http.StatusBadRequest, gin.H{"Id": "error on converting id"})
46+
return
47+
}
48+
49+
n, _, err := name.GetNameById(id)
50+
if err != nil {
51+
c.JSON(http.StatusNotFound, gin.H{"Not found": "name id not found"})
52+
return
53+
}
54+
if n.ID == 0 {
55+
c.JSON(http.StatusNotFound, gin.H{"Not found": "name id not found"})
56+
return
57+
}
58+
59+
c.JSON(http.StatusOK, n)
60+
}
61+
62+
//GetName read name by name
63+
func GetName(c *gin.Context) {
64+
var name models.NameType
65+
66+
param := c.Params.ByName("name")
67+
n, err := name.GetNameByName(strings.ToUpper(param))
68+
if err != nil {
69+
c.JSON(http.StatusNotFound, gin.H{"Not found": "name not found"})
70+
return
71+
}
72+
if n.ID == 0 {
73+
c.JSON(http.StatusNotFound, gin.H{"Not found": "name not found"})
74+
return
75+
}
76+
77+
c.JSON(http.StatusOK, n)
78+
return
79+
}
80+
81+
//UpdateName update name by id
82+
func UpdateName(c *gin.Context) {
83+
//convert id string into int
84+
param := c.Params.ByName("id")
85+
id, err := strconv.Atoi(param)
86+
if err != nil {
87+
c.JSON(http.StatusBadRequest, gin.H{"Id": "error on converting id"})
88+
return
89+
}
90+
91+
//get the name by id
92+
var n models.NameType
93+
name, db, err := n.GetNameById(id)
94+
if err != nil {
95+
c.JSON(http.StatusInternalServerError, gin.H{"Error": "name id not found"})
96+
return
97+
}
98+
if name.ID == 0 {
99+
c.JSON(http.StatusNotFound, gin.H{"Not found": "name id not found"})
100+
return
101+
}
102+
103+
//name is passed by middlewares
104+
nameValue, ok := c.Get("name")
105+
if !ok {
106+
c.JSON(http.StatusBadRequest, gin.H{"Message": "name is not present on middlewares"})
107+
return
108+
}
109+
110+
//parse nameValue into models.NameTypeInput
111+
var input models.NameType
112+
input, ok = nameValue.(models.NameType)
113+
if !ok {
114+
c.JSON(http.StatusBadRequest, gin.H{"Message": "failed to parse name"})
115+
return
116+
}
117+
118+
if input.Name == name.Name && input.Classification == name.Classification && input.Metaphone == name.Metaphone && input.NameVariations == name.NameVariations {
119+
c.JSON(http.StatusBadRequest, gin.H{"Message": "Every item on json is the same on the database id " + param})
120+
return
121+
} else {
122+
if input.Name != "" {
123+
name.Name = input.Name
124+
}
125+
if input.Classification != "" {
126+
name.Classification = input.Classification
127+
}
128+
if input.Metaphone != "" {
129+
name.Metaphone = input.Metaphone
130+
}
131+
if input.NameVariations != "" {
132+
name.NameVariations = input.NameVariations
133+
}
134+
135+
db.Save(name)
136+
c.JSON(http.StatusOK, name)
137+
}
138+
}
139+
140+
//DeleteName delete name off database by id
141+
func DeleteName(c *gin.Context) {
142+
var name models.NameType
143+
144+
param := c.Params.ByName("id")
145+
id, err := strconv.Atoi(param)
146+
if err != nil {
147+
c.JSON(http.StatusInternalServerError, gin.H{"Error": "error on converting id"})
148+
return
149+
}
150+
151+
n, _, err := name.GetNameById(id)
152+
if err != nil {
153+
c.JSON(http.StatusNotFound, gin.H{"Not found": "name id not found"})
154+
return
155+
}
156+
if n.ID == 0 {
157+
c.JSON(http.StatusNotFound, gin.H{"Not found": "name id not found"})
158+
return
159+
}
160+
161+
_, err = name.DeleteNameById(id)
162+
if err != nil {
163+
c.JSON(http.StatusNotFound, gin.H{"Not found": "name id not found"})
164+
return
165+
}
166+
167+
c.JSON(http.StatusOK, gin.H{"Delete": "id " + param + " was deleted"})
168+
}

controllers/deleteName.go

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

controllers/getID.go

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

controllers/getIPs.go

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

controllers/getName.go

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

0 commit comments

Comments
 (0)