Example #1
0
        public IHttpActionResult PutBook(int id, Book book)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

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

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

            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!BookExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(StatusCode(HttpStatusCode.NoContent));
        }
Example #2
0
        public async Task GetUserBoardsAsync_WhenTheUserDoesNotHaveBoards_MustReturnEmptyList()
        {
            var options = new DbContextOptionsBuilder <MWContext>()
                          .UseInMemoryDatabase(databaseName: _fixture.Create <string>())
                          .Options;

            using (var context = new MWContext(options))
            {
                var repo = new UserRepository(context);

                const string user1 = "user1";
                var          pb1   = _fixture.Create <PersistibleBoard>();
                var          pb2   = _fixture.Create <PersistibleBoard>();
                var          pb3   = _fixture.Create <PersistibleBoard>();

                context.PersistibleBoards.Add(pb1);
                context.PersistibleBoards.Add(pb2);
                context.PersistibleBoards.Add(pb3);
                context.SaveChanges();

                var result = await repo.GetUserBoardsAsync(user1);

                Assert.IsNotNull(result);
                Assert.AreEqual(0, result.Count);
            }
        }
Example #3
0
        public async Task GetUserBoardsAsync_MustReturnTheUserBoards()
        {
            var options = new DbContextOptionsBuilder <MWContext>()
                          .UseInMemoryDatabase(databaseName: _fixture.Create <string>())
                          .Options;

            using (var context = new MWContext(options))
            {
                var repo = new UserRepository(context);

                const string user1 = "user1";
                var          b1    = _fixture.Create <Board>();
                var          b2    = _fixture.Create <Board>();

                var pb1 = _fixture.Build <PersistibleBoard>()
                          .With(b => b.BoardDefinition, JsonConvert.SerializeObject(b1).ToString())
                          .With(b => b.Username, user1).Create();
                var pb2 = _fixture.Build <PersistibleBoard>()
                          .With(b => b.BoardDefinition, JsonConvert.SerializeObject(b2).ToString())
                          .With(b => b.Username, user1).Create();
                var pb3 = _fixture.Create <PersistibleBoard>();
                var pb4 = _fixture.Create <PersistibleBoard>();

                context.PersistibleBoards.Add(pb1);
                context.PersistibleBoards.Add(pb2);
                context.PersistibleBoards.Add(pb3);
                context.PersistibleBoards.Add(pb4);
                context.SaveChanges();

                var result = await repo.GetUserBoardsAsync(user1);

                Assert.AreEqual(2, result.Count);
            }
        }
        public async Task <ActionResult <Word> > DeleteAll(string userName)
        {
            var rows = from o in _context.Words select o;

            foreach (var row in rows)
            {
                if (row.UserName == userName)
                {
                    _context.Words.Remove(row);
                }
            }
            _context.SaveChanges();
            return(NoContent());
        }
Example #5
0
        public async Task GetUserAsync_IfTheUserDoesExists_ReturnsNull()
        {
            var options = new DbContextOptionsBuilder <MWContext>()
                          .UseInMemoryDatabase(databaseName: _fixture.Create <string>())
                          .Options;

            using (var context = new MWContext(options))
            {
                var repo = new UserRepository(context);
                var user = Utils.GetFixture().Create <User>();

                context.Users.Add(user);
                context.SaveChanges();

                Assert.IsNull(await repo.GetUserAsync(DateTime.Now.ToString()));
            }
        }
Example #6
0
        public async Task GetUserAsync_MustReturnTheUser()
        {
            var options = new DbContextOptionsBuilder <MWContext>()
                          .UseInMemoryDatabase(databaseName: _fixture.Create <string>())
                          .Options;

            using (var context = new MWContext(options))
            {
                var repo = new UserRepository(context);

                const string user1 = "user1";
                var          u1    = _fixture.Build <User>()
                                     .With(b => b.Username, user1).Create();

                context.Users.Add(u1);
                context.SaveChanges();

                var result = await repo.GetUserAsync(user1);

                Assert.AreEqual(u1, result);
            }
        }
Example #7
0
        public async Task GetUserAsync_WhenTheUserDoesNotExist_MustReturnNull()
        {
            var options = new DbContextOptionsBuilder <MWContext>()
                          .UseInMemoryDatabase(databaseName: _fixture.Create <string>())
                          .Options;

            using (var context = new MWContext(options))
            {
                var repo = new UserRepository(context);

                const string user1 = "existingUser";
                const string user2 = "nonExistingUser";
                var          u1    = _fixture.Build <User>()
                                     .With(b => b.Username, user1).Create();

                context.Users.Add(u1);
                context.SaveChanges();

                var result = await repo.GetUserAsync(user2);

                Assert.IsNull(result);
            }
        }
Example #8
0
 public void Save()
 {
     context.SaveChanges();
 }