Пример #1
0
        public void MemoirDictSameKeyMultithread()
        {
            var sideeffect = 0;

            int test(int x)
            {
                ++sideeffect;
                return(x + sideeffect);
            }

            var memoir    = Memoir.From(test, new Memoir.DictionaryCache <int, int>(), new Memoir.MultithreadLocker <int>());
            var startLine = new ManualResetEventSlim();
            var tasks     = Enumerable.Range(1, 1000).Select(_ => Task.Run(() => {
                startLine.Wait();
                return(memoir(1));
            })).ToArray();

            startLine.Set();    // go!

            Task.WaitAll(tasks.Cast <Task>().ToArray());

            var results = tasks.Select(t => t.Result).ToArray();

            results.All(r => r == 2).Should().BeTrue();
            sideeffect.Should().Be(1, "sideeffect should be called just once!");
        }
Пример #2
0
        public async Task <IActionResult> Edit(int id, Memoir memoir)
        {
            if (id != memoir.MemoirId)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    _context.Update(memoir);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!MemoirExists(memoir.MemoirId))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }
            MemoirEditDetailDeleteViewModel memoirEditDetailDeleteViewModel = new MemoirEditDetailDeleteViewModel(memoir);

            return(View(memoirEditDetailDeleteViewModel));
        }
Пример #3
0
        public async Task <IActionResult> CreateFromWishlist(string restaurantName, int rid, string restaurantLocation, string restaurantAddress)
        {
            //get current user
            var currUser = await GetCurrentUserAsync();

            var usersId = currUser.Id;

            Memoir memoir = new Memoir();

            if (ModelState.IsValid)
            {
                memoir.RId                = rid;
                memoir.Comments           = "";
                memoir.AppUserId          = usersId;
                memoir.RestaurantName     = restaurantName;
                memoir.RestaurantLocation = restaurantLocation;
                memoir.RestaurantAddress  = restaurantAddress;

                _context.Add(memoir);
                await _context.SaveChangesAsync();
            }

            return(RedirectToAction(nameof(Edit), new {
                id = memoir.MemoirId
            }));
        }
Пример #4
0
        public async Task <IActionResult> Create(string restaurantName, int rid, string restaurantLocation, string restaurantAddress)
        {
            //get current user
            var currUser = await GetCurrentUserAsync();

            var usersId = currUser.Id;

            //check to see if restaurant id exists in memoir table for this user
            int memoirCheck = (_context.Memoir
                               .Include(m => m.AppUser)
                               .Where(m => m.AppUserId == usersId).Where(m => m.RId == rid)).Count();

            //check to see if restaurant id exists in wishlist table already for this user
            int wishlistCheck = (_context.Wishlist
                                 .Include(w => w.AppUser)
                                 .Where(w => w.AppUserId == usersId).Where(w => w.RId == rid)).Count();

            //if restaurant doesn't exist in wishlist or memoir table for the user, go ahead and create the new memoir
            if (memoirCheck == 0 && wishlistCheck == 0)
            {
                Memoir memoir = new Memoir();

                if (ModelState.IsValid)
                {
                    memoir.RId                = rid;
                    memoir.Comments           = "";
                    memoir.AppUserId          = usersId;
                    memoir.RestaurantName     = restaurantName;
                    memoir.RestaurantLocation = restaurantLocation;
                    memoir.RestaurantAddress  = restaurantAddress;

                    _context.Add(memoir);
                    await _context.SaveChangesAsync();
                }

                return(RedirectToAction(nameof(Edit), new {
                    id = memoir.MemoirId
                }));
            }
            else if (memoirCheck > 0)
            {
                //if the user has already saved a memoir for this restaurant id, redirect to index
                return(RedirectToAction(nameof(Index)));
            }
            else
            {
                //if the restaurant id is in the user's wishlist, redirect to wishlist index
                return(RedirectToAction("Index", "Wishlists", new { area = "" }));
            }
        }
Пример #5
0
        public void MemoirDictSameKey()
        {
            var sideeffect = 0;

            int test(int x)
            {
                ++sideeffect;
                return(x + sideeffect);
            }

            var memoir = Memoir.DictWith <int, int>(test);

            memoir(1);

            var result = memoir(1);

            result.Should().NotBe(3, "sideeffect should be increased once only!");
            result.Should().Be(2);
        }
 public MemoirEditDetailDeleteViewModel(Memoir memoir)
 {
     Memoir = memoir;
 }