Exemplo n.º 1
0
        public async Task <IActionResult> PutCart(Guid id, Cart cart)
        {
            if (id != cart.Id)
            {
                return(BadRequest());
            }

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

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

            return(NoContent());
        }
Exemplo n.º 2
0
        public async Task <IActionResult> PutTransaction(int id, Transaction transaction)
        {
            if (id != transaction.Id)
            {
                return(BadRequest());
            }

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

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

            return(NoContent());
        }
        public async Task <IHttpActionResult> PutCategory(int id, Category category)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != category.id)
            {
                return(BadRequest());
            }

            db.Entry(category).State = EntityState.Modified;

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

            return(StatusCode(HttpStatusCode.NoContent));
        }
Exemplo n.º 4
0
        public async Task <IActionResult> Create([Bind("Id,UserName,Password,GroupId,Name,Address,Email,Phone,ProvinceId,DistrictId,CreatedDate,CreatedBy,ModifiedDate,ModifiedBy,Status")] User user)
        {
            if (ModelState.IsValid)
            {
                _context.Add(user);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(user));
        }
Exemplo n.º 5
0
        public async Task <IActionResult> Create([Bind("PhoneNumber,VerificationCode,FullName,NotificationDeviceId,Id,CreationDate,DeletedDate")] User user)
        {
            if (ModelState.IsValid)
            {
                user.Id = Guid.NewGuid();
                _context.Add(user);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(user));
        }
Exemplo n.º 6
0
        public async Task <IActionResult> Create([Bind("Name,ImageUrl,Id,CreationDate,DeletedDate")] Category category)
        {
            if (ModelState.IsValid)
            {
                category.Id = Guid.NewGuid();
                _context.Add(category);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(category));
        }
Exemplo n.º 7
0
        public async Task <IActionResult> Create([Bind("IsPayed,PaymentUrl,DeliveryAddress,OrderStatus,Id,CreationDate,DeletedDate")] Order order)
        {
            if (ModelState.IsValid)
            {
                order.Id = Guid.NewGuid();
                _context.Add(order);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(order));
        }
Exemplo n.º 8
0
        public async Task <ActionResult> Create([Bind(Include = "UserName,Password,Name,Gender,PhoneNumber,Email,Birthday,Address,CreateDate,Status")] Account account)
        {
            if (ModelState.IsValid)
            {
                db.Accounts.Add(account);
                await db.SaveChangesAsync();

                return(RedirectToAction("Index"));
            }

            return(View(account));
        }
Exemplo n.º 9
0
        public async Task <ActionResult> Create([Bind(Include = "Id,Login,Password")] User user)
        {
            if (ModelState.IsValid)
            {
                user.Id = Guid.NewGuid();
                db.Users.Add(user);
                await db.SaveChangesAsync();

                return(RedirectToAction("Index"));
            }

            return(View(user));
        }
Exemplo n.º 10
0
        public async Task <IActionResult> SaveProduct(AddOrUpdateProductVM model)
        {
            if (ModelState.IsValid)
            {
                string uniquefileName = null;
                if (model.Image != null)
                {
                    string uploadsFolder = Path.Combine(hosting.WebRootPath, "images");
                    uniquefileName = Guid.NewGuid().ToString() + "_" + model.Image.FileName;
                    string filePath = Path.Combine(uploadsFolder, uniquefileName);
                    model.Image.CopyTo(new FileStream(filePath, FileMode.Create));
                }
                Product neki;
                if (model.ProductID == 0)
                {
                    neki = new Product();
                    _Iproduct.AddProduct(neki);
                }
                else
                {
                    neki = _database.product.Find(model.ProductID);
                }

                neki.ProductNumber  = model.ProductNumber;
                neki.SubCategoryID  = model.SubCategoryID;
                neki.ManufacturerID = model.ManufacturerID;
                neki.ProductName    = model.ProductName;
                neki.ImageUrl       = uniquefileName;
                neki.Description    = model.Description;
                neki.UnitPrice      = model.UnitPrice;
                neki.UnitsInStock   = model.UnitsInStock;

                await _database.SaveChangesAsync();

                if (model.ProductID != neki.ProductID)
                {
                    var st_pr = new StockProduct
                    {
                        StockID   = 1,
                        ProductID = neki.ProductID,
                        Quantity  = neki.UnitsInStock
                    };
                    _database.Add(st_pr);
                    await _database.SaveChangesAsync();
                }
            }
            await _notificationService.SendNotification($"Dodan je novi artikal ili je izmjenjen postojeći");

            return(Redirect("/Product/Show"));
        }
Exemplo n.º 11
0
        public async Task <IActionResult> PutProductInCart(Guid id, int count)
        {
            var product = await context.Products.SingleOrDefaultAsync(prod => prod.Id == id);

            context.ProductsInCarts.Add(new ProductInCart
            {
                Cart    = user.Cart,
                Count   = count,
                Product = product
            });
            await context.SaveChangesAsync();

            return(Ok());
        }
        public async Task <int> IncreaseShoppingCartItemQuantityAsync(int pieId, int quantity)
        {
            var shoppingCartItem = await GetShoppingCartItemAsync(pieId);

            if (shoppingCartItem == null)
            {
                context.ShoppingCartItem.Add(new ShoppingCartItem {
                    ShoppingCartId = shoppingCartId, PieId = pieId, Quantity = quantity
                });
            }
            else
            {
                shoppingCartItem.Quantity            += quantity;
                context.Entry(shoppingCartItem).State = EntityState.Modified;
            }

            int count = await context.SaveChangesAsync();

            return(count);
        }
        public async Task <IActionResult> AddCategory(CategoryDTO categoryDTO)
        {
            var products = new List <Product>();

            foreach (var id in categoryDTO.Products)
            {
                products.Add(await context.Products.SingleOrDefaultAsync(product => product.Id == id));
            }

            var category = new Category
            {
                ImageUrl = categoryDTO.ImageUrl,
                Name     = categoryDTO.Name,
                Products = products
            };

            context.Categories.Add(category);
            await context.SaveChangesAsync();

            return(Ok());
        }
Exemplo n.º 14
0
        public async Task <IActionResult> EditUser(UserEditDTO userDTO)
        {
            var user = await context.Users.SingleOrDefaultAsync(u => u.Id == userDTO.Id);

            if (userDTO.FullName != null && userDTO.FullName != "")
            {
                user.FullName = userDTO.FullName;
            }
            if (userDTO.PhoneNumber != null && userDTO.PhoneNumber != "")
            {
                user.PhoneNumber = userDTO.PhoneNumber;
            }
            if (userDTO.NotificationDeviceId != null && userDTO.NotificationDeviceId != "")
            {
                user.NotificationDeviceId = userDTO.NotificationDeviceId;
            }

            await context.SaveChangesAsync();

            return(Ok());
        }
Exemplo n.º 15
0
        /* Only for Entity framework */
        public override async Task <bool> Insert(T type)
        {
            bool result;

            using (var dbContextTransaction = _dbContext.Database.BeginTransaction())
            {
                try
                {
                    table.Add(type);
                    result = await _dbContext.SaveChangesAsync() > 0;

                    dbContextTransaction.Commit();
                }
                catch (Exception e)
                {
                    dbContextTransaction.Rollback();
                    result = false;
                }
            }
            return(result);
        }
Exemplo n.º 16
0
        public async Task <IActionResult> SignIn(UserDTO userDTO)
        {
            var user = await context.Users.SingleOrDefaultAsync(usr => usr.PhoneNumber == userDTO.PhoneNumber);

            if (user == null)
            {
                user = new User
                {
                    PhoneNumber          = userDTO.PhoneNumber,
                    FullName             = userDTO.FullName,
                    NotificationDeviceId = userDTO.NotificationDeviceId,
                };
                var userEntity = context.Users.Add(user);

                var cart = new Cart
                {
                    User   = user,
                    UserId = user.Id
                };
                context.Carts.Add(cart);
                userEntity.Entity.Cart = cart;

                await context.SaveChangesAsync();
            }

            if (string.IsNullOrWhiteSpace(userDTO.VerificationCode))
            {
                Random random = new Random();
                var    code   = random.Next(1000, 9999).ToString();
                user.VerificationCode = code;

                await smsService.SendVerificationCode(user.PhoneNumber, user.VerificationCode);

                return(Ok("We sent a verification code on your phone. Please send it back with your next request"));
            }
            else
            {
                if (userDTO.VerificationCode == user.VerificationCode)
                {
                    user.VerificationCode = "";
                    return(Ok(userService.Authenticate(user.PhoneNumber)));
                }
                else
                {
                    return(BadRequest("Invalid verification code"));
                }
            }
        }
Exemplo n.º 17
0
        public async Task <IActionResult> EditOrder(OrderDTO orderDTO)
        {
            var order = await context.Orders.SingleOrDefaultAsync(ord => ord.Id == orderDTO.Id);

            if (orderDTO.PaymentUrl != null && orderDTO.PaymentUrl != "")
            {
                order.PaymentUrl = orderDTO.PaymentUrl;
            }
            if (orderDTO.DeliveryAddress != null && orderDTO.DeliveryAddress != "")
            {
                order.DeliveryAddress = orderDTO.DeliveryAddress;
            }
            if (orderDTO.OrderStatus != null && orderDTO.OrderStatus != "")
            {
                order.OrderStatus = (OrderStatus)Enum.Parse(typeof(OrderStatus), orderDTO.OrderStatus);
            }

            await context.SaveChangesAsync();

            return(Ok());
        }
Exemplo n.º 18
0
        public async Task ClearShoppingCartAsync_WhenCalledWithEmptyShoppingCart_Returns0()
        {
            // Arrange - Empty Shopping Cart
            using (var context = new OnlineShopContext(options))
            {
                var shoppingCartItems = await context.ShoppingCartItem.ToListAsync();

                context.ShoppingCartItem.RemoveRange(shoppingCartItems);
                await context.SaveChangesAsync();
            }

            // Act
            int numberOfRecordsUpdated = 0;

            using (var context = new OnlineShopContext(options))
            {
                var sut = new ShoppingCartService(context, shoppingCartId);
                numberOfRecordsUpdated = await sut.ClearShoppingCartAsync();
            }

            // Assert
            Assert.Equal(0, numberOfRecordsUpdated);
        }
Exemplo n.º 19
0
        public async Task <IActionResult> Register(UserDTO userDTO)
        {
            var user = new User
            {
                PhoneNumber          = userDTO.PhoneNumber,
                FullName             = userDTO.FullName,
                NotificationDeviceId = userDTO.NotificationDeviceId,
            };
            var userEntity = context.Users.Add(user);

            var cart = new Cart
            {
                User   = user,
                UserId = user.Id
            };

            context.Carts.Add(cart);
            userEntity.Entity.Cart = cart;

            await context.SaveChangesAsync();

            return(Ok());
        }
Exemplo n.º 20
0
        public async Task SaveUserCode(string phoneNumber, string code)
        {
            var user = await context.Users.FirstOrDefaultAsync(user => user.PhoneNumber == phoneNumber);

            if (user is null)
            {
                //user = new User
                //{
                //    PhoneNumber = phoneNumber,
                //    VerificationCode = code,
                //    Cart = new Cart(),
                //    FullName = "User-" + Guid.NewGuid().ToString(),
                //    FavoriteProducts = new List<FavoriteProduct>()
                //};
                //
                //await context.Users.AddAsync(user);
            }
            else
            {
                user.VerificationCode = code;
            }

            await context.SaveChangesAsync();
        }
Exemplo n.º 21
0
        public async Task SaveCodeToUser(string phoneNumber, string code)
        {
            var user = await context.Users.FirstOrDefaultAsync(user => user.PhoneNumber == phoneNumber);

            if (user is null)
            {
                user = new Domain.User
                {
                    PhoneNumber      = phoneNumber,
                    VerificationCode = code,
                    Cart             = new Domain.Cart(),
                    FullName         = "User-" + Guid.NewGuid().ToString(),
                    FavoriteProducts = new List <Domain.FavoriteProduct>()
                };

                await context.Users.AddAsync(user);
            }
            else
            {
                user.VerificationCode = code;
            }

            await context.SaveChangesAsync();
        }