From 2de5b87bb9de248d30b497f13decd54759014a6c Mon Sep 17 00:00:00 2001 From: Braeden57 Date: Tue, 5 Oct 2021 19:28:41 -0700 Subject: [PATCH] Update controllers --- controllers/foodController.go | 57 ++++++++++++++++ controllers/menuController.go | 117 ++++++++++++++++++++++++++++++++- database/databaseConnection.go | 2 +- main.go | 2 +- models/foodModal.go | 0 models/foodModel.go | 18 +++++ models/invoiceModal.go | 0 models/invoiceModel.go | 18 +++++ models/menuModal.go | 0 models/menuModel.go | 18 +++++ models/noteModal.go | 0 models/noteModel.go | 16 +++++ models/orderItemModal.go | 0 models/orderItemModel.go | 18 +++++ models/orderModal.go | 0 models/orderModel.go | 16 +++++ models/tableModal.go | 0 models/tableModel.go | 16 +++++ models/userModal.go | 0 models/userModel.go | 22 +++++++ 20 files changed, 317 insertions(+), 3 deletions(-) delete mode 100644 models/foodModal.go create mode 100644 models/foodModel.go delete mode 100644 models/invoiceModal.go create mode 100644 models/invoiceModel.go delete mode 100644 models/menuModal.go create mode 100644 models/menuModel.go delete mode 100644 models/noteModal.go create mode 100644 models/noteModel.go delete mode 100644 models/orderItemModal.go create mode 100644 models/orderItemModel.go delete mode 100644 models/orderModal.go create mode 100644 models/orderModel.go delete mode 100644 models/tableModal.go create mode 100644 models/tableModel.go delete mode 100644 models/userModal.go create mode 100644 models/userModel.go diff --git a/controllers/foodController.go b/controllers/foodController.go index 2a47e21..12cd043 100644 --- a/controllers/foodController.go +++ b/controllers/foodController.go @@ -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) } } diff --git a/controllers/menuController.go b/controllers/menuController.go index 8a2697e..3ef821a 100644 --- a/controllers/menuController.go +++ b/controllers/menuController.go @@ -1,29 +1,144 @@ package controller import ( + "context" + "fmt" + "log" + "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 menuCollection *mongo.Collection = database.OpenCollection(database.Client, "menu") +var validate = validator.New() + func GetMenus() gin.HandlerFunc { return func(c *gin.Context) { - + var ctx, cancel = context.WithTimeout(context.Background(), 100*time.Second) + result, err := menuCollection.Find(context.TODO(), bson.M{}) + defer cancel() + if err != nil { + c.JSON(http.StatusInternalServerError, gin.H{"error": "error occured while listing menu items"}) + } + var allMenus []bson.M + if err = result.All(ctx, &allMenus); err != nil { + log.Fatal(err) + } + c.JSON(http.StatusOK, allMenus) } } func GetMenu() gin.HandlerFunc { return func(c *gin.Context) { + var ctx, cancel = context.WithTimeout(context.Background(), 100*time.Second) + menuId := c.Param("menu_id") + var menu models.Menu + err := menuCollection.FindOne(ctx, bson.M{"food_id": menuId}).Decode(&menu) + defer cancel() + if err != nil { + c.JSON(http.StatusInternalServerError, gin.H{"error": "error occured while fetching the food item"}) + } + c.JSON(http.StatusOK, menu) } } func CreateMenu() gin.HandlerFunc { return func(c *gin.Context) { + var menu models.Menu + var ctx, cancel = context.WithTimeout(context.Background(), 100*time.Second) + if err := c.BindJSON(&menu); err != nil { + c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) + return + } + + validationErr := validate.Struct(menu) + if validationErr != nil { + c.JSON(http.StatusBadRequest, gin.H{"error": validationErr.Error()}) + return + } + + menu.Created_at, _ = time.Parse(time.RFC3339, time.Now().Format(time.RFC3339)) + menu.Updated_at, _ = time.Parse(time.RFC3339, time.Now().Format(time.RFC3339)) + menu.ID = primitive.NewObjectID() + menu.Menu_id = menu.ID.Hex() + + result, insertErr := menuCollection.InsertOne(ctx, menu) + if insertErr != nil { + msg := fmt.Sprintf("Menu item was not created") + c.JSON(http.StatusInternalServerError, gin.H{"error": msg}) + return + } + defer cancel() + c.JSON(http.StatusOK, result) } } func UpdateMenu() gin.HandlerFunc { return func(c *gin.Context) { + var ctx, cancel = context.WithTimeout(context.Background(), 100*time.Second) + var menu models.Menu + if err := c.BindJSON(&menu); err != nil { + c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) + return + } + + menuId := c.Param("menu_id") + filter := bsom.M{"menu_id": menuId} + + var updateObj primitive.D + + if menu.Start_Date != nil && menu.End_Date != nil { + if !inTimeSpan(*menu.Start_Date, *menu.End_Date, time.Now()) { + msg := "kindly retype the time" + c.JSON(http.StatusInternalServerError, gin.H{"error": msg}) + defer cancel() + return + } + + updateObj = append(updateObj, bson.E{"start_date", menu.Start_Date}) + updateObj = append(updateObj, bson.E{"end_date", menu.End_Date}) + + if menu.Name != "" { + updateObj = append(updateObj, bson.E{"name", menu.Name}) + } + if menu.Category != "" { + updateObj = append(updateObj, bson.E{"category", menu.Category}) + } + + menu.Updated_at, _ = time.Parse(time.RFC3339, time.Now().Format(time.RFC3339)) + updateObj = append(updateObj, bson.E{"updated_at", menu.Updated_at}) + + upsert:=true + + opt := options.UpdateOptions{ + Upsert: &upsert, + } + + result, err := menuCollection.UpdateOne( + ctx, + filter, + bson.D{ + {"$set", updatedObj} + }, + &opt, + ) + if err!=nil { + msg := "menu update failed" + c.JSON(http.StatusInternalServerError, gin.H{"error": msg}) + } + + defer cancel() + c.JSON(http.StatusOK, result) + } } } diff --git a/database/databaseConnection.go b/database/databaseConnection.go index 6bbfe30..966eb4a 100644 --- a/database/databaseConnection.go +++ b/database/databaseConnection.go @@ -1,4 +1,4 @@ -package databse +package database import ( "context" diff --git a/main.go b/main.go index 7fae6c3..4813f3f 100644 --- a/main.go +++ b/main.go @@ -10,7 +10,7 @@ import ( "go.mongodb.org/mongo-driver/mongo" ) -var foodCollection *mongo.Collection = database.OpenCollection(databse.Client, "food") +var foodCollection *mongo.Collection = database.OpenCollection(database.Client, "food") func main() { port := os.Getenv("PORT") diff --git a/models/foodModal.go b/models/foodModal.go deleted file mode 100644 index e69de29..0000000 diff --git a/models/foodModel.go b/models/foodModel.go new file mode 100644 index 0000000..877c268 --- /dev/null +++ b/models/foodModel.go @@ -0,0 +1,18 @@ +package models + +import ( + "time" + + "go.mongo.org/mongo-driver/bson/primitive" +) + +type Food struct { + ID primitive.ObjectID `bson:"_id"` + Name *string `json:"name" validate:"required,min=2,max=100"` + Price *float64 `json:"prive" validate:"required"` + Food_image *string `json:"food_image" validate:"required"` + Created_at time.Time `json:"created_at"` + Updated_at time.Time `json:"updated_at"` + Food_id string `json:"food_id"` + Menu_id *string `json:"menu_id" validate:"required"` +} diff --git a/models/invoiceModal.go b/models/invoiceModal.go deleted file mode 100644 index e69de29..0000000 diff --git a/models/invoiceModel.go b/models/invoiceModel.go new file mode 100644 index 0000000..cc2da88 --- /dev/null +++ b/models/invoiceModel.go @@ -0,0 +1,18 @@ +package models + +import ( + "time" + + "go.mongo.org/mongo-driver/bson/primitive" +) + +type Invoice struct { + ID primitive.ObjectID `bson:"_id"` + Invoice_id string `json:"invoice_id"` + Order_id string `json:"order_id"` + Payment_method *string `json:"payment_method" validate:"eq=CARD|eq=CASH|eq=` + Payment_status *string `json:"payment_status" validate:"required,eq=PENDING|eq=PAID` + Payment_due_date time.Time `json:"payment_due_Date"` + Created_at time.Time `json:"created_at"` + Updated_at time.Time `json:"updated_at"` +} diff --git a/models/menuModal.go b/models/menuModal.go deleted file mode 100644 index e69de29..0000000 diff --git a/models/menuModel.go b/models/menuModel.go new file mode 100644 index 0000000..6fb4db5 --- /dev/null +++ b/models/menuModel.go @@ -0,0 +1,18 @@ +package models + +import ( + "time" + + "go.mongo.org/mongo-driver/bson/primitive" +) + +type Menu struct { + ID primitive.ObjectID `bson:"_id"` + Name string `json:"name" validate:"required"` + Category string `json:"category" validate:"required"` + Start_Date *time.Time `json:"start_date"` + End_Date *time.Time `json:"end_date"` + Created_at time.Time `json:"created_at"` + Updated_at time.Time `json:"updated_at"` + Menu_id string `json:"food_id"` +} diff --git a/models/noteModal.go b/models/noteModal.go deleted file mode 100644 index e69de29..0000000 diff --git a/models/noteModel.go b/models/noteModel.go new file mode 100644 index 0000000..1351876 --- /dev/null +++ b/models/noteModel.go @@ -0,0 +1,16 @@ +package models + +import ( + "time" + + "go.mongodb.org/mongo-driver/bson/primitave" +) + +type Note struct { + ID primitave.ObjectID `bson:"_id"` + Text string `json:"text"` + Title string `json:"title"` + Created_at time.Time `json:"created_at"` + Updated_at time.Time `json:"updated_at"` + Note_id string `json:"note_id"` +} diff --git a/models/orderItemModal.go b/models/orderItemModal.go deleted file mode 100644 index e69de29..0000000 diff --git a/models/orderItemModel.go b/models/orderItemModel.go new file mode 100644 index 0000000..9e57e31 --- /dev/null +++ b/models/orderItemModel.go @@ -0,0 +1,18 @@ +package models + +import ( + "time" + + "go.mongodb.org/mongo-driver/bson/primitave" +) + +type OrderItem struct { + ID primitave.ObjectID `bson:"_id"` + Quantity *string `json:"quantity" validate:"required,eq=S|eq=M|eq=L"` + Unit_price *float64 `json:"unit_price" validate:"required"` + Created_at time.Time `json:"created_at"` + Updated_at time.Time `json:"updated_at"` + Food_id *string `json:"food_id" validate:"required"` + Order_item_id string `json:"order_item_id" required:"required"` + Order_id string `json:"order_id" required:"required"` +} diff --git a/models/orderModal.go b/models/orderModal.go deleted file mode 100644 index e69de29..0000000 diff --git a/models/orderModel.go b/models/orderModel.go new file mode 100644 index 0000000..86ed1e3 --- /dev/null +++ b/models/orderModel.go @@ -0,0 +1,16 @@ +package models + +import ( + "time" + + "go.mongodb.org/mongo-driver/bson/primitave" +) + +type Order struct { + ID primitave.ObjectID `bson:"_id"` + Order_Date time.Time `json"order_date" validate:"required"` + Created_at time.Time `json:"created_at"` + Updated_at time.Time `json:"updated_at"` + Order_id string `json:"order_id" validate:"required"` + Table_id *string `json:"table_id" validate:"requried"` +} diff --git a/models/tableModal.go b/models/tableModal.go deleted file mode 100644 index e69de29..0000000 diff --git a/models/tableModel.go b/models/tableModel.go new file mode 100644 index 0000000..a975714 --- /dev/null +++ b/models/tableModel.go @@ -0,0 +1,16 @@ +package models + +import ( + "time" + + "go.mongodb.org/mongo-driver/bson/primitive" +) + +type Table struct { + ID primitive.ObjectID `bson:"_id"` + Number_of_guests *int `json:"number_of_guests" validate:"required"` + Table_number *int `json:"table_number" validate:"required"` + Created_at time.Time `json:"created_at"` + Updated_at time.Time `json:"updated_at"` + Table_id string `json:"table_id"` +} diff --git a/models/userModal.go b/models/userModal.go deleted file mode 100644 index e69de29..0000000 diff --git a/models/userModel.go b/models/userModel.go new file mode 100644 index 0000000..49c8bf4 --- /dev/null +++ b/models/userModel.go @@ -0,0 +1,22 @@ +package models + +import ( + "time" + + "go.mongodb.org/mongo-driver/bson/primitave" +) + +type User struct { + ID primitave.ObjectID `bson:"_id"` + First_name *string `json:"first_name" validate:"required,min=2,max=100"` + Last_name *string `json:"last_name" validate:"required,min=2,max=100"` + Password *string `json:"Password" validate:"required,min=8` + Email *string `json:"email" validate:"email,required"` + Avatar *string `json:"avatar"` + Phone *string `json:"phone" validate:"required"` + Token *string `json:"token"` + Refresh_token *string `json:"refresh_token"` + Created_at time.Time `json:"created_at"` + Updated_at time.Time `json:"updated_at"` + User_id string `json:"user_id"` +}