public async Task <UserEntity> CreateUserAsync(UserEntity user)
        {
            var result = Mapping.Mapper.Map <Users>(user);

            if (result.Address != null)
            {
                await _context.Addresses.AddAsync(result.Address);

                await _context.SaveChangesAsync();

                var addressId = result.Address.AddressId;
                result.AddressId = addressId;
            }

            await _context.Users.AddAsync(result);

            await _context.SaveChangesAsync();

            await _shopCartService.CreateShopCartAsync(result.UserId, new Shopping_Cart_Entity {
                Date    = DateTime.Now,
                UserId  = result.UserId,
                IsOrder = false
            });

            return(Mapping.Mapper.Map <UserEntity>(result));
        }
        public async Task <CatalogsEntity> CreateCatalogAsync(CatalogsEntity catalog)
        {
            var result = Mapping.Mapper.Map <Catalogs>(catalog);
            await _context.AddAsync(result);

            await _context.SaveChangesAsync();

            return(Mapping.Mapper.Map <CatalogsEntity>(result));
        }
        public async Task <UserRolesEntity> CreateUserRolesAsync(UserRolesEntity userRole)
        {
            var result = Mapping.Mapper.Map <UserRoles>(userRole);

            await _context.UserRoles.AddAsync(result);

            await _context.SaveChangesAsync();

            return(Mapping.Mapper.Map <UserRolesEntity>(result));
        }
Example #4
0
        public async Task <AddressEntity> CreateUserAddressAsync(int userId, AddressEntity address)
        {
            var userModel = await _context.Users.FirstOrDefaultAsync(e => e.UserId == userId);

            var newAddress = Mapping.Mapper.Map <Addresses>(address);

            await _context.Addresses.AddAsync(newAddress);

            await _context.SaveChangesAsync();

            newAddress.User = userModel;
            await _context.SaveChangesAsync();

            var result = await _context.Addresses.Include(e => e.User)
                         .FirstOrDefaultAsync(e => e.AddressId == newAddress.AddressId);

            return(Mapping.Mapper.Map <AddressEntity>(result));
        }
Example #5
0
        public async Task <BooksEntity> CreateBookAsync(BooksEntity book)
        {
            var result = Mapping.Mapper.Map <Books>(book);
            await _context.Books.AddAsync(result);

            var catalogIds = book.Catalogs.Select(e => e.CatalogId).ToList();

            await addCatalogsAsync(result, catalogIds);

            await _context.SaveChangesAsync();

            var bookModel = await _context.Books
                            //.Include(m => m.Supplier)
                            .Include(m => m.Catalogs_Books)
                            .ThenInclude(m => m.Catalog)
                            .FirstOrDefaultAsync(m => m.BookId == result.BookId);

            return(Mapping.Mapper.Map <BooksEntity>(result));
        }
        public async Task CreateShopCartAsync(int userId, Shopping_Cart_Entity shopCart)
        {
            if (checkIfShopCartExist(userId))
            {
                var userModel = await _context.Users.FirstOrDefaultAsync(e => e.UserId == userId);

                var newShopCart = Mapping.Mapper.Map <Shopping_Carts>(shopCart);

                await _context.Shopping_Carts.AddAsync(newShopCart);

                await _context.SaveChangesAsync();


                newShopCart.User = userModel;
                await _context.SaveChangesAsync();
            }
        }