コード例 #1
0
 public void StartFriendship(int userId, int friendId)
 {
     using (DbContextTransaction transaction = dbContext.Database.BeginTransaction())
     {
         try
         {
             FriendshipType commonType = dbContext.FriendshipTypes
                                         .Where(ft => ft.TypeOwnerId == userId && ft.Title == "common").First();
             Friendship friendship = new Friendship()
             {
                 UserId   = userId,
                 FriendId = friendId,
                 TypeId   = commonType.Id
             };
             dbContext.Friendships.Add(friendship);
             dbContext.SaveChanges();
             Friendship reverseFriendship = ReverseFriendship(friendship);
             dbContext.Friendships.Add(reverseFriendship);
             dbContext.SaveChanges();
             DeleteRequest(friendId, userId);
             transaction.Commit();
         }
         catch
         {
             transaction.Rollback();
             throw;
         }
     }
 }
コード例 #2
0
        public FriendshipType FindFriendshipType(int commonOwnerId, string title)
        {
            FriendshipType type = dbContext.FriendshipTypes.Where(ft => ft.TypeOwnerId == commonOwnerId && ft.Title == title)
                                  .First();

            return(type);
        }
コード例 #3
0
        public void AddFriendshipType(string typeTitle, int ownerId)
        {
            FriendshipType type = new FriendshipType()
            {
                Title       = typeTitle,
                TypeOwnerId = ownerId
            };

            dbContext.FriendshipTypes.Add(type);
            dbContext.SaveChanges();
        }
コード例 #4
0
        private Friendship ReverseFriendship(Friendship friendship)
        {
            FriendshipType commonType        = FindFriendshipType(friendship.FriendId, "common");
            Friendship     reverseFriendship = new Friendship()
            {
                UserId   = friendship.FriendId,
                FriendId = friendship.UserId,
                TypeId   = commonType.Id
            };

            return(reverseFriendship);
        }
コード例 #5
0
ファイル: UserManager.cs プロジェクト: Yupixit/courcework
 public void AddUser(User user)
 {
     using (DbContextTransaction transaction = dbContext.Database.BeginTransaction())
     {
         try
         {
             User addedUser = dbContext.Users.Add(user);
             dbContext.SaveChanges();
             int mainWallTypeId = dbContext.WallTypes.Where(wt => wt.Title == WallTypes.Main.ToString()).First()
                                  .Id;
             int photoWallTypeId = dbContext.WallTypes.Where(wt => wt.Title == WallTypes.Photo.ToString())
                                   .First().Id;
             Wall mainWall = new Wall()
             {
                 OwnerId    = addedUser.Id,
                 WallTypeId = mainWallTypeId,
                 Title      = WallTypes.Main.ToString()
             };
             Wall avatarsWall = new Wall()
             {
                 OwnerId    = addedUser.Id,
                 WallTypeId = photoWallTypeId,
                 Title      = "Avatars"
             };
             FriendshipType commonFriendship = new FriendshipType()
             {
                 Title       = "common",
                 TypeOwnerId = addedUser.Id
             };
             dbContext.FriendshipTypes.Add(commonFriendship);
             dbContext.Walls.Add(mainWall);
             dbContext.Walls.Add(avatarsWall);
             dbContext.SaveChanges();
             transaction.Commit();
         }
         catch
         {
             transaction.Rollback();
             throw;
         }
     }
 }