public ActionResult UpdateCoupon([FromBody] UpdateCoupon data, string code)
        {
            try
            {
                var checkData = MH.CheckForDatas("Code", code, null, null, "CouponDB", "Coupon");
                if (checkData != null)
                {
                    var result = BsonSerializer.Deserialize <Coupon>(checkData);
                    var filter = Builders <BsonDocument> .Filter.Eq("Code", code);

                    if (data.ApplicableFor != null)
                    {
                        var update = MH.UpdateSingleObject(filter, "CouponDB", "Coupon", Builders <BsonDocument> .Update.Set("ApplicableFor", data.ApplicableFor));
                    }
                    if (data.ExpiryTime != null)
                    {
                        var update = MH.UpdateSingleObject(filter, "CouponDB", "Coupon", Builders <BsonDocument> .Update.Set("ExpiryTime", data.ExpiryTime));
                    }
                    if (data.Value > 0)
                    {
                        var update = MH.UpdateSingleObject(filter, "CouponDB", "Coupon", Builders <BsonDocument> .Update.Set("Value", data.Value));
                    }
                    if (data.Percentage != null)
                    {
                        var updateResult = MH.UpdateSingleObject(filter, "CouponDB", "Coupon", Builders <BsonDocument> .Update.Set("Percentage", data.Percentage));
                    }
                    if (data.UsageCount > 0)
                    {
                        var update = MH.UpdateSingleObject(filter, "CouponDB", "Coupon", Builders <BsonDocument> .Update.Set("UsageCount", data.UsageCount));
                    }
                    return(Ok(new ResponseData
                    {
                        Code = "200",
                        Message = "Coupon updated successfully",
                        Data = null
                    }));
                }
                else
                {
                    return(BadRequest(new ResponseData
                    {
                        Code = "404",
                        Message = "Coupon not found",
                        Data = null
                    }));
                }
            }
            catch (Exception ex)
            {
                LoggerDataAccess.CreateLog("CouponController", "UpdateCoupon", ex.Message);
                return(BadRequest(new ResponseData
                {
                    Code = "400",
                    Message = "Failed",
                    Data = ex.Message
                }));
            }
        }
        public async Task <ActionResult> UpdateCategory([FromBody] Category data, string productFor, string productType)
        {
            try
            {
                var checkData = MH.CheckForDatas("ProductFor", productFor, "ProductType", productType, "ProductDB", "Category");
                if (checkData != null)
                {
                    var objectId = BsonSerializer.Deserialize <Category>(checkData).Id;
                    var filter   = Builders <BsonDocument> .Filter.Eq("_id", objectId);

                    if (data.ProductFor != null)
                    {
                        var value      = BsonSerializer.Deserialize <Category>(MH.CheckForDatas("_id", objectId, null, null, "ProductDB", "Category")).ProductType;
                        var objectName = data.ProductFor + "-" + value;
                        GH.UpdateCategoryDetails(BsonSerializer.Deserialize <Category>(checkData).Id, productFor, productType, data.ProductFor, "ProductFor", objectName);
                    }
                    if (data.ProductType != null)
                    {
                        var value      = BsonSerializer.Deserialize <Category>(MH.CheckForDatas("_id", objectId, null, null, "ProductDB", "Category")).ProductFor;
                        var objectName = value + "-" + data.ProductType;
                        GH.UpdateCategoryDetails(BsonSerializer.Deserialize <Category>(checkData).Id, productFor, productType, data.ProductType, "ProductType", objectName);
                    }
                    if (data.Description != null)
                    {
                        var update = await MH.UpdateSingleObject(filter, "ProductDB", "Category", Builders <BsonDocument> .Update.Set("Description", data.Description));
                    }
                    return(Ok(new ResponseData
                    {
                        Code = "200",
                        Message = "Updated",
                        Data = null
                    }));
                }
                else
                {
                    return(BadRequest(new ResponseData
                    {
                        Code = "404",
                        Message = "Category not found",
                        Data = null
                    }));
                }
            }
            catch (Exception ex)
            {
                LoggerDataAccess.CreateLog("CategoryController", "UpdateCategory", "UpdateCategory", ex.Message);
                return(BadRequest(new ResponseData
                {
                    Code = "400",
                    Message = "Failed",
                    Data = ex.Message
                }));
            }
        }
Beispiel #3
0
        public async Task <ActionResult> InsertCoupon([FromBody] Coupon data)
        {
            try
            {
                var checkData = MH.CheckForDatas("Code", data.Code, null, null, "CouponDB", "Coupon");
                if (checkData == null)
                {
                    var collection = _db.GetCollection <Coupon>("Coupon");
                    await collection.InsertOneAsync(data);

                    return(Ok(new ResponseData
                    {
                        Code = "200",
                        Message = "Coupon inserted successfully",
                        Data = null
                    }));
                }
                else
                {
                    return(BadRequest(new ResponseData
                    {
                        Code = "401",
                        Message = "Coupon already added",
                        Data = null
                    }));
                }
            }
            catch (Exception ex)
            {
                LoggerDataAccess.CreateLog("CouponController", "InsertCoupon", "InsertCoupon", ex.Message);
                return(BadRequest(new ResponseData
                {
                    Code = "400",
                    Message = "Failed",
                    Data = ex.Message
                }));
            }
        }
 public ActionResult DeleteCoupon(string code)
 {
     try
     {
         var checkData = MH.CheckForDatas("Code", code, null, null, "CouponDB", "Coupon");
         if (checkData != null)
         {
             var delete = MH.DeleteSingleObject(Builders <BsonDocument> .Filter.Eq("Code", code), "CouponDB", "Coupon");
             return(Ok(new ResponseData
             {
                 Code = "200",
                 Message = "Coupon deleted successfully",
                 Data = null
             }));
         }
         else
         {
             return(BadRequest(new ResponseData
             {
                 Code = "404",
                 Message = "Coupon not found",
                 Data = null
             }));
         }
     }
     catch (Exception ex)
     {
         LoggerDataAccess.CreateLog("CouponController", "UpdateCoupon", ex.Message);
         return(BadRequest(new ResponseData
         {
             Code = "400",
             Message = "Failed",
             Data = ex.Message
         }));
     }
 }
        public async Task <ActionResult> UpdateProduct([FromBody] Product data, string productSKU)
        {
            try
            {
                var checkData = MH.CheckForDatas("ProductSKU", productSKU, null, null, "ProductDB", "Product");
                if (checkData != null)
                {
                    var objectId = BsonSerializer.Deserialize <Product>(checkData).Id;
                    var filter   = Builders <BsonDocument> .Filter.Eq("_id", objectId);

                    if (data.ProductFor != null)
                    {
                        productSKU = BsonSerializer.Deserialize <Product>(MH.CheckForDatas("_id", objectId, null, null, "ProductDB", "Product")).ProductSKU;
                        var objectName = data.ProductFor + "-" + productSKU.Split('-')[1] + "-" + productSKU.Split('-')[2] + "-" + productSKU.Split('-')[3] + "-" + productSKU.Split('-')[4];
                        GH.UpdateProductDetails(BsonSerializer.Deserialize <Product>(checkData).Id, productSKU, data.ProductFor, "ProductFor", objectName);
                    }
                    if (data.ProductType != null)
                    {
                        productSKU = BsonSerializer.Deserialize <Product>(MH.CheckForDatas("_id", objectId, null, null, "ProductDB", "Product")).ProductSKU;
                        var objectName = productSKU.Split('-')[0] + "-" + data.ProductType + "-" + productSKU.Split('-')[2] + "-" + productSKU.Split('-')[3] + "-" + productSKU.Split('-')[4];
                        GH.UpdateProductDetails(BsonSerializer.Deserialize <Product>(checkData).Id, productSKU, data.ProductType, "ProductType", objectName);
                    }
                    if (data.ProductDesign != null)
                    {
                        productSKU = BsonSerializer.Deserialize <Product>(MH.CheckForDatas("_id", objectId, null, null, "ProductDB", "Product")).ProductSKU;
                        var objectName = productSKU.Split('-')[0] + "-" + productSKU.Split('-')[1] + "-" + data.ProductDesign + "-" + productSKU.Split('-')[3] + "-" + productSKU.Split('-')[4];
                        GH.UpdateProductDetails(BsonSerializer.Deserialize <Product>(checkData).Id, productSKU, data.ProductDesign, "ProductDesign", objectName);
                    }
                    if (data.ProductBrand != null)
                    {
                        var update = await MH.UpdateSingleObject(filter, "ProductDB", "Product", Builders <BsonDocument> .Update.Set("ProductBrand", data.ProductBrand));
                    }
                    if (data.ProductPrice > 0)
                    {
                        var update = await MH.UpdateSingleObject(filter, "ProductDB", "Product", Builders <BsonDocument> .Update.Set("ProductPrice", data.ProductPrice));

                        double discountPercentage;
                        if (data.ProductDiscount > 0)
                        {
                            discountPercentage = data.ProductDiscount;
                        }
                        else
                        {
                            discountPercentage = BsonSerializer.Deserialize <Product>(MH.CheckForDatas("ProductSKU", productSKU, null, null, "ProductDB", "Product")).ProductDiscount;
                        }
                        var discountPrice = (data.ProductPrice - (data.ProductPrice * (discountPercentage / 100)));


                        var update1 = await MH.UpdateSingleObject(filter, "ProductDB", "Product", Builders <BsonDocument> .Update.Set("ProductDiscountPrice", discountPrice));
                    }
                    if (data.ProductDiscount > 0)
                    {
                        var update = await MH.UpdateSingleObject(filter, "ProductDB", "Product", Builders <BsonDocument> .Update.Set("ProductDiscount", data.ProductDiscount));

                        double price;
                        if (data.ProductPrice > 0)
                        {
                            price = data.ProductPrice;
                        }
                        else
                        {
                            price = BsonSerializer.Deserialize <Product>(MH.CheckForDatas("ProductSKU", productSKU, null, null, "ProductDB", "Product")).ProductPrice;
                        }
                        var discountPrice = (price - (price * (data.ProductDiscount / 100)));
                        var update1       = await MH.UpdateSingleObject(filter, "ProductDB", "Product", Builders <BsonDocument> .Update.Set("ProductDiscountPrice", discountPrice));
                    }
                    if (data.ProductStock > 0)
                    {
                        var update = await MH.UpdateSingleObject(filter, "ProductDB", "Product", Builders <BsonDocument> .Update.Set("ProductStock", data.ProductStock));
                    }
                    if (data.ProductSize != null)
                    {
                        productSKU = BsonSerializer.Deserialize <Product>(MH.CheckForDatas("_id", objectId, null, null, "ProductDB", "Product")).ProductSKU;
                        var objectName = productSKU.Split('-')[0] + "-" + productSKU.Split('-')[1] + "-" + productSKU.Split('-')[2] + "-" + productSKU.Split('-')[3] + "-" + data.ProductSize;
                        GH.UpdateProductDetails(BsonSerializer.Deserialize <Product>(checkData).Id, productSKU, data.ProductSize, "ProductSize", objectName);
                    }
                    if (data.ProductMaterial != null)
                    {
                        var update = await MH.UpdateSingleObject(filter, "ProductDB", "Product", Builders <BsonDocument> .Update.Set("ProductMaterial", data.ProductMaterial));
                    }
                    if (data.ProductRating > 0)
                    {
                        var update = await MH.UpdateSingleObject(filter, "ProductDB", "Product", Builders <BsonDocument> .Update.Set("ProductRating", data.ProductRating));
                    }
                    if (data.ProductReviews != null)
                    {
                        var update = await MH.UpdateSingleObject(filter, "ProductDB", "Product", Builders <BsonDocument> .Update.Set("ProductReviews", data.ProductReviews));
                    }
                    if (data.ProductColour != null)
                    {
                        productSKU = BsonSerializer.Deserialize <Product>(MH.CheckForDatas("_id", objectId, null, null, "ProductDB", "Product")).ProductSKU;
                        var objectName = productSKU.Split('-')[0] + "-" + productSKU.Split('-')[1] + "-" + productSKU.Split('-')[2] + "-" + data.ProductColour + "-" + productSKU.Split('-')[4];
                        GH.UpdateProductDetails(BsonSerializer.Deserialize <Product>(checkData).Id, productSKU, data.ProductColour, "ProductColour", objectName);
                    }
                    if (data.RefundApplicable != null)
                    {
                        var update = await MH.UpdateSingleObject(filter, "ProductDB", "Product", Builders <BsonDocument> .Update.Set("RefundApplicable", data.RefundApplicable));
                    }
                    if (data.ReplacementApplicable != null)
                    {
                        var update = await MH.UpdateSingleObject(filter, "ProductDB", "Product", Builders <BsonDocument> .Update.Set("ReplacementApplicable", data.ReplacementApplicable));
                    }
                    if (data.ProductDescription != null)
                    {
                        var update = await MH.UpdateSingleObject(filter, "ProductDB", "Product", Builders <BsonDocument> .Update.Set("ProductDescription", data.ProductDescription));
                    }
                    return(Ok(new ResponseData
                    {
                        Code = "200",
                        Message = "Updated",
                        Data = null
                    }));
                }
                else
                {
                    return(BadRequest(new ResponseData
                    {
                        Code = "404",
                        Message = "Product not found",
                        Data = null
                    }));
                }
            }
            catch (Exception ex)
            {
                LoggerDataAccess.CreateLog("ProductController", "UpdateProduct", "UpdateProduct", ex.Message);
                return(BadRequest(new ResponseData
                {
                    Code = "400",
                    Message = "Failed",
                    Data = ex.Message
                }));
            }
        }
Beispiel #6
0
        public async Task <ActionResult> UpdateReview([FromBody] UpdateReview data, string productSKU)
        {
            try
            {
                var checkData = MH.CheckForDatas("ProductSKU", productSKU, null, null, "ProductDB", "Product");
                if (checkData != null)
                {
                    var objectId = BsonSerializer.Deserialize <Product>(checkData).Id;
                    var filter   = Builders <BsonDocument> .Filter.Eq("_id", objectId);

                    var           reviews     = BsonSerializer.Deserialize <Product>(MH.CheckForDatas("_id", objectId, null, null, "ProductDB", "Product")).ProductReviews;
                    List <Review> reviewList  = new List <Review>();
                    Review[]      reviewArray = new Review[reviews.Length];
                    int           i           = 0;
                    if (reviews.Length == 0)
                    {
                        return(BadRequest(new ResponseData
                        {
                            Code = "401",
                            Message = "No reviews found"
                        }));
                    }
                    else
                    {
                        foreach (var review in reviews)
                        {
                            if (review.Id == data.Id)
                            {
                                review.Approved = data.Approved;
                            }
                            reviewArray[i] = review;
                            i++;
                            reviewList.Add(review);
                        }
                    }
                    var result = reviewList.FindAll(x => x.Id == data.Id);
                    if (result.Count == 0)
                    {
                        return(BadRequest(new ResponseData
                        {
                            Code = "402",
                            Message = "No reviews with id '" + data.Id + "' is found"
                        }));
                    }
                    var updateReview = await MH.UpdateSingleObject(filter, "ProductDB", "Product", Builders <BsonDocument> .Update.Set("ProductReviews", reviewArray));

                    var    updatedReviews  = BsonSerializer.Deserialize <Product>(MH.CheckForDatas("_id", objectId, null, null, "ProductDB", "Product")).ProductReviews;
                    double numberOfReviews = 0;
                    double totalRating     = 0;
                    foreach (var review in updatedReviews)
                    {
                        if (review.Approved == true)
                        {
                            numberOfReviews += 1;
                            totalRating     += review.Rating;
                        }
                    }
                    double overallRating;
                    if (totalRating > 0)
                    {
                        overallRating = totalRating / numberOfReviews;
                    }
                    else
                    {
                        overallRating = 0;
                    }
                    var updateRating = await MH.UpdateSingleObject(filter, "ProductDB", "Product", Builders <BsonDocument> .Update.Set("ProductRating", overallRating));

                    return(Ok(new ResponseData
                    {
                        Code = "200",
                        Message = "Updated"
                    }));
                }
                else
                {
                    return(BadRequest(new ResponseData
                    {
                        Code = "404",
                        Message = "Product not found"
                    }));
                }
            }
            catch (Exception ex)
            {
                LoggerDataAccess.CreateLog("ProductController", "InsertReview", ex.Message);
                return(BadRequest(new ResponseData
                {
                    Code = "400",
                    Message = "Failed",
                    Data = ex.Message
                }));
            }
        }
Beispiel #7
0
        public async Task <ActionResult> InsertReview([FromBody] Review data, string productSKU)
        {
            try
            {
                var checkData = MH.CheckForDatas("ProductSKU", productSKU, null, null, "ProductDB", "Product");
                if (checkData != null)
                {
                    var objectId = BsonSerializer.Deserialize <Product>(checkData).Id;
                    if (data != null)
                    {
                        var      reviews     = BsonSerializer.Deserialize <Product>(MH.CheckForDatas("_id", objectId, null, null, "ProductDB", "Product")).ProductReviews;
                        Review[] reviewArray = new Review[reviews.Length + 1];
                        data.Id       = reviews.Length + 1;
                        data.Approved = false;
                        int i = 0;
                        if (reviews.Length != 0)
                        {
                            foreach (var review in reviews)
                            {
                                reviewArray[i] = review;
                                i++;
                            }
                        }
                        reviewArray[i] = data;
                        var updateReview = await MH.UpdateSingleObject(Builders <BsonDocument> .Filter.Eq("_id", objectId), "ProductDB", "Product", Builders <BsonDocument> .Update.Set("ProductReviews", reviewArray));

                        var checkOrder = MH.CheckForDatas("OrderId", data.OrderId, null, null, "OrderDB", "OrderInfo");
                        if (checkOrder == null)
                        {
                            return(BadRequest(new ResponseData
                            {
                                Code = "402",
                                Message = "No orders with id '" + data.OrderId + "' is found",
                                Data = null
                            }));
                        }
                        var orderInfo = BsonSerializer.Deserialize <OrderInfo>(checkOrder);
                        List <ProductDetails> productDetails = new List <ProductDetails>();
                        foreach (var product in orderInfo.ProductDetails)
                        {
                            if (product.ProductSKU == productSKU)
                            {
                                product.Reviewed = true;
                            }
                            productDetails.Add(product);
                        }
                        var updateOrderInfo = await MH.UpdateSingleObject(Builders <BsonDocument> .Filter.Eq("OrderId", data.OrderId), "OrderDB", "OrderInfo", Builders <BsonDocument> .Update.Set("ProductDetails", productDetails));

                        return(Ok(new ResponseData
                        {
                            Code = "200",
                            Message = "Inserted",
                            Data = null
                        }));
                    }
                    else
                    {
                        return(BadRequest(new ResponseData
                        {
                            Code = "401",
                            Message = "Review data from body is empty",
                            Data = null
                        }));
                    }
                }
                else
                {
                    return(BadRequest(new ResponseData
                    {
                        Code = "404",
                        Message = "Product not found",
                        Data = null
                    }));
                }
            }
            catch (Exception ex)
            {
                LoggerDataAccess.CreateLog("ProductController", "InsertReview", ex.Message);
                return(BadRequest(new ResponseData
                {
                    Code = "400",
                    Message = "Failed",
                    Data = ex.Message
                }));
            }
        }
        public ActionResult UseCoupon([FromBody] UseCoupon data, string code)
        {
            try
            {
                var checkData = MH.CheckForDatas("Code", code, null, null, "CouponDB", "Coupon");
                if (checkData != null)
                {
                    var result = BsonSerializer.Deserialize <Coupon>(checkData);
                    var filter = Builders <BsonDocument> .Filter.Eq("Code", code);

                    if (data.UsageCount > 0)
                    {
                        var coupon = BsonSerializer.Deserialize <Coupon>(MH.CheckForDatas("Code", code, null, null, "CouponDB", "Coupon"));
                        if (result.Percentage == true)
                        {
                            var update = MH.UpdateSingleObject(filter, "CouponDB", "Coupon", Builders <BsonDocument> .Update.Set("UsageCount", result.UsageCount - data.UsageCount));
                        }
                        else
                        {
                            if (data.Amount > coupon.Value)
                            {
                                return(Ok(new ResponseData
                                {
                                    Code = "401",
                                    Message = "Amount is higher than the coupon value",
                                    Data = null
                                }));
                            }
                            else
                            {
                                var balance = coupon.Value - data.Amount;
                                var update  = MH.UpdateSingleObject(filter, "CouponDB", "Coupon", Builders <BsonDocument> .Update.Set("Value", balance));
                                if (balance == 0)
                                {
                                    var updateUsageCount = MH.UpdateSingleObject(filter, "CouponDB", "Coupon", Builders <BsonDocument> .Update.Set("UsageCount", 0));
                                }
                            }
                        }
                    }
                    return(Ok(new ResponseData
                    {
                        Code = "200",
                        Message = "Coupon updated successfully",
                        Data = null
                    }));
                }
                else
                {
                    return(BadRequest(new ResponseData
                    {
                        Code = "404",
                        Message = "Coupon not found",
                        Data = null
                    }));
                }
            }
            catch (Exception ex)
            {
                LoggerDataAccess.CreateLog("CouponController", "UseCoupon", ex.Message);
                return(BadRequest(new ResponseData
                {
                    Code = "400",
                    Message = "Failed",
                    Data = ex.Message
                }));
            }
        }
 public ActionResult CheckCoupon(string username, string code)
 {
     try
     {
         var checkData = MH.CheckForDatas("Code", code, null, null, "CouponDB", "Coupon");
         if (checkData != null)
         {
             var data = BsonSerializer.Deserialize <Coupon>(checkData);
             if (data.ExpiryTime > DateTime.UtcNow)
             {
                 if (data.ApplicableFor == "All" || data.ApplicableFor == username)
                 {
                     if (data.UsageCount != 0)
                     {
                         return(Ok(new ResponseData
                         {
                             Code = "200",
                             Message = "Coupon is valid",
                             Data = new CouponContent {
                                 Value = data.Value, Percentage = data.Percentage
                             }
                         }));
                     }
                     else
                     {
                         return(BadRequest(new ResponseData
                         {
                             Code = "403",
                             Message = "Coupon usage limit exceeded",
                             Data = null
                         }));
                     }
                 }
                 else
                 {
                     return(BadRequest(new ResponseData
                     {
                         Code = "402",
                         Message = "Coupon invalid",
                         Data = null
                     }));
                 }
             }
             else
             {
                 return(BadRequest(new ResponseData
                 {
                     Code = "401",
                     Message = "Coupon expired",
                     Data = null
                 }));
             }
         }
         else
         {
             return(BadRequest(new ResponseData
             {
                 Code = "404",
                 Message = "Coupon not found",
                 Data = null
             }));
         }
     }
     catch (Exception ex)
     {
         LoggerDataAccess.CreateLog("CouponController", "CheckCoupon", ex.Message);
         return(BadRequest(new ResponseData
         {
             Code = "400",
             Message = "Failed",
             Data = ex.Message
         }));
     }
 }