Commit f006c4b6 authored by Pukky's avatar Pukky

update commit

parent bae07d23
No preview for this file type
......@@ -11,5 +11,3 @@ COPY . /app
RUN go build -o main .
CMD ["/app/main"]
......@@ -57,7 +57,7 @@ func GetAMenu(c *fiber.Ctx) error {
objId, _ := primitive.ObjectIDFromHex(menuId)
err := menuCollection.FindOne(ctx, bson.M{"id": objId}).Decode(&menu)
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()}})
}
......@@ -85,7 +85,7 @@ func EditAMenu(c *fiber.Ctx) 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})
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()}})
......@@ -93,7 +93,7 @@ func EditAMenu(c *fiber.Ctx) error {
//get updated user details
var updatedMenu models.Menu
if result.MatchedCount == 1 {
err := menuCollection.FindOne(ctx, bson.M{"id": objId}).Decode(&updatedMenu)
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()}})
......@@ -110,7 +110,7 @@ func DeleteAMenu(c *fiber.Ctx) error {
objId, _ := primitive.ObjectIDFromHex(menuId)
result, err := menuCollection.DeleteOne(ctx, bson.M{"id": objId})
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()}})
}
......
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 promotionCollection *mongo.Collection = configs.GetCollection(configs.DB, "promotions")
func CreatePromotion(c *fiber.Ctx) error {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
var promotion models.Promotion
defer cancel()
//validate the request body
if err := c.BodyParser(&promotion); err != nil {
return c.Status(http.StatusBadRequest).JSON(responses.PromotionResponse{Status: http.StatusBadRequest, Message: "error", Data: &fiber.Map{"data": err.Error()}})
}
//use the validator library to validate required fields
if validationErr := validate.Struct(&promotion); validationErr != nil {
return c.Status(http.StatusBadRequest).JSON(responses.PromotionResponse{Status: http.StatusBadRequest, Message: "error", Data: &fiber.Map{"data": validationErr.Error()}})
}
newPromotion := models.Promotion{
Name: promotion.Name,
Image: promotion.Image,
}
result, err := promotionCollection.InsertOne(ctx, newPromotion)
if err != nil {
return c.Status(http.StatusInternalServerError).JSON(responses.PromotionResponse{Status: http.StatusInternalServerError, Message: "error", Data: &fiber.Map{"data": err.Error()}})
}
return c.Status(http.StatusCreated).JSON(responses.PromotionResponse{Status: http.StatusCreated, Message: "success", Data: &fiber.Map{"data": result}})
}
func GetAllPromotions(c *fiber.Ctx) error {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
var promotions []models.Promotion
defer cancel()
results, err := promotionCollection.Find(ctx, bson.M{})
if err != nil {
return c.Status(http.StatusInternalServerError).JSON(responses.PromotionResponse{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.Promotion
if err = results.Decode(&singleUser); err != nil {
return c.Status(http.StatusInternalServerError).JSON(responses.PromotionResponse{Status: http.StatusInternalServerError, Message: "error", Data: &fiber.Map{"data": err.Error()}})
}
promotions = append(promotions, singleUser)
}
return c.Status(http.StatusOK).JSON(
responses.PromotionResponse{Status: http.StatusOK, Message: "success", Data: &fiber.Map{"data": promotions}},
)
}
func DeleteAPromotion(c *fiber.Ctx) error {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
promotionId := c.Params("promotionId")
defer cancel()
objId, _ := primitive.ObjectIDFromHex(promotionId)
result, err := promotionCollection.DeleteOne(ctx, bson.M{"_id": objId})
if err != nil {
return c.Status(http.StatusInternalServerError).JSON(responses.PromotionResponse{Status: http.StatusInternalServerError, Message: "error", Data: &fiber.Map{"data": err.Error()}})
}
if result.DeletedCount < 1 {
return c.Status(http.StatusNotFound).JSON(
responses.PromotionResponse{Status: http.StatusNotFound, Message: "error", Data: &fiber.Map{"data": "Promotion with specified ID not found!"}},
)
}
return c.Status(http.StatusOK).JSON(
responses.PromotionResponse{Status: http.StatusOK, Message: "success", Data: &fiber.Map{"data": "Promotion successfully deleted!"}},
)
}
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/mongo"
)
var userCollection *mongo.Collection = configs.GetCollection(configs.DB, "users")
func Login(c *fiber.Ctx) error {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
var user models.User
defer cancel()
//validate the request body
if err := c.BodyParser(&user); err != nil {
return c.Status(http.StatusBadRequest).JSON(responses.UserResponse{Status: http.StatusBadRequest, Message: "error", Data: &fiber.Map{"data": err.Error()}})
}
//use the validator library to validate required fields
if validationErr := validate.Struct(&user); validationErr != nil {
return c.Status(http.StatusBadRequest).JSON(responses.UserResponse{Status: http.StatusBadRequest, Message: "error", Data: &fiber.Map{"data": validationErr.Error()}})
}
var user1 models.User
err := userCollection.FindOne(ctx, bson.M{"username": user.Username}).Decode(&user1)
if err != nil {
return c.Status(http.StatusUnauthorized).JSON(responses.UserResponse{Status: http.StatusBadRequest, Message: "error", Data: &fiber.Map{"data": err.Error()}})
}
if user1.Password != user.Password {
return c.Status(http.StatusUnauthorized).JSON(fiber.Map{
"error": "fali auth",
"msg": "password not found",
})
}
return c.Status(http.StatusOK).JSON(fiber.Map{
"status": true,
"msg": user.Username,
})
}
func CreateUser(c *fiber.Ctx) error {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
var user models.User
defer cancel()
//validate the request body
if err := c.BodyParser(&user); err != nil {
return c.Status(http.StatusBadRequest).JSON(responses.UserResponse{Status: http.StatusBadRequest, Message: "error", Data: &fiber.Map{"data": err.Error()}})
}
//use the validator library to validate required fields
if validationErr := validate.Struct(&user); validationErr != nil {
return c.Status(http.StatusBadRequest).JSON(responses.UserResponse{Status: http.StatusBadRequest, Message: "error", Data: &fiber.Map{"data": validationErr.Error()}})
}
newUser := models.User{
Username: user.Username,
Password: user.Password,
}
result, err := userCollection.InsertOne(ctx, newUser)
if err != nil {
return c.Status(http.StatusInternalServerError).JSON(responses.UserResponse{Status: http.StatusInternalServerError, Message: "error", Data: &fiber.Map{"data": err.Error()}})
}
return c.Status(http.StatusCreated).JSON(responses.UserResponse{Status: http.StatusCreated, Message: "success", Data: &fiber.Map{"data": result}})
}
......@@ -30,6 +30,8 @@ func main() {
//routes
routes.MenuRoute(app) //add this
routes.CategoryRoute(app)
routes.UserRoute(app)
routes.PromotionRoute(app)
app.Listen(":2000")
}
package models
import "go.mongodb.org/mongo-driver/bson/primitive"
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"`
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"`
Id primitive.ObjectID `json:"id" bson:"_id,omitempty"`
}
package models
import "go.mongodb.org/mongo-driver/bson/primitive"
type Promotion struct {
Name string `json:"name,omitempty" validate:"required"`
Image string `json:"image,omitempty" validate:"required"`
Id primitive.ObjectID `json:"id" bson:"_id,omitempty"`
}
package models
type User struct {
Username string `json:"username,omitempty" validate:"required"`
Password string `json:"password,omitempty" validate:"required"`
}
package responses
import "github.com/gofiber/fiber/v2"
type PromotionResponse struct {
Status int `json:"status"`
Message string `json:"message"`
Data *fiber.Map `json:"data"`
}
package responses
import "github.com/gofiber/fiber/v2"
type UserResponse 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 PromotionRoute(app *fiber.App) {
app.Post("/promotion", controllers.CreatePromotion)
app.Get("/promotion/:promotionId", controllers.GetAllPromotions)
app.Delete("/promotion/:promotionId", controllers.DeleteAPromotion)
app.Get("/promotions", controllers.GetAllPromotions)
}
package routes
import (
"fiber-mongo-api/controllers"
"github.com/gofiber/fiber/v2"
)
func UserRoute(app *fiber.App) {
app.Post("/login", controllers.Login)
app.Post("/regis", controllers.CreateUser)
}
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