Update controllers
This commit is contained in:
1 parent
338f508460
commit
2de5b87bb9
20 files changed
+317
-3
No files matched your search
@@ -1,9 +1,24 @@
|
||||
package controller
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"restaurant-management/database"
|
||||
"restaurant-management/models"
|
||||
"time"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/go-playground/validator/v10"
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||
"go.mongodb.org/mongo-driver/mongo"
|
||||
)
|
||||
|
||||
var foodCollection *mongo.Collection = database.OpenCollection(database.Client, "food")
|
||||
var menuCollection *mongo.Collection = database.OpenCollection(database.Client, "menu")
|
||||
var validate = validator.New()
|
||||
|
||||
func GetFoods() gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
|
||||
@@ -12,13 +27,55 @@ func GetFoods() gin.HandlerFunc {
|
||||
|
||||
func GetFood() gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
var ctx, cancel = context.WithTimeout(context.Background(), 100*time.Second)
|
||||
foodId := c.Param("food_id")
|
||||
var food models.Food
|
||||
|
||||
err := foodCollection.FindOne(ctx, bson.M{"food_id": foodId}).Decode(&food)
|
||||
defer cancel()
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "error occured while fetching the food item"})
|
||||
}
|
||||
c.JSON(http.StatusOK, food)
|
||||
}
|
||||
}
|
||||
|
||||
func CreateFood() gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
var ctx, cancel = context.WithTimeout(context.Background(), 100*time.Second)
|
||||
var menu models.Menu
|
||||
var food models.Food
|
||||
|
||||
if err := c.BindJSON(&food); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
}
|
||||
|
||||
validationErr := validate.Struct(food)
|
||||
if validationErr != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": validationErr.Error()})
|
||||
}
|
||||
err := menuCollection.FindOne(ctx, bson.M{"menu_id": food.Menu_id})
|
||||
defer cancel()
|
||||
if err != nil {
|
||||
msg := fmt.Sprintf("menu was not found")
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": msg})
|
||||
return
|
||||
}
|
||||
food.Created_at, _ = time.Parse(time.RFC3339, time.Now().Format(time.RFC3339))
|
||||
food.Updated_at, _ = time.Parse(time.RFC3339, time.Now().Format(time.RFC3339))
|
||||
food.ID = primitive.NewObjectID()
|
||||
food.Food_id = food.ID.Hex()
|
||||
var num = toFixed(*food.Price, 2)
|
||||
food.Price = &num
|
||||
|
||||
result, insertErr := foodCollection.InsertOne(ctx, food)
|
||||
if insertErr != nil {
|
||||
msg := fmt.Sprintf("food item was not created")
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": msg})
|
||||
return
|
||||
}
|
||||
defer cancel()
|
||||
c.JSON(http.StatusOK, result)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user