Beispiel #1
0
        public async Task <IActionResult> PutEmployee(long id, Employee employee)
        {
            if (id != employee.Id)
            {
                return(BadRequest());
            }

            _context.Entry(employee).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!EmployeeExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
Beispiel #2
0
        public async Task <IActionResult> PutMenuItem(long id, MenuItem menuItem)
        {
            if (id != menuItem.Id)
            {
                return(BadRequest());
            }

            _context.Entry(menuItem).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!MenuItemExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
Beispiel #3
0
        public async Task <IActionResult> PutTimeSheet(long id, long employeeId, TimeSheet timeSheet)
        {
            if (id != timeSheet.Id && employeeId != timeSheet.EmployeeId)
            {
                return(BadRequest());
            }

            _context.Entry(timeSheet).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!TimeSheetExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
        public async Task <IActionResult> Create([Bind("Id,Title,Info,Price")] Product product)
        {
            if (ModelState.IsValid)
            {
                _context.Add(product);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(product));
        }
Beispiel #5
0
        public async Task AddAsync(Product product)
        {
            if (_context.Database.IsInMemory())
            {
                await _context.Products.AddAsync(product);

                await _context.SaveChangesAsync();

                return;
            }

            using (var transaction = await _context.Database.BeginTransactionAsync())
            {
                await _context.Products.AddAsync(product);

                await _context.SaveChangesAsync();

                transaction.Commit();
            }
        }
Beispiel #6
0
        public async Task SignUpAsync(string username, string password, string role)
        {
            var user = await _context.Users.SingleOrDefaultAsync(u => u.Username == username);

            if (user != null)
            {
                throw new Exception($"User: {username} already in use.");
            }

            user = new User
            {
                Id       = Guid.NewGuid(),
                Username = username,
                Password = password,
                Role     = role ?? "user"
            };
            var passwordHash = _passwordHasher.HashPassword(user, password);

            user.Password = passwordHash;
            await _context.Users.AddAsync(user);

            await _context.SaveChangesAsync();
        }
        public async Task <IActionResult> AddToCart(
            [Bind("ShoppingCartItemProductId,ShoppingCartItemQuantity")]
            ViewModelAddToCart model)
        {
            // Initialize session to enable SessionId
            // TODO: Sjekke om det er nødvendig å sette navnet hver gang. Kanskje man kan sjekke om cookien finnes først?
            HttpContext.Session.SetString("_Name", "MyStore");

            DateTime dt        = DateTime.Now;
            string   SessionId = HttpContext.Session.Id;

            var ShoppingCart = new ShoppingCart()
            {
                SessionId  = SessionId,
                CreateDate = dt,
                Title      = "My shopping cart (" + dt.ToShortDateString() + ")"
                             // (not in use yet) CustomerId = 0
            };

            var ShoppingCartItem = new ShoppingCartItem()
            {
                ProductId = model.ShoppingCartItemProductId,
                Quantity  = model.ShoppingCartItemQuantity
            };

            // 1. If a ShoppingCart exist with current SessionId, get ShoppingCartId from
            // that one and use it in the ShoppingCartItem
            if (ModelState.IsValid)
            {
                // Query for ShoppingCart containing current SessionId.
                var cartInfo =
                    (from Cart in _context.ShoppingCarts
                     where Cart.SessionId == SessionId
                     select new { TempId = Cart.Id })
                    .SingleOrDefault();
                if (cartInfo != null)
                {
                    // *** [ Use existing ShoppingCart ] ***
                    // 2. Set ShoppingCartId for ShoppingCartItem
                    ShoppingCartItem.ShoppingCartId = cartInfo.TempId;
                }
                else
                {
                    // *** [ Create a new shoppingCart ] ***
                    _context.ShoppingCarts.Add(ShoppingCart);
                    await _context.SaveChangesAsync();

                    // 2. Set ShoppingCartId in ShoppingCartItem
                    ShoppingCartItem.ShoppingCartId = ShoppingCart.Id;
                }

                // 4. save or update shoppingCartItem
                // Query for ShoppingCartItem containing current ProductId
                var cartItemInfo =
                    (from CartItem in _context.ShoppingCartItems
                     where CartItem.ProductId == model.ShoppingCartItemProductId
                     select new { TempQty = CartItem.Quantity, TempId = CartItem.Id })
                    .FirstOrDefault();
                if (cartItemInfo != null)
                {
                    // 5. update quantity for existing ShoppingCartItem
                    ShoppingCartItem.Id        = cartItemInfo.TempId;
                    ShoppingCartItem.Quantity += cartItemInfo.TempQty;                     // current quantity + added quantity
                    _context.ShoppingCartItems.Update(ShoppingCartItem);
                }
                else
                {
                    // 5. create a new shoppingCartItem
                    _context.ShoppingCartItems.Add(ShoppingCartItem);
                }
                await _context.SaveChangesAsync();

                return(RedirectToAction("Index", "Home"));
            }
            return(View("Index", "Home"));
        }