Exemplo n.º 1
0
        public void LoadUserTest()
        {
            var userRepo = new UserRepo();
            var sessionRepo = new SessionRepo();
            var balanceRepo = new BalanceLogRepo();

            var user = new User();
            userRepo.Save(user);
            Assert.IsNotNull(user.Id, "Not saved.");
            var session = new Session { UserId = user.Id };
            sessionRepo.Save(session);

            var date = DateTime.UtcNow;

            balanceRepo.Save(new BalanceLog { UserId = user.Id, Amount = 3, Comment = "comment", Date = date });

            var manager = new UserManager();
            var loaded = manager.LoadBySessionKey(session.Id).WithBallanceLog();
            Assert.IsNotNull(loaded, "Not loaded");
            Assert.AreEqual(user.Id, loaded.Id, "Loaded incorrectly");
            Assert.IsNotNull(loaded.BallanceLog, "Balance log is null.");
            Assert.AreEqual(1, loaded.BallanceLog.Count(), "Balance log count incorrect.");
            var balance = loaded.BallanceLog.First();
            Assert.AreEqual(3, balance.Amount, "Amount incorrect.");
            Assert.AreEqual("comment", balance.Comment, "Comment incorrect.");
            Assert.AreEqual(date, balance.Date, "Date incorrect.");
        }
Exemplo n.º 2
0
        public DTO.Messages.Wrapper RemoveOne(DTO.Messages.AddToCart ToBeRemoved = null)
        {
            var Result = AuthorizeResponse();

            if (ToBeRemoved == null)
            {
                Result.Messages.Add("PostBody", "can't be empty");
            }
            if (string.IsNullOrWhiteSpace(ToBeRemoved.ProductId))
            {
                Result.Messages.Add("ProductId", "can't be empty");
            }
            if (ToBeRemoved.Quantity == 0)
            {
                Result.Messages.Add("Quantity", "can't be Zero (0)");
            }

            if (Result.Messages.Count > 0)
            {
                Result.Code   = 400;
                Result.Status = "Bad Request";
                return(Result);
            }

            var Session = SessionRepo.GetOne(Token.jti);
            var Removed = false;

            if (ToBeRemoved.Quantity < 0)
            {
                Session.Cart.Remove(ToBeRemoved.ProductId);
                Removed = true;
            }
            else
            {
                Session.Cart.TryGetValue(ToBeRemoved.ProductId, out int Left);
                if (Left < 1 || Left - ToBeRemoved.Quantity < 1)
                {
                    Session.Cart.Remove(ToBeRemoved.ProductId);
                    Removed = true;
                }
                else
                {
                    Session.Cart[ToBeRemoved.ProductId] = (Left - ToBeRemoved.Quantity);
                    Removed = false;
                }
            }
            SessionRepo.Save(Session);

            Result.Messages.Add("ItemStatus", Removed ? "Ok" : "Reduced");
            Result.Data = ToBeRemoved;
            return(Result);
        }
Exemplo n.º 3
0
        public DTO.Messages.Wrapper AddOne(DTO.Messages.AddToCart Adding = null)
        {
            var Result = AuthorizeResponse();

            if (Adding == null)
            {
                Result.Messages.Add("PostBody", "can't be empty");
            }
            else
            {
                if (string.IsNullOrWhiteSpace(Adding.ProductId))
                {
                    Result.Messages.Add("ProductId", "can't be empty");
                }
                else
                {
                    var Product = ProductRepo.GetOne(Adding.ProductId);
                    if (Product == null)
                    {
                        Result.Messages.Add("ProductId", "is invalid");
                    }
                    else if (Product.Scored() < 0)
                    {
                        Result.Messages.Add("Product", "is unavailable due to Promotions");
                    }
                }

                if (Adding.Quantity < 1)
                {
                    Result.Messages.Add("Quantity", "must be greater than 0");
                }
            }

            if (Result.Messages.Count > 0)
            {
                Result.Code   = 400;
                Result.Status = "Bad Request";
                return(Result);
            }

            var Session = SessionRepo.GetOne(Token.jti);

            Session.Cart.Add(Adding.ProductId, Adding.Quantity);
            SessionRepo.Save(Session);

            Result.Data = Adding;
            return(Result);
        }
Exemplo n.º 4
0
        public DTO.Messages.Wrapper AddPaymentAddress(DTO.Messages.TemporaryAddress address = null)
        {
            var Result = AuthorizeResponse();

            if (address == null)
            {
                Result.Messages.Add("PostBody", "It mustn't be empty");
            }
            if (string.IsNullOrWhiteSpace(address.Location))
            {
                Result.Messages.Add("Location", "It mustn't be empty");
            }
            if (string.IsNullOrWhiteSpace(address.Recipent))
            {
                Result.Messages.Add("Recipent", "It mustn't be empty");
            }
            if (string.IsNullOrWhiteSpace(address.Phone))
            {
                Result.Messages.Add("Phone", "It mustn't be empty");
            }

            if (Result.Messages.Count > 0)
            {
                Result.Code   = 400;
                Result.Status = "Bad Request";
                return(Result);
            }

            var Session = SessionRepo.GetOne(Token.jti);

            Session.DeliveryAddress = address;
            var Saved = SessionRepo.Save(Session);

            Result.Messages.Add("PaymentAddress", (Saved ? "Ok" : "Fail"));
            return(Result);
        }
Exemplo n.º 5
0
        public DTO.Messages.Wrapper MakeOrder([FromBody] DTO.Messages.CheckOutCart Data = null)
        {
            var Result  = AuthorizeResponse();
            var Session = SessionRepo.GetOne(Token.jti);

            if (Data == null)
            {
                Data = new DTO.Messages.CheckOutCart();
            }
            if (Session.Cart.Count == 0)
            {
                Result.Messages.Add("Cart", "is empty, please choose something before check out");
            }
            if (Session.DeliveryAddress == null)
            {
                Result.Messages.Add("DeliveryAddress", "is not set");
            }
            if (Session.PaymentAddress == null)
            {
                Result.Messages.Add("PaymentAddress", "is not set");
            }

            if (Result.Messages.Count > 0)
            {
                Result.Status = "Bad Request";
                Result.Code   = 400;
                return(Result);
            }

            var Cart     = new Dictionary <DTO.Projection.Recommendation, int>();
            var Products = ProductRepo.QueryableCollection
                           .Where(x => Session.Cart.ContainsKey(x.Id))
                           .Select(x => new DTO.Projection.Recommendation
            {
                Id     = x.Id,
                Name   = x.Name,
                Price  = x.Price,
                Images = x.Images
            });

            foreach (var Product in Products)
            {
                Session.Cart.TryGetValue(Product.Id, out int Quantity);
                Cart.Add(Product, Quantity);
            }

            var Order = new DTO.Databases.Order
            {
                Note      = Data.Note,
                OrdererId = Token.sub,
                Delivery  = Session.DeliveryAddress,
                Payment   = Session.PaymentAddress,
                Ordered   = Cart
            };

            OrderRepo.Save(Order);

            Session.Cart = new Dictionary <string, int>();
            SessionRepo.Save(Session);

            return(Result);
        }