Exemplo n.º 1
0
        private User EncodeAndStoreUser(User user, dynamic password, bool addOrNot)   //Deze functie encode de password en storet daarna de user in de database
        {
            string stringed_password = password;

            using (var deriveBytes = new Rfc2898DeriveBytes(stringed_password, 20)) // 20-byte salt
            {
                byte[] salt = deriveBytes.Salt;
                byte[] key  = deriveBytes.GetBytes(20); // 20-byte key

                string encodedSalt = Convert.ToBase64String(salt);
                string encodedKey  = Convert.ToBase64String(key);

                user.Salt = encodedSalt; //store salt into user
                user.Key  = encodedKey;  //store key into user
                // store encodedSalt and encodedKey in database
                // you could optionally skip the encoding and store the byte arrays directly
                if (addOrNot)
                {
                    _context.Add(user);
                }
                else
                {
                    _context.Update(user);
                }
                System.Console.WriteLine(user.Salt);
                //add user to database
                _context.SaveChanges();
            }
            return(user);
        }
Exemplo n.º 2
0
 public string Buy(Order oreder)
 {
     db.Orders.Add(oreder);
     //Сохраняем в бд вск изменеия
     db.SaveChanges();
     return("Спасибо," + oreder.User + ",за покупку!");
 }
Exemplo n.º 3
0
        public int AddUser(RegisterViewModel Register)
        {
            var UserEmailExist = database.Users.Where(a => a.Email.Equals(Register.Email)).FirstOrDefault();
            var UserPhoneExist = database.Users.Where(a => a.Phone.Equals(Register.Phone)).FirstOrDefault();
            var UsernameExist  = database.Users.Where(a => a.Username.Equals(Register.Username)).FirstOrDefault();

            if (UsernameExist == null)
            {
                if (UserEmailExist == null)
                {
                    if (UserPhoneExist == null)
                    {
                        User newUser = new User();
                        newUser.Username = Register.Username;
                        newUser.Email    = Register.Email;
                        newUser.Phone    = Register.Phone;
                        newUser.Password = vm.encryptPassword(Register.Password);
                        if (Register.Tailor)
                        {
                            newUser.Tailor = 1;
                        }
                        database.Users.Add(newUser);

                        //after registration each user get assigned a first friend, so TL isnt empty
                        var firstFriend = database.Users.Where(a => a.Id == 1).FirstOrDefault();
                        var newList     = new List()
                        {
                            FollowingUser = newUser, FollowedUser = firstFriend
                        };

                        database.Lists.Add(newList);
                        database.SaveChanges();
                        return(1); //successful
                    }
                    return(2);     //phone number already exist
                }
                return(3);         //email already exist
            }
            return(4);             //username already exist
        }
Exemplo n.º 4
0
        private IActionResult handleBodyPost(dynamic order)
        {
            System.Console.WriteLine(order);
            //front-end stuurt naar de backend, meerdere elementen in een array, bestaande uit producten van een order
            //elk product uit een order-array vanuit de frontend heeft de bijbehorende userId, ProductId, addressId amount,
            // { userId : 1,
            //   addressId : 1,
            //   orderProducts: {
            //     product1: {id: 1 ...
            //     product2: {id:3 ...
            //     product3:{id:9, ....
            //   }
            // }

            Dictionary <string, int> amountDict = new Dictionary <string, int>();
            List <string>            cookieList = new List <string>();

            foreach (var item in order.cookie.items)
            {
                cookieList.Add(item.ToString());
            }
            foreach (var item in cookieList)
            {
                if (!amountDict.ContainsKey(item))
                {
                    amountDict[item] = 1;
                }
                else
                {
                    amountDict[item]++;
                }
            }

            int      userId         = order.userId;
            int      addressId      = order.addressId;
            User     user_select    = _context.Users.Where(u => u.Id == userId).Select(u => u).FirstOrDefault();
            Address  address_select = _context.Addresses.Where(a => a.Id == addressId).Select(a => a).FirstOrDefault();
            DateTime createDate     = DateTime.UtcNow;
            Status   status_select  = _context.Statuses.Where(s => s.Id == 5).Select(s => s).FirstOrDefault();
            Order    new_order      = new Order()
            {
                User    = user_select,
                Status  = status_select,
                Address = address_select,
            };

            _context.Orders.Add(new_order);

            foreach (dynamic productSold in order.orderProducts)
            {
                int     productId      = productSold.id; // pakt productId
                int     productIdFreq  = amountDict[productId.ToString()];
                Product product_select = _context.Products.Where(product => product.Id == productId).Select(p => p).FirstOrDefault();
                product_select.Amount = product_select.Amount - productIdFreq;  /// Trekt freq af van stocks

                ProductSold new_productSold = new ProductSold()
                {
                    Amount  = productIdFreq,
                    Product = product_select,
                    User    = user_select,
                    Order   = new_order,
                    Date    = createDate,
                };
                _context.ProductsSold.Add(new_productSold);
                _context.Products.Update(product_select);
                // _context.SaveChanges();
                Console.WriteLine("Dit is de amount van productssold:" + new_productSold.Amount);
                Console.WriteLine("Dit is de amount van de stock van product:" + product_select.Amount);
            }
            _context.SaveChanges();

            // return (new_order);

            return(Ok(new_order));
        }
        public ActionResult ClearNotifications()
        {
            var username           = getLoggedInUser().Username;
            var unreadNotification = database.Notifications.Where(a => a.Receiver.Username == username).ToList();

            foreach (var item in unreadNotification)
            {
                item.isUnread = 1;
            }
            database.SaveChanges();
            return(null);
        }
Exemplo n.º 6
0
        public async Task <Product> InsertProduct()
        {
            using (StreamReader reader = new StreamReader(Request.Body, Encoding.UTF8))
            {
                this.RequestBody = await reader.ReadToEndAsync();
            }
            dynamic product = JValue.Parse(this.RequestBody);

            int         sizeID      = product.size;
            ProductSize productSize = _context.ProductSizes.Where(ps => ps.Id == sizeID).Select(ps => ps).FirstOrDefault();

            decimal price       = product.price;
            var     lastID      = from q in _context.Products orderby - q.Id select q.Id;
            Product new_product = new Product()
            {
                Id          = lastID.First() + 1,
                Name        = product.name.ToString(),
                Description = product.description.ToString(),
                Color       = product.color.ToString(),
                Price       = price,
                Amount      = product.amount,
                ProductSize = productSize, // referentie naar Product size..
                ImageName   = product.imageName,
            };

            _context.Add(new_product);

            // geef in de frontend collectie(array/list..) mee aan categorien called "categories" met > , {heren, shirt, Nike}
            //Deze loop voegt voor elke category string in product.categories een nieuwe ProductCategory toe aan de database
            //category is string
            var lastIDCat    = from q in _context.ProductCategory orderby - q.Id select q.Id;
            int lastIDCatInt = lastIDCat.First() + 1;

            foreach (var category in product.cat)
            {
                int      catID = category;
                Category cat   = _context.Categories.Where(c => c.Id == catID).Select(c => c).FirstOrDefault();

                ProductCategory pc = new ProductCategory()
                {
                    Id       = lastIDCatInt,
                    Product  = new_product,
                    Category = cat
                };
                _context.Add(pc); // Maak voor ProductCAtegort ook sequence met startswith, want heeft ook inital data, anders krijg je errors.
                lastIDCatInt++;
            }
            _context.SaveChanges();
            return(new_product);
        }