Пример #1
0
        /// <summary>
        /// Adds product to basket. This results in creating basket items in the database.
        /// </summary>
        /// <param name="basketId">basket id</param>
        /// <param name="productId">product id</param>
        /// <param name="quantity">number of products to be added</param>
        /// <returns>result information</returns>
        public Result AddProduct(Guid basketId, Guid productId, int quantity)
        {
            try
            {
                if (quantity <= 0)
                {
                    return(Result.Error("Provided parameter quantity must be bigger then 0."));
                }

                using (var context = new EntityModels.ServiceBusDatabaseEntities())
                {
                    var basket = context.Baskets.FirstOrDefault(p => p.Id == basketId);
                    if (basket == null)
                    {
                        return(Result.Error("Basket was not found. Please make sure basket ID is valid"));
                    }

                    var product = context.Products.FirstOrDefault(p => p.Id == productId);
                    if (product == null)
                    {
                        return(Result.Error("Product was not found. Please make sure product ID is valid"));
                    }

                    var basketItem = basket.BasketItems.FirstOrDefault(p => p.ProductId == productId);

                    if (basketItem == null)
                    {
                        context.BasketItems.Add(new EntityModels.BasketItem()
                        {
                            ProductId = productId,
                            BasketId  = basketId,
                            Quantity  = quantity,
                            Id        = Guid.NewGuid()
                        });

                        context.SaveChanges();

                        return(Result.SuccessFormat("Product has been added to basket"));
                    }
                    else  //Product exists in the basket - update BasketItems
                    {
                        basketItem.Quantity = basketItem.Quantity + quantity;
                        context.SaveChanges();

                        return(Result.SuccessFormat("Product has been increes"));
                    }
                }
            }
            catch (Exception exception)
            {
                return(Result.FatalFormat("BasketService.AddProduct Exception: {0}", exception.Message));
            }
        }
Пример #2
0
        /// <summary>
        /// This method adds product into datasource
        /// </summary>
        /// <param name="name">Name of a new product</param>
        /// <param name="price">Price of a new product</param>
        /// <param name="guid">ID of a new product</param>
        /// <returns>Result object</returns>
        public Result AddProduct(string name, double price, Guid guid)
        {
            try
            {
                if ((!string.IsNullOrWhiteSpace(name) && name.Length <= 50) && price >= 0)
                {
                    using (var context = new EntityModels.ServiceBusDatabaseEntities())
                    {
                        context.Products.Add(new EntityModels.Product()
                        {
                            Id = guid, Name = name, Price = price, ProductType = context.ProductTypes.First()
                        });

                        context.SaveChanges();

                        return(Result.SuccessFormat("Product {0} | {1} has been added", guid, name));
                    }
                }
                else
                {
                    return(Result.Error("Provided parameters are unacceptable."));
                }
            }
            catch (Exception ex)
            {
                return(Result.FatalFormat("ProductService.AddProduct Exception: {0}", ex.Message));
            }
        }
Пример #3
0
        /// <summary>
        /// Creates a new basket and returns itself
        /// </summary>
        /// <returns>data of the newly created basket</returns>
        public Basket CreateBasket()
        {
            try
            {
                using (var context = new EntityModels.ServiceBusDatabaseEntities())
                {
                    var basket = new Basket()
                    {
                        Id = Guid.NewGuid(), Result = Result.Success("New Basket created.")
                    };

                    context.Baskets.Add(new EntityModels.Basket()
                    {
                        Id = basket.Id, TotalPrice = 0
                    });
                    context.SaveChanges();

                    return(basket);
                }
            }
            catch (Exception exception)
            {
                return(new Basket {
                    Result = Result.ErrorFormat("BasketService.CreateBasket Exception: {0}", exception.Message)
                });
            }
        }
Пример #4
0
        public Result AddCampaign(Guid guid, string name, double discount)
        {
            try
            {
                if ((!string.IsNullOrWhiteSpace(name) && name.Length <= 50) && discount >= 0)
                {
                    using (var context = new EntityModels.ServiceBusDatabaseEntities())
                    {
                        context.Campaigns.Add(new EntityModels.Campaign()
                        {
                            Id = guid, Name = name, Discount = discount
                        });
                        context.SaveChanges();

                        return(Result.SuccessFormat("Product {0} | {1} has been added", guid, name));
                    }
                }
                else
                {
                    return(Result.Error("Provided parameters are unacceptable."));
                }
            }
            catch (Exception ex)
            {
                return(Result.FatalFormat("ProductService.AddProduct Exception: {0}", ex.Message));
            }
        }
Пример #5
0
        /// <summary>
        /// Removes quantity of products from basket. This results in removal of basket items from the database.
        /// </summary>
        /// <param name="basketId">basket id</param>
        /// <param name="productId">product id</param>
        /// <param name="quantity">number of products to be added</param>
        /// <returns>result information</returns>
        public Result RemoveProduct(Guid basketId, Guid productId, int quantity)
        {
            try
            {
                using (var context = new EntityModels.ServiceBusDatabaseEntities())
                {
                    var basket = context.Baskets.FirstOrDefault(p => p.Id == basketId);
                    if (basket == null)
                    {
                        return(Result.Error("Basket was not found. Please make sure basket ID is valid"));
                    }
                    var basketItem = basket.BasketItems.FirstOrDefault(p => p.ProductId == productId);
                    if (basketItem == null)
                    {
                        return(Result.Error("Product was not found. Please make sure product ID is valid"));
                    }
                    if (quantity <= 0)
                    {
                        return(Result.Error("Provided parameter quantity must be bigger then 0."));
                    }
                    if (quantity > basketItem.Quantity)
                    {
                        return(Result.Error("Provided parameter quantity is too high."));
                    }

                    if (quantity == basketItem.Quantity)
                    {
                        context.BasketItems.Remove(basketItem);
                        context.SaveChanges();

                        return(Result.SuccessFormat("Product has been deleted from basket."));
                    }

                    basketItem.Quantity = basketItem.Quantity - quantity;
                    context.SaveChanges();

                    return(Result.SuccessFormat("Product has been decreased from basket."));
                }
            }
            catch (Exception exception)
            {
                return(Result.FatalFormat("BasketService.RemoveProduct Exception: {0}", exception.Message));
            }
        }
Пример #6
0
        /// <summary>
        /// Adds campaing into the basket. This results in creation of basket campaigns in the database.
        /// </summary>
        /// <param name="basketId">basket id</param>
        /// <param name="campaignId">camaping id</param>
        /// <returns>result information</returns>
        public Result AddCampaign(Guid basketId, Guid campaignId)
        {
            try
            {
                using (var context = new EntityModels.ServiceBusDatabaseEntities())
                {
                    var basket = context.Baskets.FirstOrDefault(p => p.Id == basketId);
                    if (basket == null)
                    {
                        return(Result.Error("Basket was not found. Please make sure basket ID is valid"));
                    }

                    var campaign = context.Campaigns.FirstOrDefault(c => c.Id == campaignId);
                    if (campaign == null)
                    {
                        return(Result.Error("Campaign was not found. Please make sure campaign ID is valid"));
                    }

                    var campaignItem = basket.CampaignItems.FirstOrDefault(p => p.CampaignId == campaignId);



                    if (campaignItem == null)
                    {
                        context.CampaignItems.Add(new EntityModels.CampaignItem()
                        {
                            CampaignId = campaignId,
                            BasketId   = basketId,
                            Id         = Guid.NewGuid()
                        });

                        context.SaveChanges();

                        return(Result.SuccessFormat("Campaign has been added: "));
                    }
                    else
                    {
                        return(Result.Success("Campaign is allready in the basket"));
                    }
                }
            }
            catch (Exception exception)
            {
                return(Result.FatalFormat("BasketService.AddProduct Exception: {0}", exception.Message));
            }
        }
 /// <summary>
 /// Adds campaing into the basket. This results in creation of basket campaigns in the database.
 /// </summary>
 /// <param name="basketId">basket id</param>
 /// <param name="campaignId">camaping id</param>
 /// <returns>result information</returns>
 public SharedLibs.DataContracts.Basket CreateBasket()
 {
     try
     {
         using (var context = new EntityModels.ServiceBusDatabaseEntities())
         {
             EntityModels.Basket b = context.Baskets.Add(new EntityModels.Basket()
             {
                 Id = Guid.NewGuid()
             });
             context.SaveChanges();
             b = context.Baskets.Attach(b);
             return(new SharedLibs.DataContracts.Basket()
             {
                 Id = b.Id,
                 Result = SharedLibs.DataContracts.Result.SuccessFormat("Basket {0} was created.", b.Id)
             });
         }
     }
     catch (Exception ex)
     {
         throw new Exception("Creation of basket failed. " + ex.Message);
     }
 }
Пример #8
0
        /// <summary>
        /// Delete product item from datasource
        /// </summary>
        /// <param name="guid">ID of a product</param>
        /// <returns>Result object</returns>
        public Result DeleteProduct(Guid guid)
        {
            try
            {
                using (var context = new EntityModels.ServiceBusDatabaseEntities())
                {
                    var product = context.Products.FirstOrDefault(p => p.Id == guid);

                    if (product != null)
                    {
                        context.Products.Remove(product);
                        context.SaveChanges();

                        return(Result.SuccessFormat("Product {0} - {1} has been deleted.", product.Id, product.Name));
                    }

                    return(Result.Error("Product was not found. Please make sure product ID is valid."));
                }
            }
            catch (Exception exception)
            {
                return(Result.FatalFormat("ProductService.DeleteProduct exception has occured : {0}", exception.Message));
            }
        }
Пример #9
0
        /// <summary>
        /// This method edits product in case product exists
        /// </summary>
        /// <param name="guid">ID of a product</param>
        /// <param name="name">New name for a product</param>
        /// <param name="price">New price for a product</param>
        /// <returns>Modified product</returns>
        public Product EditProduct(Guid guid, string name, double price)
        {
            try
            {
                var originalProduct = GetProduct(guid);

                if (originalProduct.Result.ResultType == ResultType.Success)
                {
                    var editableProduct = new EntityModels.Product()
                    {
                        Id    = originalProduct.ID,
                        Name  = originalProduct.Name,
                        Price = originalProduct.Price
                    };

                    using (var context = new EntityModels.ServiceBusDatabaseEntities())
                    {
                        context.Products.Attach(editableProduct);

                        //is new name valid and different than the stored one?
                        if (!String.IsNullOrWhiteSpace(name) && name.Length <= 50 && name != editableProduct.Name)
                        {
                            editableProduct.Name = name;
                        }

                        //is price valid and different from the stored one?
                        if (price != editableProduct.Price && price >= 0)
                        {
                            editableProduct.Price = price;
                        }

                        //No changes? No need to update
                        if (context.Entry(editableProduct).State == EntityState.Unchanged)
                        {
                            originalProduct.Result = Result.WarningFormat("Product {0} was not modified.", originalProduct.ID);
                            return(originalProduct);
                        }
                        //Otherwise update db and return what you should return
                        else
                        {
                            context.SaveChanges();
                            return(new Product()
                            {
                                ID = editableProduct.Id,
                                Name = editableProduct.Name,
                                Price = editableProduct.Price.HasValue ? editableProduct.Price.Value : 0.0,
                                Result = Result.Success()
                            });
                        }
                    }
                }
                else
                {
                    return(originalProduct);
                }
            }
            catch (Exception exception)
            {
                return(new Product()
                {
                    Result =
                        Result.FatalFormat("ProductService.EditProduct exception has occured : {0}", exception.Message)
                });
            }
        }