public void When_CreateBasket_BasketCreated()
        {
            var command = new CreateBasket(id);

            command.Metadata.CausationId   = command.Metadata.CommandId;
            command.Metadata.CorrelationId = causationAndCorrelationId;

            When(command);

            var expectedEvent = new BasketCreated(id);

            expectedEvent.Metadata.CausationId   = command.Metadata.CommandId;
            expectedEvent.Metadata.CorrelationId = causationAndCorrelationId;
            expectedEvent.Metadata.ProcessId     = command.Metadata.ProcessId;

            Then(expectedEvent);
        }
Example #2
0
        private void Handle(BasketCreated evt)
        {
            var newBasket = new Basket()
            {
                Id          = evt.Id,
                OrderLines  = null,
                BasketState = BasketState.Shopping
            };

            _indexer.Index(newBasket);
            _graphClient.Cypher
            .Create("(basket:Basket {newBasket})")
            .WithParam("newBasket", newBasket)
            .ExecuteWithoutResults();

            _graphClient.Cypher
            .Match("(customer:Customer)", "(basket:Basket)")
            .Where((Customer customer) => customer.Id == evt.CustomerId)
            .AndWhere((Basket basket) => basket.Id == evt.Id)
            .Create("customer-[:HAS_BASKET]->basket")
            .ExecuteWithoutResults();
        }
Example #3
0
        public void CreateBasket()
        {
            var basketCommand = new BasketCreated()
            {
                UserID      = 1,
                BasketItems = new List <BasketItemCreated>()
                {
                    new BasketItemCreated()
                    {
                        BasePrice  = 10,
                        ProductID  = 1,
                        Quantity   = 2,
                        TotalPrice = 10,
                        Created    = DateTime.UtcNow,
                        Status     = Framework.Models.EntityStatus.Active
                    }
                }
            };

            var response = basketService.CreateBasket(basketCommand);

            Assert.AreEqual(response.Type, Common.ServiceResponseTypes.Success);
        }
Example #4
0
        private void Handle(BasketCreated evt)
        {
            var newBasket = new Basket()
            {
                Id = evt.Id,
                OrderLines = null,
                BasketState = BasketState.Shopping
            };
            _indexer.Index(newBasket);
            _graphClient.Cypher
                .Create("(basket:Basket {newBasket})")
                .WithParam("newBasket", newBasket)
                .ExecuteWithoutResults();

            _graphClient.Cypher
                .Match("(customer:Customer)", "(basket:Basket)")
                .Where((Customer customer) => customer.Id == evt.CustomerId)
                .AndWhere((Basket basket) => basket.Id == evt.Id)
                .Create("customer-[:HAS_BASKET]->basket")
                .ExecuteWithoutResults();
        }
Example #5
0
 private void Apply(BasketCreated obj)
 {
     Id = obj.Id;
     _discount = obj.Discount;
     _orderLines = FSharpList<OrderLine>.Empty;
 }
Example #6
0
 public void Apply(BasketCreated obj)
 {
     this.Id           = obj.Id;
     this.baseDiscount = obj.Discount;
     this.orderLines   = new List <OrderLine>();
 }
Example #7
0
 private void Apply(BasketCreated evt)
 {
     AggregateId = evt.AggregateId;
     State       = BasketState.Pending;
 }
Example #8
0
 private void Apply(BasketCreated obj)
 {
     Id          = obj.Id;
     _discount   = obj.Discount;
     _orderLines = FSharpList <OrderLine> .Empty;
 }
 public void Apply(BasketCreated aggregateEvent)
 {
     _customerId = aggregateEvent.CustomerId;
 }
        public JsonResult AddToBasket(int productId, int quantity)
        {
            //check if user has existing basket
            var basket = basketService.GetBaskets(new Headstone.Models.Requests.BasketRequest()
            {
                UserIds = new List <int> {
                    CurrentUser.Id
                }
            }).Result.FirstOrDefault();

            var product = metaDataServiceClient.GetProducts(new MetaData.API.Models.Queries.Live.ProductQuery()
            {
                ProductIds = new List <int> {
                    productId
                },
                Envelope = "full"
            }).Result.FirstOrDefault();

            if (basket != null)
            {
                if (basket.BasketItems.Select(p => p.ProductID).Contains(productId))
                {
                    //if product is already in the basket

                    //first remove the basketitem
                    var basketItemDeleted = new BasketItemDeleted()
                    {
                        BasketItemID = basket.BasketItems.Where(p => p.ProductID == productId).FirstOrDefault().BasketItemID
                    };

                    var deletebasketitem = basketService.DeleteBasketItem(basketItemDeleted);

                    //then recreate the basketitem with updated quantity
                    var basketItemCreated = new BasketItemCreated()
                    {
                        BasketID  = basket.BasketID,
                        ProductID = productId,
                        Quantity  = quantity + basket.BasketItems.Where(p => p.ProductID == productId).FirstOrDefault().Quantity,
                    };

                    var basketItemresponse = basketService.CreateBasketItem(basketItemCreated);
                }
                else
                {
                    var basketItemCommand = new BasketItemCreated()
                    {
                        BasketID  = basket.BasketID,
                        ProductID = productId,
                        Quantity  = quantity,
                    };

                    var bresponse = basketService.CreateBasketItem(basketItemCommand);
                }
            }
            else
            {
                //create basket if it does not exist
                var command = new BasketCreated()
                {
                    UserID      = CurrentUser.Id,
                    BasketItems = new List <BasketItemCreated>()
                    {
                        new BasketItemCreated()
                        {
                            ProductID = productId,
                            Quantity  = quantity,
                            Created   = DateTime.Now
                        }
                    }
                };

                var response = basketService.CreateBasket(command);
            }

            var model = new BasketViewModel().From(basket);

            model.BasketItems.Where(b => b.ProductID == productId).FirstOrDefault().ProductName = product.Name; // TODO: BURASI HATA VERIYOR!!
            model.BasketItems.Where(b => b.ProductID == productId).FirstOrDefault().ListPrice   = product.Properties.FirstOrDefault(k => k.Key == "ListPrice").Value;
            model.BasketItems.Where(b => b.ProductID == productId).FirstOrDefault().Price       = product.Properties.FirstOrDefault(k => k.Key == "Price").Value;


            var html = ViewToString("Basket", "~/Views/Basket/Minicart.cshtml", model);

            var controllerResponse = new HeadstoneControllerResponse()
            {
                Html = html
            };

            return(Json(controllerResponse, JsonRequestBehavior.AllowGet));
        }
Example #11
0
        public BasketServiceResponse <Basket> CreateBasket(BasketCreated ev, List <ServiceLogRecord> logRecords = null)
        {
            // Create the watch
            var sw = new Stopwatch();

            sw.Start();

            // Create a log record collection if necessary
            if (logRecords == null)
            {
                logRecords = new List <ServiceLogRecord>();
            }

            // Add log
            logRecords.Add(new ServiceLogRecord()
            {
                Type      = "DEBUG",
                TimeStamp = DateTime.Now,
                Body      = "Create Basket request received."
            });

            var response = new BasketServiceResponse <Basket>();

            #region [ Validate Request ]

            logRecords.Add(new ServiceLogRecord()
            {
                Type      = "DEBUG",
                TimeStamp = DateTime.Now,
                Body      = "User has the required permissions. Now validating the incoming event data."
            });

            // Check required data
            List <string> dataErrors = new List <string>();

            //if (ev.ResellerId == default(int) || String.IsNullOrEmpty(ev.ResellerName))
            //{
            //    dataErrors.Add("No reseller!");
            //}

            //if (ev.Items == null || ev.Items.Count == 0)
            //{
            //    dataErrors.Add("No Basket item(s)!");
            //}

            //if (String.IsNullOrEmpty(ev.Name))
            //{
            //    dataErrors.Add("No product name!");
            //}
            //if (ev.Items.Count == 0)
            //{
            //    dataErrors.Add("No Basket item");
            //}

            if (dataErrors.Count > 0)
            {
                // Add log
                logRecords.Add(new ServiceLogRecord()
                {
                    Type      = "DEBUG",
                    TimeStamp = DateTime.Now,
                    Body      = dataErrors.Count + " error(s) found within the event data! Terminating the process. Errors:" + String.Join(";", dataErrors)
                });

                // Stop the sw
                sw.Stop();

                response.Type = ServiceResponseTypes.Error;
                response.Code = ((short)HeadstoneServiceResponseCodes.Invalid_Request).ToString();
                response.PreProcessingTook = sw.ElapsedMilliseconds;
                response.Message           = "There are some errors with the incoming event data!";
                response.Errors.AddRange(dataErrors);
                response.LogRecords = logRecords;

                return(response);
            }

            #endregion

            #region [ Data manuplation ]

            // Stop the timer
            sw.Stop();

            // Set the pre-processing time and start the time
            response.PreProcessingTook = sw.ElapsedMilliseconds;
            sw.Start();
            #endregion

            #region [ Create Object ]
            logRecords.Add(new ServiceLogRecord()
            {
                Type      = "DEBUG",
                TimeStamp = DateTime.Now,
                Body      = "Creating Basket."
            });

            // Create the reseller application
            var command = new Basket()
            {
                UserID      = ev.UserID,
                BasketItems = ev.BasketItems.Select(b => new BasketItem()
                {
                    BasePrice  = b.BasePrice,
                    Created    = b.Created,
                    ProductID  = b.ProductID,
                    Quantity   = b.Quantity,
                    TotalPrice = b.TotalPrice,
                }).ToList()
            };

            // Add log
            logRecords.Add(new ServiceLogRecord()
            {
                Type      = "DEBUG",
                TimeStamp = DateTime.Now,
                Body      = string.Format("Basket created", ev.UserToken, ev.SessionId)
            });
            #endregion

            #region [ Save Basket ]

            var baseServiceResponse = new Framework.Models.Responses.ServiceResponse <Basket>();
            try
            {
                baseServiceResponse = _basketServiceBase.Create(command);
            }catch (Exception ex)
            {
                logRecords.Add(new ServiceLogRecord()
                {
                    Type      = "ERROR",
                    TimeStamp = DateTime.Now,
                    Body      = "There was an error while saving Basket! Exception: " + ex.Message
                });
            }
            if (baseServiceResponse.Type != Headstone.Framework.Models.ServiceResponseTypes.Success)
            {
                // Add log
                logRecords.Add(new ServiceLogRecord()
                {
                    Type      = "ERROR",
                    TimeStamp = DateTime.Now,
                    Body      = "There was an error while saving Basket!"
                });

                // Stop the sw
                sw.Stop();

                response.Type        = ServiceResponseTypes.Error;
                response.Code        = ((short)HeadstoneServiceResponseCodes.General_Exception).ToString();
                response.ServiceTook = sw.ElapsedMilliseconds;
                response.Message     = "There was an error while saving the request!";
                response.Errors.AddRange(baseServiceResponse.Errors);
                response.LogRecords = logRecords;

                return(response);
            }
            else
            {
                // Add log
                logRecords.Add(new ServiceLogRecord()
                {
                    Type      = "DEBUG",
                    TimeStamp = DateTime.Now,
                    Body      = string.Format("Basket successfuly created. BasketID:{0}",
                                              command.BasketID)
                });

                // Add the new object to the result
                response.Result.Add(command);

                // Set the object id
                response.BasketID = command.BasketID;
            }
            // Stop the sw
            sw.Stop();

            response.Type        = ServiceResponseTypes.Success;
            response.Code        = ((short)HeadstoneServiceResponseCodes.Request_Successfuly_Completed).ToString();
            response.ServiceTook = sw.ElapsedMilliseconds;
            response.Message     = string.Format("Basket successfuly created. BasketID:{0}",
                                                 command.BasketID);
            response.LogRecords = logRecords;

            #endregion

            return(response);
        }