예제 #1
0
        public ServiceResponse <User> CreateUser(CreateUserData data)
        {
            // Save the model to the datastore if it doesn't exists
            User user = new User();

            user.name     = data.Name;
            user.username = data.Username;
            user.balance  = 10;

            char[] nameReverse = data.Name.ToArray();
            Array.Reverse(nameReverse);
            user.password = new string(nameReverse);

            using (var context = new ahshopEntities())
            {
                User userExists = context.User.FirstOrDefault(b => b.username == data.Username);
                if (userExists != null)
                {
                    return(ServiceResponse <User> .Error(null, "Username already exists!"));
                }

                context.User.Add(user);
                context.SaveChanges();
            }
            return(ServiceResponse <User> .Success(user, "User successfully created!"));
        }
예제 #2
0
        public ServiceResponse <Purchase> PurchaseProduct(PurchaseProductData data)
        {
            User     user     = null;
            Product  product  = null;
            Purchase purchase = new Purchase();

            using (var context = new ahshopEntities())
            {
                user    = context.User.FirstOrDefault(b => b.userid == data.UserID);
                product = context.Product.FirstOrDefault(b => b.productid == data.ProductID);
                if (user == null)
                {
                    return(ServiceResponse <Purchase> .Error(null, "User not found!"));
                }
                if (product == null)
                {
                    return(ServiceResponse <Purchase> .Error(null, "Product not found!"));
                }
                if (user.balance - product.price < 0)
                {
                    return(ServiceResponse <Purchase> .Error(null, "Not enough money!"));
                }
                if (product.quantity - 1 < 0)
                {
                    return(ServiceResponse <Purchase> .Error(null, "Not enough items in shop!"));
                }

                purchase.price     = product.price;
                purchase.productid = product.productid;
                purchase.Product   = product;
                purchase.userid    = user.userid;
                purchase.User      = user;

                product.quantity -= 1;

                user.balance -= product.price;
                user.Purchase.Add(purchase);
                context.SaveChanges();
            }

            return(ServiceResponse <Purchase> .Success(Copy.ClonePurchase(purchase)));
        }