Exemplo n.º 1
0
        public IActionResult Post(Aluno aluno)
        {
            context.Add(aluno);
            context.SaveChanges();

            return(Ok(aluno));
        }
        public IActionResult Post(Professor Professor)
        {
            //return Ok("Professors: Denis, Vera, Isabella, Denis Filho");
            _context.Add(Professor);
            _context.SaveChanges();

            return(Ok(Professor));
        }
        public IActionResult Post(Aluno aluno)
        {
            //return Ok("Alunos: Denis, Vera, Isabella, Denis Filho");
            _context.Add(aluno);
            _context.SaveChanges();

            return(Ok(aluno));
        }
Exemplo n.º 4
0
        public virtual T Add(T entity, bool save = true)
        {
            _entity.Add(entity);

            if (save)
            {
                _context.SaveChanges();
            }

            return(entity);
        }
        public void TestSmartContext_DeleteEntity_DeleteShouldBeBackedUp()
        {
            using (SmartContext sCtx = new SmartContext())
            {
                User userToDelete = sCtx.GetSingle<User>();

                int userToDeleteId = userToDelete.UserId;

                sCtx.Delete(userToDelete);
                sCtx.SaveChanges();

                User deletedUsserFromOffline = this.offlineContext.Users.FirstOrDefault(usr => usr.UserId == userToDelete.UserId);

                object deleteOperation = this.offlineContext.GetAll(DeleteOperationDefinition.DeleteOperationFullTypeName)
                    .Cast<object>()
                    .FirstOrDefault(o => o.FieldValue<int>(DeleteOperationDefinition.EntityToDeleteId) == userToDeleteId);

                int deletedEntityId = deleteOperation.FieldValue<int>(DeleteOperationDefinition.EntityToDeleteId);
                string deletedEntityType = deleteOperation.FieldValue<string>((DeleteOperationDefinition.EntityToDeleteType));

                Assert.IsNull(deletedUsserFromOffline);
                Assert.AreEqual(userToDeleteId, deletedEntityId);
                Assert.AreEqual("Model.User", deletedEntityType);
            }
        }
Exemplo n.º 6
0
 public object PostWidget([FromBody] Widget widget)
 {
     try
     {
         var email = HttpContext.User.Claims.First().Value;
         var user  = _context.User.First(x => x.Email == email);
         widget.UserID = user.ID;
         _context.Widget.Add(widget);
         _context.SaveChanges();
         widget = _context.Widget.OrderByDescending(x => x.ID).First(x => x.UserID == user.ID);
         return(new { widget = widget });
     }
     catch (Exception ex)
     {
         return(new { error = ex.Message });
     }
 }
Exemplo n.º 7
0
        public void Delete(int codigo)
        {
            var model = _context.Postagem.FirstOrDefault(c => c.Codigo == codigo);

            if (model != null)
            {
                _context.Remove(model);
                _context.SaveChanges();
            }
        }
Exemplo n.º 8
0
        public bool Insert(T entity)
        {
            try
            {
                if (entity.Id == Guid.Empty)
                {
                    entity.Id = Guid.NewGuid();
                }

                entity.CreateTime = DateTime.Now;
                table.Add(entity);
                _context.SaveChanges();
                return(true);
            }
            catch (Exception)
            {
                return(false);
            }
        }
Exemplo n.º 9
0
 public object AddProblem([FromBody] Problem problem)
 {
     try
     {
         var email = HttpContext.User.Claims.First().Value;
         var user  = _context.User.First(x => x.Email == email);
         problem.UserID = user.ID;
         _context.Problem.Add(problem);
         _context.SaveChanges();
         problem = _context.Problem.OrderByDescending(x => x.ID).First(x => x.UserID == user.ID);
         return(new { problem = problem });
     }
     catch (Exception ex)
     {
         return(new { error = ex.Message });
     }
 }
        public void TestAutoSwitchSmartContext_BackupDeletes_SwitchBackToOnlineMode_SyncDeletes_ObjectsShouldBeDeletedFromOnlineStorage()
        {
            using (SmartContext sCtx = new SmartContext())
            {
                int usersInOnlineCountBefore = this.onlineContext.Users.Count();

                int usrToDeleteId = 1;
                User usrToDelete = sCtx.GetSingle<User>(usr => usr.UserId == usrToDeleteId);
                sCtx.Delete(usrToDelete);

                this.LockDbTable("Users");
                sCtx.SaveChanges();

                this.ReleaseLock();
                Thread.Sleep(20000);

                User usrToDeleteFromOnline = this.onlineContext.Users.FirstOrDefault(usr => usr.UserId == usrToDeleteId);
                int usersInOnlineCountAfter = this.onlineContext.Users.Count();
                Assert.AreNotEqual(usersInOnlineCountBefore, usersInOnlineCountAfter);
                Assert.AreEqual(usersInOnlineCountBefore - 1, usersInOnlineCountAfter);
                Assert.IsNull(usrToDeleteFromOnline);
            }
        }
        public void TestAutoSwitchSmartContext_SwitchOnDelete_ObjectShouldBeDeletedFromBackup()
        {
            using (SmartContext sCtx = new SmartContext())
            {
                User userToDelete = sCtx.GetSingle<User>();
                int userToDeleteId = userToDelete.UserId;
                sCtx.Delete(userToDelete);

                //Simulate that the online storage is not available
                this.LockDbTable("Users");

                //Upon saving the chanegs the SmartContext will switch to the backup storage
                //and delete the user from there,
                //it will also save the delete operation
                sCtx.SaveChanges();

                User deletedUserFromBackup = sCtx.GetSingle<User>(usr => usr.UserId == userToDelete.UserId);
                this.ReleaseLock();
                User userFromOnline = this.onlineContext.Users.FirstOrDefault(usr => usr.UserId == userToDelete.UserId);

                object deleteOperation = this.offlineContext.GetAll(DeleteOperationDefinition.DeleteOperationFullTypeName)
                    .Cast<object>()
                    .FirstOrDefault(o => o.FieldValue<int>(DeleteOperationDefinition.EntityToDeleteId) == userToDeleteId);

                int deletedEntityId = deleteOperation.FieldValue<int>(DeleteOperationDefinition.EntityToDeleteId);
                string deletedEntityType = deleteOperation.FieldValue<string>((DeleteOperationDefinition.EntityToDeleteType));

                //Deleted from the backup storage
                Assert.IsNull(deletedUserFromBackup);
                //Still present in the online storage
                Assert.IsNotNull(userFromOnline);
                //verify that the delete operation has been backed up
                Assert.AreEqual(userToDeleteId, deletedEntityId);
                Assert.AreEqual("Model.User", deletedEntityType);
            }
        }
Exemplo n.º 12
0
 public IActionResult Post(Professor professor)
 {
     _context.Add(professor);
     _context.SaveChanges();
     return(Ok());
 }
        public void TestSmartContext_InsertUsersWithExistingRelatedGroup_AllShouldBeBackedUp()
        {
            using (SmartContext sCtx = new SmartContext())
            {
                int usersInOfflineCountBefore = this.offlineContext.Users.Count();
                int groupsInOfflineCountBefore = this.offlineContext.Groups.Count();

                //The existing related group will actually be backed up the moment
                //it is retrieved from the SmartContext
                Group relatedGroup = sCtx.GetSingle<Group>();

                User newUser1 = new User()
                {
                    UserId = 1337,
                    Name = "Mark",
                    Email = "*****@*****.**",
                    Note = "note note note",
                    Income = 15543m
                };
                newUser1.Group = relatedGroup;

                User newUser2 = new User()
                {
                    UserId = 1338,
                    Name = "Harry",
                    Email = "*****@*****.**",
                    Note = "note note note",
                    Income = 12543m
                };
                newUser2.Group = relatedGroup;

                sCtx.Add(newUser1);
                sCtx.Add(newUser2);
                sCtx.SaveChanges();

                int usersInOfflineCountAfter = this.offlineContext.Users.Count();
                int groupsInOfflineCountAfter = this.offlineContext.Groups.Count();
                IEnumerable<User> usersFromOfflineStorage = this.offlineContext.Users.Where(usr => usr.UserId == newUser1.UserId || usr.UserId == newUser2.UserId).ToList();
                IEnumerable<Group> groupsFromOfflineStorage = this.offlineContext.Groups.Where(grp => grp.GroupId == relatedGroup.GroupId).ToList();

                Assert.AreNotEqual(usersInOfflineCountBefore, usersInOfflineCountAfter);
                Assert.AreNotEqual(groupsInOfflineCountBefore, groupsInOfflineCountAfter);
                Assert.AreEqual(2, usersFromOfflineStorage.Count());
                Assert.AreEqual(1, groupsFromOfflineStorage.Count());
            }
        }
        public void TestSmartContext_InsertUsersWithNewRelatedGroup_AllShouldBeBackedUp()
        {
            using (SmartContext sCtx = new SmartContext())
            {
                int usersInOfflineCountBefore = this.offlineContext.Users.Count();
                int groupsInOfflineCountBefore = this.offlineContext.Groups.Count();

                Group newGroup = new Group()
                {
                    GroupId = 1227,
                    Name = "New Group",
                    Description = "Group of newness"
                };

                User newUser1 = new User()
                {
                    UserId = 1337,
                    Name = "Mark",
                    Email = "*****@*****.**",
                    Note = "note note note",
                    Income = 15543m
                };
                newUser1.Group = newGroup;

                User newUser2 = new User()
                {
                    UserId = 1338,
                    Name = "Harry",
                    Email = "*****@*****.**",
                    Note = "note note note",
                    Income = 12543m
                };
                newUser2.Group = newGroup;

                sCtx.Add(newUser1);
                sCtx.Add(newUser2);
                sCtx.SaveChanges();

                int usersInOfflineCountAfter = this.offlineContext.Users.Count();
                int groupsInOfflineCountAfter = this.offlineContext.Groups.Count();
                IEnumerable<User> usersFromOfflineStorage = this.offlineContext.Users.Where(usr => usr.UserId == newUser1.UserId || usr.UserId == newUser2.UserId).ToList();
                IEnumerable<Group> groupsFromOfflineStorage = this.offlineContext.Groups.Where(grp => grp.GroupId == newGroup.GroupId).ToList();

                Assert.AreNotEqual(usersInOfflineCountBefore, usersInOfflineCountAfter);
                Assert.AreNotEqual(groupsInOfflineCountBefore, groupsInOfflineCountAfter);
                Assert.AreEqual(2, usersFromOfflineStorage.Count());
                Assert.AreEqual(1, groupsFromOfflineStorage.Count());
            }
        }
        public void TestSmartContext_UpdateUser_UpdatesShouldBeBackedUp()
        {
            using (SmartContext sCtx = new SmartContext())
            {
                User user = sCtx.GetSingle<User>();

                string newUserName = "******";
                user.Name = newUserName;
                sCtx.SaveChanges();

                User userFromOfflineStorage = this.offlineContext.Users.FirstOrDefault(usr => usr.UserId == user.UserId);

                Assert.IsNotNull(userFromOfflineStorage);
                Assert.AreEqual(newUserName, userFromOfflineStorage.Name);
            }
        }
        public void TestAutoSwitchSmartContext_SwitchOnUpdate_ObjecShouldBeUpdateInTheBackup()
        {
            using (SmartContext sCtx = new SmartContext())
            {
                User userToUpdate = sCtx.GetSingle<User>();
                string newName = "Tony Stark";
                userToUpdate.Name = newName;

                //Simulate that the online storage is not available
                this.LockDbTable("Users");

                //Upon saving the changes the SmartContext will switch to the backup storage
                //and persist the changes made to the user there
                sCtx.SaveChanges();

                User updatedUserFromBackup = sCtx.GetSingle<User>(usr => usr.UserId == userToUpdate.UserId);
                this.ReleaseLock();
                User updatedUserFromOnline = this.onlineContext.Users.FirstOrDefault(usr => usr.UserId == userToUpdate.UserId);

                Assert.AreEqual(newName, updatedUserFromBackup.Name);
                Assert.AreNotEqual(newName, updatedUserFromOnline);
            }
        }
        public void TestAutoSwitchSmartContext_SwitchOnInsert_ObjectShouldBeInsertedInTheBackup()
        {
            using (SmartContext sCtx = new SmartContext())
            {
                int usersInOfflineBefore = this.offlineContext.Users.Count();

                User newUser = new User()
                {
                    UserId = 7777,
                    Name = "Ivan",
                    Email = "*****@*****.**",
                    Note = "note note note",
                    Income = 15543m
                };

                //Simulate that the online storage is not available
                this.LockDbTable("Users");

                sCtx.Add(newUser);

                //Upon saving the changes, the SmartContext will switch to the backup storage and
                //persist the new user object there
                sCtx.SaveChanges();

                int usersInOfflineAfter = this.offlineContext.Users.Count();
                User newUserFromFromBackup = sCtx.GetSingle<User>(usr => usr.UserId == newUser.UserId);
                this.ReleaseLock();
                User newUserFromOnline = this.onlineContext.Users.FirstOrDefault(usr => usr.UserId == newUser.UserId);

                Assert.IsNotNull(newUserFromFromBackup);
                Assert.IsNull(newUserFromOnline);
                Assert.AreNotEqual(usersInOfflineBefore, usersInOfflineAfter);
                Assert.AreEqual(usersInOfflineBefore + 1, usersInOfflineAfter);
            }
        }
 public IActionResult Post(Aluno aluno)
 {
     _context.Alunos.Add(aluno);
     _context.SaveChanges();
     return(Ok(_context.Alunos));
 }
Exemplo n.º 19
0
 public bool Savechanges()
 {
     return(_context.SaveChanges() > 0);
 }
        public void TestSmartContext_InsertEntity_EntityShouldBeBackedUp()
        {
            using (SmartContext sCtx = new SmartContext())
            {
                int usersInOfflineCountBefore = this.offlineContext.Users.Count();

                User newUser = new User()
                {
                    UserId = 1337,
                    Name = "Mark",
                    Email= "*****@*****.**",
                    Note = "note note note",
                    Income = 15543m
                };

                sCtx.Add(newUser);
                sCtx.SaveChanges();

                int usersInOfflineCountAfter = this.offlineContext.Users.Count();
                User userFromOfflineStorage = this.offlineContext.Users.FirstOrDefault(usr => usr.UserId == newUser.UserId);

                Assert.IsNotNull(userFromOfflineStorage);
                Assert.AreEqual(usersInOfflineCountBefore + 1, usersInOfflineCountAfter);
            }
        }
        public void TestSmartCotnext_InsertGroupWithExistingRealtedUsers_AllShouldBeBackedUp()
        {
            using (SmartContext sCtx = new SmartContext())
            {
                int usersInOfflineCountBefore = this.offlineContext.Users.Count();
                int groupsInOfflineCountBefore = this.offlineContext.Groups.Count();

                Group newGroup = new Group()
                {
                    GroupId = 8888,
                    Name = "NewGroup",
                    Description = "New Group For Users"
                };
                sCtx.Add(newGroup);
                sCtx.SaveChanges();

                User existingUser = sCtx.GetSingle<User>();

                newGroup.UsersInGroup.Add(existingUser);

                sCtx.SaveChanges();

                int usersInOfflineCountAfter = this.offlineContext.Users.Count();
                int groupsInOfflineCountAfter = this.offlineContext.Groups.Count();
                IEnumerable<User> usersFromOfflineStorage = this.offlineContext.Users.Where(usr => usr.UserId == existingUser.UserId).ToList();
                IEnumerable<Group> groupsFromOfflineStorage = this.offlineContext.Groups.Where(grp => grp.GroupId == newGroup.GroupId).ToList();

                Assert.AreNotEqual(usersInOfflineCountBefore, usersInOfflineCountAfter);
                Assert.AreNotEqual(groupsInOfflineCountBefore, groupsInOfflineCountAfter);
                Assert.AreEqual(1, usersFromOfflineStorage.Count());
                Assert.AreEqual(1, groupsFromOfflineStorage.Count());
            }
        }
        public void TestSmartCotnext_InsertGroupWithNewRealtedUsers_AllShouldBeBackedUp()
        {
            using (SmartContext sCtx = new SmartContext())
            {
                int usersInOfflineCountBefore = this.offlineContext.Users.Count();
                int groupsInOfflineCountBefore = this.offlineContext.Groups.Count();

                Group newGroup = new Group()
                {
                    GroupId = 8888,
                    Name = "NewGroup",
                    Description = "New Group For Users"
                };

                User newUser = new User()
                {
                    UserId = 1337,
                    Name = "Mark",
                    Email = "*****@*****.**",
                    Note = "note note note",
                    Income = 15543m
                };

                newGroup.UsersInGroup.Add(newUser);

                sCtx.Add(newGroup);
                sCtx.SaveChanges();

                int usersInOfflineCountAfter = this.offlineContext.Users.Count();
                int groupsInOfflineCountAfter = this.offlineContext.Groups.Count();
                IEnumerable<User> usersFromOfflineStorage = this.offlineContext.Users.Where(usr => usr.UserId == newUser.UserId).ToList();
                IEnumerable<Group> groupsFromOfflineStorage = this.offlineContext.Groups.Where(grp => grp.GroupId == newGroup.GroupId).ToList();

                Assert.AreNotEqual(usersInOfflineCountBefore, usersInOfflineCountAfter);
                Assert.AreNotEqual(groupsInOfflineCountBefore, groupsInOfflineCountAfter);
                Assert.AreEqual(1, usersFromOfflineStorage.Count());
                Assert.AreEqual(1, groupsFromOfflineStorage.Count());
            }
        }