public int RegisterUser(UserDtoModel userDto, DateTime cartDate)
        {
            if (userDto == null)
            {
                throw new ArgumentNullException("User info not supplied");
            }

            using (EF_Models.ESDatabaseContext context = new EF_Models.ESDatabaseContext())
            {
                UnitOfWork uow    = new UnitOfWork(context);
                User       efUser = _mapper.Map <User>(userDto);
                uow.Users.AddNewUser(efUser);

                DateTime SetDateTime()
                {
                    return(DateTime.Now);
                }

                Cart efCart = new Cart()
                {
                    DateLastUpdated = cartDate
                };
                efUser.Cart.Add(efCart);
                uow.Commit();
                int userID = efUser.Id;
                return(userID);
            }
        }
 public bool CheckIfUsernameExists(string username)
 {
     using (EF_Models.ESDatabaseContext context = new EF_Models.ESDatabaseContext())
     {
         UnitOfWork uow = new UnitOfWork(context);
         return(uow.Users.CheckIfUsernameExists(username));
     }
 }
 public void DeleteItem(int itemId)
 {
     using (EF_Models.ESDatabaseContext context = new EF_Models.ESDatabaseContext())
     {
         UnitOfWork uow = new UnitOfWork(context);
         uow.Items.DeleteItem(itemId);
     }
 }
        public UserDtoModel GetUser(int id)
        {
            using (EF_Models.ESDatabaseContext context = new EF_Models.ESDatabaseContext())
            {
                UnitOfWork uow = new UnitOfWork(context);

                //EF.User efUser = uow.Users.GetAll().FirstOrDefault();

                EF_Models.User efUser = uow.Users.GetById(id);
                if (efUser is null)
                {
                    return(null);
                }

                UserDtoModel user = _mapper.Map <UserDtoModel>(efUser);

                return(user);
            }
        }
        public UserDtoModel UserLogin(string username, string passwordHash)
        {
            using (EF_Models.ESDatabaseContext context = new EF_Models.ESDatabaseContext())
            {
                UnitOfWork uow = new UnitOfWork(context);

                User efUser = uow.Users.GetByUsernameAndPassword(username, passwordHash);
                if (efUser is null)
                {
                    throw new Exception("Wrong username or password");
                }



                UserDtoModel user = _mapper.Map <UserDtoModel>(efUser);

                return(user);
            }
        }
        public int AddNewItem(ItemDtoModel item, ItemDetailDtoModel itemDetail)
        {
            using (EF_Models.ESDatabaseContext context = new EF_Models.ESDatabaseContext())
            {
                UnitOfWork uow = new UnitOfWork(context);
                if (item == null || itemDetail == null)
                {
                    throw new ArgumentNullException();
                }
                Item         efItem         = _mapper.Map <Item>(item);
                DetailedItem efDetailedItem = _mapper.Map <DetailedItem>(itemDetail);
                uow.Items.AddNewItem(efItem);
                efItem.DetailedItem.Add(efDetailedItem);
                uow.Commit();

                int itemID = efItem.Id;
                return(itemID);
            }
        }
        public List <ItemDtoModel> SearchItems(int category, int sortBy, string searchName)
        {
            using (EF_Models.ESDatabaseContext context = new EF_Models.ESDatabaseContext())
            {
                UnitOfWork          uow   = new UnitOfWork(context);
                List <ItemDtoModel> items = new List <ItemDtoModel>();

                List <EF_Models.Item> efUsers = uow.Items.GetItemsBySearch(category, sortBy, searchName);
                if (efUsers is null)
                {
                    return(null);
                }

                foreach (Item efuser in efUsers)
                {
                    items.Add(_mapper.Map <ItemDtoModel>(efuser));
                }


                return(items);
            }
        }