Commit ad33ebcd authored by Ai-Sasit's avatar Ai-Sasit

initial commit

parents
Pipeline #1643 canceled with stages
File added
FROM golang:1.19.3-bullseye
WORKDIR /app
COPY go.mod /app
COPY go.sum /app
RUN go mod download
COPY . /app
EXPOSE 80
CMD ["go", "run", "src/main.go"]
package configs
import (
"context"
"fmt"
"log"
"time"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
func ConnectDB() *mongo.Client {
client, err := mongo.NewClient(options.Client().ApplyURI("mongodb://Pukky:xejP3257@esarnshabu-database/?authSource=admin&retryWrites=true&w=majority"))
if err != nil {
log.Fatal("err")
}
ctx, _ := context.WithTimeout(context.Background(), 10*time.Second)
err = client.Connect(ctx)
if err != nil {
log.Fatal(err)
}
//ping the database
err = client.Ping(ctx, nil)
if err != nil {
log.Fatal(err)
}
fmt.Println("Connected to MongoDB")
return client
}
// Client instance
var DB *mongo.Client = ConnectDB()
// getting database collections
func GetCollection(client *mongo.Client, collectionName string) *mongo.Collection {
collection := client.Database("golangAPI").Collection(collectionName)
return collection
}
package controllers
import (
"context"
"fiber-mongo-api/configs"
"fiber-mongo-api/models"
"fiber-mongo-api/responses"
"net/http"
"time"
"github.com/gofiber/fiber/v2"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo"
)
var categoryCollection *mongo.Collection = configs.GetCollection(configs.DB, "categorys")
func CreateCategory(c *fiber.Ctx) error {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
var category models.Category
defer cancel()
//validate the request body
if err := c.BodyParser(&category); err != nil {
return c.Status(http.StatusBadRequest).JSON(responses.CategoryResponse{Status: http.StatusBadRequest, Message: "error", Data: &fiber.Map{"data": err.Error()}})
}
//use the validator library to validate required fields
if validationErr := validate.Struct(&category); validationErr != nil {
return c.Status(http.StatusBadRequest).JSON(responses.CategoryResponse{Status: http.StatusBadRequest, Message: "error", Data: &fiber.Map{"data": validationErr.Error()}})
}
newCategory := models.Category{
Name: category.Name,
}
result, err := categoryCollection.InsertOne(ctx, newCategory)
if err != nil {
return c.Status(http.StatusInternalServerError).JSON(responses.CategoryResponse{Status: http.StatusInternalServerError, Message: "error", Data: &fiber.Map{"data": err.Error()}})
}
return c.Status(http.StatusCreated).JSON(responses.CategoryResponse{Status: http.StatusCreated, Message: "success", Data: &fiber.Map{"data": result}})
}
func GetACategory(c *fiber.Ctx) error {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
categoryId := c.Params("categoryId")
var category models.Category
defer cancel()
objId, _ := primitive.ObjectIDFromHex(categoryId)
err := categoryCollection.FindOne(ctx, bson.M{"id": objId}).Decode(&category)
if err != nil {
return c.Status(http.StatusInternalServerError).JSON(responses.CategoryResponse{Status: http.StatusInternalServerError, Message: "error", Data: &fiber.Map{"data": err.Error()}})
}
return c.Status(http.StatusOK).JSON(responses.CategoryResponse{Status: http.StatusOK, Message: "success", Data: &fiber.Map{"data": category}})
}
func EditACategory(c *fiber.Ctx) error {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
categoryId := c.Params("categoryId")
var category models.Category
defer cancel()
objId, _ := primitive.ObjectIDFromHex(categoryId)
//validate the request body
if err := c.BodyParser(&category); err != nil {
return c.Status(http.StatusBadRequest).JSON(responses.CategoryResponse{Status: http.StatusBadRequest, Message: "error", Data: &fiber.Map{"data": err.Error()}})
}
//use the validator library to validate required fields
if validationErr := validate.Struct(&category); validationErr != nil {
return c.Status(http.StatusBadRequest).JSON(responses.CategoryResponse{Status: http.StatusBadRequest, Message: "error", Data: &fiber.Map{"data": validationErr.Error()}})
}
update := bson.M{"name": category.Name}
result, err := categoryCollection.UpdateOne(ctx, bson.M{"id": objId}, bson.M{"$set": update})
if err != nil {
return c.Status(http.StatusInternalServerError).JSON(responses.CategoryResponse{Status: http.StatusInternalServerError, Message: "error", Data: &fiber.Map{"data": err.Error()}})
}
//get updated user details
var updatedCategory models.Category
if result.MatchedCount == 1 {
err := categoryCollection.FindOne(ctx, bson.M{"id": objId}).Decode(&updatedCategory)
if err != nil {
return c.Status(http.StatusInternalServerError).JSON(responses.CategoryResponse{Status: http.StatusInternalServerError, Message: "error", Data: &fiber.Map{"data": err.Error()}})
}
}
return c.Status(http.StatusOK).JSON(responses.CategoryResponse{Status: http.StatusOK, Message: "success", Data: &fiber.Map{"data": updatedCategory}})
}
func DeleteACategory(c *fiber.Ctx) error {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
categoryId := c.Params("categoryId")
defer cancel()
objId, _ := primitive.ObjectIDFromHex(categoryId)
result, err := categoryCollection.DeleteOne(ctx, bson.M{"id": objId})
if err != nil {
return c.Status(http.StatusInternalServerError).JSON(responses.CategoryResponse{Status: http.StatusInternalServerError, Message: "error", Data: &fiber.Map{"data": err.Error()}})
}
if result.DeletedCount < 1 {
return c.Status(http.StatusNotFound).JSON(
responses.CategoryResponse{Status: http.StatusNotFound, Message: "error", Data: &fiber.Map{"data": "Category with specified ID not found!"}},
)
}
return c.Status(http.StatusOK).JSON(
responses.CategoryResponse{Status: http.StatusOK, Message: "success", Data: &fiber.Map{"data": "Category successfully deleted!"}},
)
}
func GetAllCategorys(c *fiber.Ctx) error {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
var categorys []models.Category
defer cancel()
results, err := categoryCollection.Find(ctx, bson.M{})
if err != nil {
return c.Status(http.StatusInternalServerError).JSON(responses.CategoryResponse{Status: http.StatusInternalServerError, Message: "error", Data: &fiber.Map{"data": err.Error()}})
}
//reading from the db in an optimal way
defer results.Close(ctx)
for results.Next(ctx) {
var singleUser models.Category
if err = results.Decode(&singleUser); err != nil {
return c.Status(http.StatusInternalServerError).JSON(responses.CategoryResponse{Status: http.StatusInternalServerError, Message: "error", Data: &fiber.Map{"data": err.Error()}})
}
categorys = append(categorys, singleUser)
}
return c.Status(http.StatusOK).JSON(
responses.CategoryResponse{Status: http.StatusOK, Message: "success", Data: &fiber.Map{"data": categorys}},
)
}
package controllers
import (
"context"
"fiber-mongo-api/configs"
"fiber-mongo-api/models"
"fiber-mongo-api/responses"
"net/http"
"time"
"github.com/go-playground/validator/v10"
"github.com/gofiber/fiber/v2"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo"
)
var menuCollection *mongo.Collection = configs.GetCollection(configs.DB, "menus")
var validate = validator.New()
func CreateMenu(c *fiber.Ctx) error {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
var menu models.Menu
defer cancel()
//validate the request body
if err := c.BodyParser(&menu); err != nil {
return c.Status(http.StatusBadRequest).JSON(responses.MenuResponse{Status: http.StatusBadRequest, Message: "error", Data: &fiber.Map{"data": err.Error()}})
}
//use the validator library to validate required fields
if validationErr := validate.Struct(&menu); validationErr != nil {
return c.Status(http.StatusBadRequest).JSON(responses.MenuResponse{Status: http.StatusBadRequest, Message: "error", Data: &fiber.Map{"data": validationErr.Error()}})
}
newMenu := models.Menu{
Name: menu.Name,
Category: menu.Category,
Price: menu.Price,
Description: menu.Description,
Image: menu.Image,
}
result, err := menuCollection.InsertOne(ctx, newMenu)
if err != nil {
return c.Status(http.StatusInternalServerError).JSON(responses.MenuResponse{Status: http.StatusInternalServerError, Message: "error", Data: &fiber.Map{"data": err.Error()}})
}
return c.Status(http.StatusCreated).JSON(responses.MenuResponse{Status: http.StatusCreated, Message: "success", Data: &fiber.Map{"data": result}})
}
func GetAMenu(c *fiber.Ctx) error {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
menuId := c.Params("menuId")
var menu models.Menu
defer cancel()
objId, _ := primitive.ObjectIDFromHex(menuId)
err := menuCollection.FindOne(ctx, bson.M{"id": objId}).Decode(&menu)
if err != nil {
return c.Status(http.StatusInternalServerError).JSON(responses.MenuResponse{Status: http.StatusInternalServerError, Message: "error", Data: &fiber.Map{"data": err.Error()}})
}
return c.Status(http.StatusOK).JSON(responses.MenuResponse{Status: http.StatusOK, Message: "success", Data: &fiber.Map{"data": menu}})
}
func EditAMenu(c *fiber.Ctx) error {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
menuId := c.Params("menuId")
var menu models.Menu
defer cancel()
objId, _ := primitive.ObjectIDFromHex(menuId)
//validate the request body
if err := c.BodyParser(&menu); err != nil {
return c.Status(http.StatusBadRequest).JSON(responses.MenuResponse{Status: http.StatusBadRequest, Message: "error", Data: &fiber.Map{"data": err.Error()}})
}
//use the validator library to validate required fields
if validationErr := validate.Struct(&menu); validationErr != nil {
return c.Status(http.StatusBadRequest).JSON(responses.MenuResponse{Status: http.StatusBadRequest, Message: "error", Data: &fiber.Map{"data": validationErr.Error()}})
}
update := bson.M{"name": menu.Name, "price": menu.Price, "description": menu.Description}
result, err := menuCollection.UpdateOne(ctx, bson.M{"id": objId}, bson.M{"$set": update})
if err != nil {
return c.Status(http.StatusInternalServerError).JSON(responses.MenuResponse{Status: http.StatusInternalServerError, Message: "error", Data: &fiber.Map{"data": err.Error()}})
}
//get updated user details
var updatedMenu models.Menu
if result.MatchedCount == 1 {
err := menuCollection.FindOne(ctx, bson.M{"id": objId}).Decode(&updatedMenu)
if err != nil {
return c.Status(http.StatusInternalServerError).JSON(responses.MenuResponse{Status: http.StatusInternalServerError, Message: "error", Data: &fiber.Map{"data": err.Error()}})
}
}
return c.Status(http.StatusOK).JSON(responses.MenuResponse{Status: http.StatusOK, Message: "success", Data: &fiber.Map{"data": updatedMenu}})
}
func DeleteAMenu(c *fiber.Ctx) error {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
menuId := c.Params("menuId")
defer cancel()
objId, _ := primitive.ObjectIDFromHex(menuId)
result, err := menuCollection.DeleteOne(ctx, bson.M{"id": objId})
if err != nil {
return c.Status(http.StatusInternalServerError).JSON(responses.MenuResponse{Status: http.StatusInternalServerError, Message: "error", Data: &fiber.Map{"data": err.Error()}})
}
if result.DeletedCount < 1 {
return c.Status(http.StatusNotFound).JSON(
responses.MenuResponse{Status: http.StatusNotFound, Message: "error", Data: &fiber.Map{"data": "Menu with specified ID not found!"}},
)
}
return c.Status(http.StatusOK).JSON(
responses.MenuResponse{Status: http.StatusOK, Message: "success", Data: &fiber.Map{"data": "Menu successfully deleted!"}},
)
}
func GetAllMenus(c *fiber.Ctx) error {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
var menus []models.Menu
defer cancel()
results, err := menuCollection.Find(ctx, bson.M{})
if err != nil {
return c.Status(http.StatusInternalServerError).JSON(responses.MenuResponse{Status: http.StatusInternalServerError, Message: "error", Data: &fiber.Map{"data": err.Error()}})
}
//reading from the db in an optimal way
defer results.Close(ctx)
for results.Next(ctx) {
var singleUser models.Menu
if err = results.Decode(&singleUser); err != nil {
return c.Status(http.StatusInternalServerError).JSON(responses.MenuResponse{Status: http.StatusInternalServerError, Message: "error", Data: &fiber.Map{"data": err.Error()}})
}
menus = append(menus, singleUser)
}
return c.Status(http.StatusOK).JSON(
responses.MenuResponse{Status: http.StatusOK, Message: "success", Data: &fiber.Map{"data": menus}},
)
}
version: '3'
services:
server:
container_name: esarnshabu-api
build: .
ports:
- "5000:80"
depends_on:
- mongodb
mongodb:
image: mongo:latest
container_name: esarnshabu-database
ports:
- 27017:27017
environment:
MONGO_INITDB_ROOT_USERNAME: Pukky
MONGO_INITDB_ROOT_PASSWORD: xejP3257
module fiber-mongo-api
go 1.17
require (
github.com/andybalholm/brotli v1.0.4 // 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/gofiber/fiber/v2 v2.39.0 // indirect
github.com/golang/snappy v0.0.4 // indirect
github.com/joho/godotenv v1.4.0 // indirect
github.com/klauspost/compress v1.15.12 // indirect
github.com/leodido/go-urn v1.2.1 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.16 // indirect
github.com/mattn/go-runewidth v0.0.14 // indirect
github.com/montanaflynn/stats v0.6.6 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/rivo/uniseg v0.2.0 // indirect
github.com/valyala/bytebufferpool v1.0.0 // indirect
github.com/valyala/fasthttp v1.41.0 // indirect
github.com/valyala/tcplisten v1.0.0 // 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-20201027041543-1326539a0a0a // indirect
go.mongodb.org/mongo-driver v1.10.3 // indirect
golang.org/x/crypto v0.0.0-20220622213112-05595931fe9d // indirect
golang.org/x/sync v0.1.0 // indirect
golang.org/x/sys v0.1.0 // indirect
golang.org/x/text v0.4.0 // indirect
)
This diff is collapsed.
package models
type Category struct {
Name string `json:"name,omitempty" validate:"required"`
}
package models
type Menu struct {
Name string `json:"name,omitempty" validate:"required"`
Category string `json:"category,omitempty" validate:"required"`
Price string `json:"price,omitempty" validate:"required"`
Description string `json:"description,omitempty" validate:"required"`
Image string `json:"image,omitempty" validate:"required"`
}
package responses
import "github.com/gofiber/fiber/v2"
type CategoryResponse struct {
Status int `json:"status"`
Message string `json:"message"`
Data *fiber.Map `json:"data"`
}
package responses
import "github.com/gofiber/fiber/v2"
type MenuResponse struct {
Status int `json:"status"`
Message string `json:"message"`
Data *fiber.Map `json:"data"`
}
package routes
import (
"fiber-mongo-api/controllers"
"github.com/gofiber/fiber/v2"
)
func CategoryRoute(app *fiber.App) {
app.Post("/category", controllers.CreateCategory)
app.Get("/category/:categoryId", controllers.GetACategory)
app.Put("/category/:categoryId", controllers.EditACategory)
app.Delete("/category/:categoryId", controllers.DeleteACategory)
app.Get("/categorys", controllers.GetAllCategorys)
}
package routes
import (
"fiber-mongo-api/controllers"
"github.com/gofiber/fiber/v2"
)
func MenuRoute(app *fiber.App) {
app.Post("/menu", controllers.CreateMenu)
app.Get("/menu/:menuId", controllers.GetAMenu)
app.Put("/menu/:menuId", controllers.EditAMenu)
app.Delete("/menu/:menuId", controllers.DeleteAMenu)
app.Get("/menus", controllers.GetAllMenus)
}
package main
import (
"fiber-mongo-api/configs"
"fiber-mongo-api/routes" //add this
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/cors"
)
func main() {
app := fiber.New()
app.Use(cors.New(
cors.Config{
AllowOrigins: "*",
AllowHeaders: "Origin, Content-Type, Accept",
},
))
//run database
configs.ConnectDB()
//routes
routes.MenuRoute(app) //add this
routes.CategoryRoute(app)
app.Listen(":80")
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment