예제 #1
0
        public int RegisterUser(User user)
        {
            try
            {
                var count = (from users in dbContext.NfUser
                             where users.Email == user.Email
                             select users).Count();
                if (count == 0)
                {
                    NfUser newUser = new NfUser();
                    newUser.FirstName     = user.FirstName;
                    newUser.LastName      = user.LastName;
                    newUser.Email         = user.Email;
                    newUser.Password      = user.Password;
                    newUser.IsActive      = true;
                    newUser.PasswordHash  = "";
                    newUser.SecurityStamp = "";

                    dbContext.Add(newUser);
                    dbContext.SaveChanges();

                    user.Id = newUser.UserId;
                    return(1);
                }
                else
                {
                    return(-1);
                }
            }
            catch (Exception ex)
            {
                return(0);
            }
        }
        public async Task <IActionResult> Create([Bind("Id,ProductName,Price,Photo")] ProductTbl productTbl)
        {
            if (ModelState.IsValid)
            {
                _context.Add(productTbl);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(productTbl));
        }
        public int AddToCart(CartModel model)
        {
            try
            {
                var item = (from cart in dbContext.ScUserCart
                            where cart.UserId == model.UserId && cart.ItemId == model.ItemId
                            select cart).FirstOrDefault();
                if (item != null)
                {
                    ScUserCart userCart = dbContext.ScUserCart.Find(item.UserCartId);

                    userCart.UserCartId = userCart.UserCartId;
                    userCart.UserId     = model.UserId;
                    userCart.ItemId     = model.ItemId;
                    userCart.Quantity   = item.Quantity + model.Quantity;

                    dbContext.Entry(userCart).State = Microsoft.EntityFrameworkCore.EntityState.Modified;
                    dbContext.SaveChanges();

                    model.UserCartId = userCart.UserCartId;
                }
                else
                {
                    ScUserCart newCart = new ScUserCart();
                    newCart.UserId   = model.UserId;
                    newCart.ItemId   = model.ItemId;
                    newCart.Quantity = model.Quantity;
                    dbContext.Add(newCart);
                    dbContext.SaveChanges();
                    model.UserCartId = newCart.UserCartId;
                }
                return(model.UserCartId);
            }
            catch (Exception ex)
            {
                return(0);
            }
        }
        public int AddItem(ItemsModel model)
        {
            try
            {
                var list = (from Items in dbContext.ScItems
                            where Items.Description == model.Description && Items.ItemCategoryId == model.ItemCategoryId
                            select Items).Count();
                if (list == 0)
                {
                    ScItems newItem = new ScItems();

                    newItem.ItemCategoryId = model.ItemCategoryId;
                    newItem.Description    = model.Description;
                    newItem.Price          = model.Price;
                    newItem.DisplayOrder   = model.DisplayOrder;
                    newItem.IsInOffer      = model.IsInOffer;
                    newItem.OfferValidFrom = model.OfferValidFrom;
                    newItem.OfferValidTo   = model.OfferValidTo;
                    newItem.OfferPrice     = model.OfferPrice;

                    dbContext.Add(newItem);
                    dbContext.SaveChanges();
                    model.ItemId = newItem.ItemId;

                    return(newItem.ItemId);
                }
                else
                {
                    return(-1);
                }
            }
            catch (Exception ex)
            {
                return(0);
            }
        }