diff --git a/.gitignore b/.gitignore new file mode 100644 index 00000000..0efc5d44 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +.env +tmp \ No newline at end of file diff --git a/controllers/controllers.go b/controllers/controllers.go new file mode 100644 index 00000000..1acaa9b0 --- /dev/null +++ b/controllers/controllers.go @@ -0,0 +1,200 @@ +package controllers + +import ( + "backend-test-two/helpers" + "backend-test-two/initializers" + "backend-test-two/models" + "context" + "fmt" + "net/http" + + "github.com/gin-gonic/gin" + "github.com/google/go-cmp/cmp" + _ "github.com/mitchellh/mapstructure" + "go.mongodb.org/mongo-driver/bson" + "go.mongodb.org/mongo-driver/bson/primitive" + "go.mongodb.org/mongo-driver/mongo/options" +) + +var mongoEnv = "MONGO_URI" +var database = "backend" +var collection = "backend-test" + +// API CONTROLLERS +func CreateLocation(c *gin.Context) { + //Connect to Atlas + uri := helpers.GetEnviromentalVariable(mongoEnv) + client, err := initializers.ConnectToAtlas(uri) + helpers.HandleError(err) + var location *models.Location + + if err := c.BindJSON(&location); err != nil { + fmt.Println(location) + c.IndentedJSON(http.StatusBadRequest, gin.H{"message": "no location found on request."}) + return + } else { + if cmp.Equal(location, &models.Location{}) { + c.IndentedJSON(http.StatusBadRequest, gin.H{"message": "location is empty."}) + } else { + + coll := client.Database(database).Collection(collection) + + res, err := coll.InsertOne(context.Background(), location) + + if err != nil { + fmt.Println(err) + } else { + fmt.Println(res) + fmt.Println(c.ClientIP()) + c.IndentedJSON(http.StatusOK, gin.H{"message": "location inserted."}) + } + } + + } + //Disconnect from Atlas + initializers.DisconnectFromAtlas(*client) +} + +func GetAllLocations(c *gin.Context) { + //Connect to Atlas + uri := helpers.GetEnviromentalVariable(mongoEnv) + client, err := initializers.ConnectToAtlas(uri) + helpers.HandleError(err) + + coll := client.Database(database).Collection(collection) + + findOptions := options.Find() + + cur, err := coll.Find(context.TODO(), bson.D{}, findOptions) + if err != nil { + fmt.Println(err) + } + var documents []models.Location + + for cur.Next(context.TODO()) { + var location models.Location + err := cur.Decode(&location) + if err != nil { + fmt.Println(err) + } + documents = append(documents, location) + } + + c.IndentedJSON(http.StatusOK, gin.H{"test": documents}) + + //Disconnect from Atlas + initializers.DisconnectFromAtlas(*client) +} + +func GetLocationById(c *gin.Context) { + //Connect to Atlas + uri := helpers.GetEnviromentalVariable(mongoEnv) + client, err := initializers.ConnectToAtlas(uri) + helpers.HandleError(err) + + //Configure parameters + locationId := c.Param("id") + + locationIdPrimitive, err := primitive.ObjectIDFromHex(locationId) + if err != nil { + fmt.Println(err) + } + //Update location and send response + coll := client.Database(database).Collection(collection) + sr := coll.FindOne(context.TODO(), bson.M{"_id": locationIdPrimitive}) + if sr.Err() != nil { + fmt.Println(sr.Err()) + if sr.Err().Error() == "mongo: no documents in result" { + c.IndentedJSON(http.StatusBadRequest, gin.H{"message": "location does not exist."}) + } + } else { + var location models.Location + sr.Decode(&location) + + c.IndentedJSON(http.StatusOK, gin.H{"location": location}) + } + + //Disconnect from Atlas + initializers.DisconnectFromAtlas(*client) +} + +func UpdateLocationsById(c *gin.Context) { + + uri := helpers.GetEnviromentalVariable(mongoEnv) + client, err := initializers.ConnectToAtlas(uri) + helpers.HandleError(err) + locationId := c.Param("id") + var update models.Location + err = c.BindJSON(&update) + helpers.HandleError(err) + + if err != nil { + c.IndentedJSON(http.StatusBadRequest, gin.H{"message": "no location found on request."}) + } else { + if cmp.Equal(&update, &models.Location{}) { + + c.IndentedJSON(http.StatusBadRequest, gin.H{"message": "location is empty."}) + + } else { + + locationIdPrimitive, err := primitive.ObjectIDFromHex(locationId) + + if err != nil { + fmt.Println(err) + } + + //Update location and send response + coll := client.Database(database).Collection(collection) + + sr, err := coll.UpdateByID(context.Background(), locationIdPrimitive, bson.M{"$set": update}) + + if err != nil { + fmt.Println(err) + } else { + if sr.MatchedCount == 0 { + c.IndentedJSON(http.StatusOK, gin.H{"message": fmt.Sprintf("id %v does not exists.", locationId)}) + } else { + c.IndentedJSON(http.StatusOK, gin.H{"message": "location updated successfully."}) + } + } + } + } + + //Disconnect from Atlas + err = initializers.DisconnectFromAtlas(*client) + helpers.HandleError(err) +} + +func DeleteLocationById(c *gin.Context) { + //Connect to Atlas + uri := helpers.GetEnviromentalVariable(mongoEnv) + client, err := initializers.ConnectToAtlas(uri) + helpers.HandleError(err) + + //Configure parameters + localtionId := c.Param("id") + localtionIdPrimitive, err := primitive.ObjectIDFromHex(localtionId) + if err != nil { + fmt.Println(err) + } + + //Delete location + coll := client.Database(database).Collection(collection) + dr, err := coll.DeleteOne(context.TODO(), bson.M{"_id": localtionIdPrimitive}) + if err != nil { + fmt.Println(err) + } + if dr.DeletedCount == 0 { + c.IndentedJSON(http.StatusOK, gin.H{"message": fmt.Sprintf("id %v does not exists.", localtionId)}) + } else { + c.IndentedJSON(http.StatusOK, gin.H{"message": "location delted successfully."}) + } + + //Disconnect from Atlas + initializers.DisconnectFromAtlas(*client) +} + +// FRONTEND CONTROLLERS +func Index(c *gin.Context) { + c.HTML(http.StatusOK, "index.html", gin.H{"content": "homepage"}) +} diff --git a/go.mod b/go.mod new file mode 100644 index 00000000..6636f508 --- /dev/null +++ b/go.mod @@ -0,0 +1,43 @@ +module backend-test-two + +go 1.20 + +require ( + github.com/gin-contrib/cors v1.4.0 + github.com/gin-gonic/gin v1.8.2 + github.com/joho/godotenv v1.5.1 + github.com/mitchellh/mapstructure v1.5.0 + go.mongodb.org/mongo-driver v1.11.1 +) + +require ( + github.com/gin-contrib/sse v0.1.0 // indirect + github.com/go-playground/locales v0.14.0 // indirect + github.com/go-playground/universal-translator v0.18.0 // indirect + github.com/go-playground/validator/v10 v10.11.1 // indirect + github.com/goccy/go-json v0.9.11 // indirect + github.com/golang/snappy v0.0.3 // indirect + github.com/google/go-cmp v0.5.6 + github.com/json-iterator/go v1.1.12 // indirect + github.com/klauspost/compress v1.15.9 // indirect + github.com/leodido/go-urn v1.2.1 // indirect + github.com/mattn/go-isatty v0.0.17 // indirect + github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect + github.com/modern-go/reflect2 v1.0.2 // indirect + github.com/montanaflynn/stats v0.0.0-20171201202039-1bf9dbcd8cbe // indirect + github.com/pelletier/go-toml/v2 v2.0.6 // indirect + github.com/pkg/errors v0.9.1 // indirect + github.com/ugorji/go/codec v1.2.7 // indirect + github.com/xdg-go/pbkdf2 v1.0.0 // indirect + github.com/xdg-go/scram v1.1.1 // indirect + github.com/xdg-go/stringprep v1.0.3 // indirect + github.com/youmark/pkcs8 v0.0.0-20181117223130-1be2e3e5546d // indirect + golang.org/x/crypto v0.0.0-20220622213112-05595931fe9d // indirect + golang.org/x/net v0.4.0 // indirect + golang.org/x/sync v0.0.0-20210220032951-036812b2e83c // indirect + golang.org/x/sys v0.3.0 // indirect + golang.org/x/text v0.5.0 // indirect + golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect + google.golang.org/protobuf v1.28.1 // indirect + gopkg.in/yaml.v2 v2.4.0 // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 00000000..eb526374 --- /dev/null +++ b/go.sum @@ -0,0 +1,140 @@ +github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/gin-contrib/cors v1.4.0 h1:oJ6gwtUl3lqV0WEIwM/LxPF1QZ5qe2lGWdY2+bz7y0g= +github.com/gin-contrib/cors v1.4.0/go.mod h1:bs9pNM0x/UsmHPBWT2xZz9ROh8xYjYkiURUfmBoMlcs= +github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE= +github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI= +github.com/gin-gonic/gin v1.8.1/go.mod h1:ji8BvRH1azfM+SYow9zQ6SZMvR8qOMZHmsCuWR9tTTk= +github.com/gin-gonic/gin v1.8.2 h1:UzKToD9/PoFj/V4rvlKqTRKnQYyz8Sc1MJlv4JHPtvY= +github.com/gin-gonic/gin v1.8.2/go.mod h1:qw5AYuDrzRTnhvusDsrov+fDIxp9Dleuu12h8nfB398= +github.com/go-playground/assert/v2 v2.0.1 h1:MsBgLAaY856+nPRTKrp3/OZK38U/wa0CcBYNjji3q3A= +github.com/go-playground/assert/v2 v2.0.1/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4= +github.com/go-playground/locales v0.14.0 h1:u50s323jtVGugKlcYeyzC0etD1HifMjqmJqb8WugfUU= +github.com/go-playground/locales v0.14.0/go.mod h1:sawfccIbzZTqEDETgFXqTho0QybSa7l++s0DH+LDiLs= +github.com/go-playground/universal-translator v0.18.0 h1:82dyy6p4OuJq4/CByFNOn/jYrnRPArHwAcmLoJZxyho= +github.com/go-playground/universal-translator v0.18.0/go.mod h1:UvRDBj+xPUEGrFYl+lu/H90nyDXpg0fqeB/AQUGNTVA= +github.com/go-playground/validator/v10 v10.10.0/go.mod h1:74x4gJWsvQexRdW8Pn3dXSGrTK4nAUsbPlLADvpJkos= +github.com/go-playground/validator/v10 v10.11.1 h1:prmOlTVv+YjZjmRmNSF3VmspqJIxJWXmqUsHwfTRRkQ= +github.com/go-playground/validator/v10 v10.11.1/go.mod h1:i+3WkQ1FvaUjjxh1kSvIA4dMGDBiPU55YFDl0WbKdWU= +github.com/goccy/go-json v0.9.7/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I= +github.com/goccy/go-json v0.9.11 h1:/pAaQDLHEoCq/5FFmSKBswWmK6H0e8g4159Kc/X/nqk= +github.com/goccy/go-json v0.9.11/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I= +github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk= +github.com/golang/snappy v0.0.1/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q= +github.com/golang/snappy v0.0.3 h1:fHPg5GQYlCeLIPB9BZqMVR5nR9A+IM5zcgeTdjMYmLA= +github.com/golang/snappy v0.0.3/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q= +github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= +github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= +github.com/google/go-cmp v0.5.6 h1:BKbKCqvP6I+rmFHt06ZmyQtvB8xAkWdhFyr0ZUNZcxQ= +github.com/google/go-cmp v0.5.6/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= +github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= +github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0= +github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4= +github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM= +github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo= +github.com/klauspost/compress v1.13.6/go.mod h1:/3/Vjq9QcHkK5uEr5lBEmyoZ1iFhe47etQ6QUkpK6sk= +github.com/klauspost/compress v1.15.9 h1:wKRjX6JRtDdrE9qwa4b/Cip7ACOshUI4smpCQanqjSY= +github.com/klauspost/compress v1.15.9/go.mod h1:PhcZ0MbTNciWF3rruxRgKxI5NkcHHrHUDtV4Yw2GlzU= +github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= +github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI= +github.com/kr/pretty v0.3.0 h1:WgNl7dwNpEZ6jJ9k1snq4pZsg7DOEN8hP9Xw0Tsjwk0= +github.com/kr/pretty v0.3.0/go.mod h1:640gp4NfQd8pI5XOwp5fnNeVWj67G7CFk/SaSQn7NBk= +github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= +github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= +github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= +github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= +github.com/leodido/go-urn v1.2.1 h1:BqpAaACuzVSgi/VLzGZIobT2z4v53pjosyNd9Yv6n/w= +github.com/leodido/go-urn v1.2.1/go.mod h1:zt4jvISO2HfUBqxjfIshjdMTYS56ZS/qv49ictyFfxY= +github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94= +github.com/mattn/go-isatty v0.0.17 h1:BTarxUcIeDqL27Mc+vyvdWYSL28zpIhv3RoTdsLMPng= +github.com/mattn/go-isatty v0.0.17/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM= +github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY= +github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo= +github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= +github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg= +github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= +github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M= +github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk= +github.com/montanaflynn/stats v0.0.0-20171201202039-1bf9dbcd8cbe h1:iruDEfMl2E6fbMZ9s0scYfZQ84/6SPL6zC8ACM2oIL0= +github.com/montanaflynn/stats v0.0.0-20171201202039-1bf9dbcd8cbe/go.mod h1:wL8QJuTMNUDYhXwkmfOly8iTdp5TEcJFWZD2D7SIkUc= +github.com/pelletier/go-toml/v2 v2.0.1/go.mod h1:r9LEWfGN8R5k0VXJ+0BkIe7MYkRdwZOjgMj2KwnJFUo= +github.com/pelletier/go-toml/v2 v2.0.6 h1:nrzqCb7j9cDFj2coyLNLaZuJTLjWjlaz6nvTvIwycIU= +github.com/pelletier/go-toml/v2 v2.0.6/go.mod h1:eumQOmlWiOPt5WriQQqoM5y18pDHwha2N+QD+EUNTek= +github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA= +github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= +github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/rogpeppe/go-internal v1.6.1/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTEfhy4qGm1nDQc= +github.com/rogpeppe/go-internal v1.8.0 h1:FCbCCtXNOY3UtUuHUYaghJg4y7Fd14rXifAYUAtL9R8= +github.com/rogpeppe/go-internal v1.8.0/go.mod h1:WmiCO8CzOY8rg0OYDC4/i/2WRWAB6poM+XZ2dLUbcbE= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= +github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= +github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= +github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= +github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk= +github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= +github.com/tidwall/pretty v1.0.0 h1:HsD+QiTn7sK6flMKIvNmpqz1qrpP3Ps6jOKIKMooyg4= +github.com/tidwall/pretty v1.0.0/go.mod h1:XNkn88O1ChpSDQmQeStsy+sBenx6DDtFZJxhVysOjyk= +github.com/ugorji/go v1.2.7/go.mod h1:nF9osbDWLy6bDVv/Rtoh6QgnvNDpmCalQV5urGCCS6M= +github.com/ugorji/go/codec v1.2.7 h1:YPXUKf7fYbp/y8xloBqZOw2qaVggbfwMlI8WM3wZUJ0= +github.com/ugorji/go/codec v1.2.7/go.mod h1:WGN1fab3R1fzQlVQTkfxVtIBhWDRqOviHU95kRgeqEY= +github.com/xdg-go/pbkdf2 v1.0.0 h1:Su7DPu48wXMwC3bs7MCNG+z4FhcyEuz5dlvchbq0B0c= +github.com/xdg-go/pbkdf2 v1.0.0/go.mod h1:jrpuAogTd400dnrH08LKmI/xc1MbPOebTwRqcT5RDeI= +github.com/xdg-go/scram v1.1.1 h1:VOMT+81stJgXW3CpHyqHN3AXDYIMsx56mEFrB37Mb/E= +github.com/xdg-go/scram v1.1.1/go.mod h1:RaEWvsqvNKKvBPvcKeFjrG2cJqOkHTiyTpzz23ni57g= +github.com/xdg-go/stringprep v1.0.3 h1:kdwGpVNwPFtjs98xCGkHjQtGKh86rDcRZN17QEMCOIs= +github.com/xdg-go/stringprep v1.0.3/go.mod h1:W3f5j4i+9rC0kuIEJL0ky1VpHXQU3ocBgklLGvcBnW8= +github.com/youmark/pkcs8 v0.0.0-20181117223130-1be2e3e5546d h1:splanxYIlg+5LfHAM6xpdFEAYOk8iySO56hMFq6uLyA= +github.com/youmark/pkcs8 v0.0.0-20181117223130-1be2e3e5546d/go.mod h1:rHwXgn7JulP+udvsHwJoVG1YGAP6VLg4y9I5dyZdqmA= +go.mongodb.org/mongo-driver v1.11.1 h1:QP0znIRTuL0jf1oBQoAoM0C6ZJfBK4kx0Uumtv1A7w8= +go.mongodb.org/mongo-driver v1.11.1/go.mod h1:s7p5vEtfbeR1gYi6pnj3c3/urpbLv2T5Sfd6Rp2HBB8= +golang.org/x/crypto v0.0.0-20210711020723-a769d52b0f97/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= +golang.org/x/crypto v0.0.0-20211215153901-e495a2d5b3d3/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4= +golang.org/x/crypto v0.0.0-20220622213112-05595931fe9d h1:sK3txAijHtOK88l68nt020reeT1ZdKLIYetKl95FzVY= +golang.org/x/crypto v0.0.0-20220622213112-05595931fe9d/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4= +golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= +golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= +golang.org/x/net v0.4.0 h1:Q5QPcMlvfxFTAPV0+07Xz/MpK9NTXu2VDUuy0FeMfaU= +golang.org/x/net v0.4.0/go.mod h1:MBQ8lrhLObU/6UmLb4fmbmk5OcyYmqtbGd/9yIeKjEE= +golang.org/x/sync v0.0.0-20210220032951-036812b2e83c h1:5KslGYwFpkhGh+Q16bwMP3cOontH8FOep7tGV86Y7SQ= +golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20210806184541-e5e7981a1069/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.3.0 h1:w8ZOecv6NaNa/zC8944JTU3vz4u6Lagfk4RPQxv92NQ= +golang.org/x/sys v0.3.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= +golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= +golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= +golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= +golang.org/x/text v0.5.0 h1:OLmvp0KP+FVG99Ct/qFiL/Fhk4zp4QQnZ7b2U+5piUM= +golang.org/x/text v0.5.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8= +golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= +golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE= +golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw= +google.golang.org/protobuf v1.28.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I= +google.golang.org/protobuf v1.28.1 h1:d0NfwRgPtno5B1Wa6L2DAG+KivqkdutMf1UhdNx175w= +google.golang.org/protobuf v1.28.1/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk= +gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q= +gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI= +gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY= +gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/helpers/helpers.go b/helpers/helpers.go new file mode 100644 index 00000000..25847683 --- /dev/null +++ b/helpers/helpers.go @@ -0,0 +1,30 @@ +package helpers + +import ( + "fmt" + "testing" + + "github.com/joho/godotenv" +) + +func GetEnviromentalVariable(variableName string) string { + envMap, err := godotenv.Read(".env") + + if err != nil { + fmt.Println("YOU NEED TO CREATE AN .ENV FILE AND PUT 'MONGO_URI='") + } + + return envMap[variableName] +} + +func HandleError(err error) { + if err != nil { + fmt.Println(err) + } +} + +func HandleErrorTest(err error, t *testing.T) { + if err != nil { + t.Error(err) + } +} diff --git a/initializers/initializers.go b/initializers/initializers.go new file mode 100644 index 00000000..d2c57d12 --- /dev/null +++ b/initializers/initializers.go @@ -0,0 +1,22 @@ +package initializers + +import ( + "context" + + "go.mongodb.org/mongo-driver/mongo" + "go.mongodb.org/mongo-driver/mongo/options" +) + +func ConnectToAtlas(uri string) (*mongo.Client, error) { + + client, err := mongo.Connect(context.TODO(), options.Client().ApplyURI(uri)) + + return client, err +} + +func DisconnectFromAtlas(client mongo.Client) error { + + err := client.Disconnect(context.TODO()) + + return err +} diff --git a/main.go b/main.go new file mode 100644 index 00000000..89ff08d6 --- /dev/null +++ b/main.go @@ -0,0 +1,31 @@ +package main + +import ( + "backend-test-two/controllers" + + "github.com/gin-contrib/cors" + "github.com/gin-gonic/gin" +) + +func main() { + + //CONFIG + router := gin.Default() + router.Use(cors.Default()) + router.Static("/statics", "./statics/") + router.LoadHTMLGlob("views/*.html") + + //API + router.POST("/api/location", controllers.CreateLocation) + router.GET("/api/location", controllers.GetAllLocations) + router.GET("/api/location/:id", controllers.GetLocationById) + router.PUT("/api/location/:id", controllers.UpdateLocationsById) + router.DELETE("/api/location/:id", controllers.DeleteLocationById) + + //PAGES + router.GET("/", controllers.Index) + + //RUN + router.Run("localhost:9090") + +} diff --git a/models/models.go b/models/models.go new file mode 100644 index 00000000..0001edd6 --- /dev/null +++ b/models/models.go @@ -0,0 +1,15 @@ +package models + +type Location struct { + Abv float64 `json:"abv,omitempty" bson:",omitempty"` + Address string `json:"address,omitempty" bson:",omitempty"` + Category string `json:"category,omitempty" bson:",omitempty"` + City string `json:"city,omitempty" bson:",omitempty"` + Coordinates []float64 `json:"coordinates,omitempty" bson:",omitempty"` + Country string `json:"country,omitempty" bson:",omitempty"` + Description string `json:"description,omitempty" bson:",omitempty"` + Ibu int `json:"ibu,omitempty" bson:",omitempty"` + Name string `json:"name,omitempty" bson:",omitempty"` + State string `json:"state,omitempty" bson:",omitempty"` + Website string `json:"website,omitempty" bson:",omitempty"` +} diff --git a/statics/scripts/index.js b/statics/scripts/index.js new file mode 100644 index 00000000..532fe31a --- /dev/null +++ b/statics/scripts/index.js @@ -0,0 +1,312 @@ +const createLocation = document.querySelector('#createsubmit') + +createLocation.addEventListener('click', async (e) => { + e.preventDefault() + const abv = parseFloat(document.querySelector('#abv').value) + const category = document.querySelector('#category').value + const city = document.querySelector('#city').value + const latitude = parseFloat(document.querySelector('#latitude').value) + const longitude = parseFloat(document.querySelector('#longitude').value) + const country = document.querySelector('#country').value + const description = document.querySelector('#description').value + const ibu = parseInt(document.querySelector('#ibu').value) + const name = document.querySelector('#name').value + const state = document.querySelector('#state').value + const website = document.querySelector('#website').value + + const coordinates = [] + coordinates.push(latitude) + coordinates.push(longitude) + const location = { + abv, + category, + city, + coordinates, + country, + description, + ibu, + name, + state, + website + } + + console.log(location) + const res = await fetch('http://localhost:9090/api/location', { + method: 'POST', + headers: { + 'Accept': 'application/json', + 'Content-Type': 'application/json' + }, + body: JSON.stringify(location) + }) + console.log(res) +}) + +const updateLocation = document.querySelector('#updatesubmit') + +updateLocation.addEventListener('click', async (e) => { + e.preventDefault() + const abv = parseFloat(document.querySelector('#abv').value) + const category = document.querySelector('#category').value + const city = document.querySelector('#city').value + const latitude = parseFloat(document.querySelector('#latitude').value) + const longitude = parseFloat(document.querySelector('#longitude').value) + const country = document.querySelector('#country').value + const description = document.querySelector('#description').value + const ibu = parseInt(document.querySelector('#ibu').value) + const name = document.querySelector('#name').value + const state = document.querySelector('#state').value + const website = document.querySelector('#website').value + + const coordinates = [] + coordinates.push(latitude) + coordinates.push(longitude) + const location = { + abv, + category, + city, + coordinates, + country, + description, + ibu, + name, + state, + website + } + + console.log(location) + + const locationId = document.querySelector('#updateId').value + + const res = await fetch(`http://localhost:9090/api/location/${locationId}`, { + method: 'PUT', + headers: { + 'Accept': 'application/json', + 'Content-Type': 'application/json' + }, + body: JSON.stringify(location) + }) + console.log(res) +}) + +const getById = document.querySelector('#getbyid') + +getById.addEventListener('click', async (e) => { + e.preventDefault() + + console.log(e.currentTarget) + const locationId = document.querySelector('.getordeletebyid-container #id').value + + let location + + await fetch(`http://localhost:9090/api/location/${locationId}`, { + headers: { + 'Accept': 'application/json', + 'Content-Type': 'application/json' + }, + }).then(res => res.json().then(data => { location = data.location })) + + const locationUl = document.querySelector('#locationbyid') + + if (locationUl.childElementCount === 0) { + fillLocationById(location, locationUl) + } else { + locationUl.innerHTML = '' + fillLocationById(location, locationUl) + } +}) + +function fillLocationById(location, locationUl) { + console.log(location) + if (location.abv !== undefined) { + locationUl.insertAdjacentHTML('beforeend', ` + +

${location.abv}

+ `) + } + if (location.address !== undefined) { + locationUl.insertAdjacentHTML('beforeend', ` + +

${location.address}

+ `) + } + if (location.category !== undefined) { + locationUl.insertAdjacentHTML('beforeend', ` + +

${location.category}

+ `) + } + if (location.city !== undefined) { + locationUl.insertAdjacentHTML('beforeend', ` + +

${location.city}

+ `) + } + if (location.coordinates[0] !== undefined) { + locationUl.insertAdjacentHTML('beforeend', ` + +

${location.coordinates[0]}

+ `) + } + if (location.coordinates[1] !== undefined) { + locationUl.insertAdjacentHTML('beforeend', ` + +

${location.longitude}

+ `) + } + if (location.country !== undefined) { + locationUl.insertAdjacentHTML('beforeend', ` + +

${location.country}

+ `) + } + if (location.description !== undefined) { + locationUl.insertAdjacentHTML('beforeend', ` + +

${location.description}

+ `) + } + if (location.ibu !== undefined) { + locationUl.insertAdjacentHTML('beforeend', ` + +

${location.ibu}

+ `) + } + if (location.name !== undefined) { + locationUl.insertAdjacentHTML('beforeend', ` + +

${location.name}

+ `) + } + if (location.state !== undefined) { + locationUl.insertAdjacentHTML('beforeend', ` + +

${location.state}

+ `) + } + if (location.website !== undefined) { + locationUl.insertAdjacentHTML('beforeend', ` + +

${location.website}

+ `) + } +} + +const deleteById = document.querySelector('#deletebyid') + +deleteById.addEventListener('click', async (e) => { + e.preventDefault() + const locationId = document.querySelector('.getordeletebyid-container #id').value + + await fetch(`http://localhost:9090/api/location/${locationId}`, { + method: "DELETE", + headers: { + 'Accept': 'application/json', + 'Content-Type': 'application/json' + }, + }).then(res => { + if (res.status === 200) { + alert("Location deleted.") + } + }) +}) + +const refillLocations = (async function fillLocations() { + document.querySelector('.locations').innerHTML = '' + let allLocations = [] + + await fetch('http://localhost:9090/api/location', { method: "get" }).then(res => res.json().then(data => allLocations = data.test)) + + console.log(allLocations) + + let contador = 0 + + allLocations.forEach(item => { + contador++ + if (contador <= 20) { + + const locationContainer = document.createElement('div') + + locationContainer.className = "location-container" + if (item.abv !== undefined) { + console.log(item.abv) + locationContainer.insertAdjacentHTML('beforeend', ` + +

${item.abv}

+ `) + } + if (item.address !== undefined) { + locationContainer.insertAdjacentHTML('beforeend', ` + +

${item.address}

+ `) + } + if (item.category !== undefined) { + locationContainer.insertAdjacentHTML('beforeend', ` + +

${item.category}

+ `) + } + if (item.city !== undefined) { + locationContainer.insertAdjacentHTML('beforeend', ` + +

${item.city}

+ `) + } + if (item.coordinates !== undefined) { + if (item.coordinates[0] !== undefined) { + locationContainer.insertAdjacentHTML('beforeend', ` + +

${item.coordinates[0]}

+ `) + } + if (item.coordinates[1] !== undefined) { + locationContainer.insertAdjacentHTML('beforeend', ` + +

${item.coordinates[1]}

+ `) + } + } + if (item.country !== undefined) { + locationContainer.insertAdjacentHTML('beforeend', ` + +

${item.country}

+ `) + } + if (item.description !== undefined) { + locationContainer.insertAdjacentHTML('beforeend', ` + +

${item.description}

+ `) + } + if (item.ibu !== undefined) { + locationContainer.insertAdjacentHTML('beforeend', ` + +

${item.ibu}

+ `) + } + if (item.name !== undefined) { + locationContainer.insertAdjacentHTML('beforeend', ` + +

${item.name}

+ `) + } + if (item.state !== undefined) { + locationContainer.insertAdjacentHTML('beforeend', ` + +

${item.state}

+ `) + } + if (item.website !== undefined) { + locationContainer.insertAdjacentHTML('beforeend', ` + +

${item.website}

+ `) + } + document.querySelector('.locations').insertAdjacentElement('beforeend', locationContainer) + } + }) +} +)() + + diff --git a/statics/styles/normalize.css b/statics/styles/normalize.css new file mode 100644 index 00000000..780ad9ca --- /dev/null +++ b/statics/styles/normalize.css @@ -0,0 +1,379 @@ +/*! normalize.css v8.0.1 | MIT License | github.com/necolas/normalize.css */ + +/* Document + ========================================================================== */ + +/** + * 1. Correct the line height in all browsers. + * 2. Prevent adjustments of font size after orientation changes in iOS. + */ + +html { + line-height: 1.15; + /* 1 */ + -webkit-text-size-adjust: 100%; + /* 2 */ +} + +/* Sections + ========================================================================== */ + +/** + * Remove the margin in all browsers. + */ + +body { + margin: 0; +} + +/** + * Render the `main` element consistently in IE. + */ + +main { + display: block; +} + +/** + * Correct the font size and margin on `h1` elements within `section` and + * `article` contexts in Chrome, Firefox, and Safari. + */ + +h1 { + font-size: 2em; + margin: 0.67em 0; +} + +/* Grouping content + ========================================================================== */ + +/** + * 1. Add the correct box sizing in Firefox. + * 2. Show the overflow in Edge and IE. + */ + +hr { + box-sizing: content-box; + /* 1 */ + height: 0; + /* 1 */ + overflow: visible; + /* 2 */ +} + +/** + * 1. Correct the inheritance and scaling of font size in all browsers. + * 2. Correct the odd `em` font sizing in all browsers. + */ + +pre { + font-family: monospace, monospace; + /* 1 */ + font-size: 1em; + /* 2 */ +} + +/* Text-level semantics + ========================================================================== */ + +/** + * Remove the gray background on active links in IE 10. + */ + +a { + background-color: transparent; +} + +/** + * 1. Remove the bottom border in Chrome 57- + * 2. Add the correct text decoration in Chrome, Edge, IE, Opera, and Safari. + */ + +abbr[title] { + border-bottom: none; + /* 1 */ + text-decoration: underline; + /* 2 */ + text-decoration: underline dotted; + /* 2 */ +} + +/** + * Add the correct font weight in Chrome, Edge, and Safari. + */ + +b, +strong { + font-weight: bolder; +} + +/** + * 1. Correct the inheritance and scaling of font size in all browsers. + * 2. Correct the odd `em` font sizing in all browsers. + */ + +code, +kbd, +samp { + font-family: monospace, monospace; + /* 1 */ + font-size: 1em; + /* 2 */ +} + +/** + * Add the correct font size in all browsers. + */ + +small { + font-size: 80%; +} + +/** + * Prevent `sub` and `sup` elements from affecting the line height in + * all browsers. + */ + +sub, +sup { + font-size: 75%; + line-height: 0; + position: relative; + vertical-align: baseline; +} + +sub { + bottom: -0.25em; +} + +sup { + top: -0.5em; +} + +/* Embedded content + ========================================================================== */ + +/** + * Remove the border on images inside links in IE 10. + */ + +img { + border-style: none; +} + +/* Forms + ========================================================================== */ + +/** + * 1. Change the font styles in all browsers. + * 2. Remove the margin in Firefox and Safari. + */ + +button, +input, +optgroup, +select, +textarea { + font-family: inherit; + /* 1 */ + font-size: 100%; + /* 1 */ + line-height: 1.15; + /* 1 */ + margin: 0; + /* 2 */ +} + +/** + * Show the overflow in IE. + * 1. Show the overflow in Edge. + */ + +button, +input { + /* 1 */ + overflow: visible; +} + +/** + * Remove the inheritance of text transform in Edge, Firefox, and IE. + * 1. Remove the inheritance of text transform in Firefox. + */ + +button, +select { + /* 1 */ + text-transform: none; +} + +/** + * Correct the inability to style clickable types in iOS and Safari. + */ + +button, +[type="button"], +[type="reset"], +[type="submit"] { + -webkit-appearance: button; +} + +/** + * Remove the inner border and padding in Firefox. + */ + +button::-moz-focus-inner, +[type="button"]::-moz-focus-inner, +[type="reset"]::-moz-focus-inner, +[type="submit"]::-moz-focus-inner { + border-style: none; + padding: 0; +} + +/** + * Restore the focus styles unset by the previous rule. + */ + +button:-moz-focusring, +[type="button"]:-moz-focusring, +[type="reset"]:-moz-focusring, +[type="submit"]:-moz-focusring { + outline: 1px dotted ButtonText; +} + +/** + * Correct the padding in Firefox. + */ + +fieldset { + padding: 0.35em 0.75em 0.625em; +} + +/** + * 1. Correct the text wrapping in Edge and IE. + * 2. Correct the color inheritance from `fieldset` elements in IE. + * 3. Remove the padding so developers are not caught out when they zero out + * `fieldset` elements in all browsers. + */ + +legend { + box-sizing: border-box; + /* 1 */ + color: inherit; + /* 2 */ + display: table; + /* 1 */ + max-width: 100%; + /* 1 */ + padding: 0; + /* 3 */ + white-space: normal; + /* 1 */ +} + +/** + * Add the correct vertical alignment in Chrome, Firefox, and Opera. + */ + +progress { + vertical-align: baseline; +} + +/** + * Remove the default vertical scrollbar in IE 10+. + */ + +textarea { + overflow: auto; +} + +/** + * 1. Add the correct box sizing in IE 10. + * 2. Remove the padding in IE 10. + */ + +[type="checkbox"], +[type="radio"] { + box-sizing: border-box; + /* 1 */ + padding: 0; + /* 2 */ +} + +/** + * Correct the cursor style of increment and decrement buttons in Chrome. + */ + +[type="number"]::-webkit-inner-spin-button, +[type="number"]::-webkit-outer-spin-button { + height: auto; +} + +/** + * 1. Correct the odd appearance in Chrome and Safari. + * 2. Correct the outline style in Safari. + */ + +[type="search"] { + -webkit-appearance: textfield; + /* 1 */ + outline-offset: -2px; + /* 2 */ +} + +/** + * Remove the inner padding in Chrome and Safari on macOS. + */ + +[type="search"]::-webkit-search-decoration { + -webkit-appearance: none; +} + +/** + * 1. Correct the inability to style clickable types in iOS and Safari. + * 2. Change font properties to `inherit` in Safari. + */ + +::-webkit-file-upload-button { + -webkit-appearance: button; + /* 1 */ + font: inherit; + /* 2 */ +} + +/* Interactive + ========================================================================== */ + +/* + * Add the correct display in Edge, IE 10+, and Firefox. + */ + +details { + display: block; +} + +/* + * Add the correct display in all browsers. + */ + +summary { + display: list-item; +} + +/* Misc + ========================================================================== */ + +/** + * Add the correct display in IE 10+. + */ + +template { + display: none; +} + +/** + * Add the correct display in IE 10. + */ + +[hidden] { + display: none; +} \ No newline at end of file diff --git a/statics/styles/reset.css b/statics/styles/reset.css new file mode 100644 index 00000000..299a6d16 --- /dev/null +++ b/statics/styles/reset.css @@ -0,0 +1,135 @@ +/* http://meyerweb.com/eric/tools/css/reset/ + v2.0 | 20110126 + License: none (public domain) +*/ + +html, +body, +div, +span, +applet, +object, +iframe, +h1, +h2, +h3, +h4, +h5, +h6, +p, +blockquote, +pre, +a, +abbr, +acronym, +address, +big, +cite, +code, +del, +dfn, +em, +img, +ins, +kbd, +q, +s, +samp, +small, +strike, +strong, +sub, +sup, +tt, +var, +b, +u, +i, +center, +dl, +dt, +dd, +ol, +ul, +li, +fieldset, +form, +label, +legend, +table, +caption, +tbody, +tfoot, +thead, +tr, +th, +td, +article, +aside, +canvas, +details, +embed, +figure, +figcaption, +footer, +header, +hgroup, +menu, +nav, +output, +ruby, +section, +summary, +time, +mark, +audio, +video { + margin: 0; + padding: 0; + border: 0; + font-size: 100%; + font: inherit; + vertical-align: baseline; +} + +/* HTML5 display-role reset for older browsers */ +article, +aside, +details, +figcaption, +figure, +footer, +header, +hgroup, +menu, +nav, +section { + display: block; +} + +body { + line-height: 1; +} + +ol, +ul { + list-style: none; +} + +blockquote, +q { + quotes: none; +} + +blockquote:before, +blockquote:after, +q:before, +q:after { + content: ''; + content: none; +} + +table { + border-collapse: collapse; + border-spacing: 0; +} \ No newline at end of file diff --git a/statics/styles/styles.css b/statics/styles/styles.css new file mode 100644 index 00000000..8fb424b1 --- /dev/null +++ b/statics/styles/styles.css @@ -0,0 +1,212 @@ +* { + transition: 2s; + font-family: sans-serif; + box-sizing: border-box; + word-break: break-all; +} + +html { + width: 100%; +} + +h1 { + margin: 0; +} + +body { + display: flex; + flex-direction: column; + gap: 50px; + background-color: #323232; + width: 100%; + +} + +main { + width: 100%; + display: flex; + flex-direction: column; + gap: 50px; +} + +.crud-section { + margin-top: 30px; + display: flex; + flex-direction: row; + justify-content: space-around; + flex-wrap: wrap; + width: 100%; +} + +.locationForm, +.getordeletebyid, +.deletebyid { + width: fit-content; + color: white; + display: flex; + flex-direction: column; + align-items: center; + text-align: center; + gap: 5px; +} + + + +.inputs-container { + display: flex; + flex-direction: column; + flex-wrap: wrap; + justify-content: center; + width: fit-content; +} + +input[type=text] { + width: max-content; + border: solid 1px black; + border-radius: 3px; + color: #323232; + margin: 4px; + background-color: white; + outline: none; +} + +input::placeholder { + color: #323232; +} + +#locations { + width: 100%; + height: 300px; + display: flex; + flex-direction: row; + flex-wrap: wrap; +} + +#createsubmit, +#updatesubmit, +#getbyid, +#deletebyid { + padding: 10px; + width: 150px; + border-radius: 30px; + color: #323232; + font-weight: 600; + cursor: pointer; +} + +input[type=submit]:hover { + width: 100%; +} + +#createsubmit { + background-color: #f7f14d; + border: solid 3px #f7f14d; +} + +#getbyid { + background-color: #4df750; + border: solid 3px #4df750; +} + +#updatesubmit { + background-color: #4d7af7; + border: solid 3px #4d7af7; +} + +#deletebyid { + background-color: #f7614d; + border: solid 3px #f7614d; +} + +.getordeletebyid-container { + display: flex; + flex-direction: column; + gap: 10px; + align-items: center; + max-width: 400px; +} + + +#locationbyid { + display: flex; + flex-direction: column; + align-items: center; + gap: 10px; +} + +label, +h1 { + color: white; +} + +#locationbyid p { + color: #4df750; +} + +#locations-section { + display: flex; + align-items: center; + flex-direction: column; + gap: 10px; + margin-bottom: 100px; + width: 100%; +} + +.locations { + display: flex; + flex-direction: row; + flex-wrap: wrap; + gap: 5px; + justify-content: center; +} + +.location-container { + background-color: red; + display: flex; + flex-direction: column; + width: 300px; + padding: 10px; + background-color: #323232; + border: solid 2px #f7f14d; + border-radius: 3px; + align-items: center; +} + +.location-container p { + background-color: #323232; + color: #4df750; + width: 100%; + text-align: center; + padding: 2px; +} + +.location-container label { + color: black; + width: 100%; + background-color: #f7f14d; + padding: 2px; + text-align: center; +} + +.location-container label:first-of-type { + border-top-left-radius: 3px; + border-top-right-radius: 3px; +} + +header { + padding-top: 100px; + display: flex; + width: 100%; + align-items: center; + justify-content: center; + font-family: 'Shrikhand', cursive; + gap: 30px; + flex-wrap: wrap; +} + +header img { + background-color: #323232; + border: solid 3px #4df750; + padding: 5px; + border-radius: 100%; +} \ No newline at end of file diff --git a/tests/helpers_test.go b/tests/helpers_test.go new file mode 100644 index 00000000..53e4e462 --- /dev/null +++ b/tests/helpers_test.go @@ -0,0 +1,29 @@ +package tests + +import ( + "backend-test-two/helpers" + "fmt" + "testing" + + "github.com/joho/godotenv" +) + +func TestGetEnviromentalVariable(t *testing.T) { + + //Arrange + envMap, err := godotenv.Read(".env") + if err != nil { + fmt.Println(err) + } else { + + //Act + result := helpers.GetEnviromentalVariable("MONGO_URI") + + //Assert + if result != envMap["MONGO_ATLAS_URI"] { + t.Errorf("GetEnviromentalVariable(MONGO_ATLAS_URI) FAILED. Expected %v, got %v.", envMap["MONGO_ATLAS_URI"], result) + } else { + t.Logf("GetEnviromentalVariable(MONGO_ATLAS_URI) PASSED. Expected %v, got %v.", envMap["MONGO_ATLAS_URI"], result) + } + } +} diff --git a/tests/initializers_test.go b/tests/initializers_test.go new file mode 100644 index 00000000..a07c91a0 --- /dev/null +++ b/tests/initializers_test.go @@ -0,0 +1,46 @@ +package tests + +import ( + "backend-test-two/helpers" + "backend-test-two/initializers" + "context" + "testing" + + "github.com/joho/godotenv" + "go.mongodb.org/mongo-driver/mongo" + "go.mongodb.org/mongo-driver/mongo/options" +) + +func TestConnectToAtlas(t *testing.T) { + envMap, err := godotenv.Read(".env") + if err != nil { + t.Error(err) + } + uri := envMap["MONGO_ATLAS_URI"] + + client, err := initializers.ConnectToAtlas(uri) + helpers.HandleErrorTest(err, t) + if client == nil { + t.Error("GetEnviromentalVariable(MONGO_ATLAS_URI) FAILED. Expected not nil, got nil.") + } else { + t.Log("GetEnviromentalVariable(MONGO_ATLAS_URI) PASSED. Expected not nil, got not nil.") + } +} + +func TestDiconnectFromAtlas(t *testing.T) { + + envMap, err := godotenv.Read(".env") + + if err != nil { + t.Error(err) + } + + uri := envMap["MONGO_ATLAS_URI"] + + client, err := mongo.Connect(context.TODO(), options.Client().ApplyURI(uri)) + helpers.HandleErrorTest(err, t) + + err = initializers.DisconnectFromAtlas(*client) + helpers.HandleErrorTest(err, t) + +} diff --git a/views/index.html b/views/index.html new file mode 100644 index 00000000..894a63b4 --- /dev/null +++ b/views/index.html @@ -0,0 +1,74 @@ + + + + + + + + + + + + + + + + +
+ +

backend-test-two(2)

+
+
+
+
+

Create or Update

+
+ + + + + + + + + + + + + + + + + + + + + + + + + +
+ + + + +
+ +
+

Get or Delete by id

+ + + +
    +
    +
    +
    +

    ALL LOCATIONS(only 20)

    +
    +
    +
    + + + + \ No newline at end of file